feat(esp32): add power-off hysteresis, change green color slighly for improved perception with red-green-blindness

This commit is contained in:
s3lph 2024-10-23 01:30:21 +02:00
parent 7f3f5f2b6c
commit 02ef61c54b
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5

View file

@ -143,9 +143,6 @@ void setup() {
pixels.show(); pixels.show();
WiFi.setHostname("spaceapimap"); WiFi.setHostname("spaceapimap");
WiFi.setTxPower(WIFI_POWER_15dBm);
WiFi.enableIpV6();
WiFi.setAutoReconnect(true);
wifiManager.setDebugOutput(true); wifiManager.setDebugOutput(true);
wifiManager.setConnectTimeout(60); wifiManager.setConnectTimeout(60);
@ -294,6 +291,7 @@ bool updateMap() {
#ifdef FEATURE_LDR #ifdef FEATURE_LDR
uint16_t rolling_average_brightness[10]; uint16_t rolling_average_brightness[10];
uint8_t rolling_average_brightness_i = 0; uint8_t rolling_average_brightness_i = 0;
bool leds_on_hysteresis = true;
uint16_t ambientBrightnessToPower() { uint16_t ambientBrightnessToPower() {
// Read brightness from LDR (0..2500 mV) and map to power level (0..255) and write into rolling average list // Read brightness from LDR (0..2500 mV) and map to power level (0..255) and write into rolling average list
rolling_average_brightness[rolling_average_brightness_i] = analogRead(FEATURE_LDR); rolling_average_brightness[rolling_average_brightness_i] = analogRead(FEATURE_LDR);
@ -306,7 +304,15 @@ uint16_t ambientBrightnessToPower() {
uint16_t brightness = sum / (sizeof(rolling_average_brightness)/sizeof(uint16_t)); uint16_t brightness = sum / (sizeof(rolling_average_brightness)/sizeof(uint16_t));
Serial.print("brightness: "); Serial.print("brightness: ");
Serial.println(brightness); Serial.println(brightness);
if (brightness < spiffs_config.brightness_thresh_lower) { if (brightness > spiffs_config.brightness_thresh_lower + 40 && leds_on_hysteresis) {
Serial.println("LED power off!");
leds_on_hysteresis = false;
}
if (brightness < spiffs_config.brightness_thresh_lower - 40 && !leds_on_hysteresis) {
Serial.println("LED power on!");
leds_on_hysteresis = true;
}
if (leds_on_hysteresis) {
uint16_t clamped = min(max(brightness, spiffs_config.brightness_thresh_upper), spiffs_config.brightness_thresh_lower); uint16_t clamped = min(max(brightness, spiffs_config.brightness_thresh_upper), spiffs_config.brightness_thresh_lower);
uint8_t power = spiffs_config.brightness_min + (spiffs_config.brightness_max - spiffs_config.brightness_min) * pow(1.0f * (spiffs_config.brightness_thresh_lower - clamped) / (spiffs_config.brightness_thresh_lower - spiffs_config.brightness_thresh_upper), 2); uint8_t power = spiffs_config.brightness_min + (spiffs_config.brightness_max - spiffs_config.brightness_min) * pow(1.0f * (spiffs_config.brightness_thresh_lower - clamped) / (spiffs_config.brightness_thresh_lower - spiffs_config.brightness_thresh_upper), 2);
// Safeguard in case the computation is somehow off! // Safeguard in case the computation is somehow off!
@ -340,7 +346,7 @@ void loop() {
pixels.setPixelColor(i, pixels.Color(power, 0, 0)); // closed -> red pixels.setPixelColor(i, pixels.Color(power, 0, 0)); // closed -> red
break; break;
case SpaceState::Open: case SpaceState::Open:
pixels.setPixelColor(i, pixels.Color(0, power, 0)); // open -> green pixels.setPixelColor(i, pixels.Color(0, power, power/8)); // open -> green
break; break;
case SpaceState::Invalid: case SpaceState::Invalid:
pixels.setPixelColor(i, pixels.Color(power, power/2, 0)); // invalid response -> orange pixels.setPixelColor(i, pixels.Color(power, power/2, 0)); // invalid response -> orange