]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/infinity60/led_controller.c
fixed write_led_page col shift, added option for lock led display
[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
28 #include "led_controller.h"
29
30 #include "suspend.h"
31
32 #include "usb_main.h"
33
34 /* Infinity60 LED MAP
35     - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
36
37   11 12 13 14 15 16 17 18 21 22 23 24 25  26 27*
38    28 31 32 33 34 35 36 37 38 41 42 43 44 45
39    46 47 48 51 52 53 54 55 56 57 58 61    62
40     63 64 65 66 67 68 71 72 73 74 75      76 77*
41   78  81  82       83         84  85  86  87
42
43 *Unused in Alphabet Layout
44 */
45
46 /*
47   each page has 0xB4 bytes
48   0 - 0x11: LED control (on/off):
49     order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
50       CAn controls Cn-8 .. Cn-1 (LSbit)
51   0x12 - 0x23: blink control (like "LED control")
52   0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
53     order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
54 */
55
56 /* Which LED should be used for CAPS LOCK indicator
57  * The usual Caps Lock position is C4-6, so the address is
58  * 0x24 + (4-1)*0x10 + (8-1) = 0x59 */
59 #if !defined(CAPS_LOCK_LED_ADDRESS)
60 #define CAPS_LOCK_LED_ADDRESS 0x46
61 #endif
62
63 #if !defined(NUM_LOCK_LED_ADDRESS)
64 #define NUM_LOCK_LED_ADDRESS 0x85
65 #endif
66
67 /* Which LED should breathe during sleep */
68 #if !defined(BREATHE_LED_ADDRESS)
69 #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
70 #endif
71
72 /* =================
73  * ChibiOS I2C setup
74  * ================= */
75 static const I2CConfig i2ccfg = {
76   400000 // clock speed (Hz); 400kHz max for IS31
77 };
78
79 /* ==============
80  *   variables
81  * ============== */
82 // internal communication buffers
83 uint8_t tx[2] __attribute__((aligned(2)));
84 uint8_t rx[1] __attribute__((aligned(2)));
85
86 // buffer for sending the whole page at once (used also as a temp buffer)
87 uint8_t full_page[0xB4+1] = {0};
88
89 // LED mask (which LEDs are present, selected by bits)
90 // See page comment above, control alternates CA matrix/CB matrix
91 // IC60 pcb uses only CA matrix.
92 // Each byte is a control pin for 8 leds ordered 8-1
93 const uint8_t is31_ic60_leds_mask[0x12] = {
94   0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
95   0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
96 };
97
98 // array to hold brightness pwm steps
99 const uint8_t pwm_levels[5] = {
100     0x00, 0x16, 0x4E, 0xA1, 0xFF
101 };
102
103 // array to write to pwm register
104 uint8_t pwm_register_array[9] = {0};
105
106
107 /* ============================
108  *   communication functions
109  * ============================ */
110 msg_t is31_select_page(uint8_t page) {
111   tx[0] = IS31_COMMANDREGISTER;
112   tx[1] = page;
113   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
114 }
115
116 msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
117   is31_select_page(page);
118   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
119 }
120
121 msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
122   is31_select_page(page);
123   tx[0] = reg;
124   tx[1] = data;
125   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
126 }
127
128 msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
129   is31_select_page(page);
130
131   tx[0] = reg;
132   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
133 }
134
135 /* ========================
136  * initialise the IS31 chip
137  * ======================== */
138 void is31_init(void) {
139   // just to be sure that it's all zeroes
140   __builtin_memset(full_page,0,0xB4+1);
141   // zero function page, all registers (assuming full_page is all zeroes)
142   is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
143   // disable hardware shutdown
144   palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
145   palSetPad(GPIOB, 16);
146   chThdSleepMilliseconds(10);
147   // software shutdown
148   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
149   chThdSleepMilliseconds(10);
150   // software shutdown disable (i.e. turn stuff on)
151   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
152   chThdSleepMilliseconds(10);
153   // zero all LED registers on all 8 pages
154   uint8_t i;
155   for(i=0; i<8; i++) {
156     is31_write_data(i, full_page, 0xB4 + 1);
157     chThdSleepMilliseconds(1);
158   }
159 }
160
161 /* ==================
162  * LED control thread
163  * ================== */
164 #define LED_MAILBOX_NUM_MSGS 5
165 static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
166 mailbox_t led_mailbox;
167 static THD_WORKING_AREA(waLEDthread, 256);
168 static THD_FUNCTION(LEDthread, arg) {
169   (void)arg;
170   chRegSetThreadName("LEDthread");
171
172   uint8_t i, page;
173
174   //persistent status variables
175   uint8_t backlight_status, lock_status, led_step_status, layer_status;
176   uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
177
178   //mailbox variables
179   uint8_t temp, msg_type, msg_led;
180   msg_t msg;
181
182 /*  //control register variables
183   uint8_t page, save_page, save_breath1, save_breath2;
184   msg_t msg, retval;
185 */
186
187 // initialize persistent variables
188 backlight_status = 0;
189 lock_status = 0;//TODO: does keyboard remember locks?
190 led_step_status = 4; //full brightness
191 layer_status = 0;
192
193   while(true) {
194     // wait for a message (asynchronous)
195     // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
196     //  be processed right away)
197     chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
198     msg_type = (msg >> 8) & 0xFF; //first byte is msg type
199     msg_led = (msg) & 0xFF; //second byte is action information
200
201     xprintf("--------------------\n");
202     xprintf("mailbox fetch\nmsg: %X\n", msg);
203     xprintf("type: %X - led: %X\n", msg_type, msg_led); //test if msg_type is 1 or 2 bytes after mask
204   switch (msg_type){
205     case KEY_LIGHT:
206     //TODO: lighting key led on keypress
207     break;
208     
209     case TOGGLE_LED:      
210       //TODO: toggle existing indicator off, or let user do this, but write frame 7 for every led change
211       //turn on single led, msg_led = row/col of led
212     xprintf("TOGGLE_LED\n");
213       set_led_bit(led_control_reg, msg_led, 1);
214
215       is31_write_data (7, led_control_reg, 0x12+1);
216       is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
217       layer_status = 7;
218       is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
219       break;
220
221     case TOGGLE_ALL:
222     xprintf("TOGGLE_ALL\n");
223       //msg_led = unused, TODO: consider using msg_led to toggle layer display
224       is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
225
226       //if LED_ALL is on then toggle off, any other layer, turn on LED_ALL
227       if(temp == 1) {
228         is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
229       } else {
230         is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 1);
231       }
232       is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
233       break;
234
235     case TOGGLE_BACKLIGHT:
236       //msg_led = unused
237       //TODO: consider Frame 0 as on/off layer and toggle led control register here
238     xprintf("TOGGLE_BACKLIGHT\n");
239       backlight_status ^= 1;
240       is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
241       layer_status = temp;
242
243       page = backlight_status == 0 ? 0 : layer_status;
244       is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, page);
245       break;
246
247     case TOGGLE_LAYER_LEDS://show layer indicator or full map of layer keys.
248       //TODO: change so user can flag which they want, indiv or full map in fn_actions
249       //msg_led = layer to toggle on
250     xprintf("TOGGLE_LAYER_LEDS\n");
251       is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
252
253       if(temp == msg_led) {
254         is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
255         layer_status = 7;
256       } else {
257         is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
258         layer_status = msg_led;
259       }
260       break;
261       
262     case TOGGLE_LOCK_LED:
263       //msg_led = 0-3 for lock flags
264       lock_status ^= msg_led; //TODO: confirm toggling works and doesn't get out of sync
265       set_lock_leds(led_control_reg, lock_status);
266       break;
267     
268     case MODE_BREATH:
269       break;
270     case STEP_BRIGHTNESS:
271       //pwm_levels[] bounds checking, loop through array
272       //TODO: find a cleaner way to walk through this logic
273       if (msg_led == 0 && led_step_status == 0) {
274         led_step_status = 4;
275       } else {
276         led_step_status--;
277       }
278       
279       if (msg_led == 1 && led_step_status == 4) {
280         led_step_status = 0;
281       } else {
282         led_step_status++;
283       }
284
285       //TODO: this seems a messy way to populate the pwm register
286       //mimic whitefox init which uses memcpy
287       //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
288       for(i=1; i<9; i++) {
289         pwm_register_array[i]=pwm_levels[led_step_status]; 
290       }
291       for(i=0; i<8; i++) {
292         pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
293         is31_write_data(0, pwm_register_array, 9);//first page controls pwm in all pages (init Display Option register)
294       }
295       break;
296
297 /*      case LED_MSG_SLEEP_LED_ON:
298         // save current settings
299         is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
300         is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
301         is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
302         // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
303         is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
304         is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
305         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
306         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
307         retval = MSG_TIMEOUT;
308         temp = 6;
309         while(retval == MSG_TIMEOUT) {
310           // switch to the other page
311           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
312           temp = (temp == 6 ? 7 : 6);
313           // the times should be sufficiently long for IS31 to finish switching pages
314           retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
315         }
316         // received a message (should be a wakeup), so restore previous state
317         chThdSleepMilliseconds(3000); // need to wait until the page change finishes
318         // note: any other messages are queued
319         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
320         is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
321         is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
322         break;
323       case LED_MSG_SLEEP_LED_OFF: 
324         // should not get here; wakeup should be received in the branch above break;
325         break;
326 */
327     }
328     xprintf("--------------------\n");
329   }
330 }
331
332
333 /* ========================
334  *    led bit processing
335  * ======================== */
336 void set_led_bit (uint8_t *led_control_reg, uint8_t msg_led, uint8_t toggle_on) {
337   uint8_t row_byte, column_bit;
338   //msg_led tens column is pin#, A-control register is every other 8 bits
339   //ones column is bit position in 8-bit mask
340   //control register will be one bit shifted into position along register's full 0x12 bytes
341   ////first byte is register address 0x00
342   row_byte = ((msg_led / 10) % 10 - 1 ) * 2 + 1;
343   column_bit = 1<<(msg_led % 10 - 1);
344
345   if (toggle_on) {
346     led_control_reg[row_byte] |= 1<<(column_bit);
347   } else {
348     led_control_reg[row_byte] &= ~1<<(column_bit);
349   }
350 }
351
352 void set_lock_leds(uint8_t *led_control_reg, uint8_t lock_status) {
353   uint8_t i;
354
355   switch (lock_status) {
356     case 1:
357       set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);
358       set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 0);
359       break;
360     case 2:
361       set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 0);
362       set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
363       break;
364     case 3:
365       set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
366       set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);
367       break;
368   }
369
370   for(i=BACKLIGHT_OFF_LOCK_LED_OFF; i<8; i++) { //set in led_controller.h
371     is31_write_data (i, led_control_reg, 0x12+1);
372   }
373 }
374
375 void write_led_page (uint8_t page, const uint8_t *led_array, uint8_t led_count) {
376 //TODO: init function that accepts array of led addresses and sets them by row
377   uint8_t i;
378   uint8_t row, col;
379   uint8_t temp_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
380
381   for(i=0;i<led_count;i++){
382     row = ((led_array[i] / 10) % 10 - 1 ) * 2 + 1;//includes 1 byte shift for led register 0x00 address
383     col = led_array[i] % 10 - 1;
384     
385     temp_control_reg[row] |= 1<<(col);
386   }
387
388   is31_write_data(page, temp_control_reg, 0x13);
389 }
390 /* =====================
391  * hook into user keymap
392  * ===================== */
393 void led_controller_init(void) {
394   uint8_t i;
395
396   /* initialise I2C */
397   /* I2C pins */
398   palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
399   palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
400   /* start I2C */
401   i2cStart(&I2CD1, &i2ccfg);
402   // try high drive (from kiibohd)
403   I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
404   // try glitch fixing (from kiibohd)
405   I2CD1.i2c->FLT = 4;
406
407   chThdSleepMilliseconds(10);
408
409   /* initialise IS31 chip */
410   is31_init();
411
412   //set Display Option Register so all pwm intensity is controlled from Frame 1
413   is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME);
414
415   /* set full pwm on Frame 1 */
416   for(i=1; i<9; i++) {
417     pwm_register_array[i]=0xFF; 
418   }
419   for(i=0; i<8; i++) {
420     pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
421     is31_write_data(0, pwm_register_array, 9);
422     chThdSleepMilliseconds(5);
423   }
424
425   //set all led bits on for Frame 2 LEDS_ALL
426   full_page[0] = 0;
427   __builtin_memcpy(full_page+1, is31_ic60_leds_mask, 0x12);
428   is31_write_data(1, full_page, 1+0x12);
429
430   /* enable breathing when the displayed page changes */
431   // Fade-in Fade-out, time = 26ms * 2^N, N=3
432   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
433   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
434
435   // clean up the lock LEDs
436   //TODO: adjust for new addressing and additional frames
437   //is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
438   //is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
439
440   /* more time consuming LED processing should be offloaded into
441    * a thread, with asynchronous messaging. */
442   chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
443   chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
444 }
445
446 //TODO: Don't know equivalent QMK hooks for these
447 //
448 //void hook_usb_suspend_entry(void) {
449 //#ifdef SLEEP_LED_ENABLE
450 //  chSysLockFromISR();
451 //  chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
452 //  chSysUnlockFromISR();
453 //#endif /* SLEEP_LED_ENABLE */
454 //}
455 //
456 //void hook_usb_suspend_loop(void) {
457 //  chThdSleepMilliseconds(100);
458 //  /* Remote wakeup */
459 //  if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
460 //    send_remote_wakeup(&USB_DRIVER);
461 //  }
462 //}
463 //
464 //void hook_usb_wakeup(void) {
465 //#ifdef SLEEP_LED_ENABLE
466 //  chSysLockFromISR();
467 //  chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
468 //  chSysUnlockFromISR();
469 //#endif /* SLEEP_LED_ENABLE */
470 //}
471 //*/