#include <ch.h>
#include <hal.h>
#include "print.h"
#include "led.h"
#include "host.h"
#include "led_controller.h"
#include "suspend.h"
#include "usb_main.h"
#if !defined(CAPS_LOCK_LED_ADDRESS)
#define CAPS_LOCK_LED_ADDRESS 46
#endif
#if !defined(NUM_LOCK_LED_ADDRESS)
#define NUM_LOCK_LED_ADDRESS 85
#endif
#if !defined(BREATHE_LED_ADDRESS)
#define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
#endif
static const I2CConfig i2ccfg = {
400000 };
uint8_t tx[2] __attribute__((aligned(2)));
uint8_t rx[1] __attribute__((aligned(2)));
uint8_t full_page[0xB4+1] = {0};
const uint8_t all_on_leds_mask[0x12] = {
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
};
const uint8_t pwm_levels[5] = {
0x00, 0x16, 0x4E, 0xA1, 0xFF
};
uint8_t pwm_register_array[9] = {0};
msg_t is31_select_page(uint8_t page) {
tx[0] = IS31_COMMANDREGISTER;
tx[1] = page;
return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, TIME_US2I(IS31_TIMEOUT));
}
msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
is31_select_page(page);
return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, TIME_US2I(IS31_TIMEOUT));
}
msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
is31_select_page(page);
tx[0] = reg;
tx[1] = data;
return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, TIME_US2I(IS31_TIMEOUT));
}
msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
is31_select_page(page);
tx[0] = reg;
return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, TIME_US2I(IS31_TIMEOUT));
}
void is31_init(void) {
__builtin_memset(full_page,0,0xB4+1);
is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
palSetPad(GPIOB, 16);
chThdSleepMilliseconds(10);
is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
chThdSleepMilliseconds(10);
is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
chThdSleepMilliseconds(10);
is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
chThdSleepMilliseconds(10);
uint8_t i;
for(i=0; i<8; i++) {
is31_write_data(i, full_page, 0xB4 + 1);
chThdSleepMilliseconds(5);
}
}
#define LED_MAILBOX_NUM_MSGS 5
static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
mailbox_t led_mailbox;
static THD_WORKING_AREA(waLEDthread, 256);
static THD_FUNCTION(LEDthread, arg) {
(void)arg;
chRegSetThreadName("LEDthread");
uint8_t i;
uint8_t control_register_word[2] = {0}; uint8_t led_control_reg[0x13] = {0};
uint8_t pwm_step_status, page_status, capslock_status, numlock_status;
uint8_t temp, msg_type;
uint8_t msg_args[3];
msg_t msg;
pwm_step_status = 4; page_status = 0; 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) {
chMBFetchTimeout(&led_mailbox, &msg, TIME_INFINITE);
msg_type = msg & 0xFF; msg_args[0] = (msg >> 8) & 0xFF;
msg_args[1] = (msg >> 16) & 0XFF;
msg_args[2] = (msg >> 24) & 0xFF;
switch (msg_type){
case SET_FULL_ROW:
write_led_byte(page_status, msg_args[1], msg_args[0]);
break;
case OFF_LED:
set_led_bit(msg_args[1], control_register_word, msg_args[0], 0);
break;
case ON_LED:
set_led_bit(msg_args[1], control_register_word, msg_args[0], 1);
break;
case TOGGLE_LED:
set_led_bit(msg_args[1], control_register_word, msg_args[0], 2);
break;
case BLINK_OFF_LED:
set_led_bit(msg_args[1], control_register_word, msg_args[0], 4);
break;
case BLINK_ON_LED:
set_led_bit(msg_args[1], control_register_word, msg_args[0], 5);
break;
case BLINK_TOGGLE_LED:
set_led_bit(msg_args[1], control_register_word, msg_args[0], 6);
break;
case TOGGLE_ALL:
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 (temp==0 || page_status > 0) {
__builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
} else {
__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);
page_status=0;
numlock_status = 0;
capslock_status = 0;
led_set(host_keyboard_leds());
}
break;
case TOGGLE_BACKLIGHT:
if (msg_args[0] == 1) {
__builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
} else {
__builtin_memset(pwm_register_array+1, 0, 8);
}
for(i=0; i<8; i++) {
pwm_register_array[0] = 0x24 + (i * 0x10);
is31_write_data(0,pwm_register_array,9);
}
break;
case DISPLAY_PAGE:
if (page_status != msg_args[0]) {
is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]);
page_status = msg_args[0];
numlock_status = 0;
capslock_status = 0;
led_set(host_keyboard_leds());
}
break;
case RESET_PAGE:
led_control_reg[0] = 0;
__builtin_memset(led_control_reg+1, 0, 0x12);
is31_write_data(msg_args[0], led_control_reg, 0x13);
led_control_reg[0] = 0x12;
is31_write_data(msg_args[0], led_control_reg, 0x13);
break;
case TOGGLE_NUM_LOCK:
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:
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:
switch (msg_args[0]) {
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;
}
__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;
}
}
}
void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uint8_t action) {
uint8_t control_reg_addr, column_bit, column_byte, temp, blink_bit;
if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
return;
}
blink_bit = action>>2; action &= ~(1<<2);
control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02; control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;
is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
chThdSleepMilliseconds(5);
is31_read_register(page, control_reg_addr, &temp); is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
column_bit = 1<<(led_addr % 10 - 1);
column_byte = temp;
switch(action) {
case 0:
column_byte &= ~column_bit;
break;
case 1:
column_byte |= column_bit;
break;
case 2:
column_byte ^= column_bit;
break;
}
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) {
uint8_t led_control_word[2] = {0};
led_control_word[0] = (row - 1 ) * 0x02; 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 pin, col;
uint8_t led_control_register[0x13] = {0};
__builtin_memset(led_control_register,0,13);
for(i=0;i<led_count;i++){
pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
col = user_led_array[i] % 10 - 1;
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 temp;
uint8_t led_control_word[2] = {0};
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_led_bit(page,led_control_word,led_addr,led_action);
}
void led_controller_init(void) {
uint8_t i;
palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2);
i2cStart(&I2CD1, &i2ccfg);
I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
I2CD1.i2c->FLT = 4;
chThdSleepMilliseconds(10);
is31_init();
is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
pwm_register_array[0] = 0;
__builtin_memset(pwm_register_array+1, 0xFF, 8);
for(i=0; i<8; i++) {
pwm_register_array[0] = 0x24 + (i * 0x10); is31_write_data(0, pwm_register_array, 9);
chThdSleepMilliseconds(5);
}
is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
}