mirror of
https://github.com/usatiuk/cardboy.git
synced 2025-10-29 07:37:48 +01:00
some power stuff
This commit is contained in:
@@ -8,5 +8,6 @@ idf_component_register(SRCS
|
|||||||
src/disp_tty.cpp
|
src/disp_tty.cpp
|
||||||
src/shutdowner.cpp
|
src/shutdowner.cpp
|
||||||
src/buttons.cpp
|
src/buttons.cpp
|
||||||
|
src/power_helper.cpp
|
||||||
PRIV_REQUIRES spi_flash esp_driver_i2c driver
|
PRIV_REQUIRES spi_flash esp_driver_i2c driver
|
||||||
INCLUDE_DIRS "include")
|
INCLUDE_DIRS "include")
|
||||||
|
|||||||
@@ -24,4 +24,6 @@
|
|||||||
#define SHR_CLK GPIO_NUM_3
|
#define SHR_CLK GPIO_NUM_3
|
||||||
#define SHR_SH GPIO_NUM_2
|
#define SHR_SH GPIO_NUM_2
|
||||||
|
|
||||||
|
#define DIRECT_BTN GPIO_NUM_1
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
21
Firmware/main/include/power_helper.hpp
Normal file
21
Firmware/main/include/power_helper.hpp
Normal 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
|
||||||
@@ -9,9 +9,9 @@
|
|||||||
class Shutdowner {
|
class Shutdowner {
|
||||||
public:
|
public:
|
||||||
static Shutdowner& get();
|
static Shutdowner& get();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Shutdowner();
|
Shutdowner();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <driver/gpio.h>
|
#include <driver/gpio.h>
|
||||||
#include <esp_err.h>
|
#include <esp_err.h>
|
||||||
|
#include <power_helper.hpp>
|
||||||
#include <rom/ets_sys.h>
|
#include <rom/ets_sys.h>
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.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 start_pooler(void* arg) { static_cast<Buttons*>(arg)->pooler(); }
|
||||||
|
|
||||||
|
static void wake(void* arg) { PowerHelper::get().set_slow(false); }
|
||||||
|
|
||||||
Buttons::Buttons() {
|
Buttons::Buttons() {
|
||||||
ESP_ERROR_CHECK(gpio_reset_pin(SHR_OUT));
|
ESP_ERROR_CHECK(gpio_reset_pin(SHR_OUT));
|
||||||
ESP_ERROR_CHECK(gpio_reset_pin(SHR_CLK));
|
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_SH, GPIO_MODE_OUTPUT));
|
||||||
ESP_ERROR_CHECK(gpio_set_direction(SHR_CLK, 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);
|
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));
|
ESP_ERROR_CHECK(gpio_set_level(SHR_CLK, 1));
|
||||||
}
|
}
|
||||||
_current = new_val;
|
_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; }
|
uint8_t Buttons::get_pressed() { return _current; }
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
#include "i2c_global.hpp"
|
#include "i2c_global.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <power_helper.hpp>
|
||||||
#include <shutdowner.hpp>
|
#include <shutdowner.hpp>
|
||||||
#include <spi_global.hpp>
|
#include <spi_global.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -54,13 +55,16 @@ extern "C" void app_main() {
|
|||||||
|
|
||||||
int rx = 30, ry = 30;
|
int rx = 30, ry = 30;
|
||||||
|
|
||||||
|
int lastmove = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// SMD::clear();
|
// SMD::clear();
|
||||||
// printf("Voltage: %f\n", BatMon::get_voltage());
|
// printf("Voltage: %f\n", BatMon::get_voltage());
|
||||||
DispTools::get().clear();
|
DispTools::get().clear();
|
||||||
tty.reset();
|
tty.reset();
|
||||||
tty.fmt("{:.1f}mA {:.1f}V {:.1f}mAh", BatMon::get().get_current(), BatMon::get().get_voltage(),
|
bool slow = PowerHelper::get().is_slow();
|
||||||
BatMon::get().get_charge());
|
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();
|
uint8_t pressed = Buttons::get().get_pressed();
|
||||||
if (pressed & L1)
|
if (pressed & L1)
|
||||||
rx -= 5;
|
rx -= 5;
|
||||||
@@ -71,6 +75,18 @@ extern "C" void app_main() {
|
|||||||
if (pressed & R4)
|
if (pressed & R4)
|
||||||
rx += 5;
|
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)
|
if (rx < 30)
|
||||||
rx = 30;
|
rx = 30;
|
||||||
if (rx > 370)
|
if (rx > 370)
|
||||||
@@ -83,7 +99,7 @@ extern "C" void app_main() {
|
|||||||
DispTools::get().draw_circle(rx, ry, 20);
|
DispTools::get().draw_circle(rx, ry, 20);
|
||||||
// printf("Restarting in %d seconds...\n", i);
|
// printf("Restarting in %d seconds...\n", i);
|
||||||
DispTools::get().draw_to_display();
|
DispTools::get().draw_to_display();
|
||||||
vTaskDelay(35 / portTICK_PERIOD_MS);
|
vTaskDelay((PowerHelper::get().is_slow() ? 1000 : 30) / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
// printf("Restarting now.\n");
|
// printf("Restarting now.\n");
|
||||||
// fflush(stdout);
|
// fflush(stdout);
|
||||||
|
|||||||
12
Firmware/main/src/power_helper.cpp
Normal file
12
Firmware/main/src/power_helper.cpp
Normal 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; }
|
||||||
@@ -15,8 +15,8 @@ Shutdowner& Shutdowner::get() {
|
|||||||
|
|
||||||
static void shutdown(void* arg) {
|
static void shutdown(void* arg) {
|
||||||
// printf("Shutting down...\n");
|
// printf("Shutting down...\n");
|
||||||
ESP_ERROR_CHECK(gpio_hold_dis(PWR_KILL));
|
// ESP_ERROR_CHECK(gpio_hold_dis(PWR_KILL));
|
||||||
ESP_ERROR_CHECK(gpio_set_level(PWR_KILL, 0));
|
// ESP_ERROR_CHECK(gpio_set_level(PWR_KILL, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
Shutdowner::Shutdowner() {
|
Shutdowner::Shutdowner() {
|
||||||
|
|||||||
Reference in New Issue
Block a user