/
main.cpp
308 lines (248 loc) · 6 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <Arduino.h>
#include <chat.h>
#include <button.h>
#define VCC 2
#define DATA_PIN 3 // Pin connected to DS of 74HC595
#define CLOCK_PIN 5 // Pin connected to SH_CP of 74HC595
#define LATCH_PIN 4 // Pin connected to ST_CP of 74HC595
#define COUNTER_PIN_001 10
#define COUNTER_PIN_010 11
#define COUNTER_PIN_100 12
#define STATUS_RED 13
#define STATUS_GRN A0
Button button_blk(A1);
Button button_gry(A2);
Button button_wht(A3);
Chat chat(ChatSource::BinaryCounter);
ChatMessage msg;
bool disarmed = false;
bool detonated = false;
bool won = false;
unsigned int counter = 0;
unsigned int last_counter = 0;
long last_debounce = millis();
int solution;
int solutions[] = {
519, 111, 771, 805, 250,
37, 206, 433, 367, 724,
485, 678, 823, 116, 989,
413, 248, 565, 382, 699 };
short counter_map[] = { 0, 9, 8, 7, 6, 5, 1, 2, 3, 4 };
unsigned int refresh(unsigned int led1, unsigned int led2);
void shiftOut(byte myDataOut);
void disarm();
void reset()
{
bool disarmed = false;
bool detonated = false;
bool won = false;
digitalWrite(STATUS_RED, HIGH);
digitalWrite(STATUS_GRN, LOW);
solution = solutions[random(20)];
counter = refresh(solution, counter);
delay(30); // bypass debounce
counter = refresh(solution, counter);
while (solution == counter)
{
solution = solutions[random(20)];
}
}
void setup()
{
chat.begin();
randomSeed(analogRead(5));
pinMode(VCC, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(COUNTER_PIN_001, INPUT);
pinMode(COUNTER_PIN_010, INPUT);
pinMode(COUNTER_PIN_100, INPUT);
pinMode(STATUS_RED, OUTPUT);
pinMode(STATUS_GRN, OUTPUT);
digitalWrite(VCC, HIGH);
digitalWrite(STATUS_RED, HIGH);
digitalWrite(STATUS_GRN, LOW);
// reset();
}
void loop()
{
if (chat.receive(&msg))
{
switch (msg.message)
{
case MessageType::Reset:
reset();
break;
case MessageType::Detonate:
detonated = true;
break;
case MessageType::Win:
won = true;
break;
}
}
if (!detonated && !disarmed && !won)
{
counter = refresh(solution, counter);
delay(10);
if (button_blk.isPressed())
{
if (
(solution == 699 && counter == 695) ||
(solution == 805 && counter == 813) ||
(solution == 485 && counter == 501) ||
(solution == 206 && counter == 204)
)
{
disarm();
}
else
{
chat.send(MessageType::Strike);
}
}
if (button_gry.isPressed())
{
if (counter % 2 == 1 && solution == counter)
{
disarm();
}
else
{
chat.send(MessageType::Strike);
}
}
if (button_wht.isPressed())
{
if (counter % 2 == 0 && solution == counter)
{
disarm();
}
else
{
chat.send(MessageType::Strike);
}
}
}
}
void disarm()
{
disarmed = true;
chat.send(MessageType::Disarm);
digitalWrite(STATUS_GRN, HIGH);
digitalWrite(STATUS_RED, LOW);
}
unsigned int reverse(unsigned int num)
{
unsigned int count = (16 - 1);
unsigned int tmp = num; // Assign num to the tmp
num >>= 1; // shift num because LSB already assigned to tmp
while(num)
{
tmp <<= 1; //shift the tmp because alread have the LSB of num
tmp |= num & 1; // putting the set bits of num
num >>= 1;
count--;
}
tmp <<= count; //when num become zero shift tmp from the remaining counts
return tmp;
}
// void prntBits(unsigned int b)
// {
// for (int i = 7; i >= 0; i--)
// Serial.print(bitRead(b, i));
// }
unsigned int refresh(unsigned int led1, unsigned int led2)
{
unsigned int led1_reversed = reverse(led1);
byte b0 = led2 >> 4;
byte b1;
byte b1m = led2 << 4; // MSB 4 bits
byte b2;
byte b2l = led1_reversed >> 14; // LSB 2 bits
byte b3 = led1_reversed >> 6;
unsigned int value = 0;
for (short i = 0; i < 10; i++) {
digitalWrite(LATCH_PIN, 0);
if (i < 6)
{
b1 = b1m;
b2 = b2l + (1 << (2 + i));
}
else
{
b1 = b1m + (1 << (i - 6));
b2 = b2l;
}
shiftOut(b0);
shiftOut(b1);
shiftOut(b2);
shiftOut(b3);
digitalWrite(LATCH_PIN, 1);
// let things settle
delay(1);
int v001 = digitalRead(COUNTER_PIN_001);
int v010 = digitalRead(COUNTER_PIN_010);
int v100 = digitalRead(COUNTER_PIN_100);
if (v001) {
value += counter_map[i];
}
if (v010) {
value += counter_map[i] * 10;
}
if (v100) {
value += counter_map[i] * 100;
}
}
if (value != last_counter) {
last_debounce = millis();
}
if ((millis() - last_debounce) > 25 && value != counter)
{
counter = value;
}
last_counter = value;
return counter;
}
void shiftOut(byte myDataOut)
{
// This shifts 8 bits out MSB first,
// on the rising edge of the clock,
// clock idles low
// internal function setup
int i = 0;
int pinState;
// clear everything out just in case to
// prepare shift register for bit shifting
digitalWrite(DATA_PIN, 0);
digitalWrite(CLOCK_PIN, 0);
// for each bit in the byte myDataOut;
// NOTICE THAT WE ARE COUNTING DOWN in our for loop
// This means that %00000001 or "1" will go through such
// that it will be pin Q0 that lights.
for (i = 7; i >= 0; i--)
{
digitalWrite(CLOCK_PIN, 0);
// if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if (myDataOut & (1 << i))
{
pinState = 1;
}
else
{
pinState = 0;
}
// Sets the pin to HIGH or LOW depending on pinState
digitalWrite(DATA_PIN, pinState);
// register shifts bits on upstroke of clock pin
digitalWrite(CLOCK_PIN, 1);
// zero the data pin after shift to prevent bleed through
digitalWrite(DATA_PIN, 0);
}
// stop shifting
digitalWrite(CLOCK_PIN, 0);
}