22#include < Adafruit_TinyUSB.h>
33#include < Arduino.h>
44#include < FatLib/FatFileSystem.h>
5- #include < RTCZero.h>
65#include < SPI.h>
76#include < WInterrupts.h>
87#include < wiring_private.h>
98
109#include " src/lib/ArduinoLowPower.h"
1110#include " src/lib/PawPet_FlashTransport.h"
11+ #include " src/lib/RTCZero.h"
1212
1313#include " src/common.h"
1414#include " src/config.h"
1717#include " src/states/gamestate.h"
1818
1919// #define DEBUG 1
20+ #define INSTALLER 1
21+
22+ #ifdef INSTALLER
23+ #include " src/states/pawos_install.h"
24+ #endif
2025
2126#ifdef DEBUG
2227#include < ZeroRegs.h>
2833static const SPIFlash_Device_t possible_devices[] = {MX25R1635F};
2934SPIClass flashSPI (&sercom2, FLASH_MISO, FLASH_SCK, FLASH_MOSI, SPI_PAD_0_SCK_1, SERCOM_RX_PAD_2);
3035PawPet_FlashTransport_SPI flashTransport (FLASH_CS, flashSPI);
31- Adafruit_SPIFlash flash (&flashTransport);
3236
3337SPIClass dispSPI (&sercom4, SHARP_MISO, SHARP_SCK, SHARP_MOSI, SPI_PAD_2_SCK_3, SERCOM_RX_PAD_0);
3438PetDisplay display (&dispSPI, SHARP_SS, DISP_WIDTH, DISP_HEIGHT);
@@ -57,6 +61,9 @@ uint16_t intBat = 300;
5761
5862void setup (void )
5963{
64+ g::g_flash = new Adafruit_SPIFlash (&flashTransport);
65+ Adafruit_SPIFlash& flash = *g::g_flash;
66+
6067#ifdef DEBUG
6168 Serial.begin (9600 );
6269
@@ -86,7 +93,37 @@ void setup(void)
8693 pinMode (PIN_BUTTON_C, INPUT_PULLUP);
8794
8895 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;
90127
91128 pinMode (PIN_UP, INPUT_PULLUP);
92129 pinMode (PIN_LEFT, INPUT_PULLUP);
@@ -108,7 +145,7 @@ void setup(void)
108145 flash.begin (possible_devices);
109146
110147 // Init file system on the flash
111- g::g_fatfs = new FatFileSystem;
148+ g::g_fatfs = new FatFileSystem () ;
112149 g::g_stats.filesysFound = g::g_fatfs->begin (&flash);
113150 g::g_stats.flashSize = flash.size ();
114151
@@ -133,7 +170,11 @@ void setup(void)
133170
134171 buttonWakeup = false ;
135172 nextSleepTime = 40000 ;
173+ #ifdef INSTALLER
174+ currentState = new InstallMenu ();
175+ #else
136176 currentState = new MenuState ();
177+ #endif
137178
138179 g::g_rtc.begin ();
139180 g::g_rtc.setHours (0 );
@@ -156,13 +197,15 @@ void setup(void)
156197 delay (500 );
157198 USBDevice.attach ();
158199#endif
159-
160- tone (PIN_BEEPER, NOTE_C4, 250 );
200+ if (!g::g_stats.filesysFound )
201+ {
202+ tone (PIN_BEEPER, NOTE_C4, 250 );
203+ }
161204}
162205
163206uint32_t sleepTicks = 0 ;
164- uint8_t keysPressed = 0 ;
165- uint8_t prevKeysPressed = 0 ;
207+ uint8_t keysState = 0 ;
208+ uint8_t prevKeysState = 0 ;
166209
167210uint32_t currentTimeMs = 0 ;
168211uint32_t frameTimeMs = 0 ;
@@ -180,11 +223,11 @@ void loop(void)
180223 Watchdog.reset ();
181224
182225 // // UPDATE ////
183- keysPressed |= readButtons ();
226+ keysState |= readButtons ();
184227
185228 currentTimeMs = millis ();
186229
187- if (keysPressed )
230+ if (keysState )
188231 {
189232 nextSleepTime = currentTimeMs + 60000 ;
190233 }
@@ -195,9 +238,11 @@ void loop(void)
195238 {
196239
197240 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;
201246
202247 uint32_t t1 = millis ();
203248 GameState *nextState = currentState->update ();
@@ -235,6 +280,8 @@ void loop(void)
235280 if (currentState->redraw )
236281 {
237282 display.fillDisplayBuffer ();
283+ display.setCursor (0 , 8 );
284+ display.setTextColor (PET_BLACK);
238285 currentState->draw (&display);
239286
240287 // TODO, draw overlay should say if it needs an update
@@ -263,8 +310,8 @@ void loop(void)
263310 droppedFrames++;
264311 }
265312
266- prevKeysPressed = keysPressed ;
267- keysPressed = 0 ;
313+ prevKeysState = keysState ;
314+ keysState = 0 ;
268315 }
269316 else
270317 {
@@ -332,6 +379,8 @@ void loop(void)
332379 if (currentState->redraw )
333380 {
334381 display.fillDisplayBuffer ();
382+ display.setCursor (0 , 8 );
383+ display.setTextColor (PET_BLACK);
335384 currentState->draw (&display);
336385 drawTimeAndBattery ();
337386 dirtyFrameBuffer = true ;
@@ -472,7 +521,7 @@ int32_t msc_read_cb(uint32_t lba, void *buffer, uint32_t bufsize)
472521{
473522 // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
474523 // 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 ;
476525}
477526
478527// Callback invoked when received WRITE10 command.
@@ -483,15 +532,15 @@ int32_t msc_write_cb(uint32_t lba, uint8_t *buffer, uint32_t bufsize)
483532 // check_uf2_handover(buffer, bufsize, 4, 5, tag);
484533 // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
485534 // 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 ;
487536}
488537
489538// Callback invoked when WRITE10 command is completed (status received and accepted by host).
490539// used to flush any pending cache.
491540void msc_flush_cb (void )
492541{
493542 // sync with flash
494- flash. syncBlocks ();
543+ g::g_flash-> syncBlocks ();
495544
496545 // clear file system's cache to force refresh
497546 g::g_fatfs->cacheClear ();
0 commit comments