|
@@ -1,8 +1,12 @@
|
|
|
-#include "ch.h"
|
|
|
|
|
-#include "hal.h"
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+ * LEDDriver.c
|
|
|
|
|
+ *
|
|
|
|
|
+ * Created on: Aug 26, 2013
|
|
|
|
|
+ * Author: Omri Iluz
|
|
|
|
|
+ */
|
|
|
|
|
|
|
|
-#include "hsv2rgb.h"
|
|
|
|
|
-#include "underglow.h"
|
|
|
|
|
|
|
+#include "ws2812.h"
|
|
|
|
|
+#include "stdlib.h"
|
|
|
|
|
|
|
|
#define BYTES_FOR_LED_BYTE 4
|
|
#define BYTES_FOR_LED_BYTE 4
|
|
|
#define NB_COLORS 3
|
|
#define NB_COLORS 3
|
|
@@ -10,18 +14,17 @@
|
|
|
#define DATA_SIZE BYTES_FOR_LED*NB_LEDS
|
|
#define DATA_SIZE BYTES_FOR_LED*NB_LEDS
|
|
|
#define RESET_SIZE 200
|
|
#define RESET_SIZE 200
|
|
|
#define PREAMBLE_SIZE 4
|
|
#define PREAMBLE_SIZE 4
|
|
|
-
|
|
|
|
|
// Define the spi your LEDs are plugged to here
|
|
// Define the spi your LEDs are plugged to here
|
|
|
-#define LEDS_SPI SPID2
|
|
|
|
|
|
|
+#define WS2812_SPI SPID2
|
|
|
// Define the number of LEDs you wish to control in your LED strip
|
|
// Define the number of LEDs you wish to control in your LED strip
|
|
|
-#define NB_LEDS 8
|
|
|
|
|
|
|
+#define NB_LEDS RGBLED_NUM
|
|
|
|
|
|
|
|
-#define LED_SPIRAL 1
|
|
|
|
|
|
|
+ #define LED_SPIRAL 1
|
|
|
|
|
|
|
|
-static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE];
|
|
|
|
|
|
|
+ static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE];
|
|
|
static uint8_t get_protocol_eq(uint8_t data, int pos);
|
|
static uint8_t get_protocol_eq(uint8_t data, int pos);
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
|
|
+ /*
|
|
|
* This lib is meant to be used asynchronously, thus the colors contained in
|
|
* This lib is meant to be used asynchronously, thus the colors contained in
|
|
|
* the txbuf will be sent in loop, so that the colors are always the ones you
|
|
* the txbuf will be sent in loop, so that the colors are always the ones you
|
|
|
* put in the table (the user thus have less to worry about)
|
|
* put in the table (the user thus have less to worry about)
|
|
@@ -37,38 +40,18 @@ static THD_WORKING_AREA(LEDS_THREAD_WA, 128);
|
|
|
static THD_FUNCTION(ledsThread, arg) {
|
|
static THD_FUNCTION(ledsThread, arg) {
|
|
|
(void) arg;
|
|
(void) arg;
|
|
|
while(1){
|
|
while(1){
|
|
|
- spiSend(&LEDS_SPI, PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE, txbuf);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-#if LED_SPIRAL
|
|
|
|
|
-/*
|
|
|
|
|
- * 'Led spiral' is a simple demo in which we put all the leds to the same
|
|
|
|
|
- * color, where this color does all the hsv circle in loop.
|
|
|
|
|
- * If you want to launch the thread that will chage the led colors to the
|
|
|
|
|
- * appropriate value, simply set LED_SPIRAL to 1.
|
|
|
|
|
- */
|
|
|
|
|
-static THD_WORKING_AREA(HSVTRANS_WA, 128);
|
|
|
|
|
-static THD_FUNCTION(hsv_transThread, arg){
|
|
|
|
|
- (void) arg;
|
|
|
|
|
- hsv_color color = {0, 255, 127};
|
|
|
|
|
- while(1){
|
|
|
|
|
- color.h += 1;
|
|
|
|
|
- color.h %= 256;
|
|
|
|
|
- set_leds_color_hsv(color);
|
|
|
|
|
- chThdSleepMilliseconds(50);
|
|
|
|
|
|
|
+ spiSend(&WS2812_SPI, PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE, txbuf);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
-static const SPIConfig spicfg = {
|
|
|
|
|
|
|
+ static const SPIConfig spicfg = {
|
|
|
NULL,
|
|
NULL,
|
|
|
- GPIOB,
|
|
|
|
|
- 15,
|
|
|
|
|
- SPI_CR1_BR_1|SPI_CR1_BR_0 // baudrate : fpclk / 8 => 1tick is 0.32us
|
|
|
|
|
|
|
+ PORT_WS2812,
|
|
|
|
|
+ PIN_WS2812,
|
|
|
|
|
+ SPI_CR1_BR_1|SPI_CR1_BR_0 // baudrate : fpclk / 8 => 1tick is 0.32us (2.25 MHz)
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
|
|
+ /*
|
|
|
* Function used to initialize the driver.
|
|
* Function used to initialize the driver.
|
|
|
*
|
|
*
|
|
|
* Starts by shutting off all the LEDs.
|
|
* Starts by shutting off all the LEDs.
|
|
@@ -77,21 +60,19 @@ static const SPIConfig spicfg = {
|
|
|
* txbuff values)
|
|
* txbuff values)
|
|
|
*/
|
|
*/
|
|
|
void leds_init(void){
|
|
void leds_init(void){
|
|
|
|
|
+ /* MOSI pin*/
|
|
|
|
|
+ palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
|
|
|
for(int i = 0; i < RESET_SIZE; i++)
|
|
for(int i = 0; i < RESET_SIZE; i++)
|
|
|
txbuf[DATA_SIZE+i] = 0x00;
|
|
txbuf[DATA_SIZE+i] = 0x00;
|
|
|
for (int i=0; i<PREAMBLE_SIZE; i++)
|
|
for (int i=0; i<PREAMBLE_SIZE; i++)
|
|
|
txbuf[i] = 0x00;
|
|
txbuf[i] = 0x00;
|
|
|
- spiAcquireBus(&LEDS_SPI); /* Acquire ownership of the bus. */
|
|
|
|
|
- spiStart(&LEDS_SPI, &spicfg); /* Setup transfer parameters. */
|
|
|
|
|
- spiSelect(&LEDS_SPI); /* Slave Select assertion. */
|
|
|
|
|
|
|
+ spiAcquireBus(&WS2812_SPI); /* Acquire ownership of the bus. */
|
|
|
|
|
+ spiStart(&WS2812_SPI, &spicfg); /* Setup transfer parameters. */
|
|
|
|
|
+ spiSelect(&WS2812_SPI); /* Slave Select assertion. */
|
|
|
chThdCreateStatic(LEDS_THREAD_WA, sizeof(LEDS_THREAD_WA),NORMALPRIO, ledsThread, NULL);
|
|
chThdCreateStatic(LEDS_THREAD_WA, sizeof(LEDS_THREAD_WA),NORMALPRIO, ledsThread, NULL);
|
|
|
-#if LED_SPIRAL
|
|
|
|
|
- chThdCreateStatic(HSVTRANS_WA, sizeof(HSVTRANS_WA),
|
|
|
|
|
- NORMALPRIO, hsv_transThread, NULL);
|
|
|
|
|
-#endif
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
|
|
+ /*
|
|
|
* As the trick here is to use the SPI to send a huge pattern of 0 and 1 to
|
|
* As the trick here is to use the SPI to send a huge pattern of 0 and 1 to
|
|
|
* the ws2812b protocol, we use this helper function to translate bytes into
|
|
* the ws2812b protocol, we use this helper function to translate bytes into
|
|
|
* 0s and 1s for the LED (with the appropriate timing).
|
|
* 0s and 1s for the LED (with the appropriate timing).
|
|
@@ -109,20 +90,20 @@ static uint8_t get_protocol_eq(uint8_t data, int pos){
|
|
|
return eq;
|
|
return eq;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
- * If you want to set a LED's color in the HSV color space, simply call this
|
|
|
|
|
- * function with a hsv_color containing the desired color and the index of the
|
|
|
|
|
- * led on the LED strip (starting from 0, the first one being the closest the
|
|
|
|
|
- * first plugged to the board)
|
|
|
|
|
- *
|
|
|
|
|
- * Only set the color of the LEDs through the functions given by this API
|
|
|
|
|
- * (unless you really know what you are doing)
|
|
|
|
|
- */
|
|
|
|
|
-void set_led_color_hsv(hsv_color color, int pos){
|
|
|
|
|
- set_led_color_rgb(hsv2rgb(color), pos);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ void WS2812_init(void) {
|
|
|
|
|
+ leds_init();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
|
|
+ void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) {
|
|
|
|
|
+ uint8_t i = 0;
|
|
|
|
|
+ while (i < number_of_leds) {
|
|
|
|
|
+ set_led_color_rgb(ledarray[i], i);
|
|
|
|
|
+ i++;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
* If you want to set a LED's color in the RGB color space, simply call this
|
|
* If you want to set a LED's color in the RGB color space, simply call this
|
|
|
* function with a hsv_color containing the desired color and the index of the
|
|
* function with a hsv_color containing the desired color and the index of the
|
|
|
* led on the LED strip (starting from 0, the first one being the closest the
|
|
* led on the LED strip (starting from 0, the first one being the closest the
|
|
@@ -131,7 +112,7 @@ void set_led_color_hsv(hsv_color color, int pos){
|
|
|
* Only set the color of the LEDs through the functions given by this API
|
|
* Only set the color of the LEDs through the functions given by this API
|
|
|
* (unless you really know what you are doing)
|
|
* (unless you really know what you are doing)
|
|
|
*/
|
|
*/
|
|
|
-void set_led_color_rgb(rgb_color color, int pos){
|
|
|
|
|
|
|
+void set_led_color_rgb(LED_TYPE color, int pos){
|
|
|
for(int j = 0; j < 4; j++)
|
|
for(int j = 0; j < 4; j++)
|
|
|
txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + j] = get_protocol_eq(color.g, j);
|
|
txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + j] = get_protocol_eq(color.g, j);
|
|
|
for(int j = 0; j < 4; j++)
|
|
for(int j = 0; j < 4; j++)
|
|
@@ -140,18 +121,12 @@ void set_led_color_rgb(rgb_color color, int pos){
|
|
|
txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE*2+j] = get_protocol_eq(color.b, j);
|
|
txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE*2+j] = get_protocol_eq(color.b, j);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
- * Same as the two above, but sets all the LEDs in the LED strip (HSV)
|
|
|
|
|
- */
|
|
|
|
|
-void set_leds_color_hsv(hsv_color color){
|
|
|
|
|
|
|
+ void set_leds_color_rgb(LED_TYPE color){
|
|
|
for(int i = 0; i < NB_LEDS; i++)
|
|
for(int i = 0; i < NB_LEDS; i++)
|
|
|
- set_led_color_hsv(color, i);
|
|
|
|
|
|
|
+ set_led_color_rgb(color, i);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
- * Same as the two above, but sets all the LEDs in the LED strip (RGB)
|
|
|
|
|
- */
|
|
|
|
|
-void set_leds_color_rgb(rgb_color color){
|
|
|
|
|
- for(int i = 0; i < NB_LEDS; i++)
|
|
|
|
|
- set_led_color_rgb(color, i);
|
|
|
|
|
-}
|
|
|
|
|
|
|
+
|
|
|
|
|
+ void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|