-
Notifications
You must be signed in to change notification settings - Fork 9
/
ButtonReader.h
48 lines (38 loc) · 871 Bytes
/
ButtonReader.h
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
#ifndef ShiftIn_h
#define ShiftIn_h
#include "Arduino.h"
class ButtonReader {
private:
uint32_t lastState = 0;
uint32_t currentState = 0;
public:
ButtonReader() {}
// setup all pins
void init() {
pinMode(4, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, INPUT);
pinMode(1, OUTPUT);
}
inline boolean state(int id) { return bitRead(currentState, id); }
uint32_t read() {
lastState = currentState;
uint32_t result = 0;
digitalWrite(1, HIGH);
digitalWrite(4, LOW);
digitalWrite(4, HIGH);
digitalWrite(1, LOW);
for(uint16_t i = 0; i < 24; i++) {
uint32_t value = digitalRead(0);
result |= (value << ((24-1) - i));
digitalWrite(1, HIGH);
digitalWrite(1, LOW);
}
currentState = result;
return result;
}
boolean update() {
return read() != lastState;
}
};
#endif