]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - keyboards/infinity60/led_controller.c
small code cleanup
[qmk_firmware.git] / keyboards / infinity60 / led_controller.c
index 4dc9b92342747c2653fe50f0ba10ff9869fd6c8d..c162e9a8f09e2d17d0fb9ecf86e0707d2ea08f95 100644 (file)
@@ -72,8 +72,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
 #endif
 
-#define DEBUG_ENABLED 0
-
 /* =================
  * ChibiOS I2C setup
  * ================= */
@@ -145,7 +143,6 @@ 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);
@@ -182,7 +179,7 @@ static THD_FUNCTION(LEDthread, arg) {
   uint8_t pwm_step_status, page_status;
 
   //mailbox variables
-  uint8_t temp, msg_type, msg_led;
+  uint8_t temp, msg_type, msg_pin, msg_col, msg_led;
   msg_t msg;
 
 /*  //control register variables
@@ -199,14 +196,17 @@ page_status = 0; //start frame 0 (all off/on)
     // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
     //  be processed right away)
     chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
-    msg_type = (msg >> 8) & 0xFF; //first byte is msg type
-    msg_led = (msg) & 0xFF; //second byte is action information
+    msg_col = (msg >> 24) & 0xFF;//if needed
+    msg_pin = (msg >> 16) & 0XFF;//if needed (SET_FULL_ROW)
+    msg_type = (msg >> 8) & 0xFF; //second byte is msg type
+    msg_led = (msg) & 0xFF; //first byte is action information
 
     xprintf("--------------------\n");
-            chThdSleepMilliseconds(10);
     xprintf("mailbox fetch\nmsg: %X\n", msg);
-            chThdSleepMilliseconds(10);
-    xprintf("type: %X - led: %X\n", msg_type, msg_led); 
+            chThdSleepMilliseconds(20);
+    xprintf("type: %X - pin: %X\n", msg_type, msg_pin); 
+            chThdSleepMilliseconds(20);
+    xprintf("col: %X - led: %X\n", msg_col, msg_led); 
             chThdSleepMilliseconds(10);
 
     switch (msg_type){
@@ -214,61 +214,75 @@ page_status = 0; //start frame 0 (all off/on)
       //TODO: lighting key led on keypress
       break;
       
-      //TODO: custom page that is written using keypresses
-      //TODO: BLINK_ON/OFF_LED
+      case SET_FULL_ROW:
+      //write full byte to pin address, msg_pin = pin #, msg_led = byte to write
+      //writes only to current page
+      xprintf("SET_FULL_ROW\n");
+        write_led_byte(page_status,msg_pin,msg_led);
+      break;
 
       case OFF_LED:      
       //on/off/toggle single led, msg_led = row/col of led
-      xprintf("OFF_LED\n");
+    xprintf("OFF_LED: %d\n", msg_led);
             chThdSleepMilliseconds(10);
         set_led_bit(7, control_register_word, msg_led, 0);
         is31_write_data (7, control_register_word, 0x02);
         break;
-
       case ON_LED:      
-      xprintf("ON_LED\n");
+    xprintf("ON_LED: %d\n", msg_led);
             chThdSleepMilliseconds(10);
         set_led_bit(7, control_register_word, msg_led, 1);
         is31_write_data (7, control_register_word, 0x02);
         break;
-
       case TOGGLE_LED:      
-      xprintf("TOGGLE_LED\n");
+    xprintf("TOGGLE_LED: %d\n", msg_led);
             chThdSleepMilliseconds(10);
         set_led_bit(7, control_register_word, msg_led, 2);
         is31_write_data (7, control_register_word, 0x02);
         break;
 
+      case BLINK_OFF_LED:      
+      //on/off/toggle single led, msg_led = row/col of led
+    xprintf("BLINK_ON: %d\n", msg_led);
+            chThdSleepMilliseconds(10);
+        set_led_bit(7, control_register_word, msg_led, 4);
+        is31_write_data (7, control_register_word, 0x02);
+        break;
+      case BLINK_ON_LED:      
+    xprintf("BLINK_OFF: %d\n", msg_led);
+            chThdSleepMilliseconds(10);
+        set_led_bit(7, control_register_word, msg_led, 5);
+        is31_write_data (7, control_register_word, 0x02);
+        break;
+      case BLINK_TOGGLE_LED:      
+    xprintf("BLINK_TOGGLE: %d\n", msg_led);
+            chThdSleepMilliseconds(10);
+        set_led_bit(7, control_register_word, msg_led, 6);
+        is31_write_data (7, control_register_word, 0x02);
+        break;
+
       case TOGGLE_ALL:
-      xprintf("TOGGLE_ALL\n");
+    xprintf("TOGGLE_ALL: %d\n", msg_led);
             chThdSleepMilliseconds(10);
         //msg_led = unused
         is31_read_register(0, 0x00, &temp);//if first byte is on, then toggle frame 0 off
         led_control_reg[0] = 0;
+
         if (temp==0 || page_status > 0) {
-          xprintf("all leds on");
-            chThdSleepMilliseconds(10);
           __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
         } else {
-          xprintf("all leds off");
-            chThdSleepMilliseconds(10);
           __builtin_memset(led_control_reg+1, 0, 0x12);
         }
         is31_write_data(0, led_control_reg, 0x13);
 
         if (page_status > 0) {
           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
-        }
 
-        //maintain lock leds
-        if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) {
-          set_lock_leds(USB_LED_NUM_LOCK, 1);
-        }
-        if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
-          set_lock_leds(USB_LED_CAPS_LOCK, 1);
-        }
+          page_status=0;
 
-        page_status=0;
+          //maintain lock leds
+          led_set(host_keyboard_leds());
+        }
         break;
 
       case TOGGLE_BACKLIGHT:
@@ -295,70 +309,72 @@ page_status = 0; //start frame 0 (all off/on)
       xprintf("DISPLAY_PAGE\n");
             chThdSleepMilliseconds(10);
         if (page_status != msg_led) {
+      xprintf(" - new page\n");
+            chThdSleepMilliseconds(10);
           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
+          page_status = msg_led;
+
+          //maintain lock leds
+          led_set(host_keyboard_leds());
         }
-        page_status = msg_led;
         break;
 
       case RESET_PAGE:
-        //led_msg = page to reset
+      //led_msg = page to reset
+      xprintf("RESET_PAGE\n");
+      chThdSleepMilliseconds(10);
         led_control_reg[0] = 0;
         __builtin_memset(led_control_reg+1, 0, 0x12);
         is31_write_data(msg_led, led_control_reg, 0x13);
-
-        //maintain lock leds
-        if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) {
-          set_lock_leds(USB_LED_NUM_LOCK, 1);
-        }
-        if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
-          set_lock_leds(USB_LED_CAPS_LOCK, 1);
-        }
         break;
         
       case TOGGLE_NUM_LOCK:
         //msg_led = 0 or 1, off/on
-        set_lock_leds(USB_LED_NUM_LOCK, msg_led);
+    xprintf("NUMLOCK: %d\n", msg_led);
+            chThdSleepMilliseconds(10);
+        set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_led, page_status);
         break;
       
       case TOGGLE_CAPS_LOCK:
+    xprintf("CAPSLOCK: %d\n", msg_led);
+            chThdSleepMilliseconds(10);
         //msg_led = 0 or 1, off/on
-        set_lock_leds(USB_LED_CAPS_LOCK, msg_led);
+        set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_led, page_status);
         break;
 
       //TODO: MODE_BREATH
-      case MODE_BREATH:
-        break;
+
       case STEP_BRIGHTNESS:
-      xprintf("TOGGLE_BACKLIGHT\n");
+      xprintf("STEP_BACKLIGHT\n");
             chThdSleepMilliseconds(10);
         //led_msg = step pwm up or down
-      switch (msg_led) {
-        case 0:
-          if (pwm_step_status == 0) {
-            pwm_step_status = 4;
-          } else {
-            pwm_step_status--;
-          }
-          break;
-        
-        case 1:
-          if (pwm_step_status == 4) {
-            pwm_step_status = 0;
-          } else {
-            pwm_step_status++;
-          }
-          break;
-      }
+        switch (msg_led) {
+          case 0:
+            if (pwm_step_status == 0) {
+              pwm_step_status = 4;
+            } else {
+              pwm_step_status--;
+            }
+            break;
+          
+          case 1:
+            if (pwm_step_status == 4) {
+              pwm_step_status = 0;
+            } else {
+              pwm_step_status++;
+            }
+            break;
+        }
 
-      //populate 8 byte rows to write on each pin
-      //first byte is register address, every 0x10 9 bytes are A-register pwm pins
-      __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
+        //populate 8 byte rows to write on each pin
+        //first byte is register address, every 0x10 9 bytes are A-register pwm pins
+        __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
 
-      for(i=0; i<8; i++) {
-        pwm_register_array[0] = 0x24 + (i * 0x10);
-        is31_write_data(0,pwm_register_array,9);
-      }
-      break;
+        for(i=0; i<8; i++) {
+          pwm_register_array[0] = 0x24 + (i * 0x10);
+          is31_write_data(0,pwm_register_array,9);
+        }
+        break;
 
 /*    case LED_MSG_SLEEP_LED_ON:
       // save current settings
@@ -393,29 +409,104 @@ page_status = 0; //start frame 0 (all off/on)
       xprintf("--------------------\n");
             chThdSleepMilliseconds(10);
     }
-#if DEBUG_ENABLED
-    uint8_t j, page;
-    //debugging code - print full led/blink/pwm registers on each frame
-          xprintf("----layer state----: %X\n", layer_state);
-      for(i=0;i<8;i++) {
-          xprintf("page: %d", i);
-            chThdSleepMilliseconds(2);
-        for(j=0;j<0xB4;j++){
-            is31_read_register(i,j,&temp);
-            chThdSleepMilliseconds(2);
-            xprintf("%02X, ", temp);
-            if(j % 9 == 0){
-            xprintf("\n");
-              if(j % 18 ==0){
-              xprintf("register");
-              xprintf("\n");
-              }
-            }
-            chThdSleepMilliseconds(1);
-        }
-            xprintf("\n");
+  }
+}
+
+/* ==============================
+ *    debug function
+ * ============================== */
+void print_debug(uint8_t page) {
+  uint8_t j, debug_temp;
+  //debugging code - print full led/blink/pwm registers on each frame
+  xprintf("----layer state----: %X\n", layer_state);
+  xprintf("page: %d\n", page);
+  chThdSleepMilliseconds(10);
+  for(j=0;j<0x24;j++){
+    if(j > 0 && j % 9 == 0){
+      xprintf("\n");
+    }
+    switch (j) {
+      case 0:
+        xprintf("\n--on-off--\n");
+        chThdSleepMilliseconds(10);
+        break;
+      case 0x12:
+        xprintf("\n--blink--\n");
+        chThdSleepMilliseconds(10);
+        break;
       }
-#endif
+    is31_read_register(page,j,&debug_temp);
+    xprintf("%02X, ", debug_temp);
+    chThdSleepMilliseconds(10);
+  }
+
+  xprintf("\n--pwm--\n");
+  chThdSleepMilliseconds(10);
+  for(j=0x24;j<0xB4;j++) {
+    is31_read_register(page,j,&debug_temp);
+    xprintf("%02X, ", debug_temp);
+    chThdSleepMilliseconds(10);
+    if(j > 0x24 && (j-3) % 8 == 0){
+      xprintf("\n");
+    }
+  }
+  xprintf("\n");
+
+  //Function Register
+  xprintf("\n--FUNCTION--\n");
+  chThdSleepMilliseconds(10);
+  for(j=0;j<0x0D;j++) {
+    is31_read_register(0x0B,j,&debug_temp);
+    switch(j) {
+      case 0:
+        xprintf("Config %02X", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 1:
+        xprintf(" - Pict %02X\n", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 2:
+        xprintf("Auto1 %02X", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 3:
+        xprintf(" - Auto2 %02X\n", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 5:
+        xprintf("Disp %02X", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 6:
+        xprintf(" - Audio %02X\n", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 7:
+        xprintf("Frame %02X", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 8:
+        xprintf(" - Breath1 %02X\n", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 9:
+        xprintf("Breath2 %02X - ", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 10:
+        xprintf(" - Shut %02X\n", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 11:
+        xprintf("AGC %02X", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+      case 12:
+        xprintf(" - ADC %02X\n", debug_temp);
+        chThdSleepMilliseconds(2);
+        break;
+    }
   }
 }
 
@@ -425,22 +516,49 @@ page_status = 0; //start frame 0 (all off/on)
 
 void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
   //returns 2 bytes led control register address and byte to write
+  //0 - bit off, 1 - bit on, 2 - toggle bit
+
+  uint8_t control_reg_addr, column_bit, column_byte, bit_temp, blink_on;
 
-  uint8_t control_reg_addr, column_bit, column_byte, temp;
   //check for valid led address
-  if (led_addr < 0 || led_addr > 90 || led_addr % 10 > 8) {
+  if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
     xprintf("Invalid address: %d\n", led_addr);
     return;
   }
 
+    xprintf("set_led_bit: %d\n", led_addr);
+    xprintf("action: %d\n", action);
+            chThdSleepMilliseconds(10);
+  //check blink bit
+  blink_on = action>>2;
+  action &= ~(1<<2); //strip blink bit
+
   //first byte is led control register address 0x00
   //msg_led tens column is pin#, ones column is bit position in 8-bit mask
   control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-register is every other byte
-  column_bit = 1<<(led_addr % 10 - 1);
+    xprintf("pre-reg_addr: %2X\n", control_reg_addr);
+            chThdSleepMilliseconds(10);
+  control_reg_addr += blink_on == 1 ? 0x12 : 0x00;//shift 12 bytes to blink register
+    xprintf("blink-reg_addr: %2X\n", control_reg_addr);
+            chThdSleepMilliseconds(10);
+    xprintf("page: %2X\n", page);
+            chThdSleepMilliseconds(10);
 
-  is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
-  column_byte = temp;
 
+  is31_read_register(page, 0x06, &bit_temp);//maintain status of leds on this byte
+    xprintf("reg 06: %2X\n", bit_temp);
+  is31_read_register(page, 0x17, &bit_temp);//maintain status of leds on this byte
+    xprintf("reg 17: %2X\n", bit_temp);
+  is31_read_register(page, 0x18, &bit_temp);//maintain status of leds on this byte
+    xprintf("reg 18: %2X\n", bit_temp);
+  is31_read_register(page, 0x19, &bit_temp);//maintain status of leds on this byte
+    xprintf("reg 19: %2X\n", bit_temp);
+  is31_read_register(page, control_reg_addr, &bit_temp);//maintain status of leds on this byte
+  column_bit = 1<<(led_addr % 10 - 1);
+  column_byte = bit_temp;
+
+    xprintf("column_byte read: %2X\n", column_byte);
+            chThdSleepMilliseconds(10);
   switch(action) {
     case 0:
       column_byte &= ~column_bit;
@@ -452,72 +570,70 @@ void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint
       column_byte ^= column_bit;
       break;
   }
+    xprintf("column_byte write: %2X\n", column_byte);
+            chThdSleepMilliseconds(10);
 
   //return word to be written in register
   led_control_reg[0] = control_reg_addr;
   led_control_reg[1] = column_byte;
 }
 
-void set_lock_leds(uint8_t lock_type, uint8_t led_on) {
-  uint8_t page, led_addr, start, temp;
-  uint8_t led_control_word[2] = {0};
-  //TODO: this function call could send led address vs lock_type.
-  //however, the switch/case allows for additional steps, like audio, depending on type
-
-  led_addr = 0;
-  switch(lock_type) {
-      case USB_LED_NUM_LOCK:
-          led_addr = NUM_LOCK_LED_ADDRESS;
-          break;
-      case USB_LED_CAPS_LOCK:
-          led_addr = CAPS_LOCK_LED_ADDRESS;
-          break;
-      #ifdef SCROLL_LOCK_LED_ADDRESS
-      case USB_LED_SCROLL_LOCK:
-          led_addr = SCROLL_LOCK_LED_ADDRESS;
-          break;
-      #endif
-      #ifdef COMPOSE_LED_ADDRESS
-      case USB_LED_COMPOSE:
-          led_addr = COMPOSE_LED_ADDRESS;
-          break;
-      #endif
-      #ifdef SCROLL_LOCK_LED_ADDRESS
-      case USB_LED_KANA:
-          led_addr = KANA_LED_ADDRESS;
-          break;
-      #endif
-  }          
-
-  //ignore frame0 if all leds are on or if option set in led_controller.h
-  //TODO: blink of all leds are on, clear blink register if not
-  is31_read_register(0, 0x00, &temp);
-  led_addr += temp == 0 ? 0 : 0x12;//send bit to blink register instead
-  start = BACKLIGHT_OFF_LOCK_LED_OFF ? 1 : 0; 
-
-  for(page=start; page<8; page++) { 
-    set_led_bit(page,led_control_word,led_addr,led_on);
-    is31_write_data(page, led_control_word, 0x02);
-  }
+void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
+  uint8_t led_control_word[2] = {0};//register address and on/off byte
+
+  led_control_word[0] = (row - 1 ) * 0x02;// A-register is every other byte
+  led_control_word[1] = led_byte;
+  is31_write_data(page, led_control_word, 0x02);
 }
 
 void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
   uint8_t i;
-  uint8_t row, col;
-  uint8_t led_control_register[0x13] = {0};//led control register start address + 0x12 bytes
+  uint8_t pin, col;
+  uint8_t led_control_register[0x13] = {0};//control register start address + 0x12 bytes
 
   __builtin_memset(led_control_register,0,13);
 
   for(i=0;i<led_count;i++){
-    row = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;// 1 byte shift for led register 0x00 address
+    // 1 byte shift for led register 0x00 address
+    pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
     col = user_led_array[i] % 10 - 1;
-    
-    led_control_register[row] |= 1<<(col);
+    led_control_register[pin] |= 1<<(col);
   }
 
   is31_write_data(page, led_control_register, 0x13);
 }
 
+void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
+  uint8_t lock_temp;
+  uint8_t led_control_word[2] = {0};
+
+    xprintf("---set lock---\n");
+            chThdSleepMilliseconds(10);
+
+  //blink if all leds are on
+  if (page == 0) {
+    is31_read_register(0, 0x00, &lock_temp);
+    xprintf("AllOnReg: %2X\n", lock_temp);
+    chThdSleepMilliseconds(10);
+    if (lock_temp == 0xFF) {
+      xprintf("AllOntrue\n");
+      chThdSleepMilliseconds(10);
+      led_action |= (1<<2); //set blink bit
+    } else {
+      xprintf("AllOnfalse\n");
+      chThdSleepMilliseconds(10);
+    }
+  }
+
+  set_led_bit(page,led_control_word,led_addr,led_action);
+
+  xprintf("led_word: %2X", led_control_word[0]);
+  xprintf("%X\n", led_control_word[1]);
+  chThdSleepMilliseconds(10);
+
+  is31_write_data(page, led_control_word, 0x02);
+}
+
 /* =====================
  * hook into user keymap
  * ===================== */
@@ -540,10 +656,11 @@ void led_controller_init(void) {
   /* initialise IS31 chip */
   is31_init();
 
-  //set Display Option Register so all pwm intensity is controlled from Frame 0
-  is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME);
+  //set Display Option Register so all pwm intensity is controlled from page 0
+  //enable blink and set blink period to 0.27s x rate
+  is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
 
-  /* set full pwm on Frame 1 */
+  /* set full pwm on page 1 */
   pwm_register_array[0] = 0;
   __builtin_memset(pwm_register_array+1, 0xFF, 8);
   for(i=0; i<8; i++) {
@@ -557,10 +674,6 @@ void led_controller_init(void) {
   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
 
-  // clean up the lock LEDs
-  set_lock_leds(USB_LED_NUM_LOCK, 0);
-  set_lock_leds(USB_LED_CAPS_LOCK, 0);
-
   /* more time consuming LED processing should be offloaded into
    * a thread, with asynchronous messaging. */
   chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);