forked from copych/AcidBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (79 loc) · 1.93 KB
/
Copy pathmain.cpp
File metadata and controls
90 lines (79 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <Arduino.h>
#include <esp_system.h>
#include "constants.h"
#include "acidbox/config.h"
#include "stream/Streamer.h"
#include "acidbox/AcidBox.h"
Streamer* streamer = nullptr;
AcidBox* acidBox = nullptr;
bool streamerMode = true; // Default to streamer mode, will be overridden in setup() based on pin state
unsigned long lastModeCheckMs = 0;
const unsigned long MODE_CHECK_INTERVAL_MS = 50;
const unsigned long MODE_CHANGE_DEBOUNCE_MS = 300;
bool pendingModeChange = false;
bool pendingModeValue = true;
unsigned long pendingModeSinceMs = 0;
static bool GetStreamerMode();
static void checkStreamerMode();
void setup()
{
#ifdef DEBUG_ON
DEBUG_PORT.begin(115200);
#endif
DEBUG("Boot: AcidBox setup() entered");
delay(1000);
pinMode(STREAMER_ENABLE_PIN, INPUT_PULLUP);
streamerMode = GetStreamerMode();
if (streamerMode)
{
DEBUG("Starting Streamer mode");
streamer = new Streamer();
streamer->Setup();
}
else
{
DEBUG("Starting AcidBox mode");
acidBox = new AcidBox();
acidBox->Setup();
}
}
void loop()
{
checkStreamerMode();
if (streamer != nullptr)
streamer->Tick();
if (acidBox != nullptr)
acidBox->Tick();
}
static void checkStreamerMode()
{
unsigned long now = millis();
if (now - lastModeCheckMs >= MODE_CHECK_INTERVAL_MS)
{
lastModeCheckMs = now;
bool currentMode = GetStreamerMode();
if (currentMode != streamerMode)
{
if (!pendingModeChange || pendingModeValue != currentMode)
{
pendingModeChange = true;
pendingModeValue = currentMode;
pendingModeSinceMs = now;
}
if (pendingModeChange && (now - pendingModeSinceMs >= MODE_CHANGE_DEBOUNCE_MS))
{
DEBUG("Mode pin change confirmed, restarting...");
delay(50);
ESP.restart();
}
}
else
{
pendingModeChange = false;
}
}
}
static bool GetStreamerMode()
{
return digitalRead(STREAMER_ENABLE_PIN) == HIGH;
}