-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcommon.cpp
255 lines (218 loc) · 6.32 KB
/
common.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
#include "common.h"
/* Defines */
#define EEPROM_ADDR_BASE 992
#define EEPROM_SIGNATURE 0x084E424FUL // "OBN\x08"
#define PAD_REPEAT_DELAY (FPS / 4)
#define PAD_REPEAT_INTERVAL (FPS / 12)
enum RECORD_STATE_T {
RECORD_NOT_READ = 0,
RECORD_INITIAL,
RECORD_STORED,
};
/* Global Variables */
MyArduboy arduboy;
RECORD_T record;
bool isRecordDirty;
int8_t padX, padY, padRepeatCount;
bool isInvalid;
/* Local Functions */
static uint16_t calcCheckSum();
static void eepSeek(int addr);
static uint8_t eepRead8(void);
static uint16_t eepRead16(void);
static uint32_t eepRead32(void);
static void eepReadBlock(void *p, size_t n);
static void eepWrite8(uint8_t val);
static void eepWrite16(uint16_t val);
static void eepWrite32(uint32_t val);
static void eepWriteBlock(const void *p, size_t n);
/* Local Variables */
PROGMEM static const byte soundTick[] = {
0x90, 69, 0, 10, 0x80, 0xF0 // arduboy.tone2(440, 10);
};
PROGMEM static const byte soundClick[] = {
0x90, 74, 0, 20, 0x80, 0xF0 // arduboy.tone2(587, 20);
};
static RECORD_STATE_T recordState = RECORD_NOT_READ;
static int16_t eepAddr;
/*---------------------------------------------------------------------------*/
/* Common Functions */
/*---------------------------------------------------------------------------*/
void readRecord(void)
{
bool isVerified = false;
eepSeek(EEPROM_ADDR_BASE);
if (eepRead32() == EEPROM_SIGNATURE) {
eepReadBlock(&record, sizeof(record));
isVerified = (eepRead16() == calcCheckSum());
}
if (isVerified) {
recordState = RECORD_STORED;
isRecordDirty = false;
dprintln(F("Read record from EEPROM"));
} else {
memset(&record, 0, sizeof(record));
record.cpuLevel = 2;
record.maxLevel = 3;
record.settings = SETTING_BIT_THINK_LED;
recordState = RECORD_INITIAL;
isRecordDirty = true;
}
setSound(arduboy.isAudioEnabled()); // Load Sound ON/OFF
}
void writeRecord(void)
{
if (!isRecordDirty) return;
if (recordState == RECORD_INITIAL) {
eepSeek(EEPROM_ADDR_BASE);
eepWrite32(EEPROM_SIGNATURE);
} else {
eepSeek(EEPROM_ADDR_BASE + 4);
}
eepWriteBlock(&record, sizeof(record));
eepWrite16(calcCheckSum());
arduboy.audio.saveOnOff(); // Save Sound ON/OFF
recordState = RECORD_STORED;
isRecordDirty = false;
dprintln(F("Write record to EEPROM"));
}
static uint16_t calcCheckSum()
{
uint16_t checkSum = (EEPROM_SIGNATURE & 0xFFFF) + (EEPROM_SIGNATURE >> 16) * 3;
uint16_t *p = (uint16_t *) &record;
for (int i = 0; i < sizeof(record) / 2; i++) {
checkSum += *p++ * (i * 2 + 5);
}
return checkSum;
}
void clearRecord(void)
{
eepSeek(EEPROM_ADDR_BASE);
for (int i = 0; i < (sizeof(record) + 6) / 4; i++) {
eepWrite32(0);
}
recordState = RECORD_INITIAL;
dprintln(F("Clear EEPROM"));
}
void handleDPad(void)
{
padX = padY = 0;
if (arduboy.buttonPressed(LEFT_BUTTON | RIGHT_BUTTON | UP_BUTTON | DOWN_BUTTON)) {
if (++padRepeatCount >= (PAD_REPEAT_DELAY + PAD_REPEAT_INTERVAL)) {
padRepeatCount = PAD_REPEAT_DELAY;
}
if (padRepeatCount == 1 || padRepeatCount == PAD_REPEAT_DELAY) {
if (arduboy.buttonPressed(LEFT_BUTTON)) padX--;
if (arduboy.buttonPressed(RIGHT_BUTTON)) padX++;
if (arduboy.buttonPressed(UP_BUTTON)) padY--;
if (arduboy.buttonPressed(DOWN_BUTTON)) padY++;
}
} else {
padRepeatCount = 0;
}
}
void drawNumber(int16_t x, int16_t y, int32_t value)
{
arduboy.setCursor(x, y);
arduboy.print(value);
}
void drawTime(int16_t x, int16_t y, uint32_t frames)
{
uint16_t h = frames / (FPS * 3600UL);
uint8_t m = frames / (FPS * 60) % 60;
uint8_t s = frames / FPS % 60;
arduboy.setCursor(x, y);
if (h == 0 && m == 0) {
if (s < 10) arduboy.print('0');
arduboy.print(s);
arduboy.print('.');
arduboy.print(frames / (FPS / 10) % 10);
} else {
if (h > 0) {
arduboy.print(h);
arduboy.print(':');
if (m < 10) arduboy.print('0');
}
arduboy.print(m);
arduboy.print(':');
if (s < 10) arduboy.print('0');
arduboy.print(s);
}
}
void invertScreen(bool isInvert)
{
arduboy.sendLCDCommand(isInvert ? OLED_PIXELS_INVERTED : OLED_PIXELS_NORMAL);
}
/*---------------------------------------------------------------------------*/
/* Sound Functions */
/*---------------------------------------------------------------------------*/
void setSound(bool on)
{
arduboy.setAudioEnabled(on);
dprint(F("audioEnabled="));
dprintln(on);
}
void playSoundTick(void)
{
arduboy.playScore2(soundTick, 255);
}
void playSoundClick(void)
{
arduboy.playScore2(soundClick, 255);
}
/*---------------------------------------------------------------------------*/
/* EEPROM Functions */
/*---------------------------------------------------------------------------*/
void eepSeek(int addr)
{
eepAddr = max(addr, EEPROM_STORAGE_SPACE_START);
}
uint8_t eepRead8(void)
{
eeprom_busy_wait();
return eeprom_read_byte((const uint8_t *) eepAddr++);
}
uint16_t eepRead16(void)
{
eeprom_busy_wait();
uint16_t ret = eeprom_read_word((const uint16_t *)eepAddr);
eepAddr += 2;
return ret;
}
uint32_t eepRead32(void)
{
eeprom_busy_wait();
uint32_t ret = eeprom_read_dword((const uint32_t *) eepAddr);
eepAddr += 4;
return ret;
}
void eepReadBlock(void *p, size_t n)
{
eeprom_busy_wait();
eeprom_read_block(p, (const void *) eepAddr, n);
eepAddr += n;
}
void eepWrite8(uint8_t val)
{
eeprom_busy_wait();
eeprom_write_byte((uint8_t *) eepAddr, val);
eepAddr++;
}
void eepWrite16(uint16_t val)
{
eeprom_busy_wait();
eeprom_write_word((uint16_t *)eepAddr, val);
eepAddr += 2;
}
void eepWrite32(uint32_t val)
{
eeprom_busy_wait();
eeprom_write_dword((uint32_t *)eepAddr, val);
eepAddr += 4;
}
void eepWriteBlock(const void *p, size_t n)
{
eeprom_busy_wait();
eeprom_write_block(p, (void *) eepAddr, n);
eepAddr += n;
}