I recently purchased an ALT mechanical keyboard from Drop (formerly Massdrop).

After going through the mandatory modding (changing Halo Trues for Aliaz switches, changing stabilizers, lubbing what needed to be lubed, etc...), I started compiling and flashing my own custom QMK firmware. It worked pretty great, but I was stuck at backlight configuration. I could not understand or guess how to configure it.

I ended up reading code and examples provided by QMK. The keyboard is built upon a 105 LED matrix.

A particular example caught my attention :

1//Specific LEDs use specified RGB values while all others are off
2{ .flags = LED_FLAG_MATCH_ID | LED_FLAG_USE_RGB, \
3 .id0 = 0xFF, \
4 .id1 = 0x00FF, \
5 .id2 = 0x0000FF00, \
6 .id3 = 0xFF000000, \
7 .r = 75, \
8 .g = 150, \
9 .b = 225 \
10},

And it struck me ! Bitmask ! The only thing that can select -- at the same time -- both a single address and multiple ones is a bitmask !

By converting hexadecimal values to decimal, we get familiar-looking numbers. They belong to the successive power of two serie :

$$ \mathrm{0xFF} = 255 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = \displaystyle\sum_{i=0}^{7} 2^{i} $$

$$ \mathrm{0x0000FF00} = 65280 = 1 + 2 + \ldots = \displaystyle\sum_{i=8}^{15} 2^{i} $$

$$ \mathrm{0xFF000000} = 4278190080 = 16 777 216 + 33 554 432 + \ldots = \displaystyle\sum_{i=24}^{31} 2^{i} $$

The keyboard is separated in 4 groups of 32 LEDs (except the last one of 9 LEDs) :

  • .id0: from Esc to a
  • .id1: from s to fn
  • .id2: from Left Arrow to Underglow LED above 5
  • .id3: from Underglow LED above 4 to the end (Underglow LED left of Ctrl)

Each LED is assigned a number from 0 to 31 based on its position in the group.

Let say we want to display a green color on the first four letters of every row (Q(16), W(17), E(18), R(19), A(31), S(0), D(1), F(2), Z(13), X(14), C(15), V(16)). We just need to add every value as a power of two :

$$ .id0 = 2^{16} + 2^{17} + 2^{18} + 2^{19} + 2^{31} = 2 148 466 688 $$ $$ .id1 = 2^0 + 2^1 + 2^2 + 2^{13} + 2^{14} + 2^{15} + 2^{16} = 122 887 $$

And we get :

1{ .flags = LED_FLAG_MATCH_ID | LED_FLAG_USE_RGB, \
2 .id0 = 2148466688, \
3 .id1 = 122887, \
4 .id2 = 0, \
5 .id3 = 0, \
6 .r = 0, \
7 .g = 255, \
8 .b = 0, \
9},

While being a smart way to target both one and multiple LEDs, it's not very easy to understand and even less to change.

Fortunately, someone very nice created a bitmask generator online.