From adc6e726f672aa7057992af54b286f7632b4fb07 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 9 May 2024 15:38:25 +0200 Subject: stop the c++ --- main/CMakeLists.txt | 4 ++-- main/init.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main/init.cpp | 54 --------------------------------------------------- main/main.c | 42 ++++++++++++++++++++++++++++++++++++++++ main/main.cpp | 42 ---------------------------------------- 5 files changed, 100 insertions(+), 98 deletions(-) create mode 100644 main/init.c delete mode 100644 main/init.cpp create mode 100644 main/main.c delete mode 100644 main/main.cpp diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index b86c077..123d1b7 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -13,8 +13,8 @@ project(puzzlebox_main C CXX ASM) pico_sdk_init() add_executable(main - main.cpp - init.cpp + main.c + init.c ) pico_enable_stdio_usb(main 1) diff --git a/main/init.c b/main/init.c new file mode 100644 index 0000000..616cfea --- /dev/null +++ b/main/init.c @@ -0,0 +1,56 @@ +#include "config.h" +#include "init.h" + +#include +#include +#include + +#include +#include + +EventGroupHandle_t init_complete; + +static void init_stdio() { + stdio_init_all(); +} + +static void init_cyw34() { + if (cyw43_arch_init_with_country(CONF_NET_COUNTRY)) + panic("cyw43_arch_init_with_country failed\n"); +} + +static void init_wifi() { + // enable 'station' mode (connect to an access point instead of acting like one) + cyw43_arch_enable_sta_mode(); + + if (cyw43_arch_wifi_connect_timeout_ms(CONF_NET_SSID, CONF_NET_PASS, CYW43_AUTH_WPA2_AES_PSK, CONF_NET_CONN_TIMEOUT)) + panic("cyw43_arch_wifi_connect failed\n"); + + // TODO: announce hostname(?) +} + +static void async_init() { + init_cyw34(); + init_wifi(); + // TODO: initialize i2c + + xEventGroupSetBits(init_complete, 1); + + // delete self + vTaskDelete(NULL); +} + +void init() { + init_complete = xEventGroupCreate(); + + // used for debug `printf` and `panic` on errors + init_stdio(); + + // defer other initialization until the task scheduler is running (important) + xTaskCreate((TaskFunction_t) async_init, "init", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 4, NULL); +} + +void await_init() { + xEventGroupWaitBits(init_complete, 1, pdFALSE, pdFALSE, portMAX_DELAY); +} + diff --git a/main/init.cpp b/main/init.cpp deleted file mode 100644 index 48f3774..0000000 --- a/main/init.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "config.h" -#include "init.h" - -#include -#include -#include - -#include -#include - -EventGroupHandle_t init_complete; - -static void init_stdio() { - stdio_init_all(); -} - -static void init_cyw34() { - if (cyw43_arch_init_with_country(CONF_NET_COUNTRY)) - panic("cyw43_arch_init_with_country failed\n"); -} - -static void init_wifi() { - // enable 'station' mode (connect to an access point instead of acting like one) - cyw43_arch_enable_sta_mode(); - - if (cyw43_arch_wifi_connect_timeout_ms(CONF_NET_SSID, CONF_NET_PASS, CYW43_AUTH_WPA2_AES_PSK, CONF_NET_CONN_TIMEOUT)) - panic("cyw43_arch_wifi_connect failed\n"); - - // TODO: announce hostname(?) -} - -void init() { - init_complete = xEventGroupCreate(); - - // used for debug `printf` and `panic` on errors - init_stdio(); - - // defer other initialization until the task scheduler is running (important) - xTaskCreate((TaskFunction_t) [](void*) { - init_cyw34(); - init_wifi(); - // TODO: initialize i2c - - xEventGroupSetBits(init_complete, 1); - - // delete self - vTaskDelete(NULL); - }, "init", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 4, NULL); -} - -void await_init() { - xEventGroupWaitBits(init_complete, 1, pdFALSE, pdFALSE, portMAX_DELAY); -} - diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..c97a808 --- /dev/null +++ b/main/main.c @@ -0,0 +1,42 @@ +#include +#include + +#include +#include + +#include "config.h" +#include "init.h" + +// #include +// #include +// #include + +void blink_task() { + await_init(); // `blink_task` uses GPIO + + while (true) { + cyw43_arch_gpio_put(LED_PIN, 0); + vTaskDelay(250 / portTICK_PERIOD_MS); + cyw43_arch_gpio_put(LED_PIN, 1); + vTaskDelay(250 / portTICK_PERIOD_MS); + } +} + +void test_task() { + int i = 0; + while (true) { + // stdio is initialized synchronously, so no `await_init` is needed + printf("hello #%d...\n", ++i); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + +int main() { + init(); + + xTaskCreate((TaskFunction_t) blink_task, "blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); + xTaskCreate((TaskFunction_t) test_task, "test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); + + vTaskStartScheduler(); +} + diff --git a/main/main.cpp b/main/main.cpp deleted file mode 100644 index c97a808..0000000 --- a/main/main.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include - -#include -#include - -#include "config.h" -#include "init.h" - -// #include -// #include -// #include - -void blink_task() { - await_init(); // `blink_task` uses GPIO - - while (true) { - cyw43_arch_gpio_put(LED_PIN, 0); - vTaskDelay(250 / portTICK_PERIOD_MS); - cyw43_arch_gpio_put(LED_PIN, 1); - vTaskDelay(250 / portTICK_PERIOD_MS); - } -} - -void test_task() { - int i = 0; - while (true) { - // stdio is initialized synchronously, so no `await_init` is needed - printf("hello #%d...\n", ++i); - vTaskDelay(1000 / portTICK_PERIOD_MS); - } -} - -int main() { - init(); - - xTaskCreate((TaskFunction_t) blink_task, "blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); - xTaskCreate((TaskFunction_t) test_task, "test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); - - vTaskStartScheduler(); -} - -- cgit v1.2.3