Merge pull request #15 from gedankenexperimenter/key-mask-clarity

Subtle change to key masking computation
pull/365/head
Jesse Vincent 6 years ago committed by GitHub
commit 6b598d2e24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -207,14 +207,28 @@ 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.
constexpr byte HIGH_BIT = B10000000;
constexpr byte HAND_BIT = B00001000;
constexpr byte ROW_BITS = B00110000;
constexpr byte COL_BITS = B00000111;
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 & HAND_BIT) {
rightHandMask.rows[row] |= 1 << (7 - (col - 8)); rightHandMask.rows[row] |= (HIGH_BIT >> (col & COL_BITS));
} else { } else {
leftHandMask.rows[row] |= 1 << (7 - col); leftHandMask.rows[row] |= (HIGH_BIT >> (col & COL_BITS));
} }
} }
@ -222,10 +236,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 & HAND_BIT) {
rightHandMask.rows[row] &= ~(1 << (7 - (col - 8))); rightHandMask.rows[row] &= ~(HIGH_BIT >> (col & COL_BITS));
} else { } else {
leftHandMask.rows[row] &= ~(1 << (7 - col)); leftHandMask.rows[row] &= ~(HIGH_BIT >> (col & COL_BITS));
} }
} }
@ -233,10 +247,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 & HAND_BIT) {
return rightHandMask.rows[row] & (1 << (7 - (col - 8))); return rightHandMask.rows[row] & (HIGH_BIT >> (col & COL_BITS));
} else { } else {
return leftHandMask.rows[row] & (1 << (7 - col)); return leftHandMask.rows[row] & (HIGH_BIT >> (col & COL_BITS));
} }
} }

Loading…
Cancel
Save