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