Refactor some led_set_kb instances (#19179)

* Refactor some led_set_kb instances

* Apply suggestions from code review

Co-authored-by: Ryan <fauxpark@gmail.com>

Co-authored-by: Ryan <fauxpark@gmail.com>
This commit is contained in:
Joel Challis 2022-12-09 01:42:22 +00:00 committed by GitHub
parent ba6ee29040
commit 99cd0b13e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 165 additions and 202 deletions

View File

@ -15,7 +15,7 @@
*/
#include "modelm101.h"
void keyboard_pre_init_kb(void) {
void led_init_ports(void) {
/* Setting status LEDs pins to output and +5V (off) */
setPinOutput(B4);
setPinOutput(B5);
@ -25,22 +25,12 @@ void keyboard_pre_init_kb(void) {
writePinHigh(B6);
}
void led_set_kb(uint8_t usb_led) {
if (usb_led & (1<<USB_LED_NUM_LOCK)) {
writePinLow(B4);
} else {
writePinHigh(B4);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
writePin(B4, !led_state.num_lock);
writePin(B6, !led_state.caps_lock);
writePin(B5, !led_state.scroll_lock);
}
if (usb_led & (1<<USB_LED_CAPS_LOCK)) {
writePinLow(B6);
} else {
writePinHigh(B6);
}
if (usb_led & (1<<USB_LED_SCROLL_LOCK)) {
writePinLow(B5);
} else {
writePinHigh(B5);
}
led_set_user(usb_led);
return res;
}

View File

@ -42,20 +42,23 @@ void backlight_set(uint8_t level) {
}
}
// Port from backlight_update_state
void led_set_kb(uint8_t usb_led) {
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
bool status[8] = {
host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK), /* LED 3 */
host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK), /* LED 2 */
host_keyboard_leds() & (1<<USB_LED_NUM_LOCK), /* LED 1 */
led_state.scroll_lock, /* LED 3 */
led_state.caps_lock, /* LED 2 */
led_state.num_lock, /* LED 1 */
layer_state & (1<<2), /* LED 6 */
layer_state & (1<<1), /* LED 5 */
layer_state & (1<<0) ? 0: 1, /* LED 4 */
layer_state & (1<<2), /* LED 6 */
layer_state & (1<<1), /* LED 5 */
layer_state & (1<<0) ? 0: 1, /* LED 4 */
layer_state & (1<<5), /* LED 8 */
layer_state & (1<<4) /* LED 7 */
};
layer_state & (1<<5), /* LED 8 */
layer_state & (1<<4) /* LED 7 */
};
indicator_leds_set(status);
indicator_leds_set(status);
}
return res;
}

View File

@ -95,22 +95,25 @@ void backlight_update_state()
show();
}
void led_set_kb(uint8_t usb_led)
{
if(usb_led & (1<<USB_LED_CAPS_LOCK)) {
backlight_state_led |= 1<<STATE_LED_CAPS_LOCK;
} else {
backlight_state_led &= ~(1<<STATE_LED_CAPS_LOCK);
}
if(usb_led & (1<<USB_LED_SCROLL_LOCK)) {
backlight_state_led |= 1<<STATE_LED_SCROLL_LOCK;
} else {
backlight_state_led &= ~(1<<STATE_LED_SCROLL_LOCK);
}
if(usb_led & (1<<USB_LED_NUM_LOCK)) {
backlight_state_led |= 1<<STATE_LED_NUM_LOCK;
} else {
backlight_state_led &= ~(1<<STATE_LED_NUM_LOCK);
}
backlight_update_state();
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
if(led_state.caps_lock) {
backlight_state_led |= 1<<STATE_LED_CAPS_LOCK;
} else {
backlight_state_led &= ~(1<<STATE_LED_CAPS_LOCK);
}
if(led_state.scroll_lock) {
backlight_state_led |= 1<<STATE_LED_SCROLL_LOCK;
} else {
backlight_state_led &= ~(1<<STATE_LED_SCROLL_LOCK);
}
if(led_state.num_lock) {
backlight_state_led |= 1<<STATE_LED_NUM_LOCK;
} else {
backlight_state_led &= ~(1<<STATE_LED_NUM_LOCK);
}
backlight_update_state();
}
return res;
}

View File

@ -39,18 +39,20 @@ void backlight_set(uint8_t level) {
}
}
void led_set_kb(uint8_t usb_led) {
bool leds[8] = {
usb_led & (1<<USB_LED_CAPS_LOCK),
usb_led & (1<<USB_LED_SCROLL_LOCK),
usb_led & (1<<USB_LED_NUM_LOCK),
layer_state & (1<<1),
layer_state & (1<<2),
layer_state & (1<<3),
layer_state & (1<<4),
layer_state & (1<<5)
};
indicator_leds_set(leds);
led_set_user(usb_led);
}
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
bool leds[8] = {
led_state.caps_lock,
led_state.scroll_lock,
led_state.num_lock,
layer_state & (1<<1),
layer_state & (1<<2),
layer_state & (1<<3),
layer_state & (1<<4),
layer_state & (1<<5)
};
indicator_leds_set(leds);
}
return res;
}

View File

@ -62,8 +62,10 @@ void matrix_init_kb(void) {
matrix_init_user();
}
void led_set_kb(uint8_t usb_led) {
// put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
writePin(LED_02, !IS_LED_ON(usb_led, USB_LED_NUM_LOCK));
led_set_user(usb_led);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
writePin(LED_02, !led_state.num_lock);
}
return res;
}

View File

@ -62,8 +62,10 @@ void matrix_init_kb(void) {
matrix_init_user();
}
void led_set_kb(uint8_t usb_led) {
// put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
writePin(LED_02, !IS_LED_ON(usb_led, USB_LED_NUM_LOCK));
led_set_user(usb_led);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
writePin(LED_02, !led_state.num_lock);
}
return res;
}

View File

@ -62,8 +62,10 @@ void matrix_init_kb(void) {
matrix_init_user();
}
void led_set_kb(uint8_t usb_led) {
// put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
writePin(LED_02, !IS_LED_ON(usb_led, USB_LED_NUM_LOCK));
led_set_user(usb_led);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
writePin(LED_02, !led_state.num_lock);
}
return res;
}

View File

@ -100,20 +100,10 @@ void matrix_init_user(void) {
setPinOutput(C6);
}
void led_set_kb(uint8_t usb_led) {
if (IS_LED_OFF(usb_led, USB_LED_NUM_LOCK)) {
writePinLow(C4);
} else {
writePinHigh(C4);
}
if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK)) {
writePinLow(C5);
} else {
writePinHigh(C5);
}
if (IS_LED_OFF(usb_led, USB_LED_SCROLL_LOCK)) {
writePinLow(C6);
} else {
writePinHigh(C6);
}
bool led_update_user(led_t led_state) {
writePin(C4, led_state.num_lock);
writePin(C5, led_state.caps_lock);
writePin(C6, led_state.scroll_lock);
return false;
}

View File

@ -3,8 +3,6 @@
#include "quantum.h"
void battery_poll(uint8_t level);
void led_set_kb(uint8_t usb_led);
void led_set_user(uint8_t usb_led);
#define XXX KC_NO

View File

@ -12,36 +12,13 @@ void matrix_init_kb(void) {
matrix_init_user();
};
void led_set_kb(uint8_t usb_led) {
// put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
writePin(D0, !led_state.caps_lock);
writePin(D1, !led_state.num_lock);
writePin(C6, !led_state.scroll_lock);
if (usb_led & (1<<USB_LED_CAPS_LOCK)) {
// output low
DDRD |= (1<<0);
PORTD &= ~(1<<0);
} else {
// Hi-Z
DDRD &= ~(1<<0);
PORTD &= ~(1<<0);
}
if (usb_led & (1<<USB_LED_NUM_LOCK)) {
// output low
DDRD |= (1<<1);
PORTD &= ~(1<<1);
} else {
// Hi-Z
DDRD &= ~(1<<1);
PORTD &= ~(1<<1);
}
if (usb_led & (1<<USB_LED_SCROLL_LOCK)) {
// output low
DDRC |= (1<<6);
PORTC &= ~(1<<6);
} else {
// Hi-Z
DDRC &= ~(1<<6);
PORTC &= ~(1<<6);
}
led_set_user(usb_led);
};
return res;
}

View File

@ -70,19 +70,20 @@ void blink_all_leds(void)
matrix_init_user();
}
void led_set_kb(uint8_t usb_led) {
// put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
//Copyright 2014 Warren Janssens <warren.janssens@gmail.com>
uint8_t leds = 0xF0;
if (usb_led & 1 << USB_LED_NUM_LOCK)
leds &= ~0x10;
if (usb_led & 1 << USB_LED_CAPS_LOCK)
leds &= ~0x80;
if (usb_led & 1 << USB_LED_SCROLL_LOCK)
leds &= ~0x20;
PORTD = (PORTD & 0x0F) | leds;
led_set_user(usb_led);
uint8_t leds = 0xF0;
if (led_state.num_lock)
leds &= ~0x10;
if (led_state.caps_lock)
leds &= ~0x80;
if (led_state.scroll_lock)
leds &= ~0x20;
PORTD = (PORTD & 0x0F) | leds;
}
return res;
}

View File

@ -151,11 +151,12 @@ void reset_keyboard_kb() {
reset_keyboard();
}
void led_set_kb(uint8_t usb_led) {
// put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
#ifdef ISSI_ENABLE
# ifdef CAPSLOCK_LED
if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
if (led_state.caps_lock) {
activateLED(0, 3, 7, 255);
} else {
activateLED(0, 3, 7, 0);
@ -163,7 +164,8 @@ void led_set_kb(uint8_t usb_led) {
# endif // CAPSLOCK_LED
#endif // ISS_ENABLE
led_set_user(usb_led);
}
return res;
}
// LFK lighting info

View File

@ -128,15 +128,17 @@ void reset_keyboard_kb(){
reset_keyboard();
}
void led_set_kb(uint8_t usb_led)
{
// Set capslock LED to Blue
if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
set_rgb(31, 0x00, 0x00, 0x7F);
}else{
set_rgb(31, 0x00, 0x00, 0x00);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
// Set capslock LED to Blue
if (led_state.caps_lock) {
set_rgb(31, 0x00, 0x00, 0x7F);
} else{
set_rgb(31, 0x00, 0x00, 0x00);
}
}
led_set_user(usb_led);
return res;
}
// Lighting info, see lighting.h for details

View File

@ -134,15 +134,17 @@ void reset_keyboard_kb(){
reset_keyboard();
}
void led_set_kb(uint8_t usb_led)
{
// Set capslock LED to Blue
if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
set_rgb(31, 0x00, 0x00, 0x7F);
}else{
set_rgb(31, 0x00, 0x00, 0x00);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
// Set capslock LED to Blue
if (led_state.caps_lock) {
set_rgb(31, 0x00, 0x00, 0x7F);
} else{
set_rgb(31, 0x00, 0x00, 0x00);
}
}
led_set_user(usb_led);
return res;
}
// Lighting info, see lighting.h for details

View File

@ -22,13 +22,13 @@ void led_init_ports(void) {
setPinOutput(D2);
}
void led_set_kb(uint8_t usb_led) {
if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK)) {
writePinHigh(B2);
} else {
writePinLow(B2);
}
led_set_user(usb_led);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
writePin(B2, led_state.caps_lock);
}
return res;
}
layer_state_t layer_state_set_user(layer_state_t state)

View File

@ -8,12 +8,14 @@ extern inline void org60_caps_led_off(void);
extern inline void org60_bl_led_off(void);
void led_set_kb(uint8_t usb_led) {
if (usb_led & (1<<USB_LED_CAPS_LOCK)) {
org60_caps_led_on();
} else {
org60_caps_led_off();
}
led_set_user(usb_led);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
if (led_state.caps_lock) {
org60_caps_led_on();
} else {
org60_caps_led_off();
}
}
return res;
}

View File

@ -8,26 +8,14 @@ extern inline void xd60_caps_led_off(void);
extern inline void xd60_bl_led_off(void);
void led_set_kb(uint8_t usb_led) {
// put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
if (usb_led & (1<<USB_LED_CAPS_LOCK)) {
xd60_caps_led_on();
} else {
xd60_caps_led_off();
}
// if (usb_led & (1<<USB_LED_NUM_LOCK)) {
// xd60_esc_led_on();
// } else {
// xd60_esc_led_off();
// }
// if (usb_led & (1<<USB_LED_SCROLL_LOCK)) {
// xd60_fn_led_on();
// } else {
// xd60_fn_led_off();
// }
led_set_user(usb_led);
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
if (led_state.caps_lock) {
xd60_caps_led_on();
} else {
xd60_caps_led_off();
}
}
return res;
}

View File

@ -27,14 +27,11 @@ void matrix_init_kb(void) {
matrix_init_user();
}
void led_set_kb(uint8_t usb_led) {
// Bit 3 is Green LED, bit 4 is Red LED.
if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK)) {
send_data = 0x18;
} else {
send_data = 0x10;
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
send_data = led_state.caps_lock ? 0x18 : 0x10;
i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x09, &send_data, 1, 20);
}
i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x09, &send_data, 1, 20);
led_set_user(usb_led);
return res;
}