-
Notifications
You must be signed in to change notification settings - Fork 11
/
sr.cpp
81 lines (66 loc) · 1.53 KB
/
sr.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
70
71
72
73
74
75
76
77
78
79
80
81
#include "sr.h"
#include <Arduino.h>
#include <stdint.h>
#include "pins.h"
#include "commands.h"
#include "debug.h"
sr sr1, sr2 = {0, -1, 0};
srAssignments sra = {
// Shift register 1
0b00100000,
0b00001000,
0b00010000,
0b00000100,
0b00000010,
0b00000001,
// Shift register 2
0b00000100,
0b00001000,
0b00100000,
0b00000010,
0b00000001,
0b00010000,
};
// Set up shift register
void setupSr() {
// Set up shift registers for buttons
pinMode(PIN_SR_LATCH, OUTPUT);
pinMode(PIN_SR_CLOCK, OUTPUT);
pinMode(PIN_SR_DATA, INPUT);
}
// Returns current shift register state as byte
// Bit 7 = Pin 7 / Bit 0= Pin 0
uint8_t srShiftIn(bool doLatch) {
uint8_t data = 0;
if (doLatch) {
digitalWrite(PIN_SR_LATCH, 1);
delayMicroseconds(20);
digitalWrite(PIN_SR_LATCH, 0);
}
for (int i = 7; i >= 0; i--) {
digitalWrite(PIN_SR_CLOCK, 0);
delayMicroseconds(2);
if (digitalRead(PIN_SR_DATA)) {
data = data | (1 << i);
}
digitalWrite(PIN_SR_CLOCK, 1);
}
//DPRINTBINLN(data);
return data;
}
// Debounce button presses
bool debounce(sr *srd) {
// First run
if (srd->previous == -1) {
srd->previous = srd->state;
return false;
}
if (srd->state != srd->previous) {
srd->previous = srd->state;
if ((millis() - srd->debounceTime) > DEBOUNCE_DELAY) {
srd->debounceTime = millis();
return true;
}
}
return false;
}