/*
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
#include "hal.h"
#include "print.h"
#include "led.h"
-#include "action_layer.h"
#include "host.h"
#include "led_controller.h"
__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++) {
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;
// 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)
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
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 {
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;
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;
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:
* 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
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;
}
//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) {
//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);
}
/* =====================