From 6bfa40ce9fc19d7e2f63f55bff82e31b4e3d8599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 7 May 2018 14:53:45 +0200 Subject: [PATCH] better neopixel commands for sparse writing --- units/neopixel/_npx_api.c | 16 ++-------------- units/neopixel/unit_neopixel.c | 24 +++++++++++++----------- units/neopixel/unit_neopixel.h | 23 ++++++----------------- units/neopixel/ws2812.c | 9 +++++---- units/neopixel/ws2812.h | 6 ++++-- 5 files changed, 30 insertions(+), 48 deletions(-) diff --git a/units/neopixel/_npx_api.c b/units/neopixel/_npx_api.c index 6dffb3f..2096e2e 100644 --- a/units/neopixel/_npx_api.c +++ b/units/neopixel/_npx_api.c @@ -32,28 +32,16 @@ error_t UU_Npx_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes) } /* Load U32, LE or BE */ -static error_t load_u32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool bige) +error_t UU_Npx_Load32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool order_bgr, bool zero_before) { CHECK_TYPE(unit, &UNIT_NEOPIXEL); struct priv *priv = unit->data; if (nbytes != 4*priv->cfg.pixels) return E_BAD_COUNT; - ws2812_load_sparse(priv->port, priv->ll_pin, bytes, priv->cfg.pixels, bige); + ws2812_load_sparse(priv->port, priv->ll_pin, bytes, priv->cfg.pixels, order_bgr, zero_before); return E_SUCCESS; } -/* Load U32, LE */ -inline error_t UU_Npx_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) -{ - return load_u32(unit, bytes, nbytes, false); -} - -/* Load U32, BE */ -inline error_t UU_Npx_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) -{ - return load_u32(unit, bytes, nbytes, true); -} - /* Get the pixel count */ error_t UU_Npx_GetCount(Unit *unit, uint16_t *count) { diff --git a/units/neopixel/unit_neopixel.c b/units/neopixel/unit_neopixel.c index abb98cd..7d9dace 100644 --- a/units/neopixel/unit_neopixel.c +++ b/units/neopixel/unit_neopixel.c @@ -11,9 +11,11 @@ enum PinCmd_ { CMD_CLEAR = 0, CMD_LOAD = 1, - CMD_LOAD_U32_LE = 2, - CMD_LOAD_U32_BE = 3, - CMD_GET_LEN = 4, + CMD_LOAD_ZRGB = 0x08, // 0,0 - trail zero, bgr + CMD_LOAD_ZBGR = 0x09, // 0,1 + CMD_LOAD_RGBZ = 0x0A, // 1,0 + CMD_LOAD_BGRZ = 0x0B, // 1,1 + CMD_GET_LEN = 0x10, }; /** Handle a request message */ @@ -32,15 +34,15 @@ static error_t Npx_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Pa bytes = pp_tail(pp, &len); return UU_Npx_Load(unit, bytes, len); - /** Load sparse (uint32_t) colors, 0x00RRGGBB, little endian. */ - case CMD_LOAD_U32_LE: + /** Load sparse (uint32_t) colors */ + case CMD_LOAD_ZRGB: + case CMD_LOAD_ZBGR: + case CMD_LOAD_RGBZ: + case CMD_LOAD_BGRZ: bytes = pp_tail(pp, &len); - return UU_Npx_LoadU32LE(unit, bytes, len); - - /** Load sparse (uint32_t) colors, 0x00RRGGBB, big endian. */ - case CMD_LOAD_U32_BE: - bytes = pp_tail(pp, &len); - return UU_Npx_LoadU32BE(unit, bytes, len); + bool trail_zero = (bool) (command & 0b10); + bool order_bgr = (bool) (command & 0b01); + return UU_Npx_Load32(unit, bytes, len, order_bgr, !trail_zero); /** Get the Neopixel strip length */ case CMD_GET_LEN:; diff --git a/units/neopixel/unit_neopixel.h b/units/neopixel/unit_neopixel.h index dcc2982..69d6aee 100644 --- a/units/neopixel/unit_neopixel.h +++ b/units/neopixel/unit_neopixel.h @@ -31,26 +31,15 @@ error_t UU_Npx_Clear(Unit *unit); error_t UU_Npx_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes); /** - * Load the strip with sparse (uint32_t) colors 0x00RRGGBB as little endian bytes - * (B, G, R, 0, ...) - * - * @param unit - * @param bytes - bytes to load - * @param nbytes - number of bytes, must be count*4 - * @return success - */ -error_t UU_Npx_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes); - -/** - * Load the strip with sparse (uint32_t) colors 0x00RRGGBB as big endian bytes - * (0, R, G, B, ...) - * + * Load from 32-bit numbers * @param unit - * @param bytes - bytes to load - * @param nbytes - number of bytes, must be count*4 + * @param bytes + * @param nbytes + * @param order_bgr + * @param zero_before * @return success */ -error_t UU_Npx_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes); +error_t UU_Npx_Load32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool order_bgr, bool zero_before); /** * Get number of pixels on the strip diff --git a/units/neopixel/ws2812.c b/units/neopixel/ws2812.c index 81e6b28..9ec090b 100644 --- a/units/neopixel/ws2812.c +++ b/units/neopixel/ws2812.c @@ -45,13 +45,14 @@ void ws2812_load_raw(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, u } /** Set many RGBs from uint32 stream */ -void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count, bool bigendian) +void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count, + bool order_bgr, bool zero_before) { vPortEnterCritical(); uint8_t b, g, r; for (uint32_t i = 0; i < count; i++) { - if (bigendian) { - rgbs++; // skip + if (zero_before) rgbs++; // skip + if (order_bgr) { b = *rgbs++; g = *rgbs++; r = *rgbs++; @@ -59,8 +60,8 @@ void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs r = *rgbs++; g = *rgbs++; b = *rgbs++; - rgbs++; // skip } + if (!zero_before) rgbs++; // skip ws2812_byte(port, ll_pin, g); ws2812_byte(port, ll_pin, r); diff --git a/units/neopixel/ws2812.h b/units/neopixel/ws2812.h index b692f10..cf68145 100644 --- a/units/neopixel/ws2812.h +++ b/units/neopixel/ws2812.h @@ -28,8 +28,10 @@ void ws2812_clear(GPIO_TypeDef *port, uint32_t ll_pin, uint32_t count); * @param ll_pin * @param rgbs - payload * @param count - number of pixels - * @param bigendian - big endian ordering + * @param order_bgr - B,G,R colors, false - R,G,B + * @param zero_before - insert padding byte before colors, false - after */ -void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count, bool bigendian); +void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count, + bool order_bgr, bool zero_before); #endif //WS2812_H