Archived
1
0
This repo is archived. You can view files and clone it, but cannot push or open issues or pull requests.
keybrd/src/Scanner_uC.cpp

64 lines
1.8 KiB
C++
Raw Normal View History

2016-07-12 13:23:24 +00:00
#include "Scanner_uC.h"
/* constructor
*/
2016-07-12 13:23:24 +00:00
Scanner_uC::Scanner_uC(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)
{
uint8_t mode;
//configure row
pinMode(STROBE_PIN, OUTPUT);
if (STROBE_ON == LOW) //if active low
{
mode = INPUT_PULLUP; //uses internal pull-up resistor
}
else
{
mode = INPUT; //requires external pull-down resistor
}
2016-07-12 13:23:24 +00:00
//configure read pins
for (uint8_t i=0; i < READ_PIN_COUNT; i++)
{
pinMode(READ_PINS[i], mode);
}
}
/* scan() Strobes the row and reads the columns.
2016-07-12 13:23:24 +00:00
Sets READ_PIN_COUNT and returns rowState.
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-07-12 13:23:24 +00:00
read_pins_t Scanner_uC::scan(uint8_t& readPinCount)
{
read_pins_t rowState = 0; //bitwise, one col per bit, 1 means key is pressed
2016-07-12 13:23:24 +00:00
read_pins_t readMask = 1; //bitwise, one col per bit, active col bit is 1
2016-07-12 13:23:24 +00:00
//strobe row on
digitalWrite(STROBE_PIN, STROBE_ON);
2016-06-08 02:24:50 +00:00
delayMicroseconds(3); //time to stablize voltage
2016-07-12 13:23:24 +00:00
//read all the read pins
for (uint8_t i=0; i < READ_PIN_COUNT; i++)
{
if ( digitalRead(READ_PINS[i]) == STROBE_ON )
{
2016-07-12 13:23:24 +00:00
rowState |= readMask;
}
2016-07-12 13:23:24 +00:00
readMask <<= 1;
}
2016-07-12 13:23:24 +00:00
//strobe row off
digitalWrite(STROBE_PIN, STROBE_OFF);
2016-06-08 02:24:50 +00:00
2016-07-12 13:23:24 +00:00
readPinCount = READ_PIN_COUNT;
return rowState;
}