Browse Source

code cleanup: do away with camelCase; whitespace & formatting

lots more to do in ws2812b_fx.*
master
parent
commit
6a2bb8114f
  1. 21
      Inc/ws2812b.h
  2. 169
      Inc/ws2812b_fx.h
  3. 149
      Src/ws2812b.c
  4. 451
      Src/ws2812b_fx.c

21
Inc/ws2812b.h

@ -36,17 +36,16 @@ typedef union __attribute__((packed, aligned(4))) ws2812b_color_u {
} ws2812b_color; } ws2812b_color;
/* Function Prototypes */ /* Function Prototypes */
void WS2812B_Init(SPI_HandleTypeDef * spi_handler); void ws2812_init(SPI_HandleTypeDef *hspi);
void WS2812B_SetDiodeColor(uint16_t diode_id, uint32_t color); void ws2812_set_led_color_val(uint16_t diode_id, uint32_t color);
void WS2812B_SetDiodeColorStruct(uint16_t diode_id, ws2812b_color color); void ws2812_set_led_color(uint16_t diode_id, ws2812b_color color);
void WS2812B_SetDiodeRGB(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b); void ws2812_set_led_rgb(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b);
void WS2812B_SetDiodeRGBX(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b, uint8_t x); void ws2812_set_led_rgbx(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b, uint8_t x);
void WS2812B_SetDiodeHSV(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t brt); void ws2812_set_led_hsv(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t value);
uint32_t WS2812B_GetColor(uint16_t diode_id); uint32_t ws2812_get_color(uint16_t diode_id);
uint8_t* WS2812B_GetPixels(void); uint8_t *ws2812_get_pixels();
void WS2812B_Refresh(); void ws2812_refresh();
// color correction
uint8_t sine8(uint8_t x); uint8_t sine8(uint8_t x);
uint8_t gamma8(uint8_t x); uint8_t gamma8(uint8_t x);
#endif /* WS2812B_H_ */ #endif /* WS2812B_H_ */

169
Inc/ws2812b_fx.h

@ -14,97 +14,98 @@
#ifndef WS2812B_FX_H_ #ifndef WS2812B_FX_H_
#define WS2812B_FX_H_ #define WS2812B_FX_H_
#define DEFAULT_COLOR 0x00F0000000 /* Includes */
#define NUM_COLORS 4 #include <stdint.h>
#define SPEED_MIN 10 /* Definitions */
#define DEFAULT_SPEED 150 #define DEFAULT_COLOR (0x00F0000000)
#define SPEED_MAX 65535 #define NUM_COLORS (4)
#define SPEED_MIN (10)
#define MODE_COUNT 58 #define DEFAULT_SPEED (150)
#define DEFAULT_MODE 0 #define SPEED_MAX (65535)
#define MODE_COUNT (58)
#define FADE_RATE 2 #define DEFAULT_MODE (0)
#define FADE_RATE (2)
// some common colors /* some common colors */
#define RED (uint32_t)0xFF0000 #define RED (0xFF0000U)
#define GREEN (uint32_t)0x00FF00 #define GREEN (0x00FF00U)
#define BLUE (uint32_t)0x0000FF #define BLUE (0x0000FFU)
#define WHITE (uint32_t)0xFFFFFF #define WHITE (0xFFFFFFU)
#define BLACK (uint32_t)0x000000 #define BLACK (0x000000U)
#define YELLOW (uint32_t)0xFFFF00 #define YELLOW (0xFFFF00U)
#define CYAN (uint32_t)0x00FFFF #define CYAN (0x00FFFFU)
#define MAGENTA (uint32_t)0xFF00FF #define MAGENTA (0xFF00FFU)
#define PURPLE (uint32_t)0x400080 #define PURPLE (0x400080U)
#define ORANGE (uint32_t)0xFF3000 #define ORANGE (0xFF3000U)
#define PINK (uint32_t)0xFF1493 #define PINK (0xFF1493U)
/* Data Structures */
typedef enum { typedef enum {
FX_OK = 0, FX_OK = 0,
FX_ERROR = 1 FX_ERROR = 1
} FX_STATUS; } FX_STATUS;
typedef enum { typedef enum {
FX_MODE_STATIC, FX_MODE_STATIC,
FX_MODE_WHITE_TO_COLOR, FX_MODE_WHITE_TO_COLOR,
FX_MODE_BLACK_TO_COLOR, FX_MODE_BLACK_TO_COLOR,
FX_MODE_BLINK, FX_MODE_BLINK,
FX_MODE_BLINK_RAINBOW, FX_MODE_BLINK_RAINBOW,
FX_MODE_STROBE, FX_MODE_STROBE,
FX_MODE_STROBE_RAINBOW, FX_MODE_STROBE_RAINBOW,
FX_MODE_BREATH, FX_MODE_BREATH,
FX_MODE_COLOR_WIPE, FX_MODE_COLOR_WIPE,
FX_MODE_COLOR_WIPE_INV, FX_MODE_COLOR_WIPE_INV,
FX_MODE_COLOR_WIPE_REV, FX_MODE_COLOR_WIPE_REV,
FX_MODE_COLOR_WIPE_REV_INV, FX_MODE_COLOR_WIPE_REV_INV,
FX_MODE_COLOR_WIPE_RANDOM, FX_MODE_COLOR_WIPE_RANDOM,
FX_MODE_COLOR_SWEEP_RANDOM, FX_MODE_COLOR_SWEEP_RANDOM,
FX_MODE_RANDOM_COLOR, FX_MODE_RANDOM_COLOR,
FX_MODE_SINGLE_DYNAMIC, FX_MODE_SINGLE_DYNAMIC,
FX_MODE_MULTI_DYNAMIC, FX_MODE_MULTI_DYNAMIC,
FX_MODE_RAINBOW, FX_MODE_RAINBOW,
FX_MODE_RAINBOW_CYCLE, FX_MODE_RAINBOW_CYCLE,
FX_MODE_FADE, FX_MODE_FADE,
FX_MODE_SCAN, FX_MODE_SCAN,
FX_MODE_DUAL_SCAN, FX_MODE_DUAL_SCAN,
FX_MODE_THEATER_CHASE, FX_MODE_THEATER_CHASE,
FX_MODE_THEATER_CHASE_RAINBOW, FX_MODE_THEATER_CHASE_RAINBOW,
FX_MODE_RUNNING_LIGHTS, FX_MODE_RUNNING_LIGHTS,
FX_MODE_TWINKLE, FX_MODE_TWINKLE,
FX_MODE_TWINKLE_RANDOM, FX_MODE_TWINKLE_RANDOM,
FX_MODE_TWINKLE_FADE, FX_MODE_TWINKLE_FADE,
FX_MODE_TWINKLE_FADE_RANDOM, FX_MODE_TWINKLE_FADE_RANDOM,
FX_MODE_SPARKLE, FX_MODE_SPARKLE,
FX_MODE_FLASH_SPARKLE, FX_MODE_FLASH_SPARKLE,
FX_MODE_HYPER_SPARKLE, FX_MODE_HYPER_SPARKLE,
FX_MODE_MULTI_STROBE, FX_MODE_MULTI_STROBE,
FX_MODE_CHASE_WHITE, FX_MODE_CHASE_WHITE,
FX_MODE_CHASE_COLOR, FX_MODE_CHASE_COLOR,
FX_MODE_CHASE_RANDOM, FX_MODE_CHASE_RANDOM,
FX_MODE_CHASE_RAINBOW, FX_MODE_CHASE_RAINBOW,
FX_MODE_CHASE_FLASH, FX_MODE_CHASE_FLASH,
FX_MODE_CHASE_FLASH_RANDOM, FX_MODE_CHASE_FLASH_RANDOM,
FX_MODE_CHASE_RAINBOW_WHITE, FX_MODE_CHASE_RAINBOW_WHITE,
FX_MODE_CHASE_BLACKOUT, FX_MODE_CHASE_BLACKOUT,
FX_MODE_CHASE_BLACKOUT_RAINBOW, FX_MODE_CHASE_BLACKOUT_RAINBOW,
FX_MODE_RUNNING_COLOR, FX_MODE_RUNNING_COLOR,
FX_MODE_RUNNING_RED_BLUE, FX_MODE_RUNNING_RED_BLUE,
FX_MODE_RUNNING_RANDOM, FX_MODE_RUNNING_RANDOM,
FX_MODE_LARSON_SCANNER, FX_MODE_LARSON_SCANNER,
FX_MODE_COMET, FX_MODE_COMET,
FX_MODE_FIREWORKS, FX_MODE_FIREWORKS,
FX_MODE_FIREWORKS_RANDOM, FX_MODE_FIREWORKS_RANDOM,
FX_MODE_MERRY_CHRISTMAS, FX_MODE_MERRY_CHRISTMAS,
FX_MODE_FIRE_FLICKER, FX_MODE_FIRE_FLICKER,
FX_MODE_FIRE_FLICKER_SOFT, FX_MODE_FIRE_FLICKER_SOFT,
FX_MODE_FIRE_FLICKER_INTENSE, FX_MODE_FIRE_FLICKER_INTENSE,
FX_MODE_CIRCUS_COMBUSTUS, FX_MODE_CIRCUS_COMBUSTUS,
FX_MODE_HALLOWEEN, FX_MODE_HALLOWEEN,
FX_MODE_BICOLOR_CHASE, FX_MODE_BICOLOR_CHASE,
FX_MODE_TRICOLOR_CHASE, FX_MODE_TRICOLOR_CHASE,
FX_MODE_ICU, FX_MODE_ICU
} fx_mode; } fx_mode;
/* Function Prototypes */
FX_STATUS WS2812BFX_Init(uint16_t Segments); FX_STATUS WS2812BFX_Init(uint16_t Segments);
FX_STATUS WS2812BFX_SegmentIncrease(void); FX_STATUS WS2812BFX_SegmentIncrease(void);
FX_STATUS WS2812BFX_SegmentDecrease(void); FX_STATUS WS2812BFX_SegmentDecrease(void);

149
Src/ws2812b.c

@ -23,12 +23,12 @@
#define BUFFER_SIZE (WORD_SIZE * 2) #define BUFFER_SIZE (WORD_SIZE * 2)
/* Macros */ /* Macros */
#define return_if_fail(cond) if(!(cond)) { return; } #define ws2812_return_if_fail(cond) if(!(cond)) { return; }
#define return_val_if_fail(cond, val) if(!(cond)) { return (val); } #define ws2812_return_val_if_fail(cond, val) if(!(cond)) { return (val); }
#define return_null_if_fail(cond) return_val_if_fail((cond), NULL) #define ws2812_return_null_if_fail(cond) ws2812_return_val_if_fail((cond), NULL)
/* Private Constants */ /* Private Constants */
static const uint8_t _sineTable[256] = { static const uint8_t sine_lut8[256] = {
128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173, 128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,
176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215, 176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,
218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244, 218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,
@ -46,7 +46,7 @@ static const uint8_t _sineTable[256] = {
37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124}; 79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124};
static const uint8_t _gammaTable[256] = { static const uint8_t gamma_lut8[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
@ -70,114 +70,104 @@ ws2812b_color ws2812b_array[WS2812B_LEDS];
/* Private Global Variables */ /* Private Global Variables */
static uint8_t buffer[BUFFER_SIZE]; static uint8_t buffer[BUFFER_SIZE];
static uint16_t CurrentLed; static uint16_t curr_led;
static uint8_t ResetSignal; static uint8_t reset_sig;
void WS2812B_Init(SPI_HandleTypeDef * spi_handler) /* Function Definitions */
void ws2812_init(SPI_HandleTypeDef *hspi)
{ {
hspi_ws2812b = spi_handler; hspi_ws2812b = hspi;
} }
void WS2812B_SetDiodeColor(uint16_t diode_id, uint32_t color) void ws2812_set_led_color_val(uint16_t diode_id, uint32_t color)
{ {
return_if_fail(diode_id < WS2812B_LEDS); ws2812_return_if_fail(diode_id < WS2812B_LEDS);
ws2812b_array[diode_id].u32 = color; ws2812b_array[diode_id].u32 = color;
} }
void WS2812B_SetDiodeColorStruct(uint16_t diode_id, ws2812b_color color) void ws2812_set_led_color(uint16_t diode_id, ws2812b_color color)
{ {
return_if_fail(diode_id < WS2812B_LEDS); ws2812_return_if_fail(diode_id < WS2812B_LEDS);
ws2812b_array[diode_id] = color; ws2812b_array[diode_id] = color;
} }
void WS2812B_SetDiodeRGB(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b) void ws2812_set_led_rgb(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b)
{ {
return_if_fail(diode_id < WS2812B_LEDS); ws2812_return_if_fail(diode_id < WS2812B_LEDS);
ws2812b_array[diode_id].rgb.red = r; ws2812b_array[diode_id].rgb.red = r;
ws2812b_array[diode_id].rgb.green = g; ws2812b_array[diode_id].rgb.green = g;
ws2812b_array[diode_id].rgb.blue = b; ws2812b_array[diode_id].rgb.blue = b;
} }
void WS2812B_SetDiodeRGBX(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b, uint8_t x) void ws2812_set_led_rgbx(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b, uint8_t x)
{ {
return_if_fail(diode_id < WS2812B_LEDS); ws2812_return_if_fail(diode_id < WS2812B_LEDS);
ws2812b_array[diode_id].rgbx.red = r; ws2812b_array[diode_id].rgbx.red = r;
ws2812b_array[diode_id].rgbx.green = g; ws2812b_array[diode_id].rgbx.green = g;
ws2812b_array[diode_id].rgbx.blue = b; ws2812b_array[diode_id].rgbx.blue = b;
ws2812b_array[diode_id].rgbx.x = x; ws2812b_array[diode_id].rgbx.x = x;
} }
uint32_t WS2812B_GetColor(uint16_t diode_id) /*
{ * Set diode with HSV model
return_val_if_fail(diode_id < WS2812B_LEDS, 0); *
* Hue 0-359
return ws2812b_array[diode_id].u32; * Saturation 0-255
} * Value 0-255
*/
uint8_t* WS2812B_GetPixels(void) void ws2812_set_led_hsv(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t value)
{
return (uint8_t*)ws2812b_array;
}
//
// Set diode with HSV model
//
// Hue 0-359
// Saturation 0-255
// Birghtness(Value) 0-255
//
void WS2812B_SetDiodeHSV(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t brt)
{ {
uint16_t sector, fract, p, q, t; uint16_t sector, fract, p, q, t;
return_if_fail(diode_id < WS2812B_LEDS); ws2812_return_if_fail(diode_id < WS2812B_LEDS);
if(sat == 0) if(sat == 0)
{ {
ws2812b_array[diode_id].rgb.red = brt; ws2812b_array[diode_id].rgb.red = value;
ws2812b_array[diode_id].rgb.green = brt; ws2812b_array[diode_id].rgb.green = value;
ws2812b_array[diode_id].rgb.blue = brt; ws2812b_array[diode_id].rgb.blue = value;
} }
else else
{ {
if(hue >= 360) hue = 359; if(hue >= 360) hue = 359;
sector = hue / 60; // Sector 0 to 5 sector = hue / 60; /* hexcone HSV model -- 6 sectors */
fract = hue % 60; fract = hue % 60;
p = (brt * (255 - sat)) / 256; p = (value * (255 - sat)) / 256;
q = (brt * (255 - (sat * fract) / 60)) / 256; q = (value * (255 - (sat * fract) / 60)) / 256;
t = (brt * (255 - (sat * (59 - fract)) / 60)) / 256; t = (value * (255 - (sat * (59 - fract)) / 60)) / 256;
switch(sector) switch(sector)
{ {
case 0: case 0:
ws2812b_array[diode_id].rgb.red = brt; ws2812b_array[diode_id].rgb.red = value;
ws2812b_array[diode_id].rgb.green = (uint8_t)t; ws2812b_array[diode_id].rgb.green = (uint8_t)t;
ws2812b_array[diode_id].rgb.blue = (uint8_t)p; ws2812b_array[diode_id].rgb.blue = (uint8_t)p;
break; break;
case 1: case 1:
ws2812b_array[diode_id].rgb.red = (uint8_t)q; ws2812b_array[diode_id].rgb.red = (uint8_t)q;
ws2812b_array[diode_id].rgb.green = brt; ws2812b_array[diode_id].rgb.green = value;
ws2812b_array[diode_id].rgb.blue = (uint8_t)p; ws2812b_array[diode_id].rgb.blue = (uint8_t)p;
break; break;
case 2: case 2:
ws2812b_array[diode_id].rgb.red = (uint8_t)p; ws2812b_array[diode_id].rgb.red = (uint8_t)p;
ws2812b_array[diode_id].rgb.green = brt; ws2812b_array[diode_id].rgb.green = value;
ws2812b_array[diode_id].rgb.blue = (uint8_t)t; ws2812b_array[diode_id].rgb.blue = (uint8_t)t;
break; break;
case 3: case 3:
ws2812b_array[diode_id].rgb.red = (uint8_t)p; ws2812b_array[diode_id].rgb.red = (uint8_t)p;
ws2812b_array[diode_id].rgb.green = (uint8_t)q; ws2812b_array[diode_id].rgb.green = (uint8_t)q;
ws2812b_array[diode_id].rgb.blue = brt; ws2812b_array[diode_id].rgb.blue = value;
break; break;
case 4: case 4:
ws2812b_array[diode_id].rgb.red = (uint8_t)t; ws2812b_array[diode_id].rgb.red = (uint8_t)t;
ws2812b_array[diode_id].rgb.green = (uint8_t)p; ws2812b_array[diode_id].rgb.green = (uint8_t)p;
ws2812b_array[diode_id].rgb.blue = brt; ws2812b_array[diode_id].rgb.blue = value;
break; break;
case 5: case 5:
default: default:
ws2812b_array[diode_id].rgb.red = brt; ws2812b_array[diode_id].rgb.red = value;
ws2812b_array[diode_id].rgb.green = (uint8_t)p; ws2812b_array[diode_id].rgb.green = (uint8_t)p;
ws2812b_array[diode_id].rgb.blue = (uint8_t)q; ws2812b_array[diode_id].rgb.blue = (uint8_t)q;
break; break;
@ -185,10 +175,22 @@ void WS2812B_SetDiodeHSV(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t b
} }
} }
void WS2812B_Refresh() uint32_t ws2812_get_color(uint16_t diode_id)
{
ws2812_return_val_if_fail(diode_id < WS2812B_LEDS, 0);
return ws2812b_array[diode_id].u32;
}
uint8_t *ws2812_get_pixels()
{
return (uint8_t*)ws2812b_array;
}
void ws2812_refresh()
{ {
CurrentLed = 0; curr_led = 0;
ResetSignal = 0; reset_sig = 0;
for(uint8_t i = 0; i < BUFFER_SIZE; i++) for(uint8_t i = 0; i < BUFFER_SIZE; i++)
buffer[i] = 0x00; buffer[i] = 0x00;
@ -196,21 +198,32 @@ void WS2812B_Refresh()
while(HAL_DMA_STATE_READY != HAL_DMA_GetState(hspi_ws2812b->hdmatx)); while(HAL_DMA_STATE_READY != HAL_DMA_GetState(hspi_ws2812b->hdmatx));
} }
uint8_t sine8(uint8_t x)
{
return sine_lut8[x];
}
uint8_t gamma8(uint8_t x)
{
return gamma_lut8[x];
}
/* Private Function Definitions */
void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi) void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
{ {
if(hspi == hspi_ws2812b) if(hspi == hspi_ws2812b)
{ {
if(!ResetSignal) if(!reset_sig)
{ {
for(uint8_t k = 0; k < WORD_SIZE; k++) for(uint8_t k = 0; k < WORD_SIZE; k++)
{ {
buffer[k] = 0x00; buffer[k] = 0x00;
} }
ResetSignal = 1; // End reset signal reset_sig = 1; /* End reset signal */
} }
else // LEDs Odd 1,3,5,7... else /* Odd LEDs 1, 3, 5, ... */
{ {
if(CurrentLed > WS2812B_LEDS) if(curr_led > WS2812B_LEDS)
{ {
HAL_SPI_DMAStop(hspi_ws2812b); HAL_SPI_DMAStop(hspi_ws2812b);
} }
@ -219,13 +232,13 @@ void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
uint8_t i = 0; uint8_t i = 0;
for(int8_t j = WORD_SIZE - 1; j >= 0; j--) for(int8_t j = WORD_SIZE - 1; j >= 0; j--)
{ {
if((ws2812b_array[CurrentLed].u32 & (1 << j)) == 0) if((ws2812b_array[curr_led].u32 & (1 << j)) == 0)
buffer[i] = ZERO; buffer[i] = ZERO;
else else
buffer[i] = ONE; buffer[i] = ONE;
i++; i++;
} }
CurrentLed++; curr_led++;
} }
} }
} }
@ -235,34 +248,24 @@ void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{ {
if(hspi == hspi_ws2812b) if(hspi == hspi_ws2812b)
{ {
if(CurrentLed > WS2812B_LEDS) if(curr_led > WS2812B_LEDS)
{ {
HAL_SPI_DMAStop(hspi_ws2812b); HAL_SPI_DMAStop(hspi_ws2812b);
} }
else else
{ {
// Even LEDs 0,2,0 /* Even LEDs 0, 2, 4, ... */
uint8_t i = WORD_SIZE; uint8_t i = WORD_SIZE;
for(int8_t j = WORD_SIZE - 1; j >= 0; j--) for(int8_t j = WORD_SIZE - 1; j >= 0; j--)
{ {
if((ws2812b_array[CurrentLed].u32 & (1 << j)) == 0) if((ws2812b_array[curr_led].u32 & (1 << j)) == 0)
buffer[i] = ZERO; buffer[i] = ZERO;
else else
buffer[i] = ONE; buffer[i] = ONE;
i++; i++;
} }
CurrentLed++; curr_led++;
} }
} }
} }
uint8_t sine8(uint8_t x)
{
return _sineTable[x];
}
uint8_t gamma8(uint8_t x)
{
return _gammaTable[x];
}

451
Src/ws2812b_fx.c

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save