ws2812.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "ch.h"
  2. #include "hal.h"
  3. #include "ws2812.h"
  4. #define BYTES_FOR_LED_BYTE 4
  5. #define NB_COLORS 3
  6. #define BYTES_FOR_LED BYTES_FOR_LED_BYTE*NB_COLORS
  7. #define DATA_SIZE BYTES_FOR_LED*NB_LEDS
  8. #define RESET_SIZE 200
  9. // Define the spi your LEDs are plugged to here
  10. #define LEDS_SPI WS2812_SPI
  11. // Define the number of LEDs you wish to control in your LED strip
  12. #define NB_LEDS RGBLED_NUM
  13. #define LED_SPIRAL 0
  14. static uint8_t txbuf[DATA_SIZE + RESET_SIZE];
  15. static uint8_t get_protocol_eq(uint8_t data, int pos);
  16. /*
  17. * This lib is meant to be used asynchronously, thus the colors contained in
  18. * the txbuf will be sent in loop, so that the colors are always the ones you
  19. * put in the table (the user thus have less to worry about)
  20. *
  21. * Since the data are sent via DMA, and the call to spiSend is a blocking one,
  22. * the processor ressources are not used to much, if you see your program being
  23. * too slow, simply add a:
  24. * chThdSleepMilliseconds(x);
  25. * after the spiSend, where you increment x untill you are satisfied with your
  26. * program speed, another trick may be to lower this thread priority : your call
  27. */
  28. static THD_WORKING_AREA(LEDS_THREAD_WA, 128);
  29. static THD_FUNCTION(ledsThread, arg) {
  30. (void) arg;
  31. while(1){
  32. spiSend(&LEDS_SPI, DATA_SIZE + RESET_SIZE, txbuf);
  33. }
  34. }
  35. #if LED_SPIRAL
  36. /*
  37. * 'Led spiral' is a simple demo in which we put all the leds to the same
  38. * color, where this color does all the hsv circle in loop.
  39. * If you want to launch the thread that will chage the led colors to the
  40. * appropriate value, simply set LED_SPIRAL to 1.
  41. */
  42. static THD_WORKING_AREA(HSVTRANS_WA, 128);
  43. static THD_FUNCTION(hsv_transThread, arg) {
  44. (void) arg;
  45. hsv_color color = {0, 255, 255};
  46. while(1){
  47. color.h += 1;
  48. color.h %= 256;
  49. set_leds_color_hsv(color);
  50. chThdSleepMilliseconds(50);
  51. }
  52. }
  53. #endif
  54. static const SPIConfig spicfg = {
  55. NULL,
  56. GPIOB,
  57. 15,
  58. SPI_CR1_BR_1|SPI_CR1_BR_0 // baudrate : fpclk / 8 => 1tick is 0.32us
  59. };
  60. /*
  61. * Function used to initialize the driver.
  62. *
  63. * Starts by shutting off all the LEDs.
  64. * Then gets access on the LED_SPI driver.
  65. * May eventually launch an animation on the LEDs (e.g. a thread setting the
  66. * txbuff values)
  67. */
  68. void leds_init(void){
  69. for(int i = 0; i < RESET_SIZE; i++)
  70. txbuf[DATA_SIZE+i] = 0x00;
  71. spiAcquireBus(&LEDS_SPI); /* Acquire ownership of the bus. */
  72. spiStart(&LEDS_SPI, &spicfg); /* Setup transfer parameters. */
  73. spiSelect(&LEDS_SPI); /* Slave Select assertion. */
  74. chThdCreateStatic(LEDS_THREAD_WA, sizeof(LEDS_THREAD_WA),NORMALPRIO, ledsThread, NULL);
  75. #if LED_SPIRAL
  76. chThdCreateStatic(HSVTRANS_WA, sizeof(HSVTRANS_WA),
  77. NORMALPRIO, hsv_transThread, NULL);
  78. #endif
  79. }
  80. /*
  81. * As the trick here is to use the SPI to send a huge pattern of 0 and 1 to
  82. * the ws2812b protocol, we use this helper function to translate bytes into
  83. * 0s and 1s for the LED (with the appropriate timing).
  84. */
  85. static uint8_t get_protocol_eq(uint8_t data, int pos){
  86. uint8_t eq = 0;
  87. if (data & (1 << (2*(3-pos))))
  88. eq = 0b1110;
  89. else
  90. eq = 0b1000;
  91. if (data & (2 << (2*(3-pos))))
  92. eq += 0b11100000;
  93. else
  94. eq += 0b10000000;
  95. return eq;
  96. }
  97. //
  98. ///*
  99. // * If you want to set a LED's color in the RGB color space, simply call this
  100. // * function with a hsv_color containing the desired color and the index of the
  101. // * led on the LED strip (starting from 0, the first one being the closest the
  102. // * first plugged to the board)
  103. // *
  104. // * Only set the color of the LEDs through the functions given by this API
  105. // * (unless you really know what you are doing)
  106. // */
  107. void set_led_color_rgb(LED_TYPE color, int pos){
  108. for(int j = 0; j < 4; j++)
  109. txbuf[BYTES_FOR_LED*pos + j] = get_protocol_eq(color.g, j);
  110. for(int j = 0; j < 4; j++)
  111. txbuf[BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE+j] = get_protocol_eq(color.r, j);
  112. for(int j = 0; j < 4; j++)
  113. txbuf[BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE*2+j] = get_protocol_eq(color.b, j);
  114. }
  115. void WS2812_init(void) {
  116. leds_init();
  117. }
  118. void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) {
  119. uint8_t i = 0;
  120. while (i < number_of_leds) {
  121. set_led_color_rgb(ledarray[i], i);
  122. i++;
  123. }
  124. }
  125. void set_leds_color_rgb(LED_TYPE color){
  126. for(int i = 0; i < NB_LEDS; i++)
  127. set_led_color_rgb(color, i);
  128. }
  129. void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds) {
  130. }