2 Copyright 2018 Massdrop Inc.
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.
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.
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/>.
18 #include "arm_atsam_protocol.h"
19 #include "tmk_core/common/led.h"
20 #include "rgb_matrix.h"
24 #ifdef USE_MASSDROP_CONFIGURATOR
26 led_instruction_t led_instructions[] = { { .end = 1 } };
27 static void led_matrix_massdrop_config_override(int i);
28 #endif // USE_MASSDROP_CONFIGURATOR
31 void SERCOM1_0_Handler( void )
33 if (SERCOM1->I2CM.INTFLAG.bit.ERROR)
35 SERCOM1->I2CM.INTFLAG.reg = SERCOM_I2CM_INTENCLR_ERROR;
39 void DMAC_0_Handler( void )
41 if (DMAC->Channel[0].CHINTFLAG.bit.TCMPL)
43 DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TCMPL;
47 i2c_led_q_running = 0;
54 if (DMAC->Channel[0].CHINTFLAG.bit.TERR)
56 DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TERR;
60 issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT];
62 issi3733_led_t led_map[ISSI3733_LED_COUNT] = ISSI3733_LED_MAP;
63 RGB led_buffer[ISSI3733_LED_COUNT];
67 uint8_t gcr_actual_last;
68 #ifdef USE_MASSDROP_CONFIGURATOR
74 #define ACT_GCR_NONE 0
78 #define LED_GCR_STEP_AUTO 2
80 static uint8_t gcr_min_counter;
81 static uint8_t v_5v_cat_hit;
83 //WARNING: Automatic GCR is in place to prevent USB shutdown and LED driver overloading
84 void gcr_compute(void)
86 uint8_t action = ACT_GCR_NONE;
87 uint8_t gcr_use = gcr_desired;
89 #ifdef USE_MASSDROP_CONFIGURATOR
90 if (led_animation_breathing)
92 gcr_use = gcr_breathe;
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
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
106 else if (v_5v_cat_hit > 1)
111 else if (v_5v_cat_hit == 1)
113 I2C3733_Control_Set(1);
114 CDC_print("USB: WARNING: Re-enabling LED drivers\r\n");
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;
127 if (gcr_actual < gcr_use) action = ACT_GCR_INC;
128 else if (gcr_actual > gcr_use) action = ACT_GCR_DEC;
131 if (action == ACT_GCR_NONE)
135 else if (action == ACT_GCR_INC)
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;
141 else if (action == ACT_GCR_DEC)
143 if (LED_GCR_STEP_AUTO > gcr_actual) //Prevent wrapping
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
149 if (usb_extra_state == USB_EXTRA_STATE_ENABLED) //If extra usb is enabled
152 if (gcr_min_counter > 200) //5ms per check = 1s delay
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");
164 //Power successfully cut back from LED drivers
165 gcr_actual -= LED_GCR_STEP_AUTO;
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)
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
183 void issi3733_prepare_arrays(void)
185 memset(issidrv,0,sizeof(issi3733_driver_t) * ISSI3733_DRIVER_COUNT);
188 uint8_t addrs[ISSI3733_DRIVER_COUNT] = ISSI3773_DRIVER_ADDRESSES;
190 for (i=0;i<ISSI3733_DRIVER_COUNT;i++)
192 issidrv[i].addr = addrs[i];
195 for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
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));
202 //BYTE: 1 + (SW-1)*2 + (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));
210 void led_matrix_prepare(void)
212 for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
214 *led_map[i].rgb.r = 0;
215 *led_map[i].rgb.g = 0;
216 *led_map[i].rgb.b = 0;
220 void led_set_one(int i, uint8_t r, uint8_t g, uint8_t b)
222 if (i < ISSI3733_LED_COUNT)
224 #ifdef USE_MASSDROP_CONFIGURATOR
225 led_matrix_massdrop_config_override(i);
234 void led_set_all(uint8_t r, uint8_t g, uint8_t b)
236 for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
238 led_set_one(i, r, g, b);
244 DBGC(DC_LED_MATRIX_INIT_BEGIN);
246 issi3733_prepare_arrays();
248 led_matrix_prepare();
253 DBGC(DC_LED_MATRIX_INIT_COMPLETE);
258 #ifdef USE_MASSDROP_CONFIGURATOR
259 if (!led_enabled) { return; } //Prevent calculations and I2C traffic if LED drivers are not enabled
261 if (!sr_exp_data.bit.SDB_N) { return; } //Prevent calculations and I2C traffic if LED drivers are not enabled
264 // Wait for previous transfer to complete
265 while (i2c_led_q_running) {}
267 // Copy buffer to live DMA region
268 for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
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;
275 #ifdef USE_MASSDROP_CONFIGURATOR
278 if (led_animation_breathing)
281 led_animation_breathe_cur += BREATHE_STEP * breathe_dir;
283 if (led_animation_breathe_cur >= BREATHE_MAX_STEP)
285 else if (led_animation_breathe_cur <= BREATHE_MIN_STEP)
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;
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;
297 pomod = (uint32_t)pomod % 10000;
300 #endif // USE_MASSDROP_CONFIGURATOR
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)
307 for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++)
308 I2C_LED_Q_GCR(drvid); //Queue data
309 gcr_actual_last = gcr_actual;
312 for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++)
313 I2C_LED_Q_PWM(drvid); //Queue data
318 void led_matrix_indicators(void)
320 uint8_t kbled = keyboard_leds();
321 if (kbled && rgb_matrix_config.enable)
323 for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++)
326 #if USB_LED_NUM_LOCK_SCANCODE != 255
327 (led_map[i].scan == USB_LED_NUM_LOCK_SCANCODE && (kbled & (1<<USB_LED_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))) ||
332 #if USB_LED_SCROLL_LOCK_SCANCODE != 255
333 (led_map[i].scan == USB_LED_SCROLL_LOCK_SCANCODE && (kbled & (1<<USB_LED_SCROLL_LOCK))) ||
335 #if USB_LED_COMPOSE_SCANCODE != 255
336 (led_map[i].scan == USB_LED_COMPOSE_SCANCODE && (kbled & (1<<USB_LED_COMPOSE))) ||
338 #if USB_LED_KANA_SCANCODE != 255
339 (led_map[i].scan == USB_LED_KANA_SCANCODE && (kbled & (1<<USB_LED_KANA))) ||
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;
352 const rgb_matrix_driver_t rgb_matrix_driver = {
355 .set_color = led_set_one,
356 .set_color_all = led_set_all
359 /*==============================================================================
360 = Legacy Lighting Support =
361 ==============================================================================*/
363 #ifdef USE_MASSDROP_CONFIGURATOR
364 // Ported from Massdrop QMK Github Repo
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;
377 static void led_run_pattern(led_setup_t *f, float* ro, float* go, float* bo, float pos) {
382 po = pos; //Reset po for new frame
384 //Add in any moving effects
385 if ((!led_animation_direction && f->ef & EF_SCR_R) || (led_animation_direction && (f->ef & EF_SCR_L)))
389 if (po > 100) po -= 100;
390 else if (po < 0) po += 100;
392 else if ((!led_animation_direction && f->ef & EF_SCR_L) || (led_animation_direction && (f->ef & EF_SCR_R)))
396 if (po > 100) po -= 100;
397 else if (po < 0) po += 100;
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
405 //Calculate the po within the start-stop percentage for color blending
406 po = (po - f->hs) / (f->he - f->hs);
408 //Add in any color effects
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;
415 else if (f->ef & EF_SUBTRACT)
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;
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;
432 static void led_matrix_massdrop_config_override(int i)
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;
442 uint8_t highest_active_layer = biton32(layer_state);
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)
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)) {
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
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);
480 led_cur_instruction++;
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;
487 if (led_animation_breathing)
495 led_buffer[i].r = (uint8_t)ro;
496 led_buffer[i].g = (uint8_t)go;
497 led_buffer[i].b = (uint8_t)bo;
500 #endif // USE_MASSDROP_CONFIGURATOR