CapsLock LED color with the new Keyboardio code?

I had been using CapsLock from donfeduardo, and it came with the ability to set an LED color when capslock was toggled. I pulled the newest Keyboardio firmware, and I see that it comes with CapsLock in there by default. That’s cool, but is there any way to set an LED color for my capslock key without installing donfeduardo’s capslock? Or more generally, how would I build a macro to both send a keystroke and set a color, either temporarily or toggle?

thanks!

The easiest would be to build a macro, something like this:

const macro_t toggleCapsLock(uint8_t key_state) {
  static bool capsOn = false;

  if (!keyToggledOn(key_state))
    return MACRO_NONE;

  capsOn = !capsOn;
  if (capsOn) {
    LEDControl.setCrgbAt(0, 0, CRGB(150, 0, 0));
  }
  return MACRO(T(CapsLock));
};

Something along these lines. You’ll need to adjust the coordinates of your capslock key, and the color. Keep in mind that this example is completely untested, but should work, hopefully.

Hrmm.

This is what I pasted in my firmware sketch:

const macro_t toggleCapsLock(uint8_t key_state) {
  static bool capsOn = false;
  if (!keyToggledOn(key_state))
    return MACRO_NONE;
  capsOn = !capsOn;
  if (capsOn) {
    LEDControl.setCrgbAt(0, 0, CRGB(150, 0, 0));
  }
  return MACRO(T(CapsLock));
}

and this is the compilation error I got:

In file included from C:\Users\eric\Documents\Arduino\hardware\keyboardio\avr\libraries\Kaleidoscope\src/kaleidoscope/plugin/Macros.h:22:0,

                 from C:\Users\eric\Documents\Arduino\hardware\keyboardio\avr\libraries\Kaleidoscope\src/Kaleidoscope-Macros.h:19,

                 from C:\Users\eric\Documents\Arduino\Model01-Firmware\Model01-Firmware.ino:30:

C:\Users\eric\Documents\Arduino\Model01-Firmware\Model01-Firmware.ino: In function 'const macro_t toggleCapsLock(uint8_t)':

C:\Users\eric\Documents\Arduino\hardware\keyboardio\avr\libraries\Kaleidoscope\src/kaleidoscope/plugin/Macros/MacroSteps.h:41:101: error: invalid conversion from 'const macro_t* {aka const unsigned char*}' to 'macro_t {aka unsigned char}' [-fpermissive]

 #define MACRO(...) ({static const macro_t __m[] PROGMEM = { __VA_ARGS__, MACRO_ACTION_END }; &__m[0]; })

                                                                                                     ^

C:\Users\eric\Documents\Arduino\Model01-Firmware\Model01-Firmware.ino:340:10: note: in expansion of macro 'MACRO'

   return MACRO(T(CapsLock));

          ^

Multiple libraries were found for "HID.h"
 Used: C:\Users\eric\Documents\Arduino\hardware\keyboardio\avr\libraries\HID
 Not used: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\HID
exit status 1
Error compiling for board Keyboardio Model 01.

What am I missing?

My example had a small bug: the type of toggleCapsLock should be const macro_t *, not const macro_t. That star is important, and the compiler is complaining about that.

Thanks! It compiles now, but it’s still not working. I barely know enough C++ to be dangerous, but here’s the macro function:

const macro_t * toggleCapsLock(uint8_t key_state) {
  static bool capsOn = false;
  if (!keyToggledOn(key_state))
    return MACRO_NONE;
  capsOn = !capsOn;
  if (capsOn) {
    LEDControl.setCrgbAt(0, 0, CRGB(0, 160, 0));
  }
  return MACRO(T(CapsLock));
}

I’m triggering it the way I trigger all other macros:

case MACRO_CAPSLOCK:
    toggleCapsLock(keyState);
    break;

but it seems like I should be doing something with the return from toggleCapsLock(). What’s the magic bit I put in the MACRO_CAPSLOCK case statement to call whatever toggleCapsLock() returns?

eric

return toggleCapsLock(keyState);

That should do it.

It works, although I had to flip the LED logic around and add something to shut the LEDs off when capslock was off. For the archives, working code is:

const macro_t * toggleCapsLock(uint8_t key_state) {
  static bool capsOn = false;
  if (!keyToggledOn(key_state))
    return MACRO_NONE;
  capsOn = !capsOn;
  if (capsOn) {
    LEDControl.setCrgbAt(0, 6, CRGB(0, 0, 0));
  }
  else {
    LEDControl.setCrgbAt(0, 6, CRGB(0, 160, 0));
  } 
  return MACRO(T(CapsLock));
}

(also useful to note: 0,6 is the RxCy coordinate of the key you want to toggle)

It’s a little touchy - if I try to toggle caps lock immediately after the keyboard comes up, it gets confused and sets the LED when capslock is off. I think this might be interacting with the code which sets the LED-key LED to blue when the keyboard first comes on. If I don’t touch capslock for a few seconds after keyboard bootup, it works fine.

thanks!

whoops…I spoke too soon.
The code I have works fine on Windows, but not on a Mac. On a Mac it changes the color but capslock does not toggle.

If I move RC 06 back to just sending Key_CapsLock, that works fine on a Mac. Is there some difference between

MACRO(T(CapsLock))

and

Key_CapsLock

that the Mac is tripping over?