diff --git a/src/Kaleidoscope-Hardware-Model01.cpp b/src/Kaleidoscope-Hardware-Model01.cpp index cbf6d0c8..4459fbd9 100644 --- a/src/Kaleidoscope-Hardware-Model01.cpp +++ b/src/Kaleidoscope-Hardware-Model01.cpp @@ -204,14 +204,22 @@ void Model01::rebootBootloader() { // 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) { if (row >= ROWS || col >= COLS) return; - if (col >= 8) { - rightHandMask.rows[row] |= 1 << (7 - (col - 8)); + if (col & 8) { + rightHandMask.rows[row] |= (128 >> (col & 7)); } 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) return; - if (col >= 8) { - rightHandMask.rows[row] &= ~(1 << (7 - (col - 8))); + if (col & 8) { + rightHandMask.rows[row] &= ~(128 >> (col & 7)); } 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) return false; - if (col >= 8) { - return rightHandMask.rows[row] & (1 << (7 - (col - 8))); + if (col & 8) { + return rightHandMask.rows[row] & (128 >> (col & 7)); } else { - return leftHandMask.rows[row] & (1 << (7 - col)); + return leftHandMask.rows[row] & (128 >> (col & 7)); } }