Initial sleep implementation, closes #6

This commit is contained in:
2026-03-03 21:27:07 +01:00
parent 5c36aa041e
commit cb433f76c9
3 changed files with 455 additions and 2 deletions

View File

@@ -38,6 +38,7 @@
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
#include "Wire.h"
#include "sleep.h"
// ─── Boot-loop detection ──────────────────────────────────────────────────────
#ifdef FEATURE_BOOT_LOOP_DETECT
@@ -241,6 +242,10 @@ void setup() {
// Seed previous-accel for jerk detection so first frame doesn't spike
prevAx = imu.readFloatAccelX(); prevAy = imu.readFloatAccelY(); prevAz = imu.readFloatAccelZ();
// Sleep manager init: must come after calibrateGyroBias() and imu.begin().
// Unconditional — sleep.h is always included, no feature flag needed.
sleepManagerInit();
bledis.setManufacturer("Seeed Studio");
bledis.setModel("XIAO nRF52840 Sense");
bledis.begin();
@@ -289,6 +294,7 @@ void setup() {
#ifdef FEATURE_PHYSICAL_BUTTONS
Serial.print(" PHYSBTN");
#endif
Serial.print(" SLEEP");
Serial.println();
bootStartMs = millis();
@@ -336,6 +342,16 @@ void loop() {
processPhysicalButtons();
#endif
// Sleep manager runs every iteration — must not be gated by LOOP_RATE_MS
// because it needs to catch the imuWakeFlag set by the ISR promptly.
// Pass idle=false when in IMU_LP (idleFrames is stale since gyro reads
// are skipped); the manager tracks its own idle timestamp internally.
{
bool idle_for_sleep = (sleepStage == SLEEP_IMU_LP) ? true
: (idleFrames >= IDLE_FRAMES);
if (sleepManagerUpdate(now, idle_for_sleep, Bluefruit.connected())) return;
}
if (now - lastTime < (unsigned long)LOOP_RATE_MS) return;
float dt = (now - lastTime) / 1000.0f;
lastTime = now;
@@ -352,7 +368,9 @@ void loop() {
}
#endif
// Gyro reads with optional temperature compensation
// Gyro reads with optional temperature compensation.
// (sleepManagerUpdate above already returns early when in IMU_LP/DEEP)
float correction = 0.0f;
#ifdef FEATURE_TEMP_COMPENSATION
if (cfg.featureFlags & FLAG_TEMP_COMP_ENABLED)
@@ -443,6 +461,8 @@ void loop() {
}
#endif
// (sleep manager already called above, before LOOP_RATE_MS gate)
int8_t moveX = 0, moveY = 0;
uint8_t flags = 0;
@@ -508,4 +528,4 @@ void loop() {
}
}
#endif
}
}