Subtle change to key masking computation

This doesn't change behaviour at all; it's just a different way to do the computation,
which I think is much clearer. I also added an explanatory comment.

* It's now all bitwise operations, without arithemetic thrown in.
* It uses the same exact formula for finding bits on both sides of the keyboard.
* It saves 14 bytes in program memory.
pull/365/head
Michael Richters 7 years ago
parent c126e1b27b
commit 0cb4ac7970

@ -204,14 +204,22 @@ void Model01::rebootBootloader() {
// happens before the watchdog reboots us // happens before the watchdog reboots us
} }
// In the maskKey(), unMaskKey(), and isKeyMasked() functions, we read and write bits in
// two bitfields -- one for each half of the keyboard. The fourth bit of the column number
// tells us which bitfield (right or left) to access, thus the "8" (B00001000). The row
// number tells us which element of the array to access. The last three bits of the column
// number tell us which of the eight bits to access, thus the "7" (B00000111), and we
// shift a bit starting from the left (B10000000, or 128) by that many places to get
// there. This is all nice and convenient because the keyboard has 64 keys, in symmetric
// halves, with eight keys per logical row.
void Model01::maskKey(byte row, byte col) { void Model01::maskKey(byte row, byte col) {
if (row >= ROWS || col >= COLS) if (row >= ROWS || col >= COLS)
return; return;
if (col >= 8) { if (col & 8) {
rightHandMask.rows[row] |= 1 << (7 - (col - 8)); rightHandMask.rows[row] |= (128 >> (col & 7));
} else { } else {
leftHandMask.rows[row] |= 1 << (7 - col); leftHandMask.rows[row] |= (128 >> (col & 7));
} }
} }
@ -219,10 +227,10 @@ void Model01::unMaskKey(byte row, byte col) {
if (row >= ROWS || col >= COLS) if (row >= ROWS || col >= COLS)
return; return;
if (col >= 8) { if (col & 8) {
rightHandMask.rows[row] &= ~(1 << (7 - (col - 8))); rightHandMask.rows[row] &= ~(128 >> (col & 7));
} else { } else {
leftHandMask.rows[row] &= ~(1 << (7 - col)); leftHandMask.rows[row] &= ~(128 >> (col & 7));
} }
} }
@ -230,10 +238,10 @@ bool Model01::isKeyMasked(byte row, byte col) {
if (row >= ROWS || col >= COLS) if (row >= ROWS || col >= COLS)
return false; return false;
if (col >= 8) { if (col & 8) {
return rightHandMask.rows[row] & (1 << (7 - (col - 8))); return rightHandMask.rows[row] & (128 >> (col & 7));
} else { } else {
return leftHandMask.rows[row] & (1 << (7 - col)); return leftHandMask.rows[row] & (128 >> (col & 7));
} }
} }

Loading…
Cancel
Save