Files
ESP32C3-0.42OLED-OS/main/main.c
T

134 lines
3.7 KiB
C

#include <stdio.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "driver/gpio.h"
/* OS Subsystems */
#include "os_event.h"
#include "os_status.h"
#include "os_gfx.h"
#include "os_input.h"
#include "os_app.h"
#include "os_cli.h"
#include "sys_wifi.h"
#include "sys_uart.h"
#include "apps.h"
static const char *TAG = "MICRO_OS";
#define PIN_LED_USER GPIO_NUM_8
#define PIN_BUTTON_BOOT GPIO_NUM_9
#define UI_REFRESH_HZ 20
#define UI_FRAME_MS (1000 / UI_REFRESH_HZ)
/* Task: Onboard LED Heartbeat */
static void led_heartbeat_task(void *pvParameters)
{
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << PIN_LED_USER),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io_conf);
while (1) {
gpio_set_level(PIN_LED_USER, 1);
vTaskDelay(pdMS_TO_TICKS(60));
gpio_set_level(PIN_LED_USER, 0);
vTaskDelay(pdMS_TO_TICKS(100));
gpio_set_level(PIN_LED_USER, 1);
vTaskDelay(pdMS_TO_TICKS(60));
gpio_set_level(PIN_LED_USER, 0);
vTaskDelay(pdMS_TO_TICKS(800));
}
}
/* Callback: Forward button gestures to App Manager */
static void on_input_event(input_action_t action, void *user_arg)
{
sys_app_dispatch_input(action);
}
/* Task: OS Display & UI Compositor Loop */
static void os_ui_task(void *pvParameters)
{
ESP_LOGI(TAG, "Starting UI compositor loop @ %d FPS", UI_REFRESH_HZ);
uint32_t last_time_ms = (uint32_t)(esp_timer_get_time() / 1000ULL);
while (1) {
uint32_t now_ms = (uint32_t)(esp_timer_get_time() / 1000ULL);
uint32_t delta_ms = now_ms - last_time_ms;
last_time_ms = now_ms;
sys_status_update();
sys_gfx_clear();
sys_app_update(delta_ms);
sys_gfx_render_toast_if_active();
sys_gfx_flush();
vTaskDelay(pdMS_TO_TICKS(UI_FRAME_MS));
}
}
void app_main(void)
{
ESP_LOGI(TAG, "=============================================");
ESP_LOGI(TAG, " Booting ABrobot ESP32-C3 Micro-OS v1.0.0 ");
ESP_LOGI(TAG, "=============================================");
// 1. Core Services & Telemetry
sys_event_init();
sys_status_init();
esp_err_t err;
// 1b. Wi-Fi and provisioning services
err = sys_wifi_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize Wi-Fi subsystem: %s", esp_err_to_name(err));
}
// 1c. Hardware UART Subsystem (TX: GPIO 21, RX: GPIO 20)
err = sys_uart_init(115200);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize UART subsystem: %s", esp_err_to_name(err));
}
// 2. Display & Graphics Subsystem
err = sys_gfx_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize graphics subsystem: %s", esp_err_to_name(err));
}
// 3. Button & Gesture Engine
sys_input_init(PIN_BUTTON_BOOT);
sys_input_register_handler(on_input_event, NULL);
// 4. Application Framework & Registry
sys_app_init();
sys_app_register(&g_app_clock);
sys_app_register(&g_app_sysmon);
sys_app_register(&g_app_serial);
sys_app_register(&g_app_game);
sys_app_register(&g_app_settings);
// Launch default startup app
sys_app_launch_by_id("clock");
// 5. Interactive USB Console Shell
sys_cli_init();
// 6. Launch OS Tasks
xTaskCreate(led_heartbeat_task, "led_hb_task", 2048, NULL, 5, NULL);
xTaskCreate(os_ui_task, "ui_task", 4096, NULL, 10, NULL);
ESP_LOGI(TAG, "Micro-OS initialized and running.");
}