some buttons on screen

This commit is contained in:
2025-03-03 01:06:49 +01:00
parent bebae2a367
commit 3fc93c7441
8 changed files with 138 additions and 12 deletions

View File

@@ -7,5 +7,6 @@ idf_component_register(SRCS
src/disp_tools.cpp
src/disp_tty.cpp
src/shutdowner.cpp
src/buttons.cpp
PRIV_REQUIRES spi_flash esp_driver_i2c driver
INCLUDE_DIRS "include")

View File

@@ -0,0 +1,36 @@
//
// Created by Stepan Usatiuk on 02.03.2025.
//
#ifndef BUTTONS_HPP
#define BUTTONS_HPP
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
typedef enum {
L1 = 1 << 1,
L2 = 1 << 6,
L3 = 1 << 0,
L4 = 1 << 7,
R1 = 1 << 5,
R2 = 1 << 2,
R3 = 1 << 4,
R4 = 1 << 3,
} btn_num;
class Buttons {
public:
static Buttons& get();
void pooler(); // FIXME:
uint8_t get_pressed();
private:
Buttons();
volatile uint8_t _current;
TaskHandle_t _pooler_task;
};
#endif // BUTTONS_HPP

View File

@@ -20,4 +20,8 @@
#define PWR_INT GPIO_NUM_10
#define PWR_KILL GPIO_NUM_12
#define SHR_OUT GPIO_NUM_23
#define SHR_CLK GPIO_NUM_3
#define SHR_SH GPIO_NUM_2
#endif

View File

@@ -16,7 +16,7 @@ class FbTty {
public:
void putchar(char c);
void putstr(const char* str);
char readchar();
void reset();
template<typename... Args>
auto fmt(std::format_string<Args...> fmt, Args&&... args) {

View File

@@ -49,7 +49,7 @@ void BatMon::pooler() {
current /= 50;
current /= 4;
_current = current;
vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
vTaskDelay(1 * 1000 / portTICK_PERIOD_MS);
}
}

View File

@@ -0,0 +1,58 @@
//
// Created by Stepan Usatiuk on 02.03.2025.
//
#include "buttons.hpp"
#include <driver/gpio.h>
#include <esp_err.h>
#include <rom/ets_sys.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "config.hpp"
Buttons& Buttons::get() {
static Buttons buttons;
return buttons;
}
static void start_pooler(void* arg) { static_cast<Buttons*>(arg)->pooler(); }
Buttons::Buttons() {
ESP_ERROR_CHECK(gpio_reset_pin(SHR_OUT));
ESP_ERROR_CHECK(gpio_reset_pin(SHR_CLK));
ESP_ERROR_CHECK(gpio_reset_pin(SHR_SH));
ESP_ERROR_CHECK(gpio_set_direction(SHR_OUT, GPIO_MODE_INPUT));
ESP_ERROR_CHECK(gpio_set_pull_mode(SHR_OUT, GPIO_FLOATING));
ESP_ERROR_CHECK(gpio_set_direction(SHR_SH, GPIO_MODE_OUTPUT));
ESP_ERROR_CHECK(gpio_set_direction(SHR_CLK, GPIO_MODE_OUTPUT));
xTaskCreate(&start_pooler, "ButtonsPooler", 2048, this, 1, &_pooler_task);
}
static void delay(unsigned long long loop) {
for (unsigned long long i = 0; i < loop; i++) {
asm volatile("nop");
}
}
void Buttons::pooler() {
while (true) {
ESP_ERROR_CHECK(gpio_set_level(SHR_SH, 0));
ESP_ERROR_CHECK(gpio_set_level(SHR_SH, 1));
uint8_t new_val = 0;
for (int i = 0; i < 8; i++) {
ESP_ERROR_CHECK(gpio_set_level(SHR_CLK, 0));
new_val |= gpio_get_level(SHR_OUT) << i;
ESP_ERROR_CHECK(gpio_set_level(SHR_CLK, 1));
}
_current = new_val;
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
uint8_t Buttons::get_pressed() { return _current; }

View File

@@ -19,7 +19,10 @@ void FbTty::draw_char(int col, int row) {
}
}
}
void FbTty::reset() {
_cur_col = 0;
_cur_row = 0;
}
void FbTty::putchar(char c) {
if (c == '\n') {
next_row();

View File

@@ -4,6 +4,7 @@
* SPDX-License-Identifier: CC0-1.0
*/
#include <buttons.hpp>
#include <cstdint>
#include <disp_tools.hpp>
#include <disp_tty.hpp>
@@ -31,12 +32,14 @@
FbTty tty;
extern "C" void app_main() {
// esp_pm_config_t pm_config = {
// .max_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, .min_freq_mhz = 16, .light_sleep_enable = true};
// ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
esp_pm_config_t pm_config = {
.max_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, .min_freq_mhz = 16, .light_sleep_enable = true};
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
printf("Hello world!\n");
Shutdowner::get();
Buttons::get();
I2cGlobal::get();
BatMon::get();
SpiGlobal::get();
@@ -49,17 +52,38 @@ extern "C" void app_main() {
tty.putstr("Hello\nworld!");
DispTools::get().draw_to_display();
int rx = 30, ry = 30;
while (true) {
// SMD::clear();
// printf("Voltage: %f\n", BatMon::get_voltage());
tty.fmt("Current: {}\n", BatMon::get().get_current());
tty.fmt("Voltage: {}\n", BatMon::get().get_voltage());
// printf("Current: %f\n", BatMon::get_current());
// printf("Charge: %f\n", BatMon::get_charge());
tty.fmt("Charge: {}\n", BatMon::get().get_charge());
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());
uint8_t pressed = Buttons::get().get_pressed();
if (pressed & L1)
rx -= 5;
if (pressed & L4)
ry += 5;
if (pressed & R1)
ry -= 5;
if (pressed & R4)
rx += 5;
if (rx < 30)
rx = 30;
if (rx > 370)
rx = 370;
if (ry < 30)
ry = 30;
if (ry > 210)
ry = 210;
// tty.fmt("Button: {}", pressed);
DispTools::get().draw_circle(rx, ry, 20);
// printf("Restarting in %d seconds...\n", i);
DispTools::get().draw_to_display();
vTaskDelay(2000 / portTICK_PERIOD_MS);
vTaskDelay(35 / portTICK_PERIOD_MS);
}
// printf("Restarting now.\n");
// fflush(stdout);