-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.cpp
159 lines (121 loc) · 2.99 KB
/
final.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const int rowPin0 = 0;
const int rowPin1 = 1;
const int rowPin2 = 2;
const int rowPin3 = 3;
const int columnPin0 = 4;
const int columnPin1 = 5;
const int columnPin2 = 6;
const int strikePin = 7;
int pinCodeRingBuffer[4];
int currentPinIndex = 0;
void setup(){
pinMode(rowPin0, OUTPUT);
digitalWrite(rowPin0, HIGH);
pinMode(rowPin1, OUTPUT);
digitalWrite(rowPin1, HIGH);
pinMode(rowPin2, OUTPUT);
digitalWrite(rowPin2, HIGH);
pinMode(rowPin3, OUTPUT);
digitalWrite(rowPin3, HIGH);
pinMode(columnPin0, INPUT_PULLUP);
pinMode(columnPin1, INPUT_PULLUP);
pinMode(columnPin2, INPUT_PULLUP);
digitalWrite(strikePin, LOW);
resetPinCode();
}
const int columns[] = { columnPin0, columnPin1, columnPin2 };
const int rows[] = { rowPin0, rowPin1, rowPin2, rowPin3 };
const int matrix[4][3] =
{ 1, 2, 3
, 4, 5, 6
, 7, 8, 9
,10, 0, 11
};
const int rowIdentityMatrix[4][4] =
{ LOW, HIGH, HIGH, HIGH
, HIGH, LOW, HIGH, HIGH
, HIGH, HIGH, LOW, HIGH
, HIGH, HIGH, HIGH, LOW
};
void loop(){
gateKeep(getCurrentKey());
}
int canRegisterKeyPress[4][3] =
{ 1, 1, 1
, 1, 1, 1
, 1, 1, 1
, 1, 1, 1
};
int getCurrentKey() {
int currentPressedKey = -1;
for (int r = 0; r < 4; r++)
{
for (int i = 0; i < 4; i++)
{
digitalWrite(rows[i], rowIdentityMatrix[r][i]);
}
for (int c = 0; c < 3; c++)
{
const bool pressingKey = digitalRead(columns[c]) == LOW;
if (pressingKey && canRegisterKeyPress[r][c])
{
canRegisterKeyPress[r][c] = false;
currentPressedKey = matrix[r][c];
}
else if (!pressingKey)
{
canRegisterKeyPress[r][c] = true;
}
delay(2);
}
delay(3);
}
return currentPressedKey;
}
bool canSendKey = true;
void gateKeep(int key) {
if (canSendKey && key >= 0)
{
canSendKey = false;
updateRingBuffer(key);
maybeStrikeIt();
}
else if (key < 0)
{
canSendKey = true;
}
}
void updateRingBuffer(int key) {
pinCodeRingBuffer[currentPinIndex] = key;
currentPinIndex = (currentPinIndex + 1) % 4;
}
void maybeStrikeIt() {
int code[] = { 1, 8, 4, 8 };
// Check if the strike should strike out
for (int i = 0; i < 4; i ++)
{
bool isValidMatch = true;
for (int j = 0; j < 4; j++)
{
if (pinCodeRingBuffer[(i+j) % 4] != code[j])
{
isValidMatch = false;
break;
}
}
if (isValidMatch)
{
reallyStrikeIt();
resetPinCode();
break;
}
}
}
void reallyStrikeIt() {
digitalWrite(strikePin, HIGH);
delay(3000);
digitalWrite(strikePin, LOW);
}
void resetPinCode() {
pinCodeRingBuffer[0] = pinCodeRingBuffer[1] = pinCodeRingBuffer[2] = pinCodeRingBuffer[3] = 99;
}