-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimon.h
128 lines (102 loc) · 2.82 KB
/
Simon.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
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
byte sequence[255];
bool reset = true;
CRGB colors[4] = {
CRGB::Red,
CRGB::Green,
CRGB::Blue,
CRGB::White,
};
uint16_t resetDelay = 1000;
uint16_t sequenceDelay = 500;
uint16_t sequencePauseDelay = 250;
bool playingSequence = true;
bool pausingBetweenSequence = true;
bool pausingBeforeSequence = true;
byte sequenceLength = 255;
byte sequenceIndex = 0;
uint32_t simon()
{
random16_add_entropy(random());
if (reset) {
for (int i = 0; i < 255; i++) {
sequence[i] = random8(0, 4);
}
sequenceLength = 1;
sequenceIndex = 0;
playingSequence = true;
pausingBetweenSequence = true;
reset = false;
fill_solid(leds, NUM_LEDS, CRGB::Black);
return resetDelay;
}
if (sequenceIndex >= sequenceLength) {
playingSequence = false;
pausingBetweenSequence = false;
sequenceIndex = 0;
}
if(pausingBeforeSequence) {
pausingBeforeSequence = false;
fill_solid(leds, NUM_LEDS, CRGB::Black);
return sequenceDelay;
}
if (pausingBetweenSequence) {
pausingBetweenSequence = false;
fill_solid(leds, NUM_LEDS, CRGB::Black);
return sequencePauseDelay;
}
byte sequenceColorIndex = sequence[sequenceIndex];
CRGB sequenceColor = colors[sequenceColorIndex];
if (playingSequence) {
sequenceIndex++;
fill_solid(leds, NUM_LEDS, sequenceColor);
pausingBetweenSequence = true;
return sequenceDelay;
}
bool buttonPressed = false;
CRGB colorPressed = CRGB::Black;
switch (command) {
case InputCommand::Red:
buttonPressed = true;
colorPressed = CRGB::Red;
break;
case InputCommand::Green:
buttonPressed = true;
colorPressed = CRGB::Green;
break;
case InputCommand::Blue:
buttonPressed = true;
colorPressed = CRGB::Blue;
break;
case InputCommand::White:
buttonPressed = true;
colorPressed = CRGB::White;
break;
case InputCommand::None:
default:
buttonPressed = false;
break;
}
if (!buttonPressed) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
return 0;
}
fill_solid(leds, NUM_LEDS, sequenceColor);
if (colorPressed == sequenceColor) {
if (sequenceIndex + 1 == sequenceLength) {
sequenceLength++;
sequenceIndex = 0;
playingSequence = true;
pausingBetweenSequence = true;
pausingBeforeSequence = true;
return sequencePauseDelay;
}
else {
sequenceIndex++;
return sequencePauseDelay;
}
}
else {
reset = true;
return resetDelay;
}
}