Explorar o código

Keep GPIO HTTP and flash control outside long critical sections

SybexXx hai 1 día
pai
achega
1865b2d74b
Modificáronse 1 ficheiros con 22 adicións e 39 borrados
  1. 22 39
      code/components/jomjol_controlGPIO/server_GPIO.cpp

+ 22 - 39
code/components/jomjol_controlGPIO/server_GPIO.cpp

@@ -827,9 +827,8 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
     bool publishMqtt = false;
     int responseStatus = 200;
 
-    // Protect only the map lookup and the immediate GPIO operation. Never keep
-    // this mutex while sending an HTTP response or publishing to the MQTT client:
-    // camera flash transitions also need the map and must not wait on network I/O.
+    // Keep only map access and the immediate GPIO read/write protected. Sending
+    // HTTP data or waiting for MQTT must not block camera flash transitions.
     {
         std::lock_guard<std::recursive_mutex> lock(_mapMutex);
 
@@ -866,8 +865,8 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
 
 #ifdef ENABLE_MQTT
     if (publishMqtt) {
-        MQTTPublish(mqttTopic, (status == "HIGH" || status == "TRUE" || status == "1")
-                                   ? "true" : "false", 1);
+        const bool high = status == "HIGH" || status == "TRUE" || status == "1";
+        MQTTPublish(mqttTopic, high ? "true" : "false", 1);
     }
 #endif
 
@@ -881,44 +880,28 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
 
 void GpioHandler::flashLightEnable(bool value)
 {
-    ESP_LOGD(TAG, "GpioHandler::flashLightEnable %s", value ? "true" : "false");
+    // The board-owned flash LED is deliberately absent from the user GPIO map.
+    // Drive its dedicated profile pin directly so enabling user GPIO channels
+    // never disables the camera flash, and do not hold the map mutex during a
+    // camera step transition.
+    const GpioBoardProfile *profile = gpioGetBoardProfile();
+    if (profile == NULL || !gpioBoardIsBuiltInFlashLed(profile->builtInFlashLedPin)) {
+        return;
+    }
 
-    std::vector<std::string> mqttTopics;
-    {
-        std::lock_guard<std::recursive_mutex> lock(_mapMutex);
-        if (gpioMap != NULL) {
-            for (std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin();
-                 it != gpioMap->end(); ++it) {
-                if (it->second->getMode() == GPIO_PIN_MODE_BUILT_IN_FLASH_LED) {
-                    std::string errorText;
-                    // Do the GPIO write under the map lock, but defer the network
-                    // publication until after unlocking. The camera task calls this
-                    // synchronously at image-capture and flow-step transitions.
-                    it->second->setValue(value, GPIO_SET_SOURCE_INTERNAL,
-                                         &errorText, false);
-                    if (errorText.empty()) {
-                        ESP_LOGD(TAG, "Flash light pin GPIO %d switched %s",
-                                 static_cast<int>(it->first), value ? "on" : "off");
-#ifdef ENABLE_MQTT
-                        const std::string &topic = it->second->getMqttTopic();
-                        if (!topic.empty()) mqttTopics.push_back(topic);
-#endif
-                    } else {
-                        ESP_LOGE(TAG, "Can't set flash light pin GPIO %d. Error: %s",
-                                 static_cast<int>(it->first), errorText.c_str());
-                    }
-                }
-                // External addressable LED modes are not exposed by the currently
-                // supported classic ESP32 board profiles.
-            }
-        }
+    const gpio_num_t flashPin = profile->builtInFlashLedPin;
+    esp_err_t result = gpio_set_direction(flashPin, GPIO_MODE_OUTPUT);
+    if (result != ESP_OK) {
+        ESP_LOGE(TAG, "Unable to configure board flash LED GPIO%d: %s",
+                 static_cast<int>(flashPin), esp_err_to_name(result));
+        return;
     }
 
-#ifdef ENABLE_MQTT
-    for (const std::string &topic : mqttTopics) {
-        MQTTPublish(topic, value ? "true" : "false", 1);
+    result = gpio_set_level(flashPin, value ? 1 : 0);
+    if (result != ESP_OK) {
+        ESP_LOGE(TAG, "Unable to switch board flash LED GPIO%d: %s",
+                 static_cast<int>(flashPin), esp_err_to_name(result));
     }
-#endif
 }
 
 gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)