]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/led_matrix.c
Cleanup/rgb matrix (#5811)
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / led_matrix.c
1 /*
2 Copyright 2018 Massdrop Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "arm_atsam_protocol.h"
19 #include "tmk_core/common/led.h"
20 #include "rgb_matrix.h"
21 #include <string.h>
22 #include <math.h>
23
24 #ifdef USE_MASSDROP_CONFIGURATOR
25 __attribute__((weak))
26 led_instruction_t led_instructions[] = { { .end = 1 } };
27 static void led_matrix_massdrop_config_override(int i);
28 #endif // USE_MASSDROP_CONFIGURATOR
29
30
31 void SERCOM1_0_Handler( void )
32 {
33     if (SERCOM1->I2CM.INTFLAG.bit.ERROR)
34     {
35         SERCOM1->I2CM.INTFLAG.reg = SERCOM_I2CM_INTENCLR_ERROR;
36     }
37 }
38
39 void DMAC_0_Handler( void )
40 {
41     if (DMAC->Channel[0].CHINTFLAG.bit.TCMPL)
42     {
43         DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TCMPL;
44
45         i2c1_stop();
46
47         i2c_led_q_running = 0;
48
49         i2c_led_q_run();
50
51         return;
52     }
53
54     if (DMAC->Channel[0].CHINTFLAG.bit.TERR)
55     {
56         DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TERR;
57     }
58 }
59
60 issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT];
61
62 issi3733_led_t led_map[ISSI3733_LED_COUNT] = ISSI3733_LED_MAP;
63 RGB led_buffer[ISSI3733_LED_COUNT];
64
65 uint8_t gcr_desired;
66 uint8_t gcr_actual;
67 uint8_t gcr_actual_last;
68 #ifdef USE_MASSDROP_CONFIGURATOR
69 uint8_t gcr_breathe;
70 float breathe_mult;
71 float pomod;
72 #endif
73
74 #define ACT_GCR_NONE    0
75 #define ACT_GCR_INC     1
76 #define ACT_GCR_DEC     2
77
78 #define LED_GCR_STEP_AUTO 2
79
80 static uint8_t gcr_min_counter;
81 static uint8_t v_5v_cat_hit;
82
83 //WARNING: Automatic GCR is in place to prevent USB shutdown and LED driver overloading
84 void gcr_compute(void)
85 {
86     uint8_t action = ACT_GCR_NONE;
87     uint8_t gcr_use = gcr_desired;
88
89 #ifdef USE_MASSDROP_CONFIGURATOR
90     if (led_animation_breathing)
91     {
92         gcr_use = gcr_breathe;
93     }
94 #endif
95
96     //If the 5v takes a catastrophic hit, disable the LED drivers briefly, assert auto gcr mode, min gcr and let the auto take over
97     if (v_5v < V5_CAT)
98     {
99         I2C3733_Control_Set(0);
100         //CDC_print("USB: WARNING: 5V catastrophic level reached! Disabling LED drivers!\r\n"); //Blocking print is bad here!
101         v_5v_cat_hit = 20; //~100ms recover
102         gcr_actual = 0; //Minimize GCR
103         usb_gcr_auto = 1; //Force auto mode enabled
104         return;
105     }
106     else if (v_5v_cat_hit > 1)
107     {
108         v_5v_cat_hit--;
109         return;
110     }
111     else if (v_5v_cat_hit == 1)
112     {
113         I2C3733_Control_Set(1);
114         CDC_print("USB: WARNING: Re-enabling LED drivers\r\n");
115         v_5v_cat_hit = 0;
116         return;
117     }
118
119     if (usb_gcr_auto)
120     {
121         if (v_5v_avg < V5_LOW) action = ACT_GCR_DEC;
122         else if (v_5v_avg > V5_HIGH && gcr_actual < gcr_use) action = ACT_GCR_INC;
123         else if (gcr_actual > gcr_use) action = ACT_GCR_DEC;
124     }
125     else
126     {
127         if (gcr_actual < gcr_use) action = ACT_GCR_INC;
128         else if (gcr_actual > gcr_use) action = ACT_GCR_DEC;
129     }
130
131     if (action == ACT_GCR_NONE)
132     {
133         gcr_min_counter = 0;
134     }
135     else if (action == ACT_GCR_INC)
136     {
137         if (LED_GCR_STEP_AUTO > LED_GCR_MAX - gcr_actual) gcr_actual = LED_GCR_MAX; //Obey max and prevent wrapping
138         else gcr_actual += LED_GCR_STEP_AUTO;
139         gcr_min_counter = 0;
140     }
141     else if (action == ACT_GCR_DEC)
142     {
143         if (LED_GCR_STEP_AUTO > gcr_actual) //Prevent wrapping
144         {
145             gcr_actual = 0;
146             //At this point, power can no longer be cut from the LED drivers, so focus on cutting out extra port if active
147             if (usb_extra_state != USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) //If not in a wait for replug state
148             {
149                 if (usb_extra_state == USB_EXTRA_STATE_ENABLED) //If extra usb is enabled
150                 {
151                     gcr_min_counter++;
152                     if (gcr_min_counter > 200) //5ms per check = 1s delay
153                     {
154                         USB_ExtraSetState(USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG);
155                         usb_extra_manual = 0; //Force disable manual mode of extra port
156                         if (usb_extra_manual) CDC_print("USB: Disabling extra port until replug and manual mode toggle!\r\n");
157                         else CDC_print("USB: Disabling extra port until replug!\r\n");
158                     }
159                 }
160             }
161         }
162         else
163         {
164             //Power successfully cut back from LED drivers
165             gcr_actual -= LED_GCR_STEP_AUTO;
166             gcr_min_counter = 0;
167
168 #ifdef USE_MASSDROP_CONFIGURATOR
169             //If breathe mode is active, the top end can fluctuate if the host can not supply enough current
170             //So set the breathe GCR to where it becomes stable
171             if (led_animation_breathing == 1)
172             {
173                 gcr_breathe = gcr_actual;
174                 //PS: At this point, setting breathing to exhale makes a noticebly shorter cycle
175                 //    and the same would happen maybe one or two more times. Therefore I'm favoring
176                 //    powering through one full breathe and letting gcr settle completely
177             }
178 #endif
179         }
180     }
181 }
182
183 void issi3733_prepare_arrays(void)
184 {
185     memset(issidrv,0,sizeof(issi3733_driver_t) * ISSI3733_DRIVER_COUNT);
186
187     int i;
188     uint8_t addrs[ISSI3733_DRIVER_COUNT] = ISSI3773_DRIVER_ADDRESSES;
189
190     for (i=0;i<ISSI3733_DRIVER_COUNT;i++)
191     {
192         issidrv[i].addr = addrs[i];
193     }
194
195     for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
196     {
197         //BYTE: 1 + (SW-1)*16 + (CS-1)
198         led_map[i].rgb.g = issidrv[led_map[i].adr.drv-1].pwm + 1 + ((led_map[i].adr.swg-1)*16 + (led_map[i].adr.cs-1));
199         led_map[i].rgb.r = issidrv[led_map[i].adr.drv-1].pwm + 1 + ((led_map[i].adr.swr-1)*16 + (led_map[i].adr.cs-1));
200         led_map[i].rgb.b = issidrv[led_map[i].adr.drv-1].pwm + 1 + ((led_map[i].adr.swb-1)*16 + (led_map[i].adr.cs-1));
201
202         //BYTE: 1 + (SW-1)*2 + (CS-1)/8
203         //BIT: (CS-1)%8
204         *(issidrv[led_map[i].adr.drv-1].onoff + 1 + (led_map[i].adr.swg-1)*2+(led_map[i].adr.cs-1)/8) |= (1<<((led_map[i].adr.cs-1)%8));
205         *(issidrv[led_map[i].adr.drv-1].onoff + 1 + (led_map[i].adr.swr-1)*2+(led_map[i].adr.cs-1)/8) |= (1<<((led_map[i].adr.cs-1)%8));
206         *(issidrv[led_map[i].adr.drv-1].onoff + 1 + (led_map[i].adr.swb-1)*2+(led_map[i].adr.cs-1)/8) |= (1<<((led_map[i].adr.cs-1)%8));
207     }
208 }
209
210 void led_matrix_prepare(void)
211 {
212     for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
213     {
214         *led_map[i].rgb.r = 0;
215         *led_map[i].rgb.g = 0;
216         *led_map[i].rgb.b = 0;
217     }
218 }
219
220 void led_set_one(int i, uint8_t r, uint8_t g, uint8_t b)
221 {
222     if (i < ISSI3733_LED_COUNT)
223     {
224 #ifdef USE_MASSDROP_CONFIGURATOR
225         led_matrix_massdrop_config_override(i);
226 #else
227         led_buffer[i].r = r;
228         led_buffer[i].g = g;
229         led_buffer[i].b = b;
230 #endif
231     }
232 }
233
234 void led_set_all(uint8_t r, uint8_t g, uint8_t b)
235 {
236   for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
237   {
238     led_set_one(i, r, g, b);
239   }
240 }
241
242 void init(void)
243 {
244     DBGC(DC_LED_MATRIX_INIT_BEGIN);
245
246     issi3733_prepare_arrays();
247
248     led_matrix_prepare();
249
250     gcr_min_counter = 0;
251     v_5v_cat_hit = 0;
252
253     DBGC(DC_LED_MATRIX_INIT_COMPLETE);
254 }
255
256 void flush(void)
257 {
258 #ifdef USE_MASSDROP_CONFIGURATOR
259     if (!led_enabled) { return; } //Prevent calculations and I2C traffic if LED drivers are not enabled
260 #else
261     if (!sr_exp_data.bit.SDB_N) { return; } //Prevent calculations and I2C traffic if LED drivers are not enabled
262 #endif
263
264     // Wait for previous transfer to complete
265     while (i2c_led_q_running) {}
266
267     // Copy buffer to live DMA region
268     for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
269     {
270         *led_map[i].rgb.r = led_buffer[i].r;
271         *led_map[i].rgb.g = led_buffer[i].g;
272         *led_map[i].rgb.b = led_buffer[i].b;
273     }
274
275 #ifdef USE_MASSDROP_CONFIGURATOR
276     breathe_mult = 1;
277
278     if (led_animation_breathing)
279     {
280         //+60us 119 LED
281         led_animation_breathe_cur += BREATHE_STEP * breathe_dir;
282
283         if (led_animation_breathe_cur >= BREATHE_MAX_STEP)
284             breathe_dir = -1;
285         else if (led_animation_breathe_cur <= BREATHE_MIN_STEP)
286             breathe_dir = 1;
287
288         //Brightness curve created for 256 steps, 0 - ~98%
289         breathe_mult = 0.000015 * led_animation_breathe_cur * led_animation_breathe_cur;
290         if (breathe_mult > 1) breathe_mult = 1;
291         else if (breathe_mult < 0) breathe_mult = 0;
292     }
293
294     //This should only be performed once per frame
295     pomod = (float)((g_rgb_counters.tick / 10) % (uint32_t)(1000.0f / led_animation_speed)) / 10.0f * led_animation_speed;
296     pomod *= 100.0f;
297     pomod = (uint32_t)pomod % 10000;
298     pomod /= 100.0f;
299
300 #endif // USE_MASSDROP_CONFIGURATOR
301
302     uint8_t drvid;
303
304     //NOTE: GCR does not need to be timed with LED processing, but there is really no harm
305     if (gcr_actual != gcr_actual_last)
306     {
307         for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++)
308             I2C_LED_Q_GCR(drvid); //Queue data
309         gcr_actual_last = gcr_actual;
310     }
311
312     for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++)
313         I2C_LED_Q_PWM(drvid); //Queue data
314
315     i2c_led_q_run();
316 }
317
318 void led_matrix_indicators(void)
319 {
320     uint8_t kbled = keyboard_leds();
321     if (kbled && rgb_matrix_config.enable)
322     {
323         for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
324         {
325             if (
326             #if USB_LED_NUM_LOCK_SCANCODE != 255
327                 (led_map[i].scan == USB_LED_NUM_LOCK_SCANCODE && (kbled & (1<<USB_LED_NUM_LOCK))) ||
328             #endif //NUM LOCK
329             #if USB_LED_CAPS_LOCK_SCANCODE != 255
330                 (led_map[i].scan == USB_LED_CAPS_LOCK_SCANCODE && (kbled & (1<<USB_LED_CAPS_LOCK))) ||
331             #endif //CAPS LOCK
332             #if USB_LED_SCROLL_LOCK_SCANCODE != 255
333                 (led_map[i].scan == USB_LED_SCROLL_LOCK_SCANCODE && (kbled & (1<<USB_LED_SCROLL_LOCK))) ||
334             #endif //SCROLL LOCK
335             #if USB_LED_COMPOSE_SCANCODE != 255
336                 (led_map[i].scan == USB_LED_COMPOSE_SCANCODE && (kbled & (1<<USB_LED_COMPOSE))) ||
337             #endif //COMPOSE
338             #if USB_LED_KANA_SCANCODE != 255
339                 (led_map[i].scan == USB_LED_KANA_SCANCODE && (kbled & (1<<USB_LED_KANA))) ||
340             #endif //KANA
341             (0))
342             {
343                 led_buffer[i].r = 255 - led_buffer[i].r;
344                 led_buffer[i].g = 255 - led_buffer[i].g;
345                 led_buffer[i].b = 255 - led_buffer[i].b;
346             }
347         }
348     }
349
350 }
351
352 const rgb_matrix_driver_t rgb_matrix_driver = {
353   .init = init,
354   .flush = flush,
355   .set_color = led_set_one,
356   .set_color_all = led_set_all
357 };
358
359 /*==============================================================================
360 =                           Legacy Lighting Support                            =
361 ==============================================================================*/
362
363 #ifdef USE_MASSDROP_CONFIGURATOR
364 // Ported from Massdrop QMK Github Repo
365
366 // TODO?: wire these up to keymap.c
367 uint8_t led_animation_orientation = 0;
368 uint8_t led_animation_direction = 0;
369 uint8_t led_animation_breathing = 0;
370 uint8_t led_animation_id = 0;
371 float led_animation_speed = 4.0f;
372 uint8_t led_lighting_mode = LED_MODE_NORMAL;
373 uint8_t led_enabled = 1;
374 uint8_t led_animation_breathe_cur = BREATHE_MIN_STEP;
375 uint8_t breathe_dir = 1;
376
377 static void led_run_pattern(led_setup_t *f, float* ro, float* go, float* bo, float pos) {
378     float po;
379
380     while (f->end != 1)
381     {
382         po = pos; //Reset po for new frame
383
384         //Add in any moving effects
385         if ((!led_animation_direction && f->ef & EF_SCR_R) || (led_animation_direction && (f->ef & EF_SCR_L)))
386         {
387             po -= pomod;
388
389             if (po > 100) po -= 100;
390             else if (po < 0) po += 100;
391         }
392         else if ((!led_animation_direction && f->ef & EF_SCR_L) || (led_animation_direction && (f->ef & EF_SCR_R)))
393         {
394             po += pomod;
395
396             if (po > 100) po -= 100;
397             else if (po < 0) po += 100;
398         }
399
400         //Check if LED's po is in current frame
401         if (po < f->hs) { f++; continue; }
402         if (po > f->he) { f++; continue; }
403         //note: < 0 or > 100 continue
404
405         //Calculate the po within the start-stop percentage for color blending
406         po = (po - f->hs) / (f->he - f->hs);
407
408         //Add in any color effects
409         if (f->ef & EF_OVER)
410         {
411             *ro = (po * (f->re - f->rs)) + f->rs;// + 0.5;
412             *go = (po * (f->ge - f->gs)) + f->gs;// + 0.5;
413             *bo = (po * (f->be - f->bs)) + f->bs;// + 0.5;
414         }
415         else if (f->ef & EF_SUBTRACT)
416         {
417             *ro -= (po * (f->re - f->rs)) + f->rs;// + 0.5;
418             *go -= (po * (f->ge - f->gs)) + f->gs;// + 0.5;
419             *bo -= (po * (f->be - f->bs)) + f->bs;// + 0.5;
420         }
421         else
422         {
423             *ro += (po * (f->re - f->rs)) + f->rs;// + 0.5;
424             *go += (po * (f->ge - f->gs)) + f->gs;// + 0.5;
425             *bo += (po * (f->be - f->bs)) + f->bs;// + 0.5;
426         }
427
428         f++;
429     }
430 }
431
432 static void led_matrix_massdrop_config_override(int i)
433 {
434     float ro = 0;
435     float go = 0;
436     float bo = 0;
437
438     float po = (led_animation_orientation)
439         ? (float)g_led_config.point[i].y / 64.f * 100
440         : (float)g_led_config.point[i].x / 224.f * 100;
441
442     uint8_t highest_active_layer = biton32(layer_state);
443
444     if (led_lighting_mode == LED_MODE_KEYS_ONLY && HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) {
445         //Do not act on this LED
446     } else if (led_lighting_mode == LED_MODE_NON_KEYS_ONLY && !HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) {
447         //Do not act on this LED
448     } else if (led_lighting_mode == LED_MODE_INDICATORS_ONLY) {
449         //Do not act on this LED (Only show indicators)
450     } else {
451         led_instruction_t* led_cur_instruction = led_instructions;
452         while (!led_cur_instruction->end) {
453             // Check if this applies to current layer
454             if ((led_cur_instruction->flags & LED_FLAG_MATCH_LAYER) &&
455                 (led_cur_instruction->layer != highest_active_layer)) {
456                 goto next_iter;
457             }
458
459             // Check if this applies to current index
460             if (led_cur_instruction->flags & LED_FLAG_MATCH_ID) {
461                 uint8_t modid = i / 32;                                     //Calculate which id# contains the led bit
462                 uint32_t modidbit = 1 << (i % 32);                          //Calculate the bit within the id#
463                 uint32_t *bitfield = &led_cur_instruction->id0 + modid;     //Add modid as offset to id0 address. *bitfield is now idX of the led id
464                 if (~(*bitfield) & modidbit) {                              //Check if led bit is not set in idX
465                     goto next_iter;
466                 }
467             }
468
469             if (led_cur_instruction->flags & LED_FLAG_USE_RGB) {
470                 ro = led_cur_instruction->r;
471                 go = led_cur_instruction->g;
472                 bo = led_cur_instruction->b;
473             } else if (led_cur_instruction->flags & LED_FLAG_USE_PATTERN) {
474                 led_run_pattern(led_setups[led_cur_instruction->pattern_id], &ro, &go, &bo, po);
475             } else if (led_cur_instruction->flags & LED_FLAG_USE_ROTATE_PATTERN) {
476                 led_run_pattern(led_setups[led_animation_id], &ro, &go, &bo, po);
477             }
478
479             next_iter:
480                 led_cur_instruction++;
481         }
482
483         if (ro > 255) ro = 255; else if (ro < 0) ro = 0;
484         if (go > 255) go = 255; else if (go < 0) go = 0;
485         if (bo > 255) bo = 255; else if (bo < 0) bo = 0;
486
487         if (led_animation_breathing)
488         {
489             ro *= breathe_mult;
490             go *= breathe_mult;
491             bo *= breathe_mult;
492         }
493     }
494
495     led_buffer[i].r = (uint8_t)ro;
496     led_buffer[i].g = (uint8_t)go;
497     led_buffer[i].b = (uint8_t)bo;
498 }
499
500 #endif // USE_MASSDROP_CONFIGURATOR