Files
WaveShare_ESP32_BoomCast_FW/BoomCast_Waveshare_Arduino/boomcast_state.cpp
T
2026-10-09 15:37:57 +02:00

160 lines
4.4 KiB
C++

#include "boomcast_state.h"
#include "boomcast_config.h"
#include <time.h>
BoomState boomState;
void BoomState::begin() {
mutex_ = xSemaphoreCreateMutex();
preferences_.begin("boomcast", false);
const char alphabet[] = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789";
auto makeSecret = [&]() {
String value;
for (int i = 0; i < 16; ++i) value += alphabet[esp_random() % (sizeof(alphabet) - 1)];
return value;
};
if (!preferences_.getString("ap_pass", "").length()) preferences_.putString("ap_pass", makeSecret());
if (!preferences_.getString("mgmt_pass", "").length()) preferences_.putString("mgmt_pass", makeSecret());
Serial.printf("[Provision] setup AP password: %s\n", preferences_.getString("ap_pass", "").c_str());
Serial.printf("[Provision] management user: admin; password: %s\n", preferences_.getString("mgmt_pass", "").c_str());
state_.sensitivity = preferences_.getUChar("sens", DEFAULT_SENSITIVITY);
state_.sensitivity = constrain(state_.sensitivity, 1, 100);
dayKey_ = preferences_.getString("day", "");
state_.todayEvents = preferences_.getInt("today", 0);
}
BoomStateSnapshot BoomState::snapshot() {
BoomStateSnapshot copy;
xSemaphoreTake(mutex_, portMAX_DELAY);
copy = state_;
xSemaphoreGive(mutex_);
return copy;
}
void BoomState::setWifi(bool connected, bool apMode, const String& ssid, const String& ip) {
xSemaphoreTake(mutex_, portMAX_DELAY);
state_.wifiConnected = connected;
state_.apMode = apMode;
state_.ssid = ssid;
state_.ip = ip;
xSemaphoreGive(mutex_);
}
void BoomState::setSdReady(bool ready) {
xSemaphoreTake(mutex_, portMAX_DELAY);
state_.sdReady = ready;
xSemaphoreGive(mutex_);
}
void BoomState::setQueueCount(int count) {
xSemaphoreTake(mutex_, portMAX_DELAY);
state_.queueCount = count;
xSemaphoreGive(mutex_);
}
void BoomState::setCapturing(bool value) {
xSemaphoreTake(mutex_, portMAX_DELAY);
state_.capturing = value;
xSemaphoreGive(mutex_);
}
void BoomState::setAudioDiagnostics(uint32_t readErrors, uint32_t recordingErrors) {
xSemaphoreTake(mutex_, portMAX_DELAY);
state_.audioReadErrors = readErrors;
state_.recordingErrors = recordingErrors;
xSemaphoreGive(mutex_);
}
void BoomState::setSyncing(bool value, const String& message) {
xSemaphoreTake(mutex_, portMAX_DELAY);
state_.syncing = value;
if (message.length()) state_.lastSyncMessage = message;
xSemaphoreGive(mutex_);
}
void BoomState::setWaveform(const int8_t* points, size_t count) {
if (!points) return;
count = min(count, BOOMCAST_WAVEFORM_POINTS);
xSemaphoreTake(mutex_, portMAX_DELAY);
memcpy(state_.waveform, points, count);
xSemaphoreGive(mutex_);
}
uint8_t BoomState::sensitivity() {
xSemaphoreTake(mutex_, portMAX_DELAY);
uint8_t v = state_.sensitivity;
xSemaphoreGive(mutex_);
return v;
}
void BoomState::setSensitivity(uint8_t value, bool persist) {
value = constrain(value, 1, 100);
xSemaphoreTake(mutex_, portMAX_DELAY);
state_.sensitivity = value;
xSemaphoreGive(mutex_);
if (persist) preferences_.putUChar("sens", value);
}
String BoomState::currentDayKey() {
struct tm now;
if (!getLocalTime(&now, 5)) return "";
char buf[11];
strftime(buf, sizeof(buf), "%Y-%m-%d", &now);
return String(buf);
}
void BoomState::rollDayIfNeeded() {
String current = currentDayKey();
if (!current.length()) return;
bool changed = false;
xSemaphoreTake(mutex_, portMAX_DELAY);
if (dayKey_ != current) {
dayKey_ = current;
state_.todayEvents = 0;
changed = true;
}
xSemaphoreGive(mutex_);
if (changed) {
preferences_.putString("day", current);
preferences_.putInt("today", 0);
}
}
void BoomState::incrementTodayEvents() {
rollDayIfNeeded();
int value;
xSemaphoreTake(mutex_, portMAX_DELAY);
value = ++state_.todayEvents;
xSemaphoreGive(mutex_);
preferences_.putInt("today", value);
}
void BoomState::requestSync(const String& filename) {
xSemaphoreTake(mutex_, portMAX_DELAY);
syncRequested_ = true;
syncFilename_ = filename;
xSemaphoreGive(mutex_);
}
bool BoomState::takeSyncRequest(String& filename) {
xSemaphoreTake(mutex_, portMAX_DELAY);
bool requested = syncRequested_;
if (requested) {
filename = syncFilename_;
syncFilename_ = "";
syncRequested_ = false;
}
xSemaphoreGive(mutex_);
return requested;
}
void BoomState::requestReboot(uint32_t delayMs) {
rebootAtMs_ = millis() + delayMs;
}
bool BoomState::rebootDue() const {
return rebootAtMs_ != 0 && (int32_t)(millis() - rebootAtMs_) >= 0;
}