Browse Source

code cleanup: do away with camelCase; whitespace & formatting

lots more to do in ws2812b_fx.*
master
parent
commit
6a2bb8114f
  1. 31
      Inc/ws2812b.h
  2. 187
      Inc/ws2812b_fx.h
  3. 159
      Src/ws2812b.c
  4. 1599
      Src/ws2812b_fx.c

31
Inc/ws2812b.h

@ -1,11 +1,11 @@ @@ -1,11 +1,11 @@
/*
* ws2812b.h
*
* The MIT License.
* Created on: 14.07.2017
* Author: Mateusz Salamon
* www.msalamon.pl
* mateusz@msalamon.pl
* The MIT License.
* Created on: 14.07.2017
* Author: Mateusz Salamon
* www.msalamon.pl
* mateusz@msalamon.pl
*/
#ifndef WS2812B_H_
@ -36,17 +36,16 @@ typedef union __attribute__((packed, aligned(4))) ws2812b_color_u { @@ -36,17 +36,16 @@ typedef union __attribute__((packed, aligned(4))) ws2812b_color_u {
} ws2812b_color;
/* Function Prototypes */
void WS2812B_Init(SPI_HandleTypeDef * spi_handler);
void WS2812B_SetDiodeColor(uint16_t diode_id, uint32_t color);
void WS2812B_SetDiodeColorStruct(uint16_t diode_id, ws2812b_color color);
void WS2812B_SetDiodeRGB(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 WS2812B_SetDiodeHSV(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t brt);
uint32_t WS2812B_GetColor(uint16_t diode_id);
uint8_t* WS2812B_GetPixels(void);
void WS2812B_Refresh();
// color correction
void ws2812_init(SPI_HandleTypeDef *hspi);
void ws2812_set_led_color_val(uint16_t diode_id, uint32_t color);
void ws2812_set_led_color(uint16_t diode_id, ws2812b_color color);
void ws2812_set_led_rgb(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b);
void ws2812_set_led_rgbx(uint16_t diode_id, uint8_t r, uint8_t g, uint8_t b, uint8_t x);
void ws2812_set_led_hsv(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t value);
uint32_t ws2812_get_color(uint16_t diode_id);
uint8_t *ws2812_get_pixels();
void ws2812_refresh();
uint8_t sine8(uint8_t x);
uint8_t gamma8(uint8_t x);
#endif /* WS2812B_H_ */

187
Inc/ws2812b_fx.h

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

159
Src/ws2812b.c

@ -1,11 +1,11 @@ @@ -1,11 +1,11 @@
/*
* ws2812b.c
*
* The MIT License.
* Created on: 14.07.2017
* Author: Mateusz Salamon
* www.msalamon.pl
* mateusz@msalamon.pl
* The MIT License.
* Created on: 14.07.2017
* Author: Mateusz Salamon
* www.msalamon.pl
* mateusz@msalamon.pl
*/
/* Includes */
@ -23,12 +23,12 @@ @@ -23,12 +23,12 @@
#define BUFFER_SIZE (WORD_SIZE * 2)
/* Macros */
#define return_if_fail(cond) if(!(cond)) { return; }
#define 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_if_fail(cond) if(!(cond)) { return; }
#define ws2812_return_val_if_fail(cond, val) if(!(cond)) { return (val); }
#define ws2812_return_null_if_fail(cond) ws2812_return_val_if_fail((cond), NULL)
/* 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,
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,
@ -46,7 +46,7 @@ static const uint8_t _sineTable[256] = { @@ -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,
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, 1, 1, 1, 1, 1, 1, 1, 1,
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]; @@ -70,114 +70,104 @@ ws2812b_color ws2812b_array[WS2812B_LEDS];
/* Private Global Variables */
static uint8_t buffer[BUFFER_SIZE];
static uint16_t CurrentLed;
static uint8_t ResetSignal;
static uint16_t curr_led;
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;
}
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;
}
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.green = g;
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.green = g;
ws2812b_array[diode_id].rgbx.blue = b;
ws2812b_array[diode_id].rgbx.x = x;
}
uint32_t WS2812B_GetColor(uint16_t diode_id)
{
return_val_if_fail(diode_id < WS2812B_LEDS, 0);
return ws2812b_array[diode_id].u32;
}
uint8_t* WS2812B_GetPixels(void)
{
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)
/*
* Set diode with HSV model
*
* Hue 0-359
* Saturation 0-255
* Value 0-255
*/
void ws2812_set_led_hsv(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t value)
{
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)
{
ws2812b_array[diode_id].rgb.red = brt;
ws2812b_array[diode_id].rgb.green = brt;
ws2812b_array[diode_id].rgb.blue = brt;
ws2812b_array[diode_id].rgb.red = value;
ws2812b_array[diode_id].rgb.green = value;
ws2812b_array[diode_id].rgb.blue = value;
}
else
{
if(hue >= 360) hue = 359;
sector = hue / 60; // Sector 0 to 5
sector = hue / 60; /* hexcone HSV model -- 6 sectors */
fract = hue % 60;
p = (brt * (255 - sat)) / 256;
q = (brt * (255 - (sat * fract) / 60)) / 256;
t = (brt * (255 - (sat * (59 - fract)) / 60)) / 256;
p = (value * (255 - sat)) / 256;
q = (value * (255 - (sat * fract) / 60)) / 256;
t = (value * (255 - (sat * (59 - fract)) / 60)) / 256;
switch(sector)
{
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.blue = (uint8_t)p;
break;
case 1:
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;
break;
case 2:
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;
break;
case 3:
ws2812b_array[diode_id].rgb.red = (uint8_t)p;
ws2812b_array[diode_id].rgb.green = (uint8_t)q;
ws2812b_array[diode_id].rgb.blue = brt;
ws2812b_array[diode_id].rgb.blue = value;
break;
case 4:
ws2812b_array[diode_id].rgb.red = (uint8_t)t;
ws2812b_array[diode_id].rgb.green = (uint8_t)p;
ws2812b_array[diode_id].rgb.blue = brt;
ws2812b_array[diode_id].rgb.blue = value;
break;
case 5:
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.blue = (uint8_t)q;
break;
@ -185,10 +175,22 @@ void WS2812B_SetDiodeHSV(uint16_t diode_id, uint16_t hue, uint8_t sat, uint8_t b @@ -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;
ResetSignal = 0;
curr_led = 0;
reset_sig = 0;
for(uint8_t i = 0; i < BUFFER_SIZE; i++)
buffer[i] = 0x00;
@ -196,21 +198,32 @@ void WS2812B_Refresh() @@ -196,21 +198,32 @@ void WS2812B_Refresh()
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)
{
if(hspi == hspi_ws2812b)
{
if(!ResetSignal)
if(!reset_sig)
{
for(uint8_t k = 0; k < WORD_SIZE; k++)
{
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);
}
@ -219,13 +232,13 @@ void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi) @@ -219,13 +232,13 @@ void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
uint8_t i = 0;
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;
else
buffer[i] = ONE;
i++;
}
CurrentLed++;
curr_led++;
}
}
}
@ -235,34 +248,24 @@ void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) @@ -235,34 +248,24 @@ void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
if(hspi == hspi_ws2812b)
{
if(CurrentLed > WS2812B_LEDS)
if(curr_led > WS2812B_LEDS)
{
HAL_SPI_DMAStop(hspi_ws2812b);
}
else
{
// Even LEDs 0,2,0
/* Even LEDs 0, 2, 4, ... */
uint8_t i = WORD_SIZE;
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;
else
buffer[i] = ONE;
i++;
}
CurrentLed++;
curr_led++;
}
}
}
uint8_t sine8(uint8_t x)
{
return _sineTable[x];
}
uint8_t gamma8(uint8_t x)
{
return _gammaTable[x];
}

1599
Src/ws2812b_fx.c

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