KeyboardManager: a class to manage the polling of a switch-matrix keyboard
Embed:
(wiki syntax)
Show/hide line numbers
KeyboardMonitor.cpp
00001 #include "kbd_mgr/KeyboardMonitor.h" 00002 00003 namespace kbd_mgr { 00004 00005 KeyboardMonitor::KeyboardMonitor (PortName inPort, std::size_t numKeysPerRow, std::size_t inLowestBit, 00006 const KeyboardMonitor::OutPinsSet &outPins) : 00007 in(inPort, ((1 << numKeysPerRow)-1) << inLowestBit), inBitShift(inLowestBit), outPins(outPins), 00008 ticker(), scanRow(0), currentState(outPins.size(), numKeysPerRow) 00009 { } 00010 00011 KeyboardMonitor::~KeyboardMonitor() 00012 { 00013 stop(); 00014 } 00015 00016 void KeyboardMonitor::start(float pollingPeriod) 00017 { 00018 if (this->outPins.empty()) { 00019 return; 00020 } 00021 00022 if (pollingPeriod < 20e-6) { 00023 pollingPeriod = 20e-6; 00024 } 00025 00026 for(OutPinsSet::const_iterator p = this->outPins.begin(); p != this->outPins.end(); ++p) { 00027 DigitalOut out(*p); 00028 out.write( p == this->outPins.begin() ? 1 : 0 ); 00029 } 00030 this->in.mode(PullDown); 00031 this->scanRow = 0; 00032 this->ticker.attach(this, &KeyboardMonitor::timerHandler, pollingPeriod); 00033 } 00034 00035 void KeyboardMonitor::stop() 00036 { 00037 this->ticker.detach(); 00038 this->in.mode(OpenDrain); 00039 } 00040 00041 void KeyboardMonitor::timerHandler() 00042 { 00043 DigitalOut out(this->outPins[this->scanRow]); 00044 out.write(1); 00045 wait_us(10); 00046 int v = (this->in.read() >> this->inBitShift); 00047 out.write(0); 00048 this->currentState.setRowState(this->scanRow, v); 00049 this->scanRow = (this->scanRow + 1) % this->currentState.getNumRows(); 00050 if (this->scanRow == 0) { 00051 invokeHandler(this->currentState); 00052 } 00053 } 00054 00055 } // kbd_mgr
Generated on Thu Jul 14 2022 19:25:04 by 1.7.2