2
2
#include < Adafruit_TinyUSB.h>
3
3
#include < Arduino.h>
4
4
#include < FatLib/FatFileSystem.h>
5
- #include < RTCZero.h>
6
5
#include < SPI.h>
7
6
#include < WInterrupts.h>
8
7
#include < wiring_private.h>
9
8
10
9
#include " src/lib/ArduinoLowPower.h"
11
10
#include " src/lib/PawPet_FlashTransport.h"
11
+ #include " src/lib/RTCZero.h"
12
12
13
13
#include " src/common.h"
14
14
#include " src/config.h"
17
17
#include " src/states/gamestate.h"
18
18
19
19
// #define DEBUG 1
20
+ #define INSTALLER 1
21
+
22
+ #ifdef INSTALLER
23
+ #include " src/states/pawos_install.h"
24
+ #endif
20
25
21
26
#ifdef DEBUG
22
27
#include < ZeroRegs.h>
28
33
static const SPIFlash_Device_t possible_devices[] = {MX25R1635F};
29
34
SPIClass flashSPI (&sercom2, FLASH_MISO, FLASH_SCK, FLASH_MOSI, SPI_PAD_0_SCK_1, SERCOM_RX_PAD_2);
30
35
PawPet_FlashTransport_SPI flashTransport (FLASH_CS, flashSPI);
31
- Adafruit_SPIFlash flash (&flashTransport);
32
36
33
37
SPIClass dispSPI (&sercom4, SHARP_MISO, SHARP_SCK, SHARP_MOSI, SPI_PAD_2_SCK_3, SERCOM_RX_PAD_0);
34
38
PetDisplay display (&dispSPI, SHARP_SS, DISP_WIDTH, DISP_HEIGHT);
@@ -57,6 +61,9 @@ uint16_t intBat = 300;
57
61
58
62
void setup (void )
59
63
{
64
+ g::g_flash = new Adafruit_SPIFlash (&flashTransport);
65
+ Adafruit_SPIFlash& flash = *g::g_flash;
66
+
60
67
#ifdef DEBUG
61
68
Serial.begin (9600 );
62
69
@@ -86,7 +93,37 @@ void setup(void)
86
93
pinMode (PIN_BUTTON_C, INPUT_PULLUP);
87
94
88
95
pinMode (PIN_BUTTON_P, INPUT_PULLUP);
89
- LowPower.attachInterruptWakeup (PIN_BUTTON_P, buttonWakeupCallback, FALLING);
96
+ // LowPower.attachInterruptWakeup(PIN_BUTTON_P, buttonWakeupCallback, FALLING);
97
+
98
+ EExt_Interrupts in = g_APinDescription[PIN_BUTTON_P].ulExtInt ;
99
+ if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI)
100
+ return ;
101
+
102
+ attachInterrupt (PIN_BUTTON_P, buttonWakeupCallback, FALLING);
103
+
104
+ // enable EIC clock
105
+ GCLK->CLKCTRL .bit .CLKEN = 0 ; // disable GCLK module
106
+ while (GCLK->STATUS .bit .SYNCBUSY ) {}
107
+
108
+ GCLK->CLKCTRL .reg = (uint16_t )(GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK3 |
109
+ GCLK_CLKCTRL_ID (GCM_EIC)); // EIC clock switched on GCLK3
110
+ while (GCLK->STATUS .bit .SYNCBUSY ) {}
111
+
112
+ // GCLK->GENCTRL.reg =
113
+ // (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_RUNSTDBY); // source for
114
+ // GCLK2 is OSCULP32K
115
+ // while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
116
+
117
+ // GCLK->GENCTRL.bit.RUNSTDBY = 1; // GCLK2 run standby
118
+ // while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
119
+
120
+ // Enable wakeup capability on pin in case being used during sleep
121
+ EIC->WAKEUP .reg |= (1 << in);
122
+
123
+ /* Errata: Make sure that the Flash does not power all the way down
124
+ * when in sleep mode. */
125
+
126
+ NVMCTRL->CTRLB .bit .SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val;
90
127
91
128
pinMode (PIN_UP, INPUT_PULLUP);
92
129
pinMode (PIN_LEFT, INPUT_PULLUP);
@@ -108,7 +145,7 @@ void setup(void)
108
145
flash.begin (possible_devices);
109
146
110
147
// Init file system on the flash
111
- g::g_fatfs = new FatFileSystem;
148
+ g::g_fatfs = new FatFileSystem () ;
112
149
g::g_stats.filesysFound = g::g_fatfs->begin (&flash);
113
150
g::g_stats.flashSize = flash.size ();
114
151
@@ -133,7 +170,11 @@ void setup(void)
133
170
134
171
buttonWakeup = false ;
135
172
nextSleepTime = 40000 ;
173
+ #ifdef INSTALLER
174
+ currentState = new InstallMenu ();
175
+ #else
136
176
currentState = new MenuState ();
177
+ #endif
137
178
138
179
g::g_rtc.begin ();
139
180
g::g_rtc.setHours (0 );
@@ -156,13 +197,15 @@ void setup(void)
156
197
delay (500 );
157
198
USBDevice.attach ();
158
199
#endif
159
-
160
- tone (PIN_BEEPER, NOTE_C4, 250 );
200
+ if (!g::g_stats.filesysFound )
201
+ {
202
+ tone (PIN_BEEPER, NOTE_C4, 250 );
203
+ }
161
204
}
162
205
163
206
uint32_t sleepTicks = 0 ;
164
- uint8_t keysPressed = 0 ;
165
- uint8_t prevKeysPressed = 0 ;
207
+ uint8_t keysState = 0 ;
208
+ uint8_t prevKeysState = 0 ;
166
209
167
210
uint32_t currentTimeMs = 0 ;
168
211
uint32_t frameTimeMs = 0 ;
@@ -180,11 +223,11 @@ void loop(void)
180
223
Watchdog.reset ();
181
224
182
225
// // UPDATE ////
183
- keysPressed |= readButtons ();
226
+ keysState |= readButtons ();
184
227
185
228
currentTimeMs = millis ();
186
229
187
- if (keysPressed )
230
+ if (keysState )
188
231
{
189
232
nextSleepTime = currentTimeMs + 60000 ;
190
233
}
@@ -195,9 +238,11 @@ void loop(void)
195
238
{
196
239
197
240
prevFrameMs = currentTimeMs;
198
-
199
- g::g_keyPressed = keysPressed;
200
- g::g_keyReleased = ~(prevKeysPressed) & (keysPressed);
241
+ // held is currenlty just previous update, a function of update loop speed
242
+ // between 66 and 2000 ms
243
+ g::g_keyPressed = ~(prevKeysState)&keysState;
244
+ g::g_keyReleased = prevKeysState & ~(keysState);
245
+ g::g_keyHeld = prevKeysState & keysState;
201
246
202
247
uint32_t t1 = millis ();
203
248
GameState *nextState = currentState->update ();
@@ -235,6 +280,8 @@ void loop(void)
235
280
if (currentState->redraw )
236
281
{
237
282
display.fillDisplayBuffer ();
283
+ display.setCursor (0 , 8 );
284
+ display.setTextColor (PET_BLACK);
238
285
currentState->draw (&display);
239
286
240
287
// TODO, draw overlay should say if it needs an update
@@ -263,8 +310,8 @@ void loop(void)
263
310
droppedFrames++;
264
311
}
265
312
266
- prevKeysPressed = keysPressed ;
267
- keysPressed = 0 ;
313
+ prevKeysState = keysState ;
314
+ keysState = 0 ;
268
315
}
269
316
else
270
317
{
@@ -332,6 +379,8 @@ void loop(void)
332
379
if (currentState->redraw )
333
380
{
334
381
display.fillDisplayBuffer ();
382
+ display.setCursor (0 , 8 );
383
+ display.setTextColor (PET_BLACK);
335
384
currentState->draw (&display);
336
385
drawTimeAndBattery ();
337
386
dirtyFrameBuffer = true ;
@@ -472,7 +521,7 @@ int32_t msc_read_cb(uint32_t lba, void *buffer, uint32_t bufsize)
472
521
{
473
522
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
474
523
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
475
- return flash. readBlocks (lba, (uint8_t *)buffer, bufsize / 512 ) ? bufsize : -1 ;
524
+ return g::g_flash-> readBlocks (lba, (uint8_t *)buffer, bufsize / 512 ) ? bufsize : -1 ;
476
525
}
477
526
478
527
// Callback invoked when received WRITE10 command.
@@ -483,15 +532,15 @@ int32_t msc_write_cb(uint32_t lba, uint8_t *buffer, uint32_t bufsize)
483
532
// check_uf2_handover(buffer, bufsize, 4, 5, tag);
484
533
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
485
534
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
486
- return flash. writeBlocks (lba, buffer, bufsize / 512 ) ? bufsize : -1 ;
535
+ return g::g_flash-> writeBlocks (lba, buffer, bufsize / 512 ) ? bufsize : -1 ;
487
536
}
488
537
489
538
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
490
539
// used to flush any pending cache.
491
540
void msc_flush_cb (void )
492
541
{
493
542
// sync with flash
494
- flash. syncBlocks ();
543
+ g::g_flash-> syncBlocks ();
495
544
496
545
// clear file system's cache to force refresh
497
546
g::g_fatfs->cacheClear ();
0 commit comments