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