5 * Lookup trinary nibble
7 * This was implemented using a switch statement before.
8 * Changing the lookup to a table did only add two bytes
9 * of memory and saved ca. 50 bytes program memory.
11 static const uint8_t __mm_nibble_table
[16]={
22 #define __mm_lookup_nibble(nibble) __mm_nibble_table[nibble & 0xf]
24 static uint8_t __attribute__((unused
)) mm_lookup_decoder(uint8_t mm_byte
)
32 low
= __mm_lookup_nibble(mm_byte
>> 4);
33 high
= __mm_lookup_nibble(mm_byte
& 0xf);
37 /* retval = 9 * high + low; */
44 static uint8_t __attribute__((unused
)) mm_lookup_key(uint8_t mm_command
)
48 * Check for aabbccdd condition
50 * a a b b c c d d mm_command
51 * XOR a b b c c d d 0 mm_command << 1
52 * Mask 1 0 1 0 1 0 1 0 0xaa
58 if ((mm_command
^ (mm_command
<< 1)) & 0xaa)
61 * Protocol differences:
62 * =====================
64 * I have an old "central control" 6022 and a "control unit" 6021
65 * for measurements and test. It is assumed that the 6022 outputs
66 * old MM1 format while the 6021 definitively outputs MM2 telegrams.
68 * In MM1, switch commands are different from MM2 with respect what
69 * happens if you release a button.
71 * When you press a button, both protocols send
73 * <aaaaaaaa><00><aabbcc11>
75 * where a = 1, b = 2, c = 4 and the keys are numerated from 0 to 7
76 * in the order 1 red, 1 green, 2 red, 2 green and so on.
78 * The last two bits correspond to "on" state of the button/coil.
80 * When a key is released under MM1 protocol, the sequence sent is
81 * analogue to the button down sequence:
83 * <aaaaaaaa><00><aabbcc00> where abc again represents the button's
84 * address and the last bits now signal "off".
86 * MM2 handles this differently:
87 * Whenever any key from the addressed decoder is released, the sequence
88 * <aaaaaaaa>00<00000000> is sent - not only for key 0, but for all
91 * While MM1 presents the theoretical possibility to press several keys
92 * independently and simultaneously (which my keyboard does NOT
93 * support), MM2 supports only one key at a time (besides strange
94 * sequences like "one down, another down, all up"...
96 * A decoder that strictly adheres to the MM1 standard would not work
97 * properly with MM2 control units. As far as I know all K83/K84
98 * decoders always worked with MM2 control units. That means that
99 * they reduce the commands to the possibilities of MM2 from the
102 * Possible use cases for the old protocol button release commands:
103 * - Determine if the protocol is MM1 or MM2
104 * - Implement hidden evil features into the controller which can
105 * only be summoned by old MM1 gear or selfmade control telegram
108 * What this code now actually does:
109 * =================================
111 * When key pressed (aabbcc11), it will send out the key number in the
112 * range 1-8 and 0 if it gets any key up command and therefore ignore
113 * the key number if it is transmitted with the key up command.
116 if (!(mm_command
& 0x01))
119 res
= (mm_command
& 0x80) * 1 + (mm_command
& 0x20) * 0x02
120 + (mm_command
& 0x08) * 0x04 + 1;