2016-07-12 13:23:24 +00:00
|
|
|
#include "Scanner_uC.h"
|
2016-06-07 09:26:25 +00:00
|
|
|
|
2016-07-14 23:28:16 +00:00
|
|
|
/* Scanner_uC functions call Arduino's Digital Pins functions
|
|
|
|
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-18 22:32:21 +00:00
|
|
|
/* constructor
|
|
|
|
*/
|
2016-09-05 20:09:59 +00:00
|
|
|
Scanner_uC::Scanner_uC(const bool strobeOn, const uint8_t readPins[], const uint8_t readPinCount)
|
|
|
|
: strobeOn(strobeOn), strobeOff(!strobeOn), readPins(readPins), readPinCount(readPinCount)
|
2016-06-18 22:32:21 +00:00
|
|
|
{
|
|
|
|
uint8_t mode;
|
|
|
|
|
2016-09-05 20:09:59 +00:00
|
|
|
//configure read pins
|
|
|
|
if (strobeOn == LOW) //if active low
|
2016-06-18 22:32:21 +00:00
|
|
|
{
|
2016-09-05 20:09:59 +00:00
|
|
|
mode = INPUT_PULLUP; //use internal pull-up resistor
|
2016-06-18 22:32:21 +00:00
|
|
|
}
|
2016-09-05 20:09:59 +00:00
|
|
|
else //if active high
|
2016-06-18 22:32:21 +00:00
|
|
|
{
|
2016-09-05 20:09:59 +00:00
|
|
|
mode = INPUT; //requires external pull-down resistor
|
2016-06-18 22:32:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 05:15:38 +00:00
|
|
|
for (uint8_t i=0; i < readPinCount; i++)
|
2016-06-18 22:32:21 +00:00
|
|
|
{
|
2016-07-15 05:15:38 +00:00
|
|
|
pinMode(readPins[i], mode);
|
2016-06-18 22:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 07:08:26 +00:00
|
|
|
/* init() is called once for each row from Row constructor.
|
2016-09-05 20:09:59 +00:00
|
|
|
Configure row-strobe pin to output.
|
|
|
|
*/
|
2016-09-06 07:08:26 +00:00
|
|
|
void Scanner_uC::init(const uint8_t strobePin)
|
2016-09-05 20:09:59 +00:00
|
|
|
{
|
|
|
|
pinMode(strobePin, OUTPUT);
|
|
|
|
}
|
|
|
|
|
2016-09-11 15:54:10 +00:00
|
|
|
/* scan() is called on every iteration of sketch loop().
|
|
|
|
scan() strobes the row's strobePin and retuns state of readPins.
|
2016-09-17 21:47:37 +00:00
|
|
|
Bit patterns are 1 bit per key.
|
2016-06-06 20:23:40 +00:00
|
|
|
*/
|
2016-09-05 20:09:59 +00:00
|
|
|
read_pins_t Scanner_uC::scan(const uint8_t strobePin)
|
2016-06-06 20:23:40 +00:00
|
|
|
{
|
2016-09-17 21:47:37 +00:00
|
|
|
read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released
|
|
|
|
read_pins_t readMask = 1; //bits, active bit is 1
|
2016-06-06 20:23:40 +00:00
|
|
|
|
2016-07-12 13:23:24 +00:00
|
|
|
//strobe row on
|
2016-09-05 20:09:59 +00:00
|
|
|
digitalWrite(strobePin, strobeOn);
|
2016-06-08 02:24:50 +00:00
|
|
|
delayMicroseconds(3); //time to stablize voltage
|
2016-06-07 09:26:25 +00:00
|
|
|
|
2016-07-12 13:23:24 +00:00
|
|
|
//read all the read pins
|
2016-07-15 05:15:38 +00:00
|
|
|
for (uint8_t i=0; i < readPinCount; i++)
|
2016-06-06 20:23:40 +00:00
|
|
|
{
|
2016-09-05 20:09:59 +00:00
|
|
|
if ( digitalRead(readPins[i]) == strobeOn )
|
2016-06-06 20:23:40 +00:00
|
|
|
{
|
2016-07-13 21:49:56 +00:00
|
|
|
readState |= readMask;
|
2016-06-06 20:23:40 +00:00
|
|
|
}
|
2016-07-12 13:23:24 +00:00
|
|
|
readMask <<= 1;
|
2016-06-06 20:23:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 13:23:24 +00:00
|
|
|
//strobe row off
|
2016-09-05 20:09:59 +00:00
|
|
|
digitalWrite(strobePin, strobeOff);
|
2016-06-08 02:24:50 +00:00
|
|
|
|
2016-07-13 21:49:56 +00:00
|
|
|
return readState;
|
2016-06-06 20:23:40 +00:00
|
|
|
}
|