diff --git a/Firmware/main/CMakeLists.txt b/Firmware/main/CMakeLists.txt index 8bc63f6..beaa9c3 100644 --- a/Firmware/main/CMakeLists.txt +++ b/Firmware/main/CMakeLists.txt @@ -8,5 +8,6 @@ idf_component_register(SRCS src/disp_tty.cpp src/shutdowner.cpp src/buttons.cpp + src/power_helper.cpp PRIV_REQUIRES spi_flash esp_driver_i2c driver INCLUDE_DIRS "include") diff --git a/Firmware/main/include/config.hpp b/Firmware/main/include/config.hpp index 858c84f..0e15b6e 100644 --- a/Firmware/main/include/config.hpp +++ b/Firmware/main/include/config.hpp @@ -24,4 +24,6 @@ #define SHR_CLK GPIO_NUM_3 #define SHR_SH GPIO_NUM_2 +#define DIRECT_BTN GPIO_NUM_1 + #endif diff --git a/Firmware/main/include/power_helper.hpp b/Firmware/main/include/power_helper.hpp new file mode 100644 index 0000000..016d28a --- /dev/null +++ b/Firmware/main/include/power_helper.hpp @@ -0,0 +1,21 @@ +// +// Created by Stepan Usatiuk on 03.03.2025. +// + +#ifndef POWER_HELPER_HPP +#define POWER_HELPER_HPP + + +class PowerHelper { +public: + static PowerHelper& get(); + + bool is_slow(); + void set_slow(bool slow); + +private: + bool _slow = false; +}; + + +#endif // POWER_HELPER_HPP diff --git a/Firmware/main/include/shutdowner.hpp b/Firmware/main/include/shutdowner.hpp index c65ecf0..f4cfa60 100644 --- a/Firmware/main/include/shutdowner.hpp +++ b/Firmware/main/include/shutdowner.hpp @@ -9,9 +9,9 @@ class Shutdowner { public: static Shutdowner& get(); + private: Shutdowner(); - }; diff --git a/Firmware/main/src/buttons.cpp b/Firmware/main/src/buttons.cpp index 72204fe..6c1062f 100644 --- a/Firmware/main/src/buttons.cpp +++ b/Firmware/main/src/buttons.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include "freertos/FreeRTOS.h" @@ -20,6 +21,8 @@ Buttons& Buttons::get() { static void start_pooler(void* arg) { static_cast(arg)->pooler(); } +static void wake(void* arg) { PowerHelper::get().set_slow(false); } + Buttons::Buttons() { ESP_ERROR_CHECK(gpio_reset_pin(SHR_OUT)); ESP_ERROR_CHECK(gpio_reset_pin(SHR_CLK)); @@ -29,6 +32,11 @@ Buttons::Buttons() { ESP_ERROR_CHECK(gpio_set_direction(SHR_SH, GPIO_MODE_OUTPUT)); ESP_ERROR_CHECK(gpio_set_direction(SHR_CLK, GPIO_MODE_OUTPUT)); + ESP_ERROR_CHECK(gpio_set_direction(DIRECT_BTN, GPIO_MODE_INPUT)); + ESP_ERROR_CHECK(gpio_set_pull_mode(DIRECT_BTN, GPIO_FLOATING)); + ESP_ERROR_CHECK(gpio_set_intr_type(DIRECT_BTN, GPIO_INTR_NEGEDGE)); + gpio_isr_handler_add(DIRECT_BTN, &wake, nullptr); + xTaskCreate(&start_pooler, "ButtonsPooler", 2048, this, 1, &_pooler_task); } @@ -52,7 +60,7 @@ void Buttons::pooler() { ESP_ERROR_CHECK(gpio_set_level(SHR_CLK, 1)); } _current = new_val; - vTaskDelay(100 / portTICK_PERIOD_MS); + vTaskDelay((PowerHelper::get().is_slow() ? 1000 : 100) / portTICK_PERIOD_MS); } } uint8_t Buttons::get_pressed() { return _current; } diff --git a/Firmware/main/src/hello_world_main.cpp b/Firmware/main/src/hello_world_main.cpp index 502a717..52b0dfe 100644 --- a/Firmware/main/src/hello_world_main.cpp +++ b/Firmware/main/src/hello_world_main.cpp @@ -26,6 +26,7 @@ #include "i2c_global.hpp" #include +#include #include #include #include @@ -54,13 +55,16 @@ extern "C" void app_main() { int rx = 30, ry = 30; + int lastmove = 0; + while (true) { // SMD::clear(); // printf("Voltage: %f\n", BatMon::get_voltage()); DispTools::get().clear(); tty.reset(); - tty.fmt("{:.1f}mA {:.1f}V {:.1f}mAh", BatMon::get().get_current(), BatMon::get().get_voltage(), - BatMon::get().get_charge()); + bool slow = PowerHelper::get().is_slow(); + tty.fmt("{:.1f}mA {:.1f}V {:.1f}mAh {}", BatMon::get().get_current(), BatMon::get().get_voltage(), + BatMon::get().get_charge(), slow ? "S" : ""); uint8_t pressed = Buttons::get().get_pressed(); if (pressed & L1) rx -= 5; @@ -71,6 +75,18 @@ extern "C" void app_main() { if (pressed & R4) rx += 5; + if (pressed == 0 && !slow) + lastmove++; + else if (pressed != 0) { + lastmove = 0; + PowerHelper::get().set_slow(false); + } + + if (lastmove > 20) { + lastmove = 0; + PowerHelper::get().set_slow(true); + } + if (rx < 30) rx = 30; if (rx > 370) @@ -83,7 +99,7 @@ extern "C" void app_main() { DispTools::get().draw_circle(rx, ry, 20); // printf("Restarting in %d seconds...\n", i); DispTools::get().draw_to_display(); - vTaskDelay(35 / portTICK_PERIOD_MS); + vTaskDelay((PowerHelper::get().is_slow() ? 1000 : 30) / portTICK_PERIOD_MS); } // printf("Restarting now.\n"); // fflush(stdout); diff --git a/Firmware/main/src/power_helper.cpp b/Firmware/main/src/power_helper.cpp new file mode 100644 index 0000000..a99d694 --- /dev/null +++ b/Firmware/main/src/power_helper.cpp @@ -0,0 +1,12 @@ +// +// Created by Stepan Usatiuk on 03.03.2025. +// + +#include "power_helper.hpp" + +PowerHelper& PowerHelper::get() { + static PowerHelper powerHelper; + return powerHelper; +} +bool PowerHelper::is_slow() { return _slow; } +void PowerHelper::set_slow(bool slow) { _slow = slow; } diff --git a/Firmware/main/src/shutdowner.cpp b/Firmware/main/src/shutdowner.cpp index 6af253d..2073891 100644 --- a/Firmware/main/src/shutdowner.cpp +++ b/Firmware/main/src/shutdowner.cpp @@ -15,8 +15,8 @@ Shutdowner& Shutdowner::get() { static void shutdown(void* arg) { // printf("Shutting down...\n"); - ESP_ERROR_CHECK(gpio_hold_dis(PWR_KILL)); - ESP_ERROR_CHECK(gpio_set_level(PWR_KILL, 0)); + // ESP_ERROR_CHECK(gpio_hold_dis(PWR_KILL)); + // ESP_ERROR_CHECK(gpio_set_level(PWR_KILL, 0)); } Shutdowner::Shutdowner() {