-
Notifications
You must be signed in to change notification settings - Fork 0
/
x80_modes.ino
177 lines (145 loc) · 3.98 KB
/
x80_modes.ino
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
uint16_t mScore;
void (*modeCurrent)();
// initial mode, just shows image and highscore and goes to countdown
void modeReady() {
dispSetImage(imgStart);
while (ioScan(1) != 0) {} // wait a bit for button release after fail mode
delay(500);
while (true) {
dispShowNumber(highscoreAmount);
if (modeBlinkAndWait(1, 50, 50, &modeCountdown)) return;
dispSetImage(imgStart);
if (modeBlinkAndWait(1, 50, 50, &modeCountdown)) return;
}
}
// 3 second countdown with sound and images, then goes to buttonWait mode
void modeCountdown() {
mScore = 0;
uint8_t resetStage = 0;
ioLedMask(15);
dispSetImage(imgCountdown3);
playbackStart(sndBtn, sizeof(sndBtn));
delay(1000);
if (ioScan(1) == 5) resetStage++;
ioLedMask(6);
dispSetImage(imgCountdown2);
playbackStart(sndBtn, sizeof(sndBtn));
delay(1000);
if (ioScan(1) == 10) resetStage++;
ioLedMask(0);
dispSetImage(imgCountdown1);
playbackStart(sndBtn, sizeof(sndBtn));
delay(1000);
if (ioScan(1) == 5) resetStage++;
if (resetStage < 3) {
playbackStart(sndBtn, sizeof(sndBtn));
modeSet(&modeButtonWait);
} else {
modeSet(&modeReset);
}
}
uint8_t modeCalcBits(uint8_t ratio) {
switch (ratio) {
case 0: return 0b00000000;
case 1: return 0b10000000;
case 2: return 0b11000000;
case 3: return 0b11100000;
case 4: return 0b11110000;
case 5: return 0b11111000;
case 6: return 0b11111100;
case 7: return 0b11111110;
default: return 0b11111111;
}
}
// picks one button at random, lights up the correct led, shows hint image and waits for pressing this button
// wait time lowers with score, goes to buttonOk or fail modes
void modeButtonWait() {
uint8_t btn = random(4);
ioLedMask(1 << btn);
switch (btn) {
case 0: dispSetImage(imgBtn1); break;
case 1: dispSetImage(imgBtn2); break;
case 2: dispSetImage(imgBtn3); break;
case 3: dispSetImage(imgBtn4); break;
}
uint16_t t = 100 - min(90, mScore * 2 / 3);
uint16_t tNow = t;
uint8_t b1;
do {
uint8_t ratio = map(tNow, 0, t, 0, 31);
dispFramebuffer[0] = modeCalcBits(ratio);
dispFramebuffer[1] = ratio < 8 ? 0 : modeCalcBits(ratio - 8);
dispFramebuffer[2] = ratio < 16 ? 0 : modeCalcBits(ratio - 16);
dispFramebuffer[3] = ratio < 24 ? 0 : modeCalcBits(ratio - 24);
dispUpdate();
b1 = ioScanOnce();
if (b1 == 1 << btn) {
modeSet(&modeButtonOk);
return;
} else if (b1 != 0) {
modeSet(&modeFail);
return;
}
delay(2); // adjusted for display update rate
} while (tNow--);
modeSet(&modeFail);
}
// plays sound, shows highscore and then goes to buttonWait again
void modeButtonOk() {
playbackStart(sndBtn, sizeof(sndBtn));
ioLedMask(0);
mScore++;
dispShowNumber(mScore);
delay(1000);
modeSet(&modeButtonWait);
}
// plays sound, checks highscore and mocks the player. goes to ready
void modeFail() {
playbackStart(sndFail, sizeof(sndFail));
bool isHi = mScore > highscoreAmount;
if (isHi) {
dispSetImage(imgHiscore);
highscoreSet(mScore);
} else {
dispSetImage(imgFail);
}
while (ioScan(1) != 0) {}
delay(1500); // wait some time for button release
uint8_t cnt = 10;
while (cnt--) {
dispShowNumber(mScore);
if (modeBlinkAndWait(3, 20, 10, &modeCountdown)) return;
if (isHi) {
dispSetImage(imgHiscore);
} else {
dispSetImage(imgFail);
}
if (modeBlinkAndWait(3, 20, 10, &modeCountdown)) return;
}
modeSet(&modeReady);
}
void modeReset() {
highscoreSet(0);
dispSetImage(imgReset);
while (ioScan(1) != 0) {} // wait a bit for button release after fail mode
delay(2000);
modeSet(&modeReady);
}
void modeSet(void (*cb)()) {
modeCurrent = cb;
}
uint8_t modeBlinkAndWait(uint8_t repeats, uint8_t onCycles, uint8_t offCycles, void (*mode)()) {
while (repeats--) {
ioLedMask(15);
if (ioScan(onCycles)) {
modeSet(mode);
return 1;
}
ioLedMask(0);
if (ioScan(offCycles)) {
modeSet(mode);
return 1;
}
}
return 0;
}