Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions VortexEngine/src/Modes/Modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ bool Modes::init()
return true;
#endif
ByteStream headerBuffer;
Storage::read(0, headerBuffer);
unserializeSaveHeader(headerBuffer);
if (!Storage::read(0, headerBuffer) || !unserializeSaveHeader(headerBuffer)) {
// cannot read or load header? corrupted header?
// just mark this as a new firmware and it will trigger a fresh header
// write later in the main loop when modes get loaded
m_globalFlags |= MODES_FLAG_NEW_FIRMWARE;
}
m_loaded = false;
#ifdef VORTEX_LIB
// enable the adv menus by default in vortex lib
Expand Down Expand Up @@ -690,15 +694,15 @@ void Modes::clearModes()

void Modes::setStartupMode(uint8_t index)
{
// zero out the upper nibble to disable
// zero out the upper nibble to clear it
m_globalFlags &= 0x0F;
// or in the index value shifted into the upper nibble
// OR in the index value shifted into the upper nibble
m_globalFlags |= (index << 4) & 0xF0;
}

uint8_t Modes::startupMode()
{
// zero out the upper nibble to disable
// return the upper nibble of the global flags
return (m_globalFlags & 0xF0) >> 4;
}

Expand Down
16 changes: 15 additions & 1 deletion VortexEngine/src/Modes/Modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ class Mode;
// WARNING!! The upper 4 bits of the flags are taken by the startup mode id,
// you can only use 4 global flags!

// This is a new special feature used by the Chromadeck when it flashes a new
// firmware to the Duo it will set the global flags to 0xF0 like this:
//
// 0 = button lock enabled
// 0 = one click mode enabled
// 0 = advanced menus enabled
// 0 = keychain mode enabled
// 1111 = Startup Mode Index 15 (impossible)
//
// If this flag is present then the Duo needs to turn on and write out it's
// save header because a new firmware has been flashed and the save header
// will still have the old version number saved inside
#define MODES_FLAG_NEW_FIRMWARE 0xF0

class Modes
{
// private unimplemented constructor
Expand Down Expand Up @@ -119,7 +133,7 @@ class Modes

// set or get flags
static bool setFlag(uint8_t flag, bool enable, bool save = true);
static bool getFlag(uint8_t flag) { return ((m_globalFlags & flag) != 0); }
static bool getFlag(uint8_t flag) { return ((m_globalFlags & flag) == flag); }

// reset flags to factory default (must save after)
static void resetFlags() { m_globalFlags = 0; }
Expand Down
30 changes: 21 additions & 9 deletions VortexEngine/src/VortexEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,26 @@ void VortexEngine::runMainLogic()
}

#ifdef VORTEX_EMBEDDED
// ESD PROTECTION!
// Sometimes the chip can be turned on via ESD triggering the wakeup pin
// if the engine makes it here in less than 2 ticks that means the device turned on
// via ESD and not via a normal click which cannot possibly be done in less than 1 tick
if (now < 2) {
// if that happens then just gracefully go back to sleep to prevent the chip
// from turning on randomly in a plastic bag
// do not save on ESD re-sleep
// originally this check was believed to protect against ESD but not so sure
// anymore, it's clear that it stops the chip from turning on when it initially
// receives power but ESD tests fail now (put chip in sandwich bag and shake it).
// This is now where initialization runs right after a fresh firmware flash.
// The first tick of the engine is 1 not 0 because oops.
if (now == 1) {
// This check is for whether a new firmware was just flashed, if a new
// firmware was flashed then write out a new save header
if (Modes::getFlag(MODES_FLAG_NEW_FIRMWARE)) {
// reset the flags back to normal
Modes::resetFlags();
// try to load the modes and hope it works, need the num modes to write a
// new save header and they may have backed up and restored a custom number
// of modes
Modes::load();
// then save the new header with current version and num modes etc
Modes::saveHeader();
}
// then just gracefully go back to sleep to prevent the chip from turning
// on randomly when it receives power
enterSleep(false);
return;
}
Expand Down Expand Up @@ -417,7 +429,7 @@ void VortexEngine::enterSleep(bool save)
g_pButton->enableWake();
// Set sleep mode to POWER DOWN mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// enable the sleep boo lright before we enter sleep, this will allow
// enable the sleep bool right before we enter sleep, this will allow
// the main loop to break and return
m_sleeping = true;
// enter sleep
Expand Down