KeyboardManager: a class to manage the polling of a switch-matrix keyboard
KeyboardMonitor.cpp@3:1310c57aca77, 2011-02-03 (annotated)
- Committer:
- osmeest
- Date:
- Thu Feb 03 22:01:57 2011 +0000
- Revision:
- 3:1310c57aca77
- Parent:
- 2:eb4cc53ff33d
improve code structure, add key mapping and long key press handling
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 3:1310c57aca77 | 1 | #include "kbd_mgr/KeyboardMonitor.h" |
osmeest | 3:1310c57aca77 | 2 | |
osmeest | 3:1310c57aca77 | 3 | namespace kbd_mgr { |
osmeest | 3:1310c57aca77 | 4 | |
osmeest | 3:1310c57aca77 | 5 | KeyboardMonitor::KeyboardMonitor(PortName inPort, std::size_t numKeysPerRow, std::size_t inLowestBit, |
osmeest | 3:1310c57aca77 | 6 | const KeyboardMonitor::OutPinsSet &outPins) : |
osmeest | 3:1310c57aca77 | 7 | in(inPort, ((1 << numKeysPerRow)-1) << inLowestBit), inBitShift(inLowestBit), outPins(outPins), |
osmeest | 3:1310c57aca77 | 8 | ticker(), scanRow(0), currentState(outPins.size(), numKeysPerRow) |
osmeest | 3:1310c57aca77 | 9 | { } |
osmeest | 3:1310c57aca77 | 10 | |
osmeest | 3:1310c57aca77 | 11 | KeyboardMonitor::~KeyboardMonitor() |
osmeest | 3:1310c57aca77 | 12 | { |
osmeest | 3:1310c57aca77 | 13 | stop(); |
osmeest | 3:1310c57aca77 | 14 | } |
osmeest | 3:1310c57aca77 | 15 | |
osmeest | 3:1310c57aca77 | 16 | void KeyboardMonitor::start(float pollingPeriod) |
osmeest | 3:1310c57aca77 | 17 | { |
osmeest | 3:1310c57aca77 | 18 | if (this->outPins.empty()) { |
osmeest | 3:1310c57aca77 | 19 | return; |
osmeest | 3:1310c57aca77 | 20 | } |
osmeest | 3:1310c57aca77 | 21 | |
osmeest | 3:1310c57aca77 | 22 | if (pollingPeriod < 20e-6) { |
osmeest | 3:1310c57aca77 | 23 | pollingPeriod = 20e-6; |
osmeest | 3:1310c57aca77 | 24 | } |
osmeest | 3:1310c57aca77 | 25 | |
osmeest | 3:1310c57aca77 | 26 | for(OutPinsSet::const_iterator p = this->outPins.begin(); p != this->outPins.end(); ++p) { |
osmeest | 3:1310c57aca77 | 27 | DigitalOut out(*p); |
osmeest | 3:1310c57aca77 | 28 | out.write( p == this->outPins.begin() ? 1 : 0 ); |
osmeest | 3:1310c57aca77 | 29 | } |
osmeest | 3:1310c57aca77 | 30 | this->in.mode(PullDown); |
osmeest | 3:1310c57aca77 | 31 | this->scanRow = 0; |
osmeest | 3:1310c57aca77 | 32 | this->ticker.attach(this, &KeyboardMonitor::timerHandler, pollingPeriod); |
osmeest | 3:1310c57aca77 | 33 | } |
osmeest | 3:1310c57aca77 | 34 | |
osmeest | 3:1310c57aca77 | 35 | void KeyboardMonitor::stop() |
osmeest | 3:1310c57aca77 | 36 | { |
osmeest | 3:1310c57aca77 | 37 | this->ticker.detach(); |
osmeest | 3:1310c57aca77 | 38 | this->in.mode(OpenDrain); |
osmeest | 3:1310c57aca77 | 39 | } |
osmeest | 3:1310c57aca77 | 40 | |
osmeest | 3:1310c57aca77 | 41 | void KeyboardMonitor::timerHandler() |
osmeest | 3:1310c57aca77 | 42 | { |
osmeest | 3:1310c57aca77 | 43 | DigitalOut out(this->outPins[this->scanRow]); |
osmeest | 3:1310c57aca77 | 44 | out.write(1); |
osmeest | 3:1310c57aca77 | 45 | wait_us(10); |
osmeest | 3:1310c57aca77 | 46 | int v = (this->in.read() >> this->inBitShift); |
osmeest | 3:1310c57aca77 | 47 | out.write(0); |
osmeest | 3:1310c57aca77 | 48 | this->currentState.setRowState(this->scanRow, v); |
osmeest | 3:1310c57aca77 | 49 | this->scanRow = (this->scanRow + 1) % this->currentState.getNumRows(); |
osmeest | 3:1310c57aca77 | 50 | if (this->scanRow == 0) { |
osmeest | 3:1310c57aca77 | 51 | invokeHandler(this->currentState); |
osmeest | 3:1310c57aca77 | 52 | } |
osmeest | 3:1310c57aca77 | 53 | } |
osmeest | 3:1310c57aca77 | 54 | |
osmeest | 3:1310c57aca77 | 55 | } // kbd_mgr |