]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/infinity60/led_controller.c
a66edb9277944fcae85b894f4953dcdb5b8afae7
[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  * 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 #if !defined(CAPS_LOCK_LED_ADDRESS)
61 #define CAPS_LOCK_LED_ADDRESS 46
62 #endif
63
64 #if !defined(NUM_LOCK_LED_ADDRESS)
65 #define NUM_LOCK_LED_ADDRESS 85
66 #endif
67
68 /* Which LED should breathe during sleep */
69 #if !defined(BREATHE_LED_ADDRESS)
70 #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
71 #endif
72
73 /* =================
74  * ChibiOS I2C setup
75  * ================= */
76 static const I2CConfig i2ccfg = {
77   400000 // clock speed (Hz); 400kHz max for IS31
78 };
79
80 /* ==============
81  *   variables
82  * ============== */
83 // internal communication buffers
84 uint8_t tx[2] __attribute__((aligned(2)));
85 uint8_t rx[1] __attribute__((aligned(2)));
86
87 // buffer for sending the whole page at once (used also as a temp buffer)
88 uint8_t full_page[0xB4+1] = {0};
89
90 // LED mask (which LEDs are present, selected by bits)
91 // IC60 pcb uses only CA matrix.
92 // Each byte is a control pin for 8 leds ordered 8-1
93 const uint8_t all_on_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   palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
144   palSetPad(GPIOB, 16);
145   chThdSleepMilliseconds(10);
146   // software shutdown disable (i.e. turn stuff on)
147   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
148   chThdSleepMilliseconds(10);
149   // zero all LED registers on all 8 pages
150   uint8_t i;
151   for(i=0; i<8; i++) {
152     is31_write_data(i, full_page, 0xB4 + 1);
153     chThdSleepMilliseconds(5);
154   }
155 }
156
157 /* ==================
158  * LED control thread
159  * ================== */
160 #define LED_MAILBOX_NUM_MSGS 5
161 static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
162 mailbox_t led_mailbox;
163 static THD_WORKING_AREA(waLEDthread, 256);
164 static THD_FUNCTION(LEDthread, arg) {
165   (void)arg;
166   chRegSetThreadName("LEDthread");
167
168   uint8_t i;
169   uint8_t control_register_word[2] = {0};//2 bytes: register address, byte to write
170   uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
171
172   //persistent status variables
173   uint8_t pwm_step_status, page_status;
174
175   //mailbox variables
176   uint8_t temp, msg_type;
177   uint8_t msg_args[3];
178   msg_t msg;
179
180   // initialize persistent variables
181   pwm_step_status = 4; //full brightness
182   page_status = 0; //start frame 0 (all off/on)
183
184   while(true) {
185     // wait for a message (asynchronous)
186     // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
187     //  be processed right away
188     chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
189     msg_type = msg & 0xFF; //first byte is action information
190     msg_args[0] = (msg >> 8) & 0xFF;
191     msg_args[1] = (msg >> 16) & 0XFF;
192     msg_args[2] = (msg >> 24) & 0xFF;
193
194     switch (msg_type){
195       case SET_FULL_ROW:
196       //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write
197       //writes only to currently displayed page
198         write_led_byte(page_status, msg_args[1], msg_args[0]);
199         break;
200
201       case OFF_LED:
202       //on/off/toggle single led, msg_args[0] = row/col of led
203         set_led_bit(msg_args[1], control_register_word, msg_args[0], 0);
204         is31_write_data (msg_args[1], control_register_word, 0x02);
205         break;
206       case ON_LED:
207         set_led_bit(msg_args[1], control_register_word, msg_args[0], 1);
208         is31_write_data (msg_args[1], control_register_word, 0x02);
209         break;
210       case TOGGLE_LED:
211         set_led_bit(msg_args[1], control_register_word, msg_args[0], 2);
212         is31_write_data (msg_args[1], control_register_word, 0x02);
213         break;
214
215       case BLINK_OFF_LED:
216       //on/off/toggle single led, msg_args[0] = row/col of led
217         set_led_bit(msg_args[1], control_register_word, msg_args[0], 4);
218         is31_write_data (msg_args[1], control_register_word, 0x02);
219         break;
220       case BLINK_ON_LED:
221         set_led_bit(msg_args[1], control_register_word, msg_args[0], 5);
222         is31_write_data (msg_args[1], control_register_word, 0x02);
223         break;
224       case BLINK_TOGGLE_LED:
225         set_led_bit(msg_args[1], control_register_word, msg_args[0], 6);
226         is31_write_data (msg_args[1], control_register_word, 0x02);
227         break;
228
229       case TOGGLE_ALL:
230       //turn on/off all leds, msg_args = unused
231         is31_read_register(0, 0x00, &temp);
232         led_control_reg[0] = 0;
233
234         //if first leds are already on, toggle frame 0 off
235         if (temp==0 || page_status > 0) {
236           __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
237         } else {
238           __builtin_memset(led_control_reg+1, 0, 0x12);
239         }
240         is31_write_data(0, led_control_reg, 0x13);
241
242         if (page_status > 0) {
243           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
244
245           page_status=0;
246
247           //maintain lock leds
248           led_set(host_keyboard_leds());
249         }
250         break;
251
252       case TOGGLE_BACKLIGHT:
253         //msg_args[0] = on/off
254
255         //populate 9 byte rows to be written to each pin, first byte is register (pin) address
256         if (msg_args[0] == 1) {
257           __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
258         } else {
259           __builtin_memset(pwm_register_array+1, 0, 8);
260         }
261
262         for(i=0; i<8; i++) {
263         //first byte is register address, every 0x10 9 bytes is A-matrix pwm pins
264           pwm_register_array[0] = 0x24 + (i * 0x10);
265           is31_write_data(0,pwm_register_array,9);
266         }
267         break;
268
269       case DISPLAY_PAGE:
270       //msg_args[0] = page to toggle on
271         if (page_status != msg_args[0]) {
272           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]);
273           page_status = msg_args[0];
274
275           //maintain lock leds
276           led_set(host_keyboard_leds());
277         }
278         break;
279
280       case RESET_PAGE:
281       //led_args[0] = page to reset
282         led_control_reg[0] = 0;
283         __builtin_memset(led_control_reg+1, 0, 0x12);
284         is31_write_data(msg_args[0], led_control_reg, 0x13);
285         break;
286
287       case TOGGLE_NUM_LOCK:
288       //msg_args[0] = 0 or 1, off/on
289         set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status);
290         break;
291       case TOGGLE_CAPS_LOCK:
292       //msg_args[0] = 0 or 1, off/on
293         set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status);
294         break;
295
296       case STEP_BRIGHTNESS:
297       //led_args[0] = step up (1) or down (0)
298         switch (msg_args[0]) {
299           case 0:
300             if (pwm_step_status == 0) {
301               pwm_step_status = 4;
302             } else {
303               pwm_step_status--;
304             }
305             break;
306
307           case 1:
308             if (pwm_step_status == 4) {
309               pwm_step_status = 0;
310             } else {
311               pwm_step_status++;
312             }
313             break;
314         }
315
316         //populate 8 byte arrays to write on each pin
317         //first byte is register address, every 0x10 9 bytes are A-matrix pwm pins
318         __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
319
320         for(i=0; i<8; i++) {
321           pwm_register_array[0] = 0x24 + (i * 0x10);
322           is31_write_data(0,pwm_register_array,9);
323         }
324         break;
325     }
326   }
327 }
328
329 /* ==============================
330  *    led processing functions
331  * ============================== */
332
333 void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
334   //returns 2 bytes: led control register address and byte to write
335   //action: 0 - off, 1 - on, 2 - toggle, 4 - blink on, 5 - blink off, 6 - toggle blink
336
337   uint8_t control_reg_addr, column_bit, column_byte, temp, blink_bit;
338
339   //check for valid led address
340   if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
341     return;
342   }
343
344   //check for blink bit
345   blink_bit = action>>2;
346   action &= ~(1<<2); //strip blink bit
347
348   //first byte is led control register address 0x00
349   //led_addr tens column is pin#, ones column is bit position in 8-bit mask
350   control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte
351   control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register
352
353   is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
354   column_bit = 1<<(led_addr % 10 - 1);
355   column_byte = temp;
356
357   switch(action) {
358     case 0:
359       column_byte &= ~column_bit;
360       break;
361     case 1:
362       column_byte |= column_bit;
363       break;
364     case 2:
365       column_byte ^= column_bit;
366       break;
367   }
368
369   //return word to be written in register
370   led_control_reg[0] = control_reg_addr;
371   led_control_reg[1] = column_byte;
372 }
373
374 void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
375   uint8_t led_control_word[2] = {0};//register address and on/off byte
376
377   led_control_word[0] = (row - 1 ) * 0x02;// A-matrix is every other byte
378   led_control_word[1] = led_byte;
379   is31_write_data(page, led_control_word, 0x02);
380 }
381
382 void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
383   uint8_t i;
384   uint8_t pin, col;
385   uint8_t led_control_register[0x13] = {0};
386
387   __builtin_memset(led_control_register,0,13);
388
389   for(i=0;i<led_count;i++){
390     //shift pin by 1 for led register 0x00 address
391     pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
392     col = user_led_array[i] % 10 - 1;
393     led_control_register[pin] |= 1<<(col);
394   }
395
396   is31_write_data(page, led_control_register, 0x13);
397 }
398
399 void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
400   uint8_t temp;
401   uint8_t led_control_word[2] = {0};
402
403   //blink if all leds are on
404   if (page == 0) {
405     is31_read_register(0, 0x00, &temp);
406     if (temp == 0xFF) {
407       led_action |= (1<<2); //set blink bit
408     }
409   }
410
411   set_led_bit(page,led_control_word,led_addr,led_action);
412   is31_write_data(page, led_control_word, 0x02);
413 }
414
415 /* =====================
416  * hook into user keymap
417  * ===================== */
418 void led_controller_init(void) {
419   uint8_t i;
420
421   /* initialise I2C */
422   /* I2C pins */
423   palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
424   palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
425   /* start I2C */
426   i2cStart(&I2CD1, &i2ccfg);
427   // try high drive (from kiibohd)
428   I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
429   // try glitch fixing (from kiibohd)
430   I2CD1.i2c->FLT = 4;
431
432   chThdSleepMilliseconds(10);
433
434   /* initialise IS31 chip */
435   is31_init();
436
437   //set Display Option Register so all pwm intensity is controlled from page 0
438   //enable blink and set blink period to 0.27s x rate
439   is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
440
441   /* set full pwm on page 1 */
442   pwm_register_array[0] = 0;
443   __builtin_memset(pwm_register_array+1, 0xFF, 8);
444   for(i=0; i<8; i++) {
445     pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
446     is31_write_data(0, pwm_register_array, 9);
447     chThdSleepMilliseconds(5);
448   }
449
450   /* enable breathing when the displayed page changes */
451   // Fade-in Fade-out, time = 26ms * 2^N, N=3
452   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
453   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
454
455   /* more time consuming LED processing should be offloaded into
456    * a thread, with asynchronous messaging. */
457   chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
458   chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
459 }