From d4df71135552f783d134b3cb76264c7007f88a98 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Sun, 18 Dec 2022 16:29:02 +0100 Subject: [PATCH] changed LED behavior of the ESP32, to distiguish between stationary and mobile mode, disabled WiFi state LED, added reboot esp32 command --- ADIS_ESP32_Eclipse/main/challenge_app.c | 26 +++++++++++++++++++++---- ADIS_ESP32_Eclipse/main/challenge_nvs.c | 1 + ADIS_ESP32_Eclipse/main/led.c | 11 +++++++++-- ADIS_ESP32_Eclipse/main/led.h | 2 ++ ADIS_ESP32_Eclipse/main/wifi.c | 12 ++++++------ 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/ADIS_ESP32_Eclipse/main/challenge_app.c b/ADIS_ESP32_Eclipse/main/challenge_app.c index 9f6a3f5..a0455bb 100644 --- a/ADIS_ESP32_Eclipse/main/challenge_app.c +++ b/ADIS_ESP32_Eclipse/main/challenge_app.c @@ -17,7 +17,7 @@ #include "McuUtility.h" #include "challenge_com.h" #include "challenge_nvs.h" - +#include "led.h" /* LOCAL Var */ /* identifies robot Mode for the challenge: true = robot is stationary, false = robot is moveable */ @@ -28,7 +28,7 @@ static TaskHandle_t appTaskHandle; /* task handle */ /* local function declarations */ static void Challenge_App_SetRobotMode(bool modeStationary, bool storeToNvs); - +static void Challenge_App_RebootEsp32(void); static void appTask(void *pv){ /*Task argument not used @@ -76,7 +76,9 @@ BaseType_t res; void Challenge_App_Init(void){ Challenge_Nvs_Init(); - Challenge_Nvs_GetRobotModeFromNVS(&_robotModeStationary); + bool retrievedMode = false; + Challenge_Nvs_GetRobotModeFromNVS(&retrievedMode); + Challenge_App_SetRobotMode(retrievedMode, false); // do not store to NVS, not required res = xTaskCreate(appTask, "ChallengeAppTask", 4096/sizeof(StackType_t), (void*)"ARG_0", tskIDLE_PRIORITY, &appTaskHandle); if(res != pdPASS){ @@ -91,11 +93,23 @@ bool Challenge_App_GetRobotMode(void){ static void Challenge_App_SetRobotMode(bool modeStationary, bool storeToNvs){ _robotModeStationary = modeStationary; ESP_LOGI(TAG, "Changed robot mode to %s", _robotModeStationary?(unsigned char*)"stationary":(unsigned char*)"mobile"); + // update led behavior to be able to differentiate the robots + if(modeStationary){ + LED_SetOnOffTime(5000, 1000); + } else{ + LED_SetOnOffTime(500, 500); + } + if(storeToNvs){ Challenge_Nvs_StoreRobotModeToNVS(modeStationary); } } +static void Challenge_App_RebootEsp32(void){ + fflush(stdout); + esp_restart(); +} + /*********/ /* SHELL */ /*********/ @@ -111,6 +125,7 @@ static uint8_t PrintHelp(const McuShell_StdIOType *io) { McuShell_SendHelpStr((unsigned char*)" help|status", (unsigned char*)"Shows Challenge help or status\r\n", io->stdOut); McuShell_SendHelpStr((unsigned char*)" setMode s(tationary)", (unsigned char*)"Sets the mode of this robot to stationary\r\n", io->stdOut); McuShell_SendHelpStr((unsigned char*)" setMode m(mobile)", (unsigned char*)"Sets the mode of this robot to mobile\r\n", io->stdOut); + McuShell_SendHelpStr((unsigned char*)" reboot", (unsigned char*)"Reboots the ESP32\r\n", io->stdOut); return ERR_OK; } @@ -129,7 +144,10 @@ uint8_t Challenge_ParseShellCommand(const unsigned char* cmd, bool *handled, con McuUtility_strcmp((char*)cmd, (char*)"challenge setMode m")==0 ) { *handled = TRUE; Challenge_App_SetRobotMode(false, true); // set mobile - } + }else if (McuUtility_strcmp((char*)cmd, (char*)"challenge reboot")==0) { + *handled = TRUE; + Challenge_App_RebootEsp32(); + } return ERR_OK; } #endif /* PL_CONFIG_USE_SHELL */ diff --git a/ADIS_ESP32_Eclipse/main/challenge_nvs.c b/ADIS_ESP32_Eclipse/main/challenge_nvs.c index 4ce6bc7..9b34638 100644 --- a/ADIS_ESP32_Eclipse/main/challenge_nvs.c +++ b/ADIS_ESP32_Eclipse/main/challenge_nvs.c @@ -156,6 +156,7 @@ bool Challenge_Nvs_GetRobotModeFromNVS(bool *mode){ if(notInitialized){ success = Challenge_Nvs_StoreRobotModeToNVS(false); // initialize to false + *mode = false; // set out variable to false too } Challenge_Nvs_ReleaseMutex(); diff --git a/ADIS_ESP32_Eclipse/main/led.c b/ADIS_ESP32_Eclipse/main/led.c index 6307eb5..8e35096 100644 --- a/ADIS_ESP32_Eclipse/main/led.c +++ b/ADIS_ESP32_Eclipse/main/led.c @@ -22,7 +22,7 @@ static McuLED_Handle_t ledHandle; static TaskHandle_t taskHandle; static bool blinkyIsRunning = false; -#define LED_OFF_TIME_MS 1000 +static uint32_t offTimeMs = 1000; /* default off time of LED */ static uint32_t onTimeMs = 10; /* default on time of LED */ /* red led on the shield is on IO10, LOW active */ @@ -32,6 +32,13 @@ static uint32_t onTimeMs = 10; /* default on time of LED */ void LED_SetOnTime(uint32_t ms) { onTimeMs = ms; } +void LED_SetOffTime(uint32_t ms) { + offTimeMs = ms; +} +void LED_SetOnOffTime(uint32_t onMs, uint32_t offMs){ + onTimeMs = onMs; + offTimeMs = offMs; +} void LED_On(void) { McuLED_On(ledHandle); @@ -88,7 +95,7 @@ static void blinkyTask(void *pv) { LED_On(); vTaskDelay(pdMS_TO_TICKS(onTimeMs)); LED_Off(); - vTaskDelay(pdMS_TO_TICKS(LED_OFF_TIME_MS)); + vTaskDelay(pdMS_TO_TICKS(offTimeMs)); } } diff --git a/ADIS_ESP32_Eclipse/main/led.h b/ADIS_ESP32_Eclipse/main/led.h index 3a28c63..0813309 100644 --- a/ADIS_ESP32_Eclipse/main/led.h +++ b/ADIS_ESP32_Eclipse/main/led.h @@ -15,6 +15,8 @@ extern "C" { #endif void LED_SetOnTime(uint32_t ms); +void LED_SetOffTime(uint32_t ms); +void LED_SetOnOffTime(uint32_t onMs, uint32_t offMs); void LED_Suspend(void); void LED_Resume(void); void LED_GetStatus(unsigned char *buf, size_t bufSize); diff --git a/ADIS_ESP32_Eclipse/main/wifi.c b/ADIS_ESP32_Eclipse/main/wifi.c index 89d083b..0b6ef2d 100644 --- a/ADIS_ESP32_Eclipse/main/wifi.c +++ b/ADIS_ESP32_Eclipse/main/wifi.c @@ -258,15 +258,15 @@ static void WiFiTask(void *pv) { McuLog_info("Initialize WiFi"); initialise_wifi(); -#if PL_CONFIG_USE_BLINKY - LED_SetOnTime(LED_ON_TIME_MS_DISCONNECTED); -#endif +//#if PL_CONFIG_USE_BLINKY +// LED_SetOnTime(LED_ON_TIME_MS_DISCONNECTED); +//#endif for(;;) { vTaskDelay(pdMS_TO_TICKS(5000)); isConnected = xEventGroupGetBits(s_wifi_event_group)&WIFI_CONNECTED_BIT; - #if PL_CONFIG_USE_BLINKY - LED_SetOnTime(isConnected?LED_ON_TIME_MS_CONNECTED:LED_ON_TIME_MS_DISCONNECTED); - #endif +// #if PL_CONFIG_USE_BLINKY +// LED_SetOnTime(isConnected?LED_ON_TIME_MS_CONNECTED:LED_ON_TIME_MS_DISCONNECTED); +// #endif if (state == WIFI_STATE_INIT && isConnected) { /* first connection */ state = WIFI_STATE_CONNECTED; ESP_LOGI(TAG, "WiFi is connected.");