get rid of powerhelper

This commit is contained in:
2025-10-12 22:54:13 +02:00
parent df55b8f2e1
commit 031ff1952b
13 changed files with 2 additions and 142 deletions

View File

@@ -8,7 +8,6 @@ idf_component_register(
"src/display.cpp" "src/display.cpp"
"src/fs_helper.cpp" "src/fs_helper.cpp"
"src/i2c_global.cpp" "src/i2c_global.cpp"
"src/power_helper.cpp"
"src/shutdowner.cpp" "src/shutdowner.cpp"
"src/spi_global.cpp" "src/spi_global.cpp"
INCLUDE_DIRS INCLUDE_DIRS

View File

@@ -1,30 +0,0 @@
//
// Created by Stepan Usatiuk on 03.03.2025.
//
#ifndef POWER_HELPER_HPP
#define POWER_HELPER_HPP
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
class PowerHelper {
public:
static PowerHelper& get();
bool is_slow() const;
void set_slow(bool slow);
BaseType_t reset_slow_isr(BaseType_t* xHigherPriorityTaskWoken);
void delay(int slow_ms, int normal_ms);
void install_isr();
private:
PowerHelper();
bool _slow = false;
EventGroupHandle_t _event_group;
};
#endif // POWER_HELPER_HPP

View File

@@ -59,7 +59,6 @@ private:
class StorageService; class StorageService;
class RandomService; class RandomService;
class HighResClockService; class HighResClockService;
class PowerService;
class FilesystemService; class FilesystemService;
class LoopHooksService; class LoopHooksService;
@@ -68,7 +67,6 @@ private:
std::unique_ptr<StorageService> storageService; std::unique_ptr<StorageService> storageService;
std::unique_ptr<RandomService> randomService; std::unique_ptr<RandomService> randomService;
std::unique_ptr<HighResClockService> highResClockService; std::unique_ptr<HighResClockService> highResClockService;
std::unique_ptr<PowerService> powerService;
std::unique_ptr<FilesystemService> filesystemService; std::unique_ptr<FilesystemService> filesystemService;
std::unique_ptr<EventBus> eventBus; std::unique_ptr<EventBus> eventBus;
std::unique_ptr<LoopHooksService> loopHooksService; std::unique_ptr<LoopHooksService> loopHooksService;

View File

@@ -4,8 +4,6 @@
#include "cardboy/backend/esp/bat_mon.hpp" #include "cardboy/backend/esp/bat_mon.hpp"
#include "cardboy/backend/esp/power_helper.hpp"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@@ -105,7 +103,7 @@ void BatMon::pooler() {
_charge = capToMah(ReadRegister(0x05)); _charge = capToMah(ReadRegister(0x05));
_current = regToCurrent(ReadRegister(0x0B)); _current = regToCurrent(ReadRegister(0x0B));
_voltage = regToVoltage(ReadRegister(0x09)); _voltage = regToVoltage(ReadRegister(0x09));
PowerHelper::get().delay(10000, 1000); vTaskDelay(pdMS_TO_TICKS(10000));
if (_voltage < 3.0f) { if (_voltage < 3.0f) {
Shutdowner::get().shutdown(); Shutdowner::get().shutdown();
} }

View File

@@ -7,7 +7,6 @@
#include <driver/gpio.h> #include <driver/gpio.h>
#include <esp_err.h> #include <esp_err.h>
#include <rom/ets_sys.h> #include <rom/ets_sys.h>
#include "cardboy/backend/esp/power_helper.hpp"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@@ -38,12 +37,10 @@ static void wakeup(void* arg) {
ESP_ERROR_CHECK(gpio_wakeup_enable(EXP_INT, GPIO_INTR_HIGH_LEVEL)); ESP_ERROR_CHECK(gpio_wakeup_enable(EXP_INT, GPIO_INTR_HIGH_LEVEL));
is_on_low = false; is_on_low = false;
BaseType_t xResult = pdFAIL;
BaseType_t xHigherPriorityTaskWoken = pdFALSE; BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(Buttons::get()._pooler_task, 0, eNoAction, &xHigherPriorityTaskWoken); xTaskNotifyFromISR(Buttons::get()._pooler_task, 0, eNoAction, &xHigherPriorityTaskWoken);
PowerHelper::get().reset_slow_isr(&xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
} else { } else {
ESP_ERROR_CHECK(gpio_set_intr_type(EXP_INT, GPIO_INTR_LOW_LEVEL)); ESP_ERROR_CHECK(gpio_set_intr_type(EXP_INT, GPIO_INTR_LOW_LEVEL));

View File

@@ -7,7 +7,6 @@
#include "cardboy/backend/esp/display.hpp" #include "cardboy/backend/esp/display.hpp"
#include "cardboy/backend/esp/fs_helper.hpp" #include "cardboy/backend/esp/fs_helper.hpp"
#include "cardboy/backend/esp/i2c_global.hpp" #include "cardboy/backend/esp/i2c_global.hpp"
#include "cardboy/backend/esp/power_helper.hpp"
#include "cardboy/backend/esp/shutdowner.hpp" #include "cardboy/backend/esp/shutdowner.hpp"
#include "cardboy/backend/esp/spi_global.hpp" #include "cardboy/backend/esp/spi_global.hpp"
@@ -112,12 +111,6 @@ public:
[[nodiscard]] std::uint64_t micros() override { return static_cast<std::uint64_t>(esp_timer_get_time()); } [[nodiscard]] std::uint64_t micros() override { return static_cast<std::uint64_t>(esp_timer_get_time()); }
}; };
class EspRuntime::PowerService final : public cardboy::sdk::IPowerManager {
public:
void setSlowMode(bool enable) override { PowerHelper::get().set_slow(enable); }
[[nodiscard]] bool isSlowMode() const override { return PowerHelper::get().is_slow(); }
};
class EspRuntime::FilesystemService final : public cardboy::sdk::IFilesystem { class EspRuntime::FilesystemService final : public cardboy::sdk::IFilesystem {
public: public:
bool mount() override { return FsHelper::get().mount() == ESP_OK; } bool mount() override { return FsHelper::get().mount() == ESP_OK; }
@@ -141,7 +134,6 @@ EspRuntime::EspRuntime() : framebuffer(), input(), clock() {
storageService = std::make_unique<StorageService>(); storageService = std::make_unique<StorageService>();
randomService = std::make_unique<RandomService>(); randomService = std::make_unique<RandomService>();
highResClockService = std::make_unique<HighResClockService>(); highResClockService = std::make_unique<HighResClockService>();
powerService = std::make_unique<PowerService>();
filesystemService = std::make_unique<FilesystemService>(); filesystemService = std::make_unique<FilesystemService>();
eventBus = std::make_unique<EventBus>(); eventBus = std::make_unique<EventBus>();
loopHooksService = std::make_unique<LoopHooksService>(); loopHooksService = std::make_unique<LoopHooksService>();
@@ -151,7 +143,6 @@ EspRuntime::EspRuntime() : framebuffer(), input(), clock() {
services.storage = storageService.get(); services.storage = storageService.get();
services.random = randomService.get(); services.random = randomService.get();
services.highResClock = highResClockService.get(); services.highResClock = highResClockService.get();
services.powerManager = powerService.get();
services.filesystem = filesystemService.get(); services.filesystem = filesystemService.get();
services.eventBus = eventBus.get(); services.eventBus = eventBus.get();
services.loopHooks = loopHooksService.get(); services.loopHooks = loopHooksService.get();
@@ -171,7 +162,6 @@ void EspRuntime::initializeHardware() {
ensureNvsInit(); ensureNvsInit();
PowerHelper::get();
Shutdowner::get(); Shutdowner::get();
Buttons::get(); Buttons::get();
@@ -181,7 +171,6 @@ void EspRuntime::initializeHardware() {
} }
Shutdowner::get().install_isr(); Shutdowner::get().install_isr();
PowerHelper::get().install_isr();
Buttons::get().install_isr(); Buttons::get().install_isr();
I2cGlobal::get(); I2cGlobal::get();
@@ -235,7 +224,7 @@ std::uint32_t EspClock::millis_impl() {
void EspClock::sleep_ms_impl(std::uint32_t ms) { void EspClock::sleep_ms_impl(std::uint32_t ms) {
if (ms == 0) if (ms == 0)
return; return;
PowerHelper::get().delay(static_cast<int>(ms), static_cast<int>(ms)); vTaskDelay(pdMS_TO_TICKS(ms));
} }
} // namespace cardboy::backend::esp } // namespace cardboy::backend::esp

View File

@@ -1,58 +0,0 @@
//
// Created by Stepan Usatiuk on 03.03.2025.
//
#include "cardboy/backend/esp/power_helper.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
PowerHelper& PowerHelper::get() {
static PowerHelper powerHelper;
return powerHelper;
}
bool PowerHelper::is_slow() const { return _slow; }
void PowerHelper::set_slow(bool slow) {
_slow = slow;
if (_slow) {
xEventGroupClearBits(_event_group, 1);
} else {
xEventGroupSetBits(_event_group, 1);
}
}
BaseType_t PowerHelper::reset_slow_isr(BaseType_t* xHigherPriorityTaskWoken) {
_slow = false;
return xEventGroupSetBitsFromISR(_event_group, 1, xHigherPriorityTaskWoken);
}
static void wakeup(void* arg) {
BaseType_t xHigherPriorityTaskWoken, xResult;
xHigherPriorityTaskWoken = pdFALSE;
xResult = static_cast<PowerHelper*>(arg)->reset_slow_isr(&xHigherPriorityTaskWoken);
if (xResult != pdFAIL) {
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
};
}
PowerHelper::PowerHelper() : _event_group(xEventGroupCreate()) { set_slow(false); }
void PowerHelper::delay(int slow_ms, int normal_ms) {
if (is_slow()) {
auto cur_ticks = xTaskGetTickCount();
TickType_t to_wait = slow_ms / portTICK_PERIOD_MS;
TickType_t to_wait_normal = normal_ms / portTICK_PERIOD_MS;
auto expected_ticks = cur_ticks + to_wait_normal;
xEventGroupWaitBits(_event_group, 1, pdFALSE, pdTRUE, to_wait);
auto realTicks = xTaskGetTickCount();
if (realTicks < expected_ticks) {
vTaskDelay(expected_ticks - realTicks);
}
} else {
vTaskDelay(normal_ms / portTICK_PERIOD_MS);
}
}
void PowerHelper::install_isr() {
// gpio_isr_handler_add(EXP_INT, wakeup, this);
}

View File

@@ -185,8 +185,6 @@ private:
state.paused = false; state.paused = false;
dirty = true; dirty = true;
scheduleDropTimer(); scheduleDropTimer();
if (auto* power = context.powerManager())
power->setSlowMode(false);
} }
void handleButtons(const AppButtonEvent& evt) { void handleButtons(const AppButtonEvent& evt) {
@@ -204,8 +202,6 @@ private:
reset(); reset();
} else { } else {
state.paused = !state.paused; state.paused = !state.paused;
if (auto* power = context.powerManager())
power->setSlowMode(state.paused);
} }
dirty = true; dirty = true;
} }
@@ -387,8 +383,6 @@ private:
cancelDropTimer(); cancelDropTimer();
if (auto* buzzer = context.buzzer()) if (auto* buzzer = context.buzzer())
buzzer->beepGameOver(); buzzer->beepGameOver();
if (auto* power = context.powerManager())
power->setSlowMode(true);
} else { } else {
if (auto* buzzer = context.buzzer()) if (auto* buzzer = context.buzzer())
buzzer->beepLock(); buzzer->beepLock();

View File

@@ -60,14 +60,6 @@ public:
[[nodiscard]] virtual std::uint64_t micros() = 0; [[nodiscard]] virtual std::uint64_t micros() = 0;
}; };
class IPowerManager {
public:
virtual ~IPowerManager() = default;
virtual void setSlowMode(bool enable) = 0;
[[nodiscard]] virtual bool isSlowMode() const = 0;
};
class IFilesystem { class IFilesystem {
public: public:
virtual ~IFilesystem() = default; virtual ~IFilesystem() = default;
@@ -83,7 +75,6 @@ struct Services {
IStorage* storage = nullptr; IStorage* storage = nullptr;
IRandom* random = nullptr; IRandom* random = nullptr;
IHighResClock* highResClock = nullptr; IHighResClock* highResClock = nullptr;
IPowerManager* powerManager = nullptr;
IFilesystem* filesystem = nullptr; IFilesystem* filesystem = nullptr;
IEventBus* eventBus = nullptr; IEventBus* eventBus = nullptr;
ILoopHooks* loopHooks = nullptr; ILoopHooks* loopHooks = nullptr;

View File

@@ -73,15 +73,6 @@ private:
const std::chrono::steady_clock::time_point start; const std::chrono::steady_clock::time_point start;
}; };
class DesktopPowerManager final : public cardboy::sdk::IPowerManager {
public:
void setSlowMode(bool enable) override { slowMode = enable; }
[[nodiscard]] bool isSlowMode() const override { return slowMode; }
private:
bool slowMode = false;
};
class DesktopFilesystem final : public cardboy::sdk::IFilesystem { class DesktopFilesystem final : public cardboy::sdk::IFilesystem {
public: public:
DesktopFilesystem(); DesktopFilesystem();
@@ -194,7 +185,6 @@ private:
DesktopStorage storageService; DesktopStorage storageService;
DesktopRandom randomService; DesktopRandom randomService;
DesktopHighResClock highResService; DesktopHighResClock highResService;
DesktopPowerManager powerService;
DesktopFilesystem filesystemService; DesktopFilesystem filesystemService;
DesktopEventBus eventBusService; DesktopEventBus eventBusService;
cardboy::sdk::Services services{}; cardboy::sdk::Services services{};

View File

@@ -241,7 +241,6 @@ DesktopRuntime::DesktopRuntime() :
services.storage = &storageService; services.storage = &storageService;
services.random = &randomService; services.random = &randomService;
services.highResClock = &highResService; services.highResClock = &highResService;
services.powerManager = &powerService;
services.filesystem = &filesystemService; services.filesystem = &filesystemService;
services.eventBus = &eventBusService; services.eventBus = &eventBusService;
services.loopHooks = nullptr; services.loopHooks = nullptr;

View File

@@ -61,7 +61,6 @@ struct AppContext {
[[nodiscard]] IStorage* storage() const { return services ? services->storage : nullptr; } [[nodiscard]] IStorage* storage() const { return services ? services->storage : nullptr; }
[[nodiscard]] IRandom* random() const { return services ? services->random : nullptr; } [[nodiscard]] IRandom* random() const { return services ? services->random : nullptr; }
[[nodiscard]] IHighResClock* highResClock() const { return services ? services->highResClock : nullptr; } [[nodiscard]] IHighResClock* highResClock() const { return services ? services->highResClock : nullptr; }
[[nodiscard]] IPowerManager* powerManager() const { return services ? services->powerManager : nullptr; }
[[nodiscard]] IFilesystem* filesystem() const { return services ? services->filesystem : nullptr; } [[nodiscard]] IFilesystem* filesystem() const { return services ? services->filesystem : nullptr; }
[[nodiscard]] IEventBus* eventBus() const { return services ? services->eventBus : nullptr; } [[nodiscard]] IEventBus* eventBus() const { return services ? services->eventBus : nullptr; }
[[nodiscard]] ILoopHooks* loopHooks() const { return services ? services->loopHooks : nullptr; } [[nodiscard]] ILoopHooks* loopHooks() const { return services ? services->loopHooks : nullptr; }

View File

@@ -63,12 +63,6 @@ std::string StatusBar::prepareRightText() const {
right.assign(buf); right.assign(buf);
} }
if (services_->powerManager && services_->powerManager->isSlowMode()) {
if (!right.empty())
right.append(" ");
right.append("SLOW");
}
if (services_->buzzer && services_->buzzer->isMuted()) { if (services_->buzzer && services_->buzzer->isMuted()) {
if (!right.empty()) if (!right.empty())
right.append(" "); right.append(" ");