tmk_core icon indicating copy to clipboard operation
tmk_core copied to clipboard

Add weak attribute to led_set

Open p3lim opened this issue 9 years ago • 4 comments

This makes LEDs optional, lots of boards don't have any.

p3lim avatar Feb 14 '16 00:02 p3lim

It makes sense. I like to change LED api it self later but this would be useful until then.

Only adding the attribute 'weak' is enough for this pupose? don't you need default implementation somewhere?

tmk avatar Apr 01 '16 23:04 tmk

It's enough, in my tests it built without errors/warnings.

p3lim avatar Apr 02 '16 10:04 p3lim

I just tested, it can compiled as you said but it stops working or run away when pressing Capslock. From my understanding with weak attribute symbols are compiled as 0(EDIT: when no implementation is given). So if you don't have any implementation of led_set() the weak reference 0 is used when it is called. In the end it jumps into address 0 and go away.

tmk avatar Apr 02 '16 10:04 tmk

I guess we have two solutions;

  1. give default implementation In common/led.c:
__attribute__((weak))        
void led_set(uint8_t usb_led)
{
}

or use alias like: __attribute__((weak, alias("_led_set"))) ?

  1. check before calling it We have to check the reference everywhere led_set() is called.
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index eb7b096..bfca3e7 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -173,5 +173,5 @@ MATRIX_LOOP_END:
 void keyboard_set_leds(uint8_t leds)
 {
     if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }
-    led_set(leds);
+    if (led_set) led_set(leds);
 }

http://www.cs.virginia.edu/~wh5a/blog/The%20weak%20attribute%20of%20gcc.html

tmk avatar Apr 02 '16 11:04 tmk