From 54859cae646e15a80914ad906b22620b5fa96930 Mon Sep 17 00:00:00 2001 From: Mateusz Salamon Date: Wed, 7 Nov 2018 22:23:10 +0100 Subject: [PATCH] Working demo without segments feature --- Inc/ws2812b_fx.h | 6 +++--- README.md | 5 +++++ Src/main.c | 43 ++++++++++++++++++++++++++++++++++++++----- Src/ws2812b_fx.c | 21 +++++++-------------- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/Inc/ws2812b_fx.h b/Inc/ws2812b_fx.h index 9bbf1eb..bd3c275 100644 --- a/Inc/ws2812b_fx.h +++ b/Inc/ws2812b_fx.h @@ -25,7 +25,7 @@ #define DEFAULT_BRIGHTNESS 50 #define BRIGHTNESS_MAX 255 -#define MODE_COUNT 62 +#define MODE_COUNT 58 #define DEFAULT_MODE 0 #define FADE_RATE 2 @@ -121,8 +121,8 @@ void WS2812BFX_SetColorStruct(uint8_t id, ws2812b_color c); void WS2812BFX_SetColorRGB(uint8_t id, uint8_t r, uint8_t g, uint8_t b); void WS2812BFX_SetColorHSV(uint8_t id, uint16_t h, uint8_t s, uint8_t v); void WS2812BFX_SetColor(uint8_t id, uint32_t c); -void WS2812BFX_SetColorAll(uint32_t c); -void WS2812BFX_SetColorAllRGB(uint8_t r, uint8_t g, uint8_t b); +void WS2812BFX_SetAll(uint32_t c); +void WS2812BFX_SetAllRGB(uint8_t r, uint8_t g, uint8_t b); void WS2812BFX_RGBtoHSV(uint8_t r, uint8_t g, uint8_t b, uint16_t *h, uint8_t *s, uint8_t *v); void WS2812BFX_HSVtoRGB(uint16_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g, uint8_t *b); diff --git a/README.md b/README.md index eca0885..da97395 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,10 @@ www.msalamon.pl Diodes are driving by SPI interface with DMA. +Library description: +https://msalamon.pl/adresowalne-diody-ws2812b-na-stm32-cz-1/ +https://msalamon.pl/adresowalne-diody-ws2812b-na-stm32-cz-2/ + The FX library is based od WS2812FX Arduino librarby by kitesurfer1404 - https://github.com/kitesurfer1404/WS2812FX + diff --git a/Src/main.c b/Src/main.c index ed7ade3..faad4f5 100644 --- a/Src/main.c +++ b/Src/main.c @@ -57,13 +57,20 @@ /* USER CODE BEGIN Includes */ #include "ws2812b.h" #include "ws2812b_fx.h" +#include "usbd_cdc_if.h" + +#define MODE_DELAY 15000 /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ +uint8_t USBDataTX[40]; // Array for transmission USB messages +uint8_t USBDataLength; // USB message length +volatile uint16_t ModeTimer; +uint8_t Mode; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -112,13 +119,17 @@ int main(void) MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ WS2812B_Init(&hspi1); - WS2812BFX_SetSpeed(5000); - WS2812BFX_SetColorRGB(0, 0,255,0); - WS2812BFX_SetColorRGB(1, 32,0,0); - WS2812BFX_SetColorRGB(2, 0,64,0); - WS2812BFX_SetMode(FX_MODE_WHITE_TO_COLOR); + WS2812BFX_SetSpeed(150); + WS2812BFX_SetColorRGB(0, 0,0,0); + WS2812BFX_SetColorRGB(1, 64,0,0); + WS2812BFX_SetColorRGB(2, 0,0,64); + + Mode = 0; + WS2812BFX_SetMode(Mode); WS2812BFX_Start(); + WS2812BFX_SetColorRGB(0, 0, 64,0); + ModeTimer = MODE_DELAY; /* USER CODE END 2 */ /* Infinite loop */ @@ -129,6 +140,27 @@ int main(void) /* USER CODE BEGIN 3 */ WS2812BFX_Callback(); + + if(!ModeTimer) + { + for(uint8_t i = 0; i < WS2812B_LEDS; i++) + WS2812B_SetDiodeColor(i, 0); + WS2812B_Refresh(); + + USBDataLength = sprintf((char*)USBDataTX, "Mode number: %d\n\r", Mode); + CDC_Transmit_FS(USBDataTX, USBDataLength); + if(Mode == 1 || Mode == 2) + WS2812BFX_SetSpeed(3000); + else + WS2812BFX_SetSpeed(200); + + WS2812BFX_SetMode(Mode); + + if(Mode < MODE_COUNT - 1) Mode++; + else Mode = 0; + + ModeTimer = MODE_DELAY; + } } /* USER CODE END 3 */ @@ -197,6 +229,7 @@ void SystemClock_Config(void) void HAL_SYSTICK_Callback(void) { WS2812BFX_SysTickCallback(); + if(ModeTimer) ModeTimer--; } /* USER CODE END 4 */ diff --git a/Src/ws2812b_fx.c b/Src/ws2812b_fx.c index 0071ea0..4ef3931 100644 --- a/Src/ws2812b_fx.c +++ b/Src/ws2812b_fx.c @@ -228,24 +228,17 @@ void WS2812BFX_SetColor(uint8_t id, uint32_t c) mColor_w[id].blue = (c&0x000000FF); } -void WS2812BFX_SetColorAll(uint32_t c) +void WS2812BFX_SetAll(uint32_t c) { - mColor[0] = c; - mColor_w[0].red = ((c>>16)&0x000000FF); - mColor_w[0].green = ((c>>8)&0x000000FF); - mColor_w[0].blue = (c&0x000000FF); + for(uint16_t i = 0; i < WS2812B_LEDS; i++) { - WS2812B_SetDiodeRGB(i, mColor_w[0].red, mColor_w[0].green, mColor_w[0].blue); + WS2812B_SetDiodeRGB(i, ((c>>16)&0xFF), ((c>>8)&0xFF), (c&0xFF)); } } -void WS2812BFX_SetColorAllRGB(uint8_t r, uint8_t g, uint8_t b) +void WS2812BFX_SetAllRGB(uint8_t r, uint8_t g, uint8_t b) { - mColor[0] = ((r<<16)|(g<<8)|b); - mColor_w[0].red = r; - mColor_w[0].green = g; - mColor_w[0].blue = b; for(uint16_t i = 0; i < WS2812B_LEDS; i++) { WS2812B_SetDiodeRGB(i, r, g, b); @@ -629,7 +622,7 @@ void mode_black_to_color(void) void blink(uint32_t color1, uint32_t color2, uint8_t strobe) { uint32_t color = ((mCounterModeCall & 1) == 0) ? color1 : color2; - WS2812BFX_SetColorAll(color); + WS2812BFX_SetAll(color); if((mCounterModeCall & 1) == 0) iModeDelay = strobe ? 20 : mSpeed / 2; else @@ -679,7 +672,7 @@ void mode_breath(void) uint8_t g = mModeColor_w[0].green * lum / 256; uint8_t b = mModeColor_w[0].blue * lum / 256; - WS2812BFX_SetColorAllRGB(r, g, b); + WS2812BFX_SetAllRGB(r, g, b); mCounterModeStep += 2; if(mCounterModeStep > (512-15)) mCounterModeStep = 15; iModeDelay = delay; @@ -776,7 +769,7 @@ void mode_color_sweep_random(void) void mode_random_color(void) { mAuxParam = get_random_wheel_index(mAuxParam); // aux_param will store our random color wheel index - WS2812BFX_SetColorAll(color_wheel(mAuxParam)); + WS2812BFX_SetAll(color_wheel(mAuxParam)); iModeDelay = mSpeed; }