2016-06-09 19:29:31 +00:00
|
|
|
#include "RowScanner_PinsArray.h"
|
2016-06-07 09:26:25 +00:00
|
|
|
|
2016-06-18 22:32:21 +00:00
|
|
|
/* constructor
|
|
|
|
*/
|
2016-06-30 10:40:39 +00:00
|
|
|
RowScanner_PinsArray::RowScanner_PinsArray(const uint8_t STROBE_PIN,
|
|
|
|
const uint8_t READ_PINS[], const uint8_t READ_PIN_COUNT)
|
|
|
|
: STROBE_PIN(STROBE_PIN), READ_PINS(READ_PINS), READ_PIN_COUNT(READ_PIN_COUNT)
|
2016-06-18 22:32:21 +00:00
|
|
|
{
|
|
|
|
uint8_t mode;
|
|
|
|
|
|
|
|
//configure row
|
2016-06-30 10:40:39 +00:00
|
|
|
pinMode(STROBE_PIN, OUTPUT);
|
2016-06-18 22:32:21 +00:00
|
|
|
|
2016-07-09 16:47:22 +00:00
|
|
|
if (STROBE_ON == LOW) //if active low
|
2016-06-18 22:32:21 +00:00
|
|
|
{
|
2016-07-09 16:47:22 +00:00
|
|
|
mode = INPUT_PULLUP; //uses internal pull-up resistor
|
2016-06-18 22:32:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-09 16:47:22 +00:00
|
|
|
mode = INPUT; //requires external pull-down resistor
|
2016-06-18 22:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//configure cols
|
|
|
|
for (uint8_t i=0; i < READ_PIN_COUNT; i++)
|
|
|
|
{
|
2016-06-30 10:40:39 +00:00
|
|
|
pinMode(READ_PINS[i], mode);
|
2016-06-18 22:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scan() Strobes the row and reads the columns.
|
2016-07-09 09:59:28 +00:00
|
|
|
Sets KEY_COUNT and returns rowState.
|
2016-06-18 22:32:21 +00:00
|
|
|
|
|
|
|
https://www.arduino.cc/en/Tutorial/DigitalPins
|
|
|
|
https://www.arduino.cc/en/Reference/PinMode
|
|
|
|
https://www.arduino.cc/en/Reference/DigitalWrite
|
|
|
|
https://www.arduino.cc/en/Reference/DigitalRead
|
|
|
|
https://www.arduino.cc/en/Reference/Constants > Digital Pins modes: INPUT, INPUT_PULLUP, and OUTPUT
|
2016-06-06 20:23:40 +00:00
|
|
|
*/
|
2016-07-09 09:59:28 +00:00
|
|
|
read_pins_t RowScanner_PinsArray::scan(uint8_t& keyCount)
|
2016-06-06 20:23:40 +00:00
|
|
|
{
|
2016-07-09 09:59:28 +00:00
|
|
|
read_pins_t rowState = 0; //bitwise, one col per bit, 1 means key is pressed
|
|
|
|
read_pins_t rowMask = 1; //bitwise, one col per bit, active col bit is 1
|
2016-06-06 20:23:40 +00:00
|
|
|
|
2016-07-09 16:47:22 +00:00
|
|
|
digitalWrite(STROBE_PIN, STROBE_ON);
|
2016-06-08 02:24:50 +00:00
|
|
|
delayMicroseconds(3); //time to stablize voltage
|
2016-06-07 09:26:25 +00:00
|
|
|
|
2016-06-09 19:29:31 +00:00
|
|
|
//read all the column pins
|
2016-06-07 01:37:48 +00:00
|
|
|
for (uint8_t i=0; i < READ_PIN_COUNT; i++)
|
2016-06-06 20:23:40 +00:00
|
|
|
{
|
2016-07-09 16:47:22 +00:00
|
|
|
if ( digitalRead(READ_PINS[i]) == STROBE_ON )
|
2016-06-06 20:23:40 +00:00
|
|
|
{
|
2016-07-09 09:59:28 +00:00
|
|
|
rowState |= rowMask;
|
2016-06-06 20:23:40 +00:00
|
|
|
}
|
2016-07-09 09:59:28 +00:00
|
|
|
rowMask <<= 1;
|
2016-06-06 20:23:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 16:47:22 +00:00
|
|
|
digitalWrite(STROBE_PIN, STROBE_OFF);
|
2016-06-08 02:24:50 +00:00
|
|
|
|
2016-07-09 09:59:28 +00:00
|
|
|
keyCount = READ_PIN_COUNT;
|
2016-06-06 20:23:40 +00:00
|
|
|
return rowState;
|
|
|
|
}
|