led_controller.c 22 KB

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