added fade in if a new connection is established

This commit is contained in:
2026-01-20 08:14:52 +01:00
parent 31dcc5f502
commit 60f12e22e4

View File

@@ -122,6 +122,17 @@ unsigned long lastPacketTime = 0;
const unsigned long watchdogTimeout = 5000; // 5 seconds
unsigned long lastFadeTime = 0;
// -------------------------------
// Connection state machine
// -------------------------------
enum ConnectionState {
STATE_DISCONNECTED, // fade to zero
STATE_CONNECTING, // fade in 0 → 100%
STATE_CONNECTED // normal UDP-driven slew
};
ConnectionState connectionState = STATE_DISCONNECTED;
// -------------------------------
// ESPUI controls
// -------------------------------
@@ -162,8 +173,6 @@ float applyCalibration(uint8_t ch, float logicalDuty) {
// Calibration UI helpers & callbacks
// -------------------------------
void refreshCalibrationUI() {
String label = "CH" + String(selectedCalChannel) + " (" + channelLabels[selectedCalChannel] + ")";
for (int i = 0; i < 5; i++) {
ESPUI.updateControlValue(calInputs[i], String(calibratedPoints[selectedCalChannel][i], 2));
}
@@ -183,7 +192,6 @@ void calChannelCallback(Control *sender, int type) {
refreshCalibrationUI();
}
void calPointCallback(Control *sender, int type) {
int index = sender->id - calInputs[0];
if (index >= 0 && index < 5) {
@@ -507,7 +515,7 @@ void setup() {
);
// Start ESPUI
ESPUI.sliderContinuous = true; // < enables live slider updates
ESPUI.sliderContinuous = true; // enables live slider updates
ESPUI.begin("Analog Monitor UI");
}
@@ -535,9 +543,29 @@ void loop() {
if (idx == NUM_CHANNELS) {
// Start a new slew toward the target
// First valid packet after being disconnected → start CONNECTING
if (connectionState == STATE_DISCONNECTED) {
Serial.println("STATE CHANGE: DISCONNECTED → CONNECTING (UDP connection established)");
connectionState = STATE_CONNECTING;
// Initialize fade-in: start from 0 on all non-override channels
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) { // < NEW
if (!overrideActive[ch]) {
currentDuty[ch] = 0.0f;
}
}
lastPacketTime = millis(); // prevent watchdog during fade-in
// Ignore this packet's values during fade-in (Option B)
}
else if (connectionState == STATE_CONNECTING) {
// Ignore UDP values during fade-in, just keep watchdog alive
lastPacketTime = millis();
}
else if (connectionState == STATE_CONNECTED) {
// Normal UDP-driven update
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) {
targetDuty[ch] = values[ch];
slewStartDuty[ch] = currentDuty[ch];
}
@@ -545,7 +573,7 @@ void loop() {
slewStartTime = millis();
lastPacketTime = millis();
// -------- Improved Debug Output --------
// Debug output
Serial.println("Received UDP packet:");
for (int i = 0; i < NUM_CHANNELS; i++) {
Serial.print(" CH");
@@ -563,16 +591,26 @@ void loop() {
Serial.println();
}
}
}
// -------- Slew-rate limiting --------
if (millis() - lastPacketTime <= watchdogTimeout) {
// -------- Connection state machine --------
unsigned long now = millis();
switch (connectionState) {
case STATE_CONNECTED: {
// Check for lost connection
if (now - lastPacketTime > watchdogTimeout) {
Serial.println("STATE CHANGE: CONNECTED → DISCONNECTED (UDP connection lost)");
connectionState = STATE_DISCONNECTED;
break;
}
// Normal slew-rate limiting
float progress = (float)(now - slewStartTime) / (float)slewDuration;
if (progress > 1.0f) progress = 1.0f;
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) {
float newDuty = slewStartDuty[ch] + (targetDuty[ch] - slewStartDuty[ch]) * progress;
currentDuty[ch] = newDuty;
@@ -582,15 +620,62 @@ void loop() {
ledcWrite(pwmPins[ch], duty);
}
}
} break;
case STATE_CONNECTING: {
// If we lose packets even while connecting, fall back to DISCONNECTED
if (now - lastPacketTime > watchdogTimeout) {
Serial.println("STATE CHANGE: CONNECTING → DISCONNECTED (no packets during fade-in)");
connectionState = STATE_DISCONNECTED;
break;
}
// -------- Watchdog fade-to-zero --------
if (millis() - lastPacketTime > watchdogTimeout) {
// Fade-in animation: 0 → 100% on all non-override channels
const unsigned long fadeInterval = 1;
const float fadeInFactor = 0.999f; // fast at start, slow near 100%
if (now - lastFadeTime >= fadeInterval) {
lastFadeTime = now;
bool allReached = true;
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) {
float cd = currentDuty[ch];
// Exponential approach to 100%
cd = cd * fadeInFactor + 100.0f * (1.0f - fadeInFactor);
currentDuty[ch] = cd;
float calibratedDuty = applyCalibration(ch, cd);
int duty = (int)((calibratedDuty / 100.0f) * ((1 << pwmResolution) - 1));
ledcWrite(pwmPins[ch], duty);
// Check if we're close enough to 100%
if (cd < 99.0f) {
allReached = false;
}
}
}
if (allReached) {
Serial.println("STATE CHANGE: CONNECTING → CONNECTED (fade-in complete)");
// Initialize slew baseline from currentDuty
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
slewStartDuty[ch] = currentDuty[ch];
}
slewStartTime = millis();
connectionState = STATE_CONNECTED;
}
}
} break;
case STATE_DISCONNECTED: {
// Watchdog fade-to-zero (always active in this state)
const unsigned long fadeInterval = 1;
if (millis() - lastFadeTime >= fadeInterval) {
lastFadeTime = millis();
if (now - lastFadeTime >= fadeInterval) {
lastFadeTime = now;
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
@@ -609,5 +694,6 @@ void loop() {
}
}
}
} break;
}
}