KeyboardManager: a class to manage the polling of a switch-matrix keyboard
KeyboardMonitor.cpp@2:eb4cc53ff33d, 2011-01-23 (annotated)
- Committer:
- osmeest
- Date:
- Sun Jan 23 23:15:36 2011 +0000
- Revision:
- 2:eb4cc53ff33d
- Child:
- 3:1310c57aca77
Thorough split of the code to have one responsibility per class. Generalisation of the keyboard layout (not limited to 4x4 anymore). Introduction of the kbd_mgr namespace (also apparent in header filenames).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 2:eb4cc53ff33d | 1 | #include "kbd_mgr/KeyboardMonitor.h" |
osmeest | 2:eb4cc53ff33d | 2 | |
osmeest | 2:eb4cc53ff33d | 3 | namespace kbd_mgr { |
osmeest | 2:eb4cc53ff33d | 4 | |
osmeest | 2:eb4cc53ff33d | 5 | KeyboardMonitor::KeyboardMonitor(PortName inPort, std::size_t numKeysPerRow, std::size_t inLowestBit, |
osmeest | 2:eb4cc53ff33d | 6 | const KeyboardMonitor::OutPinsSet &outPins, KeyboardStateHandler *handler) : |
osmeest | 2:eb4cc53ff33d | 7 | in(inPort, ((1 << numKeysPerRow)-1) << inLowestBit), inBitShift(inLowestBit), outPins(outPins), handler(handler), |
osmeest | 2:eb4cc53ff33d | 8 | ticker(), scanRow(0), currentState(outPins.size(), numKeysPerRow) |
osmeest | 2:eb4cc53ff33d | 9 | { } |
osmeest | 2:eb4cc53ff33d | 10 | |
osmeest | 2:eb4cc53ff33d | 11 | void KeyboardMonitor::start(float pollingPeriod) |
osmeest | 2:eb4cc53ff33d | 12 | { |
osmeest | 2:eb4cc53ff33d | 13 | if (this->outPins.empty()) { |
osmeest | 2:eb4cc53ff33d | 14 | return; |
osmeest | 2:eb4cc53ff33d | 15 | } |
osmeest | 2:eb4cc53ff33d | 16 | |
osmeest | 2:eb4cc53ff33d | 17 | if (pollingPeriod < 20e-6) { |
osmeest | 2:eb4cc53ff33d | 18 | pollingPeriod = 20e-6; |
osmeest | 2:eb4cc53ff33d | 19 | } |
osmeest | 2:eb4cc53ff33d | 20 | |
osmeest | 2:eb4cc53ff33d | 21 | for(OutPinsSet::const_iterator p = this->outPins.begin(); p != this->outPins.end(); ++p) { |
osmeest | 2:eb4cc53ff33d | 22 | DigitalOut out(*p); |
osmeest | 2:eb4cc53ff33d | 23 | out.write( p == this->outPins.begin() ? 1 : 0 ); |
osmeest | 2:eb4cc53ff33d | 24 | } |
osmeest | 2:eb4cc53ff33d | 25 | this->in.mode(PullDown); |
osmeest | 2:eb4cc53ff33d | 26 | this->scanRow = 0; |
osmeest | 2:eb4cc53ff33d | 27 | this->ticker.attach(this, &KeyboardMonitor::timerHandler, pollingPeriod); |
osmeest | 2:eb4cc53ff33d | 28 | } |
osmeest | 2:eb4cc53ff33d | 29 | |
osmeest | 2:eb4cc53ff33d | 30 | void KeyboardMonitor::stop() |
osmeest | 2:eb4cc53ff33d | 31 | { |
osmeest | 2:eb4cc53ff33d | 32 | this->ticker.detach(); |
osmeest | 2:eb4cc53ff33d | 33 | this->in.mode(OpenDrain); |
osmeest | 2:eb4cc53ff33d | 34 | } |
osmeest | 2:eb4cc53ff33d | 35 | |
osmeest | 2:eb4cc53ff33d | 36 | void KeyboardMonitor::timerHandler() |
osmeest | 2:eb4cc53ff33d | 37 | { |
osmeest | 2:eb4cc53ff33d | 38 | DigitalOut out(this->outPins[this->scanRow]); |
osmeest | 2:eb4cc53ff33d | 39 | out.write(1); |
osmeest | 2:eb4cc53ff33d | 40 | wait_us(10); |
osmeest | 2:eb4cc53ff33d | 41 | int v = (this->in.read() >> this->inBitShift); |
osmeest | 2:eb4cc53ff33d | 42 | out.write(0); |
osmeest | 2:eb4cc53ff33d | 43 | this->currentState.setRowState(this->scanRow, v); |
osmeest | 2:eb4cc53ff33d | 44 | this->scanRow = (this->scanRow + 1) % this->currentState.getNumRows(); |
osmeest | 2:eb4cc53ff33d | 45 | if (this->scanRow == 0) { |
osmeest | 2:eb4cc53ff33d | 46 | if (this->handler) { |
osmeest | 2:eb4cc53ff33d | 47 | (*this->handler)(this->currentState); |
osmeest | 2:eb4cc53ff33d | 48 | } |
osmeest | 2:eb4cc53ff33d | 49 | } |
osmeest | 2:eb4cc53ff33d | 50 | } |
osmeest | 2:eb4cc53ff33d | 51 | |
osmeest | 2:eb4cc53ff33d | 52 | } // kbd_mgr |