]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - keyboards/infinity60/led_controller.c
Add back original copyrights
[qmk_firmware.git] / keyboards / infinity60 / led_controller.c
index a66edb9277944fcae85b894f4953dcdb5b8afae7..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
@@ -25,7 +26,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "hal.h"
 #include "print.h"
 #include "led.h"
-#include "action_layer.h"
 #include "host.h"
 
 #include "led_controller.h"
@@ -140,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++) {
@@ -170,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;
@@ -180,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)
@@ -191,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
@@ -199,39 +209,37 @@ static THD_FUNCTION(LEDthread, arg) {
         break;
 
       case OFF_LED:
-      //on/off/toggle single led, msg_args[0] = row/col of led
+      //on/off/toggle single led, msg_args[0] = row/col of led, msg_args[1] = page
         set_led_bit(msg_args[1], control_register_word, msg_args[0], 0);
-        is31_write_data (msg_args[1], control_register_word, 0x02);
         break;
       case ON_LED:
         set_led_bit(msg_args[1], control_register_word, msg_args[0], 1);
-        is31_write_data (msg_args[1], control_register_word, 0x02);
         break;
       case TOGGLE_LED:
         set_led_bit(msg_args[1], control_register_word, msg_args[0], 2);
-        is31_write_data (msg_args[1], control_register_word, 0x02);
         break;
 
       case BLINK_OFF_LED:
       //on/off/toggle single led, msg_args[0] = row/col of led
         set_led_bit(msg_args[1], control_register_word, msg_args[0], 4);
-        is31_write_data (msg_args[1], control_register_word, 0x02);
         break;
       case BLINK_ON_LED:
         set_led_bit(msg_args[1], control_register_word, msg_args[0], 5);
-        is31_write_data (msg_args[1], control_register_word, 0x02);
         break;
       case BLINK_TOGGLE_LED:
         set_led_bit(msg_args[1], control_register_word, msg_args[0], 6);
-        is31_write_data (msg_args[1], control_register_word, 0x02);
         break;
 
       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 {
@@ -244,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;
@@ -272,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;
@@ -282,15 +294,25 @@ static THD_FUNCTION(LEDthread, arg) {
         led_control_reg[0] = 0;
         __builtin_memset(led_control_reg+1, 0, 0x12);
         is31_write_data(msg_args[0], led_control_reg, 0x13);
+
+        //repeat for blink register
+        led_control_reg[0] = 0x12;
+        is31_write_data(msg_args[0], led_control_reg, 0x13);
         break;
 
       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:
@@ -330,7 +352,7 @@ static THD_FUNCTION(LEDthread, arg) {
  *    led processing functions
  * ============================== */
 
-void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
+void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uint8_t action) {
   //returns 2 bytes: led control register address and byte to write
   //action: 0 - off, 1 - on, 2 - toggle, 4 - blink on, 5 - blink off, 6 - toggle blink
 
@@ -341,16 +363,18 @@ void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint
     return;
   }
 
-  //check for blink bit
-  blink_bit = action>>2;
+  blink_bit = action>>2;//check for blink bit
   action &= ~(1<<2); //strip blink bit
 
-  //first byte is led control register address 0x00
   //led_addr tens column is pin#, ones column is bit position in 8-bit mask
   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;
 
@@ -367,8 +391,9 @@ void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint
   }
 
   //return word to be written in register
-  led_control_reg[0] = control_reg_addr;
-  led_control_reg[1] = column_byte;
+  led_control_word[0] = control_reg_addr;
+  led_control_word[1] = column_byte;
+  is31_write_data (page, led_control_word, 0x02);
 }
 
 void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
@@ -402,14 +427,17 @@ 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);
+    is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
+
     if (temp == 0xFF) {
       led_action |= (1<<2); //set blink bit
     }
   }
 
   set_led_bit(page,led_control_word,led_addr,led_action);
-  is31_write_data(page, led_control_word, 0x02);
 }
 
 /* =====================