mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-05-06 13:56:05 +02:00
Merge pull request #12 from lorow/feature/serial-manager-upgrades
Keep serial manager alive when in WiFi mode and still connected to serial, rework serial manager commands receiver
This commit is contained in:
@@ -115,6 +115,7 @@ void ProjectConfig::setMDNSConfig(const std::string &hostname)
|
|||||||
{
|
{
|
||||||
ESP_LOGD(CONFIGURATION_TAG, "Updating MDNS config");
|
ESP_LOGD(CONFIGURATION_TAG, "Updating MDNS config");
|
||||||
this->config.mdns.hostname.assign(hostname);
|
this->config.mdns.hostname.assign(hostname);
|
||||||
|
this->config.device.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectConfig::setCameraConfig(const uint8_t vflip,
|
void ProjectConfig::setCameraConfig(const uint8_t vflip,
|
||||||
|
|||||||
@@ -20,9 +20,15 @@ void SerialManager::setup()
|
|||||||
usb_serial_jtag_driver_install(&usb_serial_jtag_config);
|
usb_serial_jtag_driver_install(&usb_serial_jtag_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SerialManager::isConnected()
|
||||||
|
{
|
||||||
|
// in preparation for handling uart as well
|
||||||
|
return usb_serial_jtag_is_connected();
|
||||||
|
}
|
||||||
|
|
||||||
void SerialManager::try_receive()
|
void SerialManager::try_receive()
|
||||||
{
|
{
|
||||||
int current_position = 0;
|
static auto current_position = 0;
|
||||||
int len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
|
int len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
|
||||||
|
|
||||||
// If driver is uninstalled or an error occurs, abort read gracefully
|
// If driver is uninstalled or an error occurs, abort read gracefully
|
||||||
@@ -31,40 +37,24 @@ void SerialManager::try_receive()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// since we've got something on the serial port
|
if (len > 0)
|
||||||
// we gotta keep reading until we've got the whole message
|
|
||||||
while (len > 0)
|
|
||||||
{
|
{
|
||||||
// Prevent buffer overflow
|
|
||||||
if (current_position + len >= BUF_SIZE)
|
|
||||||
{
|
|
||||||
int copy_len = BUF_SIZE - 1 - current_position;
|
|
||||||
if (copy_len > 0)
|
|
||||||
{
|
|
||||||
memcpy(this->data + current_position, this->temp_data, copy_len);
|
|
||||||
current_position += copy_len;
|
|
||||||
}
|
|
||||||
// Drop the rest of the input to avoid overflow
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
memcpy(this->data + current_position, this->temp_data, (size_t)len);
|
|
||||||
current_position += len;
|
|
||||||
len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
|
|
||||||
if (len < 0)
|
|
||||||
{
|
|
||||||
// Driver likely uninstalled during handover; stop processing this cycle
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current_position)
|
|
||||||
{
|
|
||||||
// once we're done, we can terminate the string and reset the counter
|
|
||||||
data[current_position] = '\0';
|
|
||||||
current_position = 0;
|
|
||||||
|
|
||||||
// Notify main that a command was received during startup
|
// Notify main that a command was received during startup
|
||||||
notify_startup_command_received();
|
notify_startup_command_received();
|
||||||
|
}
|
||||||
|
|
||||||
|
// since we've got something on the serial port
|
||||||
|
// we gotta keep reading until we've got the whole message
|
||||||
|
// we will submit the command once we get a newline, a return or the buffer is full
|
||||||
|
for (auto i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
this->data[current_position++] = this->temp_data[i];
|
||||||
|
// if we're at the end of the buffer, try to process the command anyway
|
||||||
|
// if we've got a new line, we've finished sending the commands, process them
|
||||||
|
if (current_position >= BUF_SIZE || this->data[current_position - 1] == '\n' || this->data[current_position - 1] == '\r')
|
||||||
|
{
|
||||||
|
data[current_position] = '\0';
|
||||||
|
current_position = 0;
|
||||||
|
|
||||||
const auto result = this->commandManager->executeFromJson(std::string_view(reinterpret_cast<const char *>(this->data)));
|
const auto result = this->commandManager->executeFromJson(std::string_view(reinterpret_cast<const char *>(this->data)));
|
||||||
const auto resultMessage = result.getResult();
|
const auto resultMessage = result.getResult();
|
||||||
@@ -72,6 +62,7 @@ void SerialManager::try_receive()
|
|||||||
(void)written; // ignore errors if driver already uninstalled
|
(void)written; // ignore errors if driver already uninstalled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Function to notify that a command was received during startup
|
// Function to notify that a command was received during startup
|
||||||
void SerialManager::notify_startup_command_received()
|
void SerialManager::notify_startup_command_received()
|
||||||
@@ -79,11 +70,11 @@ void SerialManager::notify_startup_command_received()
|
|||||||
setStartupCommandReceived(true);
|
setStartupCommandReceived(true);
|
||||||
|
|
||||||
// Cancel the startup timer if it's still running
|
// Cancel the startup timer if it's still running
|
||||||
if (timerHandle != nullptr)
|
if (timerHandle != nullptr && *timerHandle != nullptr)
|
||||||
{
|
{
|
||||||
esp_timer_stop(*timerHandle);
|
esp_timer_stop(*timerHandle);
|
||||||
esp_timer_delete(*timerHandle);
|
esp_timer_delete(*timerHandle);
|
||||||
timerHandle = nullptr;
|
*timerHandle = nullptr;
|
||||||
ESP_LOGI("[MAIN]", "Startup timer cancelled, staying in heartbeat mode");
|
ESP_LOGI("[MAIN]", "Startup timer cancelled, staying in heartbeat mode");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -112,7 +103,7 @@ bool SerialManager::should_send_heartbeat()
|
|||||||
// Always send heartbeat during startup delay or if no WiFi configured
|
// Always send heartbeat during startup delay or if no WiFi configured
|
||||||
|
|
||||||
// If startup timer is still running, always send heartbeat
|
// If startup timer is still running, always send heartbeat
|
||||||
if (timerHandle != nullptr)
|
if (timerHandle != nullptr && *timerHandle != nullptr)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ class SerialManager
|
|||||||
public:
|
public:
|
||||||
explicit SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle, std::shared_ptr<ProjectConfig> deviceConfig);
|
explicit SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle, std::shared_ptr<ProjectConfig> deviceConfig);
|
||||||
void setup();
|
void setup();
|
||||||
|
bool isConnected();
|
||||||
void try_receive();
|
void try_receive();
|
||||||
void send_heartbeat();
|
void send_heartbeat();
|
||||||
bool should_send_heartbeat();
|
bool should_send_heartbeat();
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
TaskHandle_t serialManagerHandle;
|
TaskHandle_t serialManagerHandle;
|
||||||
|
|
||||||
esp_timer_handle_t timerHandle;
|
esp_timer_handle_t timerHandle = nullptr;
|
||||||
QueueHandle_t eventQueue = xQueueCreate(10, sizeof(SystemEvent));
|
QueueHandle_t eventQueue = xQueueCreate(10, sizeof(SystemEvent));
|
||||||
QueueHandle_t ledStateQueue = xQueueCreate(10, sizeof(uint32_t));
|
QueueHandle_t ledStateQueue = xQueueCreate(10, sizeof(uint32_t));
|
||||||
QueueHandle_t cdcMessageQueue = xQueueCreate(3, sizeof(cdc_command_packet_t));
|
QueueHandle_t cdcMessageQueue = xQueueCreate(3, sizeof(cdc_command_packet_t));
|
||||||
@@ -201,9 +201,16 @@ void startWiFiMode(bool shouldCloseSerialManager)
|
|||||||
ESP_LOGI("[MAIN]", "Starting WiFi streaming mode.");
|
ESP_LOGI("[MAIN]", "Starting WiFi streaming mode.");
|
||||||
if (shouldCloseSerialManager)
|
if (shouldCloseSerialManager)
|
||||||
{
|
{
|
||||||
ESP_LOGI("[MAIN]", "Closing serial manager task.");
|
if (!serialManager->isConnected())
|
||||||
|
{
|
||||||
|
ESP_LOGI("[MAIN]", "We're not connected to serial. Closing serial manager task.");
|
||||||
vTaskDelete(serialManagerHandle);
|
vTaskDelete(serialManagerHandle);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGI("[MAIN]", "We're still connected to serial. Serial manager task will remain running.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wifiManager->Begin();
|
wifiManager->Begin();
|
||||||
mdnsManager.start();
|
mdnsManager.start();
|
||||||
|
|||||||
Reference in New Issue
Block a user