]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - keyboards/infinity60/led_controller.c
fix the clueboard layout json
[qmk_firmware.git] / keyboards / infinity60 / led_controller.c
index 04793cc01f79dba0cd23586c6ef8dcd27b6ae362..f319f8c68830b355f9527933d30de58ad0c50cd5 100644 (file)
@@ -1,5 +1,6 @@
 /*
 Copyright 2016 flabbergast <s3+flabbergast@sdfeu.org>
+Copyright 2017 jpetermans <tibcmhhm@gmail.com>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -139,12 +140,19 @@ void is31_init(void) {
   __builtin_memset(full_page,0,0xB4+1);
   // zero function page, all registers (assuming full_page is all zeroes)
   is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
+  // disable hardware shutdown
   palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
   palSetPad(GPIOB, 16);
   chThdSleepMilliseconds(10);
-  // software shutdown disable (i.e. turn stuff on)
+  // software shutdown
   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
   chThdSleepMilliseconds(10);
+  // zero function page, all registers
+  is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
+  chThdSleepMilliseconds(10);
+  // software shutdown disable (i.e. turn stuff on)
+  is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
+  chThdSleepMilliseconds(10);
   // zero all LED registers on all 8 pages
   uint8_t i;
   for(i=0; i<8; i++) {
@@ -169,7 +177,7 @@ static THD_FUNCTION(LEDthread, arg) {
   uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
 
   //persistent status variables
-  uint8_t pwm_step_status, page_status;
+  uint8_t pwm_step_status, page_status, capslock_status, numlock_status;
 
   //mailbox variables
   uint8_t temp, msg_type;
@@ -179,6 +187,8 @@ static THD_FUNCTION(LEDthread, arg) {
   // initialize persistent variables
   pwm_step_status = 4; //full brightness
   page_status = 0; //start frame 0 (all off/on)
+  numlock_status = (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? 1 : 0;
+  capslock_status = (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? 1 : 0;
 
   while(true) {
     // wait for a message (asynchronous)
@@ -190,6 +200,7 @@ static THD_FUNCTION(LEDthread, arg) {
     msg_args[1] = (msg >> 16) & 0XFF;
     msg_args[2] = (msg >> 24) & 0xFF;
 
+
     switch (msg_type){
       case SET_FULL_ROW:
       //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write
@@ -221,10 +232,14 @@ static THD_FUNCTION(LEDthread, arg) {
 
       case TOGGLE_ALL:
       //turn on/off all leds, msg_args = unused
+        is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
+        chThdSleepMilliseconds(5);
         is31_read_register(0, 0x00, &temp);
+        is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
+
         led_control_reg[0] = 0;
 
-        //if first leds are already on, toggle frame 0 off
+        //toggle led mask based on current state (temp)
         if (temp==0 || page_status > 0) {
           __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
         } else {
@@ -237,7 +252,9 @@ static THD_FUNCTION(LEDthread, arg) {
 
           page_status=0;
 
-          //maintain lock leds
+          //maintain lock leds, reset to off and force recheck to blink of all leds toggled on
+          numlock_status = 0;
+          capslock_status = 0;
           led_set(host_keyboard_leds());
         }
         break;
@@ -265,7 +282,9 @@ static THD_FUNCTION(LEDthread, arg) {
           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]);
           page_status = msg_args[0];
 
-          //maintain lock leds
+          //maintain lock leds, reset to off and force recheck for new page
+          numlock_status = 0;
+          capslock_status = 0;
           led_set(host_keyboard_leds());
         }
         break;
@@ -283,11 +302,17 @@ static THD_FUNCTION(LEDthread, arg) {
 
       case TOGGLE_NUM_LOCK:
       //msg_args[0] = 0 or 1, off/on
-        set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status);
+        if (numlock_status != msg_args[0]) {
+          set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status);
+          numlock_status = msg_args[0];
+        }
         break;
       case TOGGLE_CAPS_LOCK:
       //msg_args[0] = 0 or 1, off/on
-        set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status);
+        if (capslock_status != msg_args[0]) {
+          set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status);
+          capslock_status = msg_args[0];
+        }
         break;
 
       case STEP_BRIGHTNESS:
@@ -345,7 +370,11 @@ void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uin
   control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte
   control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register
 
+  is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
+  chThdSleepMilliseconds(5);
   is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
+  is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
+
   column_bit = 1<<(led_addr % 10 - 1);
   column_byte = temp;
 
@@ -398,8 +427,10 @@ void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
 
   //blink if all leds are on
   if (page == 0) {
+    is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
+    chThdSleepMilliseconds(5);
     is31_read_register(0, 0x00, &temp);
-    chThdSleepMilliseconds(10);
+    is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
 
     if (temp == 0xFF) {
       led_action |= (1<<2); //set blink bit