You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.9 KiB

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