From 41364db93aa76368bc03f8dcb1a3491849282164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 3 Feb 2018 10:48:24 +0100 Subject: [PATCH] split neopixel --- units/neopixel/_npx_api.c | 65 ++++++++++++ units/neopixel/_npx_init.c | 64 ++++++++++++ units/neopixel/_npx_init.h | 20 ++++ units/neopixel/_npx_internal.h | 24 +++++ units/neopixel/_npx_settings.c | 64 ++++++++++++ units/neopixel/_npx_settings.h | 31 ++++++ units/neopixel/unit_neopixel.c | 182 +-------------------------------- 7 files changed, 271 insertions(+), 179 deletions(-) create mode 100644 units/neopixel/_npx_api.c create mode 100644 units/neopixel/_npx_init.c create mode 100644 units/neopixel/_npx_init.h create mode 100644 units/neopixel/_npx_internal.h create mode 100644 units/neopixel/_npx_settings.c create mode 100644 units/neopixel/_npx_settings.h diff --git a/units/neopixel/_npx_api.c b/units/neopixel/_npx_api.c new file mode 100644 index 0000000..b174cab --- /dev/null +++ b/units/neopixel/_npx_api.c @@ -0,0 +1,65 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" +#include "unit_neopixel.h" + +#define NPX_INTERNAL +#include "_npx_internal.h" +#include "ws2812.h" + +/* Clear the strip */ +error_t UU_NEOPIXEL_Clear(Unit *unit) +{ + CHECK_TYPE(unit, &UNIT_NEOPIXEL); + + struct priv *priv = unit->data; + ws2812_clear(priv->port, priv->ll_pin, priv->pixels); + return E_SUCCESS; +} + +/* Load packed */ +error_t UU_NEOPIXEL_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes) +{ + CHECK_TYPE(unit, &UNIT_NEOPIXEL); + + struct priv *priv = unit->data; + if (nbytes != 3*priv->pixels) return E_BAD_COUNT; + ws2812_load_raw(priv->port, priv->ll_pin, packed_rgb, priv->pixels); + return E_SUCCESS; +} + +/* Load U32, LE or BE */ +static error_t load_u32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool bige) +{ + CHECK_TYPE(unit, &UNIT_NEOPIXEL); + + struct priv *priv = unit->data; + if (nbytes != 4*priv->pixels) return E_BAD_COUNT; + ws2812_load_sparse(priv->port, priv->ll_pin, bytes, priv->pixels, bige); + return E_SUCCESS; +} + +/* Load U32, LE */ +inline error_t UU_NEOPIXEL_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) +{ + return load_u32(unit, bytes, nbytes, false); +} + +/* Load U32, BE */ +inline error_t UU_NEOPIXEL_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) +{ + return load_u32(unit, bytes, nbytes, true); +} + +/* Get the pixel count */ +error_t UU_NEOPIXEL_GetCount(Unit *unit, uint16_t *count) +{ + CHECK_TYPE(unit, &UNIT_NEOPIXEL); + + struct priv *priv = unit->data; + *count = priv->pixels; + return E_SUCCESS; +} diff --git a/units/neopixel/_npx_init.c b/units/neopixel/_npx_init.c new file mode 100644 index 0000000..332f9e3 --- /dev/null +++ b/units/neopixel/_npx_init.c @@ -0,0 +1,64 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" + +#define NPX_INTERNAL +#include "_npx_internal.h" +#include "_npx_init.h" +#include "ws2812.h" + +/** Allocate data structure and set defaults */ +error_t Npx_preInit(Unit *unit) +{ + struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv)); + if (priv == NULL) + + // some defaults + priv->pin_number = 0; + priv->port_name = 'A'; + priv->pixels = 1; + + return E_SUCCESS; +} + +/** Finalize unit set-up */ +error_t Npx_init(Unit *unit) +{ + bool suc = true; + struct priv *priv = unit->data; + + // --- Parse config --- + priv->ll_pin = hw_pin2ll(priv->pin_number, &suc); + priv->port = hw_port2periph(priv->port_name, &suc); + Resource rsc = hw_pin2resource(priv->port_name, priv->pin_number, &suc); + if (!suc) return E_BAD_CONFIG; + + // --- Claim resources --- + TRY(rsc_claim(unit, rsc)); + + // --- Init hardware --- + LL_GPIO_SetPinMode(priv->port, priv->ll_pin, LL_GPIO_MODE_OUTPUT); + LL_GPIO_SetPinOutputType(priv->port, priv->ll_pin, LL_GPIO_OUTPUT_PUSHPULL); + LL_GPIO_SetPinSpeed(priv->port, priv->ll_pin, LL_GPIO_SPEED_FREQ_HIGH); + + // clear strip + + ws2812_clear(priv->port, priv->ll_pin, priv->pixels); + + return E_SUCCESS; +} + +/** Tear down the unit */ +void Npx_deInit(Unit *unit) +{ + // pins are de-inited during teardown + + // Release all resources + rsc_teardown(unit); + + // Free memory + free_ck(unit->data); +} diff --git a/units/neopixel/_npx_init.h b/units/neopixel/_npx_init.h new file mode 100644 index 0000000..f90146b --- /dev/null +++ b/units/neopixel/_npx_init.h @@ -0,0 +1,20 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_NPX_INIT_H +#define GEX_F072_NPX_INIT_H + +#ifndef NPX_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** Finalize unit set-up */ +error_t Npx_init(Unit *unit); + +/** Tear down the unit */ +void Npx_deInit(Unit *unit); + +#endif //GEX_F072_NPX_INIT_H diff --git a/units/neopixel/_npx_internal.h b/units/neopixel/_npx_internal.h new file mode 100644 index 0000000..eb171bb --- /dev/null +++ b/units/neopixel/_npx_internal.h @@ -0,0 +1,24 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_NPX_INTERNAL_H +#define GEX_F072_NPX_INTERNAL_H + +#ifndef NPX_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** Private data structure */ +struct priv { + char port_name; + uint8_t pin_number; + uint16_t pixels; + + uint32_t ll_pin; + GPIO_TypeDef *port; +}; + +#endif //GEX_F072_NPX_INTERNAL_H diff --git a/units/neopixel/_npx_settings.c b/units/neopixel/_npx_settings.c new file mode 100644 index 0000000..a2b0a12 --- /dev/null +++ b/units/neopixel/_npx_settings.c @@ -0,0 +1,64 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" + +#define NPX_INTERNAL +#include "_npx_internal.h" +#include "_npx_settings.h" + +/** Load from a binary buffer stored in Flash */ +void Npx_loadBinary(Unit *unit, PayloadParser *pp) +{ + struct priv *priv = unit->data; + + priv->port_name = pp_char(pp); + priv->pin_number = pp_u8(pp); + priv->pixels = pp_u16(pp); +} + +/** Write to a binary buffer for storing in Flash */ +void Npx_writeBinary(Unit *unit, PayloadBuilder *pb) +{ + struct priv *priv = unit->data; + + pb_char(pb, priv->port_name); + pb_u8(pb, priv->pin_number); + pb_u16(pb, priv->pixels); +} + +// ------------------------------------------------------------------------ + +/** Parse a key-value pair from the INI file */ +error_t Npx_loadIni(Unit *unit, const char *key, const char *value) +{ + bool suc = true; + struct priv *priv = unit->data; + + if (streq(key, "pin")) { + suc = parse_pin(value, &priv->port_name, &priv->pin_number); + } + else if (streq(key, "pixels")) { + priv->pixels = (uint16_t) avr_atoi(value); + } + else { + return E_BAD_KEY; + } + + if (!suc) return E_BAD_VALUE; + return E_SUCCESS; +} + +/** Generate INI file section for the unit */ +void Npx_writeIni(Unit *unit, IniWriter *iw) +{ + struct priv *priv = unit->data; + + iw_comment(iw, "Data pin"); + iw_entry(iw, "pin", "%c%d", priv->port_name, priv->pin_number); + + iw_comment(iw, "Number of pixels"); + iw_entry(iw, "pixels", "%d", priv->pixels); +} diff --git a/units/neopixel/_npx_settings.h b/units/neopixel/_npx_settings.h new file mode 100644 index 0000000..3d175c1 --- /dev/null +++ b/units/neopixel/_npx_settings.h @@ -0,0 +1,31 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_NPX_SETTINGS_H +#define GEX_F072_NPX_SETTINGS_H + +#ifndef NPX_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** Allocate data structure and set defaults */ +error_t Npx_preInit(Unit *unit); + +/** Load from a binary buffer stored in Flash */ +void Npx_loadBinary(Unit *unit, PayloadParser *pp); + +/** Write to a binary buffer for storing in Flash */ +void Npx_writeBinary(Unit *unit, PayloadBuilder *pb); + +// ------------------------------------------------------------------------ + +/** Parse a key-value pair from the INI file */ +error_t Npx_loadIni(Unit *unit, const char *key, const char *value); + +/** Generate INI file section for the unit */ +void Npx_writeIni(Unit *unit, IniWriter *iw); + +#endif //GEX_F072_NPX_SETTINGS_H diff --git a/units/neopixel/unit_neopixel.c b/units/neopixel/unit_neopixel.c index 984d5f3..f116140 100644 --- a/units/neopixel/unit_neopixel.c +++ b/units/neopixel/unit_neopixel.c @@ -2,188 +2,12 @@ // Created by MightyPork on 2017/11/25. // -#include "utils/avrlibc.h" -#include "comm/messages.h" #include "unit_base.h" #include "unit_neopixel.h" -#include "ws2812.h" -/** Private data structure */ -struct priv { - char port_name; - uint8_t pin_number; - uint16_t pixels; - - uint32_t ll_pin; - GPIO_TypeDef *port; -}; - -// ------------------------------------------------------------------------ - -/** Load from a binary buffer stored in Flash */ -static void Npx_loadBinary(Unit *unit, PayloadParser *pp) -{ - struct priv *priv = unit->data; - - priv->port_name = pp_char(pp); - priv->pin_number = pp_u8(pp); - priv->pixels = pp_u16(pp); -} - -/** Write to a binary buffer for storing in Flash */ -static void Npx_writeBinary(Unit *unit, PayloadBuilder *pb) -{ - struct priv *priv = unit->data; - - pb_char(pb, priv->port_name); - pb_u8(pb, priv->pin_number); - pb_u16(pb, priv->pixels); -} - -// ------------------------------------------------------------------------ - -/** Parse a key-value pair from the INI file */ -static error_t Npx_loadIni(Unit *unit, const char *key, const char *value) -{ - bool suc = true; - struct priv *priv = unit->data; - - if (streq(key, "pin")) { - suc = parse_pin(value, &priv->port_name, &priv->pin_number); - } - else if (streq(key, "pixels")) { - priv->pixels = (uint16_t) avr_atoi(value); - } - else { - return E_BAD_KEY; - } - - if (!suc) return E_BAD_VALUE; - return E_SUCCESS; -} - -/** Generate INI file section for the unit */ -static void Npx_writeIni(Unit *unit, IniWriter *iw) -{ - struct priv *priv = unit->data; - - iw_comment(iw, "Data pin"); - iw_entry(iw, "pin", "%c%d", priv->port_name, priv->pin_number); - - iw_comment(iw, "Number of pixels"); - iw_entry(iw, "pixels", "%d", priv->pixels); -} - -// ------------------------------------------------------------------------ - -/** Allocate data structure and set defaults */ -static error_t Npx_preInit(Unit *unit) -{ - struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv)); - if (priv == NULL) - - // some defaults - priv->pin_number = 0; - priv->port_name = 'A'; - priv->pixels = 1; - - return E_SUCCESS; -} - -/** Finalize unit set-up */ -static error_t Npx_init(Unit *unit) -{ - bool suc = true; - struct priv *priv = unit->data; - - // --- Parse config --- - priv->ll_pin = hw_pin2ll(priv->pin_number, &suc); - priv->port = hw_port2periph(priv->port_name, &suc); - Resource rsc = hw_pin2resource(priv->port_name, priv->pin_number, &suc); - if (!suc) return E_BAD_CONFIG; - - // --- Claim resources --- - TRY(rsc_claim(unit, rsc)); - - // --- Init hardware --- - LL_GPIO_SetPinMode(priv->port, priv->ll_pin, LL_GPIO_MODE_OUTPUT); - LL_GPIO_SetPinOutputType(priv->port, priv->ll_pin, LL_GPIO_OUTPUT_PUSHPULL); - LL_GPIO_SetPinSpeed(priv->port, priv->ll_pin, LL_GPIO_SPEED_FREQ_HIGH); - - // clear strip - - ws2812_clear(priv->port, priv->ll_pin, priv->pixels); - - return E_SUCCESS; -} - -/** Tear down the unit */ -static void Npx_deInit(Unit *unit) -{ - // pins are de-inited during teardown - - // Release all resources - rsc_teardown(unit); - - // Free memory - free_ck(unit->data); -} - -// ------------------------------------------------------------------------ - -/* Clear the strip */ -error_t UU_NEOPIXEL_Clear(Unit *unit) -{ - CHECK_TYPE(unit, &UNIT_NEOPIXEL); - - struct priv *priv = unit->data; - ws2812_clear(priv->port, priv->ll_pin, priv->pixels); - return E_SUCCESS; -} - -/* Load packed */ -error_t UU_NEOPIXEL_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes) -{ - CHECK_TYPE(unit, &UNIT_NEOPIXEL); - - struct priv *priv = unit->data; - if (nbytes != 3*priv->pixels) return E_BAD_COUNT; - ws2812_load_raw(priv->port, priv->ll_pin, packed_rgb, priv->pixels); - return E_SUCCESS; -} - -/* Load U32, LE or BE */ -static error_t load_u32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool bige) -{ - CHECK_TYPE(unit, &UNIT_NEOPIXEL); - - struct priv *priv = unit->data; - if (nbytes != 4*priv->pixels) return E_BAD_COUNT; - ws2812_load_sparse(priv->port, priv->ll_pin, bytes, priv->pixels, bige); - return E_SUCCESS; -} - -/* Load U32, LE */ -inline error_t UU_NEOPIXEL_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) -{ - return load_u32(unit, bytes, nbytes, false); -} - -/* Load U32, BE */ -inline error_t UU_NEOPIXEL_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) -{ - return load_u32(unit, bytes, nbytes, true); -} - -/* Get the pixel count */ -error_t UU_NEOPIXEL_GetCount(Unit *unit, uint16_t *count) -{ - CHECK_TYPE(unit, &UNIT_NEOPIXEL); - - struct priv *priv = unit->data; - *count = priv->pixels; - return E_SUCCESS; -} +#define NPX_INTERNAL +#include "_npx_settings.h" +#include "_npx_init.h" enum PinCmd_ { CMD_CLEAR = 0,