From 69330546bace75fe8f7ffd6b06a2bbfa00dcc31b Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Thu, 26 Sep 2024 14:44:54 -0400 Subject: [PATCH] Serial --- main/CMakeLists.txt | 2 +- main/SerialClient_Task.c | 274 +++++++++++++++++++++++++++++++++++++++ main/SerialClient_Task.h | 24 ++++ main/main.c | 80 +----------- 4 files changed, 302 insertions(+), 78 deletions(-) create mode 100644 main/SerialClient_Task.c create mode 100644 main/SerialClient_Task.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index c931610..1a99bf6 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "LEDController_Task.c" - "MQTTClient_Task.c" + "SerialClient_Task.c" "main.c" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/main/SerialClient_Task.c b/main/SerialClient_Task.c new file mode 100644 index 0000000..80fda48 --- /dev/null +++ b/main/SerialClient_Task.c @@ -0,0 +1,274 @@ +#include "SerialClient_Task.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" +#include +#include +#include + +#include "esp_log.h" +#include "esp_crt_bundle.h" +#include "esp_console.h" +#include "linenoise/linenoise.h" +#include "argtable3/argtable3.h" +#include "esp_vfs_cdcacm.h" +#include "esp_timer.h" + +#include "LEDController_Task.h" + +static const char* TAG_SerialClient = "SerialClient"; +const char* prompt = LOG_COLOR_I CONFIG_IDF_TARGET "> " LOG_RESET_COLOR; + +SerialClient_Data data; + +void Pomodoro_Task(void *pvParameter) +{ + SerialClient_Data* data2 = (SerialClient_Data*)pvParameter; + + LEDController_Command command = + { + .Effect = (enum LEDController_Effect)STATIC, + .Segment = -1, + .Ring = -1, + .Red = 0, + .Green = 0, + .Blue = 0 + }; + + xQueueSend(*(data2->Effect_Queue), (void*) &command, (TickType_t)0); + + int64_t time_work = 1000 * 1000 * 60 * 25; + int64_t time_free = 1000 * 1000 * 60 * 5; + + int64_t start_time = esp_timer_get_time(); + int state = 0; + + int led_state = 0; + while(42) + { + if(state == 0) + { + if(start_time + time_work > esp_timer_get_time()) + { + command.Segment = -1; + command.Ring = led_state; + command.Red = 255; + + xQueueSend(*(data2->Effect_Queue), (void*) &command, (TickType_t)0); + + if(led_state == 0) + { + command.Ring = 2; + } + else + { + command.Ring = led_state-1; + } + + command.Red = 0; + + xQueueSend(*(data2->Effect_Queue), (void*) &command, (TickType_t)0); + + led_state++; + + if(led_state > 2) + { + led_state = 0; + } + } + else + { + start_time = esp_timer_get_time(); + state = 1; + command.Red = 0; + } + } + else + { + if(start_time + time_free > esp_timer_get_time()) + { + command.Ring = -1; + command.Segment = led_state; + command.Green = 255; + + xQueueSend(*(data2->Effect_Queue), (void*) &command, (TickType_t)0); + + if(led_state == 0) + { + command.Segment = 2; + } + else + { + command.Segment = led_state-1; + } + command.Green = 0; + + xQueueSend(*(data2->Effect_Queue), (void*) &command, (TickType_t)0); + + led_state++; + + if(led_state > 2) + { + led_state = 0; + } + } + else + { + start_time = esp_timer_get_time(); + state = 0; + command.Green = 0; + } + } + + vTaskDelay(1000 / portTICK_PERIOD_MS); + ESP_LOGD(TAG_SerialClient, "Pomodoro"); + } +} + +TaskHandle_t pomodorotask; + +static int pomodoro_start(int argc, char **argv) +{ + ESP_LOGI(TAG_SerialClient, "Start Pomodoro"); + xTaskCreate(&Pomodoro_Task, "Pomodoro_Task", 4096, &data, 5, &pomodorotask); + + return 0; +} + +static int pomodoro_stop(int argc, char **argv) +{ + ESP_LOGI(TAG_SerialClient, "Stop Pomodoro"); + + vTaskDelete( pomodorotask ); + + LEDController_Command command = + { + .Effect = (enum LEDController_Effect)STATIC, + .Segment = -1, + .Ring = -1, + .Red = 0, + .Green = 0, + .Blue = 0 + }; + + xQueueSend(*(data.Effect_Queue), (void*) &command, (TickType_t)0); + + + return 0; +} + + +static void register_pomodoro(void) +{ + const esp_console_cmd_t cmd = { + .command = "pomodoro_start", + .help = "Start Pomodoro", + .hint = NULL, + .func = &pomodoro_start, + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); + + const esp_console_cmd_t cmd2 = { + .command = "pomodoro_stop", + .help = "Stop Pomodoro", + .hint = NULL, + .func = &pomodoro_stop, + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd2) ); +} + +void SerialClient_Init(SerialClient_Config* config, SerialClient_Data* data) +{ + data->Effect_Queue = config->Effect_Queue; + + ESP_LOGI(TAG_SerialClient, "Init"); + + /* Disable buffering on stdin */ + setvbuf(stdin, NULL, _IONBF, 0); + + /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */ + esp_vfs_dev_cdcacm_set_rx_line_endings(ESP_LINE_ENDINGS_CR); + /* Move the caret to the beginning of the next line on '\n' */ + esp_vfs_dev_cdcacm_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); + + /* Enable non-blocking mode on stdin and stdout */ + fcntl(fileno(stdout), F_SETFL, 0); + fcntl(fileno(stdin), F_SETFL, 0); + + /* Initialize the console */ + esp_console_config_t console_config = { + .max_cmdline_args = 8, + .max_cmdline_length = 256, + .hint_color = atoi(LOG_COLOR_CYAN) + }; + ESP_ERROR_CHECK( esp_console_init(&console_config) ); + + /* Configure linenoise line completion library */ + /* Enable multiline editing. If not set, long commands will scroll within + * single line. + */ + linenoiseSetMultiLine(1); + + /* Tell linenoise where to get command completions and hints */ + linenoiseSetCompletionCallback(&esp_console_get_completion); + linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint); + + /* Set command history size */ + linenoiseHistorySetMaxLen(10); + + esp_console_register_help_command(); + + register_pomodoro(); +} + + + +void SerialClient_Task(void *pvParameter) +{ + SerialClient_Config* config = pvParameter; + + SerialClient_Init(config, &data); + + LEDController_Command command = + { + .Effect = (enum LEDController_Effect)STATIC, + .Segment = -1, + .Ring = -1, + .Red = 0, + .Green = 0, + .Blue = 10 + }; + + xQueueSend(*(data.Effect_Queue), (void*) &command, (TickType_t)0); + + ESP_LOGI(TAG_SerialClient, "Run"); + + while(true) { + /* Get a line using linenoise. + * The line is returned when ENTER is pressed. + */ + char* line = linenoise(prompt); + if (line == NULL) { /* Ignore empty lines */ + continue; + } + /* Add the command to the history */ + linenoiseHistoryAdd(line); + + /* Try to run the command */ + int ret; + esp_err_t err = esp_console_run(line, &ret); + if (err == ESP_ERR_NOT_FOUND) { + printf("Unrecognized command\n"); + } else if (err == ESP_ERR_INVALID_ARG) { + // command was empty + } else if (err == ESP_OK && ret != ESP_OK) { + printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret)); + } else if (err != ESP_OK) { + printf("Internal error: %s\n", esp_err_to_name(err)); + } + /* linenoise allocates line buffer on the heap, so need to free it */ + linenoiseFree(line); + } +} \ No newline at end of file diff --git a/main/SerialClient_Task.h b/main/SerialClient_Task.h new file mode 100644 index 0000000..f69c43c --- /dev/null +++ b/main/SerialClient_Task.h @@ -0,0 +1,24 @@ +#ifndef SERIAL_CLIENT_TASK +#define SERIAL_CLIENT_TASK + +#include "led_strip.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" + +typedef struct SerialClient_Config +{ + // Effect Queue + QueueHandle_t* Effect_Queue; +} SerialClient_Config; + +typedef struct SerialClient_Data +{ + // Effect Queue + QueueHandle_t* Effect_Queue; +} SerialClient_Data; + +void SerialClient_Init(SerialClient_Config* config, SerialClient_Data* data); +void SerialClient_Task(void *pvParameter); + +#endif \ No newline at end of file diff --git a/main/main.c b/main/main.c index fafebc4..0ecafe3 100644 --- a/main/main.c +++ b/main/main.c @@ -6,29 +6,16 @@ #include "esp_log.h" #include "esp_netif.h" #include "nvs_flash.h" -#include "esp_mac.h" #include "LEDController_Task.h" -#include "MQTTClient_Task.h" +#include "SerialClient_Task.h" static const char *TAG_MAIN = "MAIN"; -/////////////////////////////////////////////////////////////// -// WARNING -// NEED FIXS: -// https://github.com/espressif/esp-idf/pull/12675 -/////////////////////////////////////////////////////////////// - - void app_main(void) { ESP_LOGI(TAG_MAIN, "Init"); - // INIT for WLAN - ESP_ERROR_CHECK(nvs_flash_init()); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - int Segment_GPIOs[3] = {10, 12, 14}; QueueHandle_t ledcontroller_queue = xQueueCreate(9+1, sizeof(LEDController_Command)); LEDController_Config ledcontroller_config = @@ -41,78 +28,17 @@ void app_main(void) }; xTaskCreate(&LEDController_Task, "LEDController_Task", 4096, (void*) &ledcontroller_config, 5, NULL); - unsigned char mac_base[6] = {0}; - esp_efuse_mac_get_default(mac_base); - esp_read_mac(mac_base, ESP_MAC_WIFI_STA); - char mac[] = "00:00:00:00:00:00"; - sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0],mac_base[1],mac_base[2],mac_base[3],mac_base[4],mac_base[5]); - - MQTTClient_Config mqttclient_config = + SerialClient_Config serialclient_config = { - .SSID = CONFIG_FABLIGHT_WIFI_SSID, - .PSK = CONFIG_FABLIGHT_WIFI_PASSWORD, - .Host = CONFIG_FABLIGHT_MQTT_HOST, - .Username = CONFIG_FABLIGHT_MQTT_USER, - .Password = CONFIG_FABLIGHT_MQTT_PASSWORD, - .ID = mac, .Effect_Queue = &ledcontroller_queue }; - xTaskCreate(&MQTTClient_Task, "MQTTClient_Task", 4096, (void*) &mqttclient_config, 5, NULL); + xTaskCreate(&SerialClient_Task, "SerialClient_Task", 4096, (void*) &serialclient_config, 5, NULL); ESP_LOGI(TAG_MAIN, "Run"); - LEDController_Command command1 = - { - .Effect = (enum LEDController_Effect)STATIC, - .Segment = -1, - .Ring = -1, - .Red = 255, - .Green = 200, - .Blue = 0 - }; - - LEDController_Command command2 = - { - .Effect = (enum LEDController_Effect)STATIC, - .Segment = -1, - .Ring = -1, - .Red = 0, - .Green = 0, - .Blue = 0 - }; - - int active = 0; while(42) { - // for(int i=0; i < ledcontroller_config.Segment_Count; i++) - // { - // if(i == active) - // { - // command1.Segment = i; - // xQueueSend(ledcontroller_queue, (void*) &command1, (TickType_t)0); - // } - // else - // { - // command2.Segment = i; - // xQueueSend(ledcontroller_queue, (void*) &command2, (TickType_t)0); - // } - // } - - // vTaskDelay(256 / portTICK_PERIOD_MS); - // active++; - - // if(active >= ledcontroller_config.Segment_Count) - // { - // active = 0; - // } - - vTaskDelay(10000 / portTICK_PERIOD_MS); ESP_LOGD(TAG_MAIN, "Update"); - // //xQueueSend(ledcontroller_queue, (void*) &command1, (TickType_t)0); - - // vTaskDelay(1000 / portTICK_PERIOD_MS); - // ESP_LOGD(TAG_MAIN, "Update"); - // //xQueueSend(ledcontroller_queue, (void*) &command2, (TickType_t)0); } } \ No newline at end of file