From 1b93e0cf91a7bbe212ccc0482569104d28e7a529 Mon Sep 17 00:00:00 2001 From: Mouise Date: Sat, 13 Jun 2026 13:37:44 +0200 Subject: [PATCH] Initial commit --- .clangd | 2 + .devcontainer/Dockerfile | 13 +++++ .devcontainer/devcontainer.json | 19 +++++++ .gitignore | 78 +++++++++++++++++++++++++++++ .vscode/c_cpp_properties.json | 20 ++++++++ .vscode/launch.json | 10 ++++ .vscode/settings.json | 19 +++++++ CMakeLists.txt | 8 +++ dependencies.lock | 35 +++++++++++++ main/CMakeLists.txt | 2 + main/buttons/buttons.c | 43 ++++++++++++++++ main/buttons/buttons.h | 30 ++++++++++++ main/idf_component.yml | 17 +++++++ main/main.c | 87 +++++++++++++++++++++++++++++++++ 14 files changed, 383 insertions(+) create mode 100644 .clangd create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitignore create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 CMakeLists.txt create mode 100644 dependencies.lock create mode 100644 main/CMakeLists.txt create mode 100644 main/buttons/buttons.c create mode 100644 main/buttons/buttons.h create mode 100644 main/idf_component.yml create mode 100644 main/main.c diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..437f255 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Remove: [-f*, -m*] diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..dafb8ad --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,13 @@ +ARG DOCKER_TAG=latest +FROM espressif/idf:${DOCKER_TAG} + +ENV LC_ALL=C.UTF-8 +ENV LANG=C.UTF-8 + +RUN apt-get update -y && apt-get install udev -y + +RUN echo "source /opt/esp/idf/export.sh > /dev/null 2>&1" >> ~/.bashrc + +ENTRYPOINT [ "/opt/esp/entrypoint.sh" ] + +CMD ["/bin/bash", "-c"] \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..246b79f --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,19 @@ +{ + "name": "ESP-IDF QEMU", + "build": { + "dockerfile": "Dockerfile" + }, + "customizations": { + "vscode": { + "settings": { + "terminal.integrated.defaultProfile.linux": "bash", + "idf.gitPath": "/usr/bin/git" + }, + "extensions": [ + "espressif.esp-idf-extension", + "espressif.esp-idf-web" + ] + } + }, + "runArgs": ["--privileged"] +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7805557 --- /dev/null +++ b/.gitignore @@ -0,0 +1,78 @@ +# macOS +.DS_Store +.AppleDouble +.LSOverride + +# Directory metadata +.directory + +# Temporary files +*~ +*.swp +*.swo +*.bak +*.tmp + +# Log files +*.log + +# Build artifacts and directories +**/build/ +build/ +*.o +*.a +*.out +*.exe # For any host-side utilities compiled on Windows + +# ESP-IDF specific build outputs +*.bin +*.elf +*.map +flasher_args.json # Generated in build directory +sdkconfig.old +sdkconfig + +# ESP-IDF dependencies +# For older versions or manual component management +/components/.idf/ +**/components/.idf/ +# For modern ESP-IDF component manager +managed_components/ +# If ESP-IDF tools are installed/referenced locally to the project +.espressif/ + +# CMake generated files +CMakeCache.txt +CMakeFiles/ +cmake_install.cmake +install_manifest.txt +CTestTestfile.cmake + +# Python environment files +*.pyc +*.pyo +*.pyd +__pycache__/ +*.egg-info/ +dist/ + +# Virtual environment folders +venv/ +.venv/ +env/ + +# Language Servers +.clangd/ +.ccls-cache/ +compile_commands.json + +# Windows specific +Thumbs.db +ehthumbs.db +Desktop.ini + +# User-specific configuration files +*.user +*.workspace # General workspace files, can be from various tools +*.suo # Visual Studio Solution User Options +*.sln.docstates # Visual Studio diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c565797 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,20 @@ +{ + "configurations": [ + { + "name": "ESP-IDF", + "compilerPath": "C:\\Espressif\\tools\\xtensa-esp-elf\\esp-15.2.0_20251204\\xtensa-esp-elf\\bin\\xtensa-esp32s3-elf-gcc.exe", + "compileCommands": "${config:idf.buildPath}/compile_commands.json", + "intelliSenseMode": "gcc-x86", + "includePath": [ + "${workspaceFolder}/**" + ], + "browse": { + "path": [ + "${workspaceFolder}" + ], + "limitSymbolsToIncludedHeaders": true + } + } + ], + "version": 4 +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3694ae4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,10 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "gdbtarget", + "request": "attach", + "name": "Eclipse CDT GDB Adapter" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5017714 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,19 @@ +{ + "C_Cpp.intelliSenseEngine": "default", + "idf.extensionActivationMode": "always", + "idf.openOcdConfigs": [ + "board/esp32s3-builtin.cfg" + ], + "idf.portWin": "COM10", + "idf.currentSetup": "C:\\esp\\v6.0.1\\esp-idf", + "idf.customExtraVars": { + "IDF_TARGET": "esp32s3" + }, + "clangd.path": "C:\\Espressif\\tools\\esp-clang\\esp-20.1.1_20250829\\esp-clang\\bin\\clangd.exe", + "clangd.arguments": [ + "--background-index", + "--query-driver=**", + "--compile-commands-dir=c:\\Users\\Jade\\ESP-IDF\\ESP32-TrainThing\\build" + ], + "idf.flashType": "UART" +} diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..09bc0f6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.22) + +set(COMPONENTS main) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(ESP32-TrainThing) diff --git a/dependencies.lock b/dependencies.lock new file mode 100644 index 0000000..09f4619 --- /dev/null +++ b/dependencies.lock @@ -0,0 +1,35 @@ +dependencies: + espressif/button: + component_hash: d0afa32f0e50d60bc0c6fc23f7eea98adc6b02cfe70b590bc52c23c506745287 + dependencies: + - name: espressif/cmake_utilities + registry_url: https://components.espressif.com + require: private + version: '*' + - name: idf + require: private + version: '>=4.0' + source: + registry_url: https://components.espressif.com/ + type: service + version: 4.1.6 + espressif/cmake_utilities: + component_hash: 05165f30922b422b4b90c08845e6d449329b97370fbd06309803d8cb539d79e3 + dependencies: + - name: idf + require: private + version: '>=4.1' + source: + registry_url: https://components.espressif.com + type: service + version: 1.1.1 + idf: + source: + type: idf + version: 6.0.1 +direct_dependencies: +- espressif/button +- idf +manifest_hash: 3c0e2b99c6461e4de9aa94d1d88f9dd122de14a3060761adde6da132d918ca1a +target: esp32s3 +version: 3.0.0 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..1b2bb02 --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.c" "buttons/buttons.c" + INCLUDE_DIRS "." "buttons") \ No newline at end of file diff --git a/main/buttons/buttons.c b/main/buttons/buttons.c new file mode 100644 index 0000000..74207bf --- /dev/null +++ b/main/buttons/buttons.c @@ -0,0 +1,43 @@ +#include "buttons.h" +#include "iot_button.h" +#include "button_gpio.h" +#include "esp_log.h" +#include "esp_err.h" +#include "esp_check.h" + +static const char* TAG = "TrainThing/buttons"; + +const action actions[] = { + {.btnAcc = FRONT_LIGHT_INCREASE, .name = "FRONT_LIGHT_INCREASE", .pin = 1, .shortcut = 'a'}, + {.btnAcc = FRONT_LIGHT_DECREASE, .name = "FRONT_LIGHT_DECREASE", .pin = 2, .shortcut = 'b'}, + {.btnAcc = BACK_LIGHT_INCREASE, .name = "BACK_LIGHT_INCREASE", .pin = 3, .shortcut = 'c'}, + {.btnAcc = BACK_LIGHT_DECREASE, .name = "BACK_LIGHT_DECREASE", .pin = 4, .shortcut = 'd'}, + {.btnAcc = CAB_LIGHT, .name = "CAB_LIGHT", .pin = 5, .shortcut = 'e'}, + {.btnAcc = WIPPERS, .name = "WIPPERS", .pin = 6, .shortcut = 'f'}, + {.btnAcc = SANDER, .name = "SANDER", .pin = 7, .shortcut = 'g'}, + {.btnAcc = STARTER, .name = "STARTER", .pin = 8, .shortcut = 'h'}, + {.btnAcc = CUT, .name = "CUT", .pin = 9, .shortcut = 'i'}, + {.btnAcc = HORN, .name = "HORN", .pin = 10, .shortcut = 'j'}, +}; +void button_event_cb(void *arg, void *data) +{ + action *a = (action*)data; + + button_event_t event = iot_button_get_event(arg); + ESP_LOGI(TAG, "%s pin:%d name:%s shortcut:%c", iot_button_get_event_str(event), (int)a->pin, (char*)a->name, (char)a->shortcut); +} + +void initialize_buttons(){ + const button_config_t btn_cfg = {0}; + button_handle_t btns[5]; + + for (size_t i = 0; i < sizeof(actions) / sizeof(actions[0]); i++) + { + const button_gpio_config_t btn_gpio_cfg = { + .gpio_num = actions[i].pin, + .active_level = 0, + }; + ESP_RETURN_VOID_ON_ERROR(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btns[i]), TAG, ""); + iot_button_register_cb(btns[i], BUTTON_PRESS_DOWN, NULL, button_event_cb, (void *) &actions[i]); + } +} \ No newline at end of file diff --git a/main/buttons/buttons.h b/main/buttons/buttons.h new file mode 100644 index 0000000..92ea30b --- /dev/null +++ b/main/buttons/buttons.h @@ -0,0 +1,30 @@ +#ifndef BUTTONS_H + +#define BUTTONS_H + +typedef enum { + FRONT_LIGHT_INCREASE, + FRONT_LIGHT_DECREASE, + BACK_LIGHT_INCREASE, + BACK_LIGHT_DECREASE, + CAB_LIGHT, + WIPPERS, + SANDER, + STARTER, + CUT, + HORN, +}buttonAction; + +typedef struct +{ + buttonAction btnAcc; + const char* name; + char shortcut; + int pin; + +}action; + +void button_event_cb(void *arg, void *data); + +void initialize_buttons(); +#endif // BUTTONS_H \ No newline at end of file diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..812ae52 --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,17 @@ +## IDF Component Manager Manifest File +dependencies: + ## Required IDF version + idf: + version: '>=4.1.0' + # # Put list of dependencies here + # # For components maintained by Espressif: + # component: "~1.0.0" + # # For 3rd party components: + # username/component: ">=1.0.0,<2.0.0" + # username2/component2: + # version: "~1.0.0" + # # For transient dependencies `public` flag can be set. + # # `public` flag doesn't have an effect dependencies of the `main` component. + # # All dependencies of `main` are public by default. + # public: true + espressif/button: ^4.1.6 diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..46dc3e9 --- /dev/null +++ b/main/main.c @@ -0,0 +1,87 @@ +#include +#include "iot_button.h" +#include "button_gpio.h" +#include "esp_log.h" +#include "esp_err.h" +#include "esp_check.h" +#include "buttons/buttons.h" + +static const char* TAG = "TrainThing"; + + + +/* + +static void button_single_click_cb(void *arg,void *usr_data) +{ + ESP_LOGI(TAG, "BUTTON_SINGLE_CLICK"); +} +static void button_long_press_1_cb(void *arg,void *usr_data) +{ + ESP_LOGI(TAG, "BUTTON_LONG_PRESS_START_1"); +} + +static void button_long_press_2_cb(void *arg,void *usr_data) +{ + ESP_LOGI(TAG, "BUTTON_LONG_PRESS_START_2"); +} + +static void button_click_2_cb(void *arg,void *usr_data) +{ + ESP_LOGI(TAG, "BUTTON 2 click"); +} +*/ + + +void app_main(void) +{ + initialize_buttons(); +/* + for (size_t i = 0; i < sizeof(btn_name_table) / sizeof(btn_name_table[0]); ++i) { + ESP_LOGI(TAG, "%s %d %c", btn_name_table[i].name, btn_name_table[i].btnAcc, btn_name_table[i].shortcut ); + + }*/ + + + + + + //ESP_RETURN_VOID_ON_ERROR(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btn), TAG, ""); + + + /*ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb,NULL), TAG, ""); + + button_event_args_t args ={.long_press.press_time = 2000}; + ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_LONG_PRESS_UP, NULL, button_event_cb, NULL), TAG, ""); + + args.long_press.press_time = 5000; + //ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_LONG_PRESS_UP, &args, button_long_press_2_cb, NULL), TAG, ""); + + ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_DOUBLE_CLICK, NULL, button_event_cb, NULL), TAG, ""); +*/ + + +/* + iot_button_register_cb(btn, BUTTON_PRESS_DOWN, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_PRESS_UP, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_PRESS_REPEAT, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_PRESS_REPEAT_DONE, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, NULL, button_event_cb, NULL); + + + button_event_args_t args = { + .multiple_clicks.clicks = 2, + }; + iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_event_cb, (void *)2); + args.multiple_clicks.clicks = 3; + iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_event_cb, (void *)3); + + iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, NULL, button_event_cb, NULL); + iot_button_register_cb(btn, BUTTON_PRESS_END, NULL, button_event_cb, NULL); +*/ +} + +