diff options
| author | jpetermans <tibcmhhm@gmail.com> | 2017-05-25 13:00:21 -0700 |
|---|---|---|
| committer | jpetermans <tibcmhhm@gmail.com> | 2017-05-25 13:00:21 -0700 |
| commit | ac97870801a684b228cb7a667ea423d0ec381b1e (patch) | |
| tree | 63ec7e1ee1476995f97b1b3cf60f4b48c52e6455 | |
| parent | 083267680d15b5d03d31c6775b1046063c762e78 (diff) | |
| download | qmk_firmware-ac97870801a684b228cb7a667ea423d0ec381b1e.tar.gz qmk_firmware-ac97870801a684b228cb7a667ea423d0ec381b1e.zip | |
reduce recurring writes to lock leds
| -rw-r--r-- | keyboards/infinity60/led_controller.c | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/keyboards/infinity60/led_controller.c b/keyboards/infinity60/led_controller.c index 04793cc01..5c177d26b 100644 --- a/keyboards/infinity60/led_controller.c +++ b/keyboards/infinity60/led_controller.c | |||
| @@ -135,16 +135,24 @@ msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) { | |||
| 135 | * initialise the IS31 chip | 135 | * initialise the IS31 chip |
| 136 | * ======================== */ | 136 | * ======================== */ |
| 137 | void is31_init(void) { | 137 | void is31_init(void) { |
| 138 | xprintf("_is31_init\n"); | ||
| 138 | // just to be sure that it's all zeroes | 139 | // just to be sure that it's all zeroes |
| 139 | __builtin_memset(full_page,0,0xB4+1); | 140 | __builtin_memset(full_page,0,0xB4+1); |
| 140 | // zero function page, all registers (assuming full_page is all zeroes) | 141 | // zero function page, all registers (assuming full_page is all zeroes) |
| 141 | is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1); | 142 | is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1); |
| 143 | // disable hardware shutdown | ||
| 142 | palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL); | 144 | palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL); |
| 143 | palSetPad(GPIOB, 16); | 145 | palSetPad(GPIOB, 16); |
| 144 | chThdSleepMilliseconds(10); | 146 | chThdSleepMilliseconds(10); |
| 145 | // software shutdown disable (i.e. turn stuff on) | 147 | // software shutdown |
| 146 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON); | 148 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON); |
| 147 | chThdSleepMilliseconds(10); | 149 | chThdSleepMilliseconds(10); |
| 150 | // zero function page, all registers | ||
| 151 | is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1); | ||
| 152 | chThdSleepMilliseconds(10); | ||
| 153 | // software shutdown disable (i.e. turn stuff on) | ||
| 154 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF); | ||
| 155 | chThdSleepMilliseconds(10); | ||
| 148 | // zero all LED registers on all 8 pages | 156 | // zero all LED registers on all 8 pages |
| 149 | uint8_t i; | 157 | uint8_t i; |
| 150 | for(i=0; i<8; i++) { | 158 | for(i=0; i<8; i++) { |
| @@ -169,7 +177,7 @@ static THD_FUNCTION(LEDthread, arg) { | |||
| 169 | uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes | 177 | uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes |
| 170 | 178 | ||
| 171 | //persistent status variables | 179 | //persistent status variables |
| 172 | uint8_t pwm_step_status, page_status; | 180 | uint8_t pwm_step_status, page_status, capslock_status, numlock_status; |
| 173 | 181 | ||
| 174 | //mailbox variables | 182 | //mailbox variables |
| 175 | uint8_t temp, msg_type; | 183 | uint8_t temp, msg_type; |
| @@ -179,6 +187,8 @@ static THD_FUNCTION(LEDthread, arg) { | |||
| 179 | // initialize persistent variables | 187 | // initialize persistent variables |
| 180 | pwm_step_status = 4; //full brightness | 188 | pwm_step_status = 4; //full brightness |
| 181 | page_status = 0; //start frame 0 (all off/on) | 189 | page_status = 0; //start frame 0 (all off/on) |
| 190 | numlock_status = (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? 1 : 0; | ||
| 191 | capslock_status = (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? 1 : 0; | ||
| 182 | 192 | ||
| 183 | while(true) { | 193 | while(true) { |
| 184 | // wait for a message (asynchronous) | 194 | // wait for a message (asynchronous) |
| @@ -190,38 +200,53 @@ static THD_FUNCTION(LEDthread, arg) { | |||
| 190 | msg_args[1] = (msg >> 16) & 0XFF; | 200 | msg_args[1] = (msg >> 16) & 0XFF; |
| 191 | msg_args[2] = (msg >> 24) & 0xFF; | 201 | msg_args[2] = (msg >> 24) & 0xFF; |
| 192 | 202 | ||
| 203 | xprintf("msg_type: %d-%d-%d\n", msg_type, msg_args[0], msg_args[1]); | ||
| 204 | |||
| 193 | switch (msg_type){ | 205 | switch (msg_type){ |
| 194 | case SET_FULL_ROW: | 206 | case SET_FULL_ROW: |
| 207 | xprintf("FULL ROW: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 195 | //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write | 208 | //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write |
| 196 | //writes only to currently displayed page | 209 | //writes only to currently displayed page |
| 197 | write_led_byte(page_status, msg_args[1], msg_args[0]); | 210 | write_led_byte(page_status, msg_args[1], msg_args[0]); |
| 198 | break; | 211 | break; |
| 199 | 212 | ||
| 200 | case OFF_LED: | 213 | case OFF_LED: |
| 214 | xprintf("OFF: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 201 | //on/off/toggle single led, msg_args[0] = row/col of led, msg_args[1] = page | 215 | //on/off/toggle single led, msg_args[0] = row/col of led, msg_args[1] = page |
| 202 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 0); | 216 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 0); |
| 203 | break; | 217 | break; |
| 204 | case ON_LED: | 218 | case ON_LED: |
| 219 | xprintf("ON: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 205 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 1); | 220 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 1); |
| 206 | break; | 221 | break; |
| 207 | case TOGGLE_LED: | 222 | case TOGGLE_LED: |
| 223 | xprintf("TOGGLE: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 208 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 2); | 224 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 2); |
| 209 | break; | 225 | break; |
| 210 | 226 | ||
| 211 | case BLINK_OFF_LED: | 227 | case BLINK_OFF_LED: |
| 228 | xprintf("B_on: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 212 | //on/off/toggle single led, msg_args[0] = row/col of led | 229 | //on/off/toggle single led, msg_args[0] = row/col of led |
| 213 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 4); | 230 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 4); |
| 214 | break; | 231 | break; |
| 215 | case BLINK_ON_LED: | 232 | case BLINK_ON_LED: |
| 233 | xprintf("B_off: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 216 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 5); | 234 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 5); |
| 217 | break; | 235 | break; |
| 218 | case BLINK_TOGGLE_LED: | 236 | case BLINK_TOGGLE_LED: |
| 237 | xprintf("B_togg: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 219 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 6); | 238 | set_led_bit(msg_args[1], control_register_word, msg_args[0], 6); |
| 220 | break; | 239 | break; |
| 221 | 240 | ||
| 222 | case TOGGLE_ALL: | 241 | case TOGGLE_ALL: |
| 223 | //turn on/off all leds, msg_args = unused | 242 | //turn on/off all leds, msg_args = unused |
| 243 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON); | ||
| 244 | chThdSleepMilliseconds(5); | ||
| 224 | is31_read_register(0, 0x00, &temp); | 245 | is31_read_register(0, 0x00, &temp); |
| 246 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF); | ||
| 247 | xprintf("TOGGLE_ALL: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 248 | xprintf("temp: %d\n", temp); | ||
| 249 | |||
| 225 | led_control_reg[0] = 0; | 250 | led_control_reg[0] = 0; |
| 226 | 251 | ||
| 227 | //if first leds are already on, toggle frame 0 off | 252 | //if first leds are already on, toggle frame 0 off |
| @@ -243,6 +268,7 @@ static THD_FUNCTION(LEDthread, arg) { | |||
| 243 | break; | 268 | break; |
| 244 | 269 | ||
| 245 | case TOGGLE_BACKLIGHT: | 270 | case TOGGLE_BACKLIGHT: |
| 271 | xprintf("TOGGLE_BKLT: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 246 | //msg_args[0] = on/off | 272 | //msg_args[0] = on/off |
| 247 | 273 | ||
| 248 | //populate 9 byte rows to be written to each pin, first byte is register (pin) address | 274 | //populate 9 byte rows to be written to each pin, first byte is register (pin) address |
| @@ -261,6 +287,7 @@ static THD_FUNCTION(LEDthread, arg) { | |||
| 261 | 287 | ||
| 262 | case DISPLAY_PAGE: | 288 | case DISPLAY_PAGE: |
| 263 | //msg_args[0] = page to toggle on | 289 | //msg_args[0] = page to toggle on |
| 290 | xprintf("DSPY_PG: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 264 | if (page_status != msg_args[0]) { | 291 | if (page_status != msg_args[0]) { |
| 265 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]); | 292 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]); |
| 266 | page_status = msg_args[0]; | 293 | page_status = msg_args[0]; |
| @@ -282,15 +309,24 @@ static THD_FUNCTION(LEDthread, arg) { | |||
| 282 | break; | 309 | break; |
| 283 | 310 | ||
| 284 | case TOGGLE_NUM_LOCK: | 311 | case TOGGLE_NUM_LOCK: |
| 312 | xprintf("NMLK: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 285 | //msg_args[0] = 0 or 1, off/on | 313 | //msg_args[0] = 0 or 1, off/on |
| 286 | set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status); | 314 | if (numlock_status != msg_args[0]) { |
| 315 | set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status); | ||
| 316 | numlock_status = msg_args[0]; | ||
| 317 | } | ||
| 287 | break; | 318 | break; |
| 288 | case TOGGLE_CAPS_LOCK: | 319 | case TOGGLE_CAPS_LOCK: |
| 320 | xprintf("CPLK: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 289 | //msg_args[0] = 0 or 1, off/on | 321 | //msg_args[0] = 0 or 1, off/on |
| 290 | set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status); | 322 | if (capslock_status != msg_args[0]) { |
| 323 | set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status); | ||
| 324 | capslock_status = msg_args[0]; | ||
| 325 | } | ||
| 291 | break; | 326 | break; |
| 292 | 327 | ||
| 293 | case STEP_BRIGHTNESS: | 328 | case STEP_BRIGHTNESS: |
| 329 | xprintf("Step: %d-%d\n", msg_args[0], msg_args[1]); | ||
| 294 | //led_args[0] = step up (1) or down (0) | 330 | //led_args[0] = step up (1) or down (0) |
| 295 | switch (msg_args[0]) { | 331 | switch (msg_args[0]) { |
| 296 | case 0: | 332 | case 0: |
| @@ -337,6 +373,7 @@ void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uin | |||
| 337 | if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) { | 373 | if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) { |
| 338 | return; | 374 | return; |
| 339 | } | 375 | } |
| 376 | xprintf("_set action-led: %x-%d\n", action, led_addr); | ||
| 340 | 377 | ||
| 341 | blink_bit = action>>2;//check for blink bit | 378 | blink_bit = action>>2;//check for blink bit |
| 342 | action &= ~(1<<2); //strip blink bit | 379 | action &= ~(1<<2); //strip blink bit |
| @@ -344,10 +381,17 @@ void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uin | |||
| 344 | //led_addr tens column is pin#, ones column is bit position in 8-bit mask | 381 | //led_addr tens column is pin#, ones column is bit position in 8-bit mask |
| 345 | control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte | 382 | control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte |
| 346 | control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register | 383 | control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register |
| 384 | xprintf("_set control address: %x\n", control_reg_addr); | ||
| 347 | 385 | ||
| 386 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON); | ||
| 387 | chThdSleepMilliseconds(5); | ||
| 348 | is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte | 388 | is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte |
| 389 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF); | ||
| 390 | xprintf("_set temp_byte_mask: %x\n", temp); | ||
| 391 | |||
| 349 | column_bit = 1<<(led_addr % 10 - 1); | 392 | column_bit = 1<<(led_addr % 10 - 1); |
| 350 | column_byte = temp; | 393 | column_byte = temp; |
| 394 | xprintf("_set col_byte_mask: %x\n", column_byte); | ||
| 351 | 395 | ||
| 352 | switch(action) { | 396 | switch(action) { |
| 353 | case 0: | 397 | case 0: |
| @@ -398,13 +442,16 @@ void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) { | |||
| 398 | 442 | ||
| 399 | //blink if all leds are on | 443 | //blink if all leds are on |
| 400 | if (page == 0) { | 444 | if (page == 0) { |
| 445 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON); | ||
| 446 | chThdSleepMilliseconds(5); | ||
| 401 | is31_read_register(0, 0x00, &temp); | 447 | is31_read_register(0, 0x00, &temp); |
| 402 | chThdSleepMilliseconds(10); | 448 | is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF); |
| 403 | 449 | ||
| 404 | if (temp == 0xFF) { | 450 | if (temp == 0xFF) { |
| 405 | led_action |= (1<<2); //set blink bit | 451 | led_action |= (1<<2); //set blink bit |
| 406 | } | 452 | } |
| 407 | } | 453 | } |
| 454 | xprintf("_lock action: %d\n", led_action); | ||
| 408 | 455 | ||
| 409 | set_led_bit(page,led_control_word,led_addr,led_action); | 456 | set_led_bit(page,led_control_word,led_addr,led_action); |
| 410 | } | 457 | } |
| @@ -415,6 +462,7 @@ void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) { | |||
| 415 | void led_controller_init(void) { | 462 | void led_controller_init(void) { |
| 416 | uint8_t i; | 463 | uint8_t i; |
| 417 | 464 | ||
| 465 | xprintf("led_init\n"); | ||
| 418 | /* initialise I2C */ | 466 | /* initialise I2C */ |
| 419 | /* I2C pins */ | 467 | /* I2C pins */ |
| 420 | palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL | 468 | palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL |
