2016-07-12 13:23:24 +00:00
|
|
|
#include "Row.h"
|
2016-09-05 20:09:59 +00:00
|
|
|
|
|
|
|
/* constructor
|
2016-09-12 06:28:27 +00:00
|
|
|
init() is called once for each row, to set scanner's uC strobePin to output.
|
2016-09-05 20:09:59 +00:00
|
|
|
*/
|
|
|
|
Row::Row(ScannerInterface& refScanner, const uint8_t strobePin,
|
2016-09-12 06:28:27 +00:00
|
|
|
Key* const ptrsKeys[], const uint8_t keyCount)
|
2016-09-05 20:09:59 +00:00
|
|
|
: refScanner(refScanner), strobePin(strobePin),
|
2016-09-12 06:28:27 +00:00
|
|
|
ptrsKeys(ptrsKeys), keyCount(keyCount), debounced(0)
|
2016-09-05 20:09:59 +00:00
|
|
|
{
|
2016-09-06 07:08:26 +00:00
|
|
|
refScanner.init(strobePin);
|
2016-09-05 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* process() scans the row and calls any newly pressed or released keys.
|
|
|
|
Bitwise variables are 1 bit per key.
|
|
|
|
*/
|
|
|
|
void Row::process()
|
|
|
|
{
|
|
|
|
read_pins_t readState; //bitwise, 1 means key is pressed, 0 means released
|
|
|
|
read_pins_t debouncedChanged; //bitwise, 1 means debounced changed
|
|
|
|
|
|
|
|
readState = refScanner.scan(strobePin);
|
|
|
|
debouncedChanged = debouncer.debounce(readState, debounced);
|
2016-09-12 06:28:27 +00:00
|
|
|
send(keyCount, debouncedChanged);
|
2016-09-05 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 13:23:24 +00:00
|
|
|
/*
|
2016-07-14 23:28:16 +00:00
|
|
|
send() calls key's press() or release() function if key was pressed or released.
|
2016-07-12 13:23:24 +00:00
|
|
|
Both parameters are bitwise.
|
|
|
|
*/
|
2016-09-12 06:28:27 +00:00
|
|
|
void Row::send(const uint8_t keyCount, const read_pins_t debouncedChanged)
|
2016-07-12 13:23:24 +00:00
|
|
|
{
|
|
|
|
read_pins_t isFallingEdge; //bitwise, 1 means falling edge
|
|
|
|
read_pins_t isRisingEdge; //bitwise, 1 means rising edge
|
2016-07-14 23:28:16 +00:00
|
|
|
read_pins_t readMask; //bitwise, active bit is 1
|
2016-07-12 13:23:24 +00:00
|
|
|
uint8_t i; //index for ptrsKeys[i] array
|
|
|
|
|
|
|
|
//bit=1 if last debounced changed from 1 to 0, else bit=0
|
|
|
|
isFallingEdge = debouncedChanged & ~debounced;
|
|
|
|
|
|
|
|
//bit=1 if last debounced changed from 0 to 1, else bit=0
|
|
|
|
isRisingEdge = debouncedChanged & debounced;
|
|
|
|
|
2016-09-12 06:28:27 +00:00
|
|
|
for (readMask=1, i=0; i < keyCount; readMask<<=1, i++) //for each key in row
|
2016-07-12 13:23:24 +00:00
|
|
|
{
|
|
|
|
//release before press avoids impossible key sequence
|
|
|
|
if (readMask & isFallingEdge) //if key was released
|
|
|
|
{
|
|
|
|
ptrsKeys[i]->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readMask & isRisingEdge) //if key was pressed
|
|
|
|
{
|
|
|
|
ptrsKeys[i]->press();
|
|
|
|
keyWasPressed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-18 02:26:00 +00:00
|
|
|
|
2016-07-12 13:23:24 +00:00
|
|
|
void Row::keyWasPressed()
|
|
|
|
{
|
2016-07-14 23:28:16 +00:00
|
|
|
//empty in Row class. To unstick sticky keys, override keyWasPressed() in derived Row class.
|
2016-07-12 13:23:24 +00:00
|
|
|
}
|