-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathScanner_uC.cpp
69 lines (59 loc) · 2 KB
/
Scanner_uC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "Scanner_uC.h"
/* 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
*/
/* constructor
*/
Scanner_uC::Scanner_uC(const bool activeState, const uint8_t readPins[], const uint8_t readPinCount)
: activeState(activeState), readPins(readPins), readPinCount(readPinCount)
{
uint8_t mode;
//configure read pins
if (activeState == LOW) //if active low
{
mode = INPUT_PULLUP; //use internal pull-up resistor
}
else //if active high
{
mode = INPUT; //requires external pull-down resistor
}
for (uint8_t i=0; i < readPinCount; i++)
{
pinMode(readPins[i], mode);
}
}
/* init() is called once for each row from Row constructor.
Configure row-strobe pin to output.
*/
void Scanner_uC::init(const uint8_t strobePin)
{
pinMode(strobePin, OUTPUT);
}
/* scan() is called on every iteration of sketch loop().
scan() strobes the row's strobePin and retuns state of readPins.
Bit patterns are 1 bit per key.
*/
read_pins_t Scanner_uC::scan(const uint8_t strobePin)
{
read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released
read_pins_t readMask = 1; //bits, active bit is 1
//strobe on
digitalWrite(strobePin, activeState);
delayMicroseconds(3); //time to stablize voltage
//read all the read pins
for (uint8_t i=0; i < readPinCount; i++)
{
if ( digitalRead(readPins[i]) == activeState )
{
readState |= readMask;
}
readMask <<= 1;
}
//strobe off
digitalWrite(strobePin, !activeState);
return readState;
}