some power stuff

This commit is contained in:
2025-03-03 01:30:23 +01:00
parent 996f922e35
commit db23b0eb80
8 changed files with 67 additions and 7 deletions

View File

@@ -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")

View File

@@ -24,4 +24,6 @@
#define SHR_CLK GPIO_NUM_3
#define SHR_SH GPIO_NUM_2
#define DIRECT_BTN GPIO_NUM_1
#endif

View File

@@ -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

View File

@@ -9,9 +9,9 @@
class Shutdowner {
public:
static Shutdowner& get();
private:
Shutdowner();
};

View File

@@ -6,6 +6,7 @@
#include <driver/gpio.h>
#include <esp_err.h>
#include <power_helper.hpp>
#include <rom/ets_sys.h>
#include "freertos/FreeRTOS.h"
@@ -20,6 +21,8 @@ Buttons& Buttons::get() {
static void start_pooler(void* arg) { static_cast<Buttons*>(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; }

View File

@@ -26,6 +26,7 @@
#include "i2c_global.hpp"
#include <memory>
#include <power_helper.hpp>
#include <shutdowner.hpp>
#include <spi_global.hpp>
#include <string>
@@ -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);

View File

@@ -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; }

View File

@@ -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() {