Boot: input grace period; persist high score + volume via NVS

- Ignore all input for ~1.2 s after power-up/reset (INPUT_GRACE_MS):
  the keyboard controller can emit stale bytes and pins can float,
  which used to start a game right after a restart.
- High score + volume survive power cycles: Preferences (NVS, 'mc'
  namespace) loaded in setup(), saved on game over and on every
  volume key (U/V/M). arduino-esp32 commits NVS in end(), so
  savePrefs() does putUInt x2 + end() + begin() reopen.
- SoundEngine::setVolume() restores a saved level (0 = stay muted,
  lastVol_ keeps unmute working).
- Emulator: file-backed Preferences shim (mc-prefs.cfg, gitignored)
  so persistence is testable on the desktop.
This commit is contained in:
Dejvino
2026-11-11 07:05:59 +01:00
parent a5921032d7
commit 798f8600a0
5 changed files with 106 additions and 2 deletions
+1
View File
@@ -8,3 +8,4 @@ config
emulator/missilecommand-emu emulator/missilecommand-emu
emulator/seq*.bmp emulator/seq*.bmp
emulator/mc-prefs.cfg
+57
View File
@@ -0,0 +1,57 @@
/**
* Preferences shim: backs the ESP32 NVS Preferences API with a plain text
* file next to the emulator binary ("mc-prefs.cfg"), so the high score and
* volume survive emulator restarts just like on the device.
*/
#pragma once
#include <cstdint>
#include <cstdio>
#include <string>
#include <unordered_map>
class Preferences {
public:
bool begin(const char *ns, bool readOnly = false)
{
(void)ns;
(void)readOnly;
map_.clear();
if (FILE *f = fopen(path_, "r")) {
char key[64];
unsigned val;
while (fscanf(f, "%63s %u", key, &val) == 2) {
map_[key] = val;
}
fclose(f);
}
return true;
}
void end() {} // file-backed: every putUInt already flushes
uint32_t getUInt(const char *key, uint32_t defaultValue = 0)
{
auto it = map_.find(key);
return it == map_.end() ? defaultValue : it->second;
}
void putUInt(const char *key, uint32_t value)
{
map_[key] = value;
flush();
}
private:
void flush()
{
if (FILE *f = fopen(path_, "w")) {
for (const auto &kv : map_) {
fprintf(f, "%s %u\n", kv.first.c_str(), kv.second);
}
fclose(f);
}
}
std::unordered_map<std::string, uint32_t> map_;
const char *path_ = "mc-prefs.cfg";
};
+38 -2
View File
@@ -21,6 +21,7 @@
#include <SPI.h> #include <SPI.h>
#include <TFT_eSPI.h> #include <TFT_eSPI.h>
#include <Wire.h> #include <Wire.h>
#include <Preferences.h>
#include "utilities.h" #include "utilities.h"
#include "TouchDrvGT911.hpp" #include "TouchDrvGT911.hpp"
#include "sound_engine.h" #include "sound_engine.h"
@@ -53,6 +54,10 @@ static const float ROCKET_SPEED = 6.0f; // px / frame
static const float ROCKET_RADIUS = 24.0f; static const float ROCKET_RADIUS = 24.0f;
static const uint32_t BANNER_MS = 1500; static const uint32_t BANNER_MS = 1500;
static const uint32_t BONUS_MS = 3400; static const uint32_t BONUS_MS = 3400;
// Ignore all input during the first moments after power-up/reset: the
// keyboard controller may still emit stale bytes and pins may float, which
// used to start a game right after a restart.
static const uint32_t INPUT_GRACE_MS = 1200;
static const float ROCKET_SPEED_SIDE = 3.4f; // side batteries fire slow rockets; static const float ROCKET_SPEED_SIDE = 3.4f; // side batteries fire slow rockets;
// only the center battery is fast // only the center battery is fast
static const int MAX_CRAFTS = 2; // bombers + satellites static const int MAX_CRAFTS = 2; // bombers + satellites
@@ -183,6 +188,19 @@ static void sfxNoise(uint16_t d, uint16_t a) { sfx(SND_NOISE, 0, 0,
static void sfxDeny() { sfxTone(220, 90, 8000); } static void sfxDeny() { sfxTone(220, 90, 8000); }
// ---------------------------------------------------------------------------
// Persistent settings (NVS): high score + volume survive power cycles
// ---------------------------------------------------------------------------
static Preferences prefs;
static void savePrefs()
{
prefs.putUInt("hi", (uint32_t)highScore);
prefs.putUInt("vol", (uint32_t)sound.volume());
prefs.end(); // commit to NVS
prefs.begin("mc", false); // reopen for the next save
}
// Attack alarm, played once as a wave-start intro (like the original): // Attack alarm, played once as a wave-start intro (like the original):
// four falling tones, then gameplay begins and the alarm is done. // four falling tones, then gameplay begins and the alarm is done.
static void sfxSirenIntro() static void sfxSirenIntro()
@@ -1178,6 +1196,7 @@ static void enterGameOver()
{ {
sfxGameOver(); sfxGameOver();
if (score > highScore) highScore = score; if (score > highScore) highScore = score;
savePrefs(); // the run is final: commit high score (+ current volume)
tft.fillScreen(TFT_BLACK); tft.fillScreen(TFT_BLACK);
stars(40); stars(40);
@@ -1364,20 +1383,23 @@ static void pollKeyboard()
char c = tolower(k); char c = tolower(k);
// volume keys work in every state // volume keys work in every state (and persist immediately)
if (c == 'u') { if (c == 'u') {
sound.volumeUp(); sound.volumeUp();
volOSDUntil = millis() + 1200; volOSDUntil = millis() + 1200;
savePrefs();
return; return;
} }
if (c == 'v') { if (c == 'v') {
sound.volumeDown(); sound.volumeDown();
volOSDUntil = millis() + 1200; volOSDUntil = millis() + 1200;
savePrefs();
return; return;
} }
if (c == 'm') { if (c == 'm') {
sound.toggleMute(); sound.toggleMute();
volOSDUntil = millis() + 1200; volOSDUntil = millis() + 1200;
savePrefs();
return; return;
} }
@@ -1553,15 +1575,29 @@ void setup()
} }
Serial.printf("Screen %dx%d, ground at %d\n", scrW, scrH, groundY); Serial.printf("Screen %dx%d, ground at %d\n", scrW, scrH, groundY);
// restore persisted high score and volume
prefs.begin("mc", false);
highScore = (int)prefs.getUInt("hi", 0);
sound.setVolume((uint8_t)prefs.getUInt("vol", 5));
drawTitle(); drawTitle();
} }
void loop() void loop()
{ {
// swallow all input right after power-up/reset: the keyboard controller
// may still emit stale bytes and pins may float, which used to start
// a game immediately after a restart
uint32_t now = millis();
if (now < INPUT_GRACE_MS) {
delay(1);
return;
}
pollTrackball(); // hot path: sample the ball every iteration (~1 ms) pollTrackball(); // hot path: sample the ball every iteration (~1 ms)
static uint32_t nextFrame = 0; static uint32_t nextFrame = 0;
uint32_t now = millis();
if ((int32_t)(now - nextFrame) < 0) { if ((int32_t)(now - nextFrame) < 0) {
delay(1); delay(1);
return; return;
+9
View File
@@ -103,6 +103,15 @@ void SoundEngine::toggleMute()
} }
} }
// Restore a saved volume level (from NVS at boot); 0 keeps the device muted
// while lastVol_ stays at its default so unmute still works.
void SoundEngine::setVolume(uint8_t v)
{
if (v > VOL_MAX) v = VOL_MAX;
vol_ = v;
if (vol_ > 0) lastVol_ = vol_;
}
// scaled peak amplitude for the current volume level // scaled peak amplitude for the current volume level
static inline uint32_t scaledAmp(uint16_t amp, uint8_t vol) static inline uint32_t scaledAmp(uint16_t amp, uint8_t vol)
{ {
+1
View File
@@ -39,6 +39,7 @@ public:
void volumeUp(); void volumeUp();
void volumeDown(); void volumeDown();
void toggleMute(); void toggleMute();
void setVolume(uint8_t v); // restore a saved level (0 = muted)
uint8_t volume() const { return vol_; } uint8_t volume() const { return vol_; }
private: private: