]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/infinity60/led_controller.c
4dc9b92342747c2653fe50f0ba10ff9869fd6c8d
[qmk_firmware.git] / keyboards / infinity60 / led_controller.c
1 /*
2 Copyright 2016 flabbergast <s3+flabbergast@sdfeu.org>
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 /*
19  * LED controller code
20  * WF uses IS31FL3731C matrix LED driver from ISSI
21  * datasheet: http://www.issi.com/WW/pdf/31FL3731C.pdf
22  */
23
24 #include "ch.h"
25 #include "hal.h"
26 #include "print.h"
27 #include "led.h"
28 #include "action_layer.h"
29 #include "host.h"
30
31 #include "led_controller.h"
32
33 #include "suspend.h"
34
35 #include "usb_main.h"
36
37 /* Infinity60 LED MAP
38     - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
39
40   11 12 13 14 15 16 17 18 21 22 23 24 25  26 27*
41    28 31 32 33 34 35 36 37 38 41 42 43 44 45
42    46 47 48 51 52 53 54 55 56 57 58 61    62
43     63 64 65 66 67 68 71 72 73 74 75      76 77*
44   78  81  82       83         84  85  86  87
45
46 *Unused in Alphabet Layout
47 */
48
49 /*
50   each page has 0xB4 bytes
51   0 - 0x11: LED control (on/off):
52     order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
53       CAn controls Cn-8 .. Cn-1 (LSbit)
54   0x12 - 0x23: blink control (like "LED control")
55   0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
56     order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
57 */
58
59 /* Which LED should be used for CAPS LOCK indicator
60  * The usual Caps Lock position is C4-6, so the address is
61  * 0x24 + (4-1)*0x10 + (8-1) = 0x59 */
62 #if !defined(CAPS_LOCK_LED_ADDRESS)
63 #define CAPS_LOCK_LED_ADDRESS 46
64 #endif
65
66 #if !defined(NUM_LOCK_LED_ADDRESS)
67 #define NUM_LOCK_LED_ADDRESS 85
68 #endif
69
70 /* Which LED should breathe during sleep */
71 #if !defined(BREATHE_LED_ADDRESS)
72 #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
73 #endif
74
75 #define DEBUG_ENABLED 0
76
77 /* =================
78  * ChibiOS I2C setup
79  * ================= */
80 static const I2CConfig i2ccfg = {
81   400000 // clock speed (Hz); 400kHz max for IS31
82 };
83
84 /* ==============
85  *   variables
86  * ============== */
87 // internal communication buffers
88 uint8_t tx[2] __attribute__((aligned(2)));
89 uint8_t rx[1] __attribute__((aligned(2)));
90
91 // buffer for sending the whole page at once (used also as a temp buffer)
92 uint8_t full_page[0xB4+1] = {0};
93
94 // LED mask (which LEDs are present, selected by bits)
95 // See page comment above, control alternates CA matrix/CB matrix
96 // IC60 pcb uses only CA matrix.
97 // Each byte is a control pin for 8 leds ordered 8-1
98 const uint8_t all_on_leds_mask[0x12] = {
99   0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
100   0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
101 };
102
103 // array to hold brightness pwm steps
104 const uint8_t pwm_levels[5] = {
105     0x00, 0x16, 0x4E, 0xA1, 0xFF
106 };
107
108 // array to write to pwm register
109 uint8_t pwm_register_array[9] = {0};
110
111
112 /* ============================
113  *   communication functions
114  * ============================ */
115 msg_t is31_select_page(uint8_t page) {
116   tx[0] = IS31_COMMANDREGISTER;
117   tx[1] = page;
118   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
119 }
120
121 msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
122   is31_select_page(page);
123   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
124 }
125
126 msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
127   is31_select_page(page);
128   tx[0] = reg;
129   tx[1] = data;
130   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
131 }
132
133 msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
134   is31_select_page(page);
135
136   tx[0] = reg;
137   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
138 }
139
140 /* ========================
141  * initialise the IS31 chip
142  * ======================== */
143 void is31_init(void) {
144   // just to be sure that it's all zeroes
145   __builtin_memset(full_page,0,0xB4+1);
146   // zero function page, all registers (assuming full_page is all zeroes)
147   is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
148   // disable hardware shutdown
149   palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
150   palSetPad(GPIOB, 16);
151   chThdSleepMilliseconds(10);
152   // software shutdown
153   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
154   chThdSleepMilliseconds(10);
155   // software shutdown disable (i.e. turn stuff on)
156   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
157   chThdSleepMilliseconds(10);
158   // zero all LED registers on all 8 pages
159   uint8_t i;
160   for(i=0; i<8; i++) {
161     is31_write_data(i, full_page, 0xB4 + 1);
162     chThdSleepMilliseconds(1);
163   }
164 }
165
166 /* ==================
167  * LED control thread
168  * ================== */
169 #define LED_MAILBOX_NUM_MSGS 5
170 static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
171 mailbox_t led_mailbox;
172 static THD_WORKING_AREA(waLEDthread, 256);
173 static THD_FUNCTION(LEDthread, arg) {
174   (void)arg;
175   chRegSetThreadName("LEDthread");
176
177   uint8_t i;
178   uint8_t control_register_word[2] = {0};//2 bytes: register address, byte to write
179   uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
180
181   //persistent status variables
182   uint8_t pwm_step_status, page_status;
183
184   //mailbox variables
185   uint8_t temp, msg_type, msg_led;
186   msg_t msg;
187
188 /*  //control register variables
189   uint8_t page, save_page, save_breath1, save_breath2;
190   msg_t msg, retval;
191 */
192
193 // initialize persistent variables
194 pwm_step_status = 4; //full brightness
195 page_status = 0; //start frame 0 (all off/on)
196
197   while(true) {
198     // wait for a message (asynchronous)
199     // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
200     //  be processed right away)
201     chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
202     msg_type = (msg >> 8) & 0xFF; //first byte is msg type
203     msg_led = (msg) & 0xFF; //second byte is action information
204
205     xprintf("--------------------\n");
206             chThdSleepMilliseconds(10);
207     xprintf("mailbox fetch\nmsg: %X\n", msg);
208             chThdSleepMilliseconds(10);
209     xprintf("type: %X - led: %X\n", msg_type, msg_led); 
210             chThdSleepMilliseconds(10);
211
212     switch (msg_type){
213       case KEY_LIGHT:
214       //TODO: lighting key led on keypress
215       break;
216       
217       //TODO: custom page that is written using keypresses
218       //TODO: BLINK_ON/OFF_LED
219
220       case OFF_LED:      
221       //on/off/toggle single led, msg_led = row/col of led
222       xprintf("OFF_LED\n");
223             chThdSleepMilliseconds(10);
224         set_led_bit(7, control_register_word, msg_led, 0);
225         is31_write_data (7, control_register_word, 0x02);
226         break;
227
228       case ON_LED:      
229       xprintf("ON_LED\n");
230             chThdSleepMilliseconds(10);
231         set_led_bit(7, control_register_word, msg_led, 1);
232         is31_write_data (7, control_register_word, 0x02);
233         break;
234
235       case TOGGLE_LED:      
236       xprintf("TOGGLE_LED\n");
237             chThdSleepMilliseconds(10);
238         set_led_bit(7, control_register_word, msg_led, 2);
239         is31_write_data (7, control_register_word, 0x02);
240         break;
241
242       case TOGGLE_ALL:
243       xprintf("TOGGLE_ALL\n");
244             chThdSleepMilliseconds(10);
245         //msg_led = unused
246         is31_read_register(0, 0x00, &temp);//if first byte is on, then toggle frame 0 off
247         led_control_reg[0] = 0;
248         if (temp==0 || page_status > 0) {
249           xprintf("all leds on");
250             chThdSleepMilliseconds(10);
251           __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
252         } else {
253           xprintf("all leds off");
254             chThdSleepMilliseconds(10);
255           __builtin_memset(led_control_reg+1, 0, 0x12);
256         }
257         is31_write_data(0, led_control_reg, 0x13);
258
259         if (page_status > 0) {
260           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
261         }
262
263         //maintain lock leds
264         if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) {
265           set_lock_leds(USB_LED_NUM_LOCK, 1);
266         }
267         if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
268           set_lock_leds(USB_LED_CAPS_LOCK, 1);
269         }
270
271         page_status=0;
272         break;
273
274       case TOGGLE_BACKLIGHT:
275         //msg_led = on/off
276       xprintf("TOGGLE_BACKLIGHT\n");
277             chThdSleepMilliseconds(10);
278
279         //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
280         if (msg_led == 1) {
281           __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
282         } else {
283           __builtin_memset(pwm_register_array+1, 0, 8);
284         }
285
286         for(i=0; i<8; i++) {
287         //first byte is register address, every 0x10 9 bytes is A-register pwm pins
288           pwm_register_array[0] = 0x24 + (i * 0x10);
289           is31_write_data(0,pwm_register_array,9);
290         }
291         break;
292
293       case DISPLAY_PAGE://show single layer indicator or full map of layer
294         //msg_led = page to toggle on
295       xprintf("DISPLAY_PAGE\n");
296             chThdSleepMilliseconds(10);
297         if (page_status != msg_led) {
298           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
299         }
300         page_status = msg_led;
301         break;
302
303       case RESET_PAGE:
304         //led_msg = page to reset
305         led_control_reg[0] = 0;
306         __builtin_memset(led_control_reg+1, 0, 0x12);
307         is31_write_data(msg_led, led_control_reg, 0x13);
308
309         //maintain lock leds
310         if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) {
311           set_lock_leds(USB_LED_NUM_LOCK, 1);
312         }
313         if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
314           set_lock_leds(USB_LED_CAPS_LOCK, 1);
315         }
316         break;
317         
318       case TOGGLE_NUM_LOCK:
319         //msg_led = 0 or 1, off/on
320         set_lock_leds(USB_LED_NUM_LOCK, msg_led);
321         break;
322       
323       case TOGGLE_CAPS_LOCK:
324         //msg_led = 0 or 1, off/on
325         set_lock_leds(USB_LED_CAPS_LOCK, msg_led);
326         break;
327
328       //TODO: MODE_BREATH
329       case MODE_BREATH:
330         break;
331       case STEP_BRIGHTNESS:
332       xprintf("TOGGLE_BACKLIGHT\n");
333             chThdSleepMilliseconds(10);
334         //led_msg = step pwm up or down
335       switch (msg_led) {
336         case 0:
337           if (pwm_step_status == 0) {
338             pwm_step_status = 4;
339           } else {
340             pwm_step_status--;
341           }
342           break;
343         
344         case 1:
345           if (pwm_step_status == 4) {
346             pwm_step_status = 0;
347           } else {
348             pwm_step_status++;
349           }
350           break;
351       }
352
353       //populate 8 byte rows to write on each pin
354       //first byte is register address, every 0x10 9 bytes are A-register pwm pins
355       __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
356
357       for(i=0; i<8; i++) {
358         pwm_register_array[0] = 0x24 + (i * 0x10);
359         is31_write_data(0,pwm_register_array,9);
360       }
361       break;
362
363 /*    case LED_MSG_SLEEP_LED_ON:
364       // save current settings
365         is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
366         is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
367         is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
368         // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
369         is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
370         is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
371         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
372         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
373         retval = MSG_TIMEOUT;
374         temp = 6;
375         while(retval == MSG_TIMEOUT) {
376           // switch to the other page
377           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
378           temp = (temp == 6 ? 7 : 6);
379           // the times should be sufficiently long for IS31 to finish switching pages
380           retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
381         }
382         // received a message (should be a wakeup), so restore previous state
383         chThdSleepMilliseconds(3000); // need to wait until the page change finishes
384         // note: any other messages are queued
385         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
386         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
387         is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
388         break;
389       case LED_MSG_SLEEP_LED_OFF: 
390         // should not get here; wakeup should be received in the branch above break;
391         break;
392 */  
393       xprintf("--------------------\n");
394             chThdSleepMilliseconds(10);
395     }
396 #if DEBUG_ENABLED
397     uint8_t j, page;
398     //debugging code - print full led/blink/pwm registers on each frame
399           xprintf("----layer state----: %X\n", layer_state);
400       for(i=0;i<8;i++) {
401           xprintf("page: %d", i);
402             chThdSleepMilliseconds(2);
403         for(j=0;j<0xB4;j++){
404             is31_read_register(i,j,&temp);
405             chThdSleepMilliseconds(2);
406             xprintf("%02X, ", temp);
407             if(j % 9 == 0){
408             xprintf("\n");
409               if(j % 18 ==0){
410               xprintf("register");
411               xprintf("\n");
412               }
413             }
414             chThdSleepMilliseconds(1);
415         }
416             xprintf("\n");
417       }
418 #endif
419   }
420 }
421
422 /* ==============================
423  *    led processing functions
424  * ============================== */
425
426 void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
427   //returns 2 bytes led control register address and byte to write
428
429   uint8_t control_reg_addr, column_bit, column_byte, temp;
430   //check for valid led address
431   if (led_addr < 0 || led_addr > 90 || led_addr % 10 > 8) {
432     xprintf("Invalid address: %d\n", led_addr);
433     return;
434   }
435
436   //first byte is led control register address 0x00
437   //msg_led tens column is pin#, ones column is bit position in 8-bit mask
438   control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-register is every other byte
439   column_bit = 1<<(led_addr % 10 - 1);
440
441   is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
442   column_byte = temp;
443
444   switch(action) {
445     case 0:
446       column_byte &= ~column_bit;
447       break;
448     case 1:
449       column_byte |= column_bit;
450       break;
451     case 2:
452       column_byte ^= column_bit;
453       break;
454   }
455
456   //return word to be written in register
457   led_control_reg[0] = control_reg_addr;
458   led_control_reg[1] = column_byte;
459 }
460
461 void set_lock_leds(uint8_t lock_type, uint8_t led_on) {
462   uint8_t page, led_addr, start, temp;
463   uint8_t led_control_word[2] = {0};
464   //TODO: this function call could send led address vs lock_type.
465   //however, the switch/case allows for additional steps, like audio, depending on type
466
467   led_addr = 0;
468   switch(lock_type) {
469       case USB_LED_NUM_LOCK:
470           led_addr = NUM_LOCK_LED_ADDRESS;
471           break;
472       case USB_LED_CAPS_LOCK:
473           led_addr = CAPS_LOCK_LED_ADDRESS;
474           break;
475       #ifdef SCROLL_LOCK_LED_ADDRESS
476       case USB_LED_SCROLL_LOCK:
477           led_addr = SCROLL_LOCK_LED_ADDRESS;
478           break;
479       #endif
480       #ifdef COMPOSE_LED_ADDRESS
481       case USB_LED_COMPOSE:
482           led_addr = COMPOSE_LED_ADDRESS;
483           break;
484       #endif
485       #ifdef SCROLL_LOCK_LED_ADDRESS
486       case USB_LED_KANA:
487           led_addr = KANA_LED_ADDRESS;
488           break;
489       #endif
490   }          
491
492   //ignore frame0 if all leds are on or if option set in led_controller.h
493   //TODO: blink of all leds are on, clear blink register if not
494   is31_read_register(0, 0x00, &temp);
495   led_addr += temp == 0 ? 0 : 0x12;//send bit to blink register instead
496   start = BACKLIGHT_OFF_LOCK_LED_OFF ? 1 : 0; 
497
498   for(page=start; page<8; page++) { 
499     set_led_bit(page,led_control_word,led_addr,led_on);
500     is31_write_data(page, led_control_word, 0x02);
501   }
502 }
503
504 void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
505   uint8_t i;
506   uint8_t row, col;
507   uint8_t led_control_register[0x13] = {0};//led control register start address + 0x12 bytes
508
509   __builtin_memset(led_control_register,0,13);
510
511   for(i=0;i<led_count;i++){
512     row = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;// 1 byte shift for led register 0x00 address
513     col = user_led_array[i] % 10 - 1;
514     
515     led_control_register[row] |= 1<<(col);
516   }
517
518   is31_write_data(page, led_control_register, 0x13);
519 }
520
521 /* =====================
522  * hook into user keymap
523  * ===================== */
524 void led_controller_init(void) {
525   uint8_t i;
526
527   /* initialise I2C */
528   /* I2C pins */
529   palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
530   palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
531   /* start I2C */
532   i2cStart(&I2CD1, &i2ccfg);
533   // try high drive (from kiibohd)
534   I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
535   // try glitch fixing (from kiibohd)
536   I2CD1.i2c->FLT = 4;
537
538   chThdSleepMilliseconds(10);
539
540   /* initialise IS31 chip */
541   is31_init();
542
543   //set Display Option Register so all pwm intensity is controlled from Frame 0
544   is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME);
545
546   /* set full pwm on Frame 1 */
547   pwm_register_array[0] = 0;
548   __builtin_memset(pwm_register_array+1, 0xFF, 8);
549   for(i=0; i<8; i++) {
550     pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
551     is31_write_data(0, pwm_register_array, 9);
552     chThdSleepMilliseconds(5);
553   }
554
555   /* enable breathing when the displayed page changes */
556   // Fade-in Fade-out, time = 26ms * 2^N, N=3
557   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
558   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
559
560   // clean up the lock LEDs
561   set_lock_leds(USB_LED_NUM_LOCK, 0);
562   set_lock_leds(USB_LED_CAPS_LOCK, 0);
563
564   /* more time consuming LED processing should be offloaded into
565    * a thread, with asynchronous messaging. */
566   chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
567   chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
568 }
569
570 //TODO: Don't know equivalent QMK hooks for these
571 //
572 //void hook_usb_suspend_entry(void) {
573 //#ifdef SLEEP_LED_ENABLE
574 //  chSysLockFromISR();
575 //  chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
576 //  chSysUnlockFromISR();
577 //#endif /* SLEEP_LED_ENABLE */
578 //}
579 //
580 //void hook_usb_suspend_loop(void) {
581 //  chThdSleepMilliseconds(100);
582 //  /* Remote wakeup */
583 //  if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
584 //    send_remote_wakeup(&USB_DRIVER);
585 //  }
586 //}
587 //
588 //void hook_usb_wakeup(void) {
589 //#ifdef SLEEP_LED_ENABLE
590 //  chSysLockFromISR();
591 //  chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
592 //  chSysUnlockFromISR();
593 //#endif /* SLEEP_LED_ENABLE */
594 //}
595 //*/