handle mouse events

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-09 00:17:54 +02:00
parent 60fccfeccc
commit 266114d046
53 changed files with 1144 additions and 1008 deletions

View File

@@ -1,42 +1,43 @@
#include "button_handling.h"
#include "sdkconfig.h"
#include <stdio.h>
#include <string.h>
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_mac.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#include <stdio.h>
#include <string.h>
#include "common.h"
static const char* TAG = "button_handling";
static const char *TAG = "button_handling";
#define DEBOUNCE_TIME_MS (500)
#define BUTTON_QUEUE_LENGTH 5
#define BUTTON_QUEUE_LENGTH 5
#define BUTTON_QUEUE_ITEM_SIZE sizeof(uint8_t)
#define WLED_GPIO GPIO_NUM_47
#define WLED_RMT_CHANNEL RMT_CHANNEL_0
#define WLED_RESOLUTION_HZ (10000000)
#define WLED_GPIO GPIO_NUM_47
#define WLED_RMT_CHANNEL RMT_CHANNEL_0
#define WLED_RESOLUTION_HZ (10000000)
#define WLED_ON_DURATION_MS (100)
#define NUM_LEDS (1)
#define NUM_LEDS (1)
const uint8_t pins[] =
{BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK};
const uint8_t pins[] = {BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK};
QueueHandle_t buttonQueue = NULL;
volatile int64_t last_interrupt_time = 0;
void IRAM_ATTR button_isr_handler(void* arg) {
void IRAM_ATTR button_isr_handler(void *arg)
{
int64_t now = esp_timer_get_time();
if((now - last_interrupt_time) > (DEBOUNCE_TIME_MS * 1000)) {
if ((now - last_interrupt_time) > (DEBOUNCE_TIME_MS * 1000))
{
last_interrupt_time = now;
uintptr_t pin_value = (uintptr_t)arg;
@@ -45,26 +46,31 @@ void IRAM_ATTR button_isr_handler(void* arg) {
xQueueSendFromISR(buttonQueue, &press_signal, &higherPriorityTaskWoken);
if(higherPriorityTaskWoken) {
if (higherPriorityTaskWoken)
{
portYIELD_FROM_ISR();
}
}
}
void setupButtons(void) {
void setupButtons(void)
{
buttonQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, BUTTON_QUEUE_ITEM_SIZE);
if(buttonQueue == NULL) {
if (buttonQueue == NULL)
{
ESP_LOGE(TAG, "Error while Queue creation!");
return;
}
ESP_LOGI(TAG, "Button Queue created.");
esp_err_t isr_service_err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
if(isr_service_err != ESP_OK && isr_service_err != ESP_ERR_INVALID_STATE) {
if (isr_service_err != ESP_OK && isr_service_err != ESP_ERR_INVALID_STATE)
{
ESP_LOGE(TAG, "Error in gpio_install_isr_service: %s", esp_err_to_name(isr_service_err));
}
for(int i = 0; i < sizeof(pins) / sizeof(pins[0]); i++) {
for (int i = 0; i < sizeof(pins) / sizeof(pins[0]); i++)
{
const uint8_t pin = pins[i];
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_NEGEDGE;
@@ -75,8 +81,9 @@ void setupButtons(void) {
gpio_config(&io_conf);
uintptr_t pin_as_arg = (uintptr_t)pin;
esp_err_t add_isr_err = gpio_isr_handler_add(pin, button_isr_handler, (void*)pin_as_arg);
if(add_isr_err != ESP_OK) {
esp_err_t add_isr_err = gpio_isr_handler_add(pin, button_isr_handler, (void *)pin_as_arg);
if (add_isr_err != ESP_OK)
{
ESP_LOGE(TAG, "Error in gpio_isr_handler_add: %s", esp_err_to_name(add_isr_err));
}

View File

@@ -1,9 +1,10 @@
#pragma once
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
void setupButtons(void);
void setupButtons(void);
#ifdef __cplusplus
}
#endif

View File

@@ -1,8 +1,8 @@
#pragma once
#define BUTTON_UP GPIO_NUM_1
#define BUTTON_DOWN GPIO_NUM_6
#define BUTTON_LEFT GPIO_NUM_3
#define BUTTON_RIGHT GPIO_NUM_5
#define BUTTON_UP GPIO_NUM_1
#define BUTTON_DOWN GPIO_NUM_6
#define BUTTON_LEFT GPIO_NUM_3
#define BUTTON_RIGHT GPIO_NUM_5
#define BUTTON_SELECT GPIO_NUM_18
#define BUTTON_BACK GPIO_NUM_16
#define BUTTON_BACK GPIO_NUM_16

View File

@@ -1,19 +1,23 @@
#include "setup.h"
#include "freertos/idf_additions.h"
#include "setup.h"
void app_task(void* param) {
void app_task(void *param)
{
setup();
while(1) {
while (1)
{
loop();
}
}
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
void app_main(void) {
xTaskCreatePinnedToCore(app_task, "main_loop", 4096, NULL, 5, NULL, tskIDLE_PRIORITY + 1);
}
void app_main(void)
{
xTaskCreatePinnedToCore(app_task, "main_loop", 4096, NULL, 5, NULL, tskIDLE_PRIORITY + 1);
}
#ifdef __cplusplus
}
#endif

View File

@@ -1,7 +1,5 @@
#include "setup.h"
#include <stdio.h>
#include <string.h>
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
@@ -9,6 +7,8 @@
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include <stdio.h>
#include <string.h>
#include "driver/rmt_encoder.h"
#include "driver/rmt_tx.h"
@@ -16,22 +16,22 @@
#include "u8g2.h"
#include "u8g2_esp32_hal.h"
#include "common.h"
#include "button_handling.h"
#include "common.h"
#define PIN_SDA GPIO_NUM_35
#define PIN_SCL GPIO_NUM_36
#define PIN_RST GPIO_NUM_NC
#define WLED_GPIO GPIO_NUM_47
#define WLED_RMT_CHANNEL RMT_CHANNEL_0
#define WLED_RESOLUTION_HZ (10000000)
#define WLED_GPIO GPIO_NUM_47
#define WLED_RMT_CHANNEL RMT_CHANNEL_0
#define WLED_RESOLUTION_HZ (10000000)
#define WLED_ON_DURATION_MS (100)
#define NUM_LEDS (1)
#define NUM_LEDS (1)
uint8_t last_value = 0;
static const char* TAG = "main";
static const char *TAG = "main";
extern QueueHandle_t buttonQueue;
@@ -43,7 +43,8 @@ int64_t wled_turn_off_time = 0;
u8g2_t u8g2;
uint8_t received_signal;
static void init_rmt_ws2812b(void) {
static void init_rmt_ws2812b(void)
{
ESP_LOGI(TAG, "Initialize RMT TX Channel for WS2812B on GPIO %d", WLED_GPIO);
rmt_tx_channel_config_t tx_chan_config = {
.gpio_num = WLED_GPIO,
@@ -66,15 +67,18 @@ static void init_rmt_ws2812b(void) {
ESP_ERROR_CHECK(rmt_enable(rmt_led_chan));
}
static void set_wled_color(uint8_t r, uint8_t g, uint8_t b) {
if(!rmt_led_chan || !rmt_led_encoder) {
static void set_wled_color(uint8_t r, uint8_t g, uint8_t b)
{
if (!rmt_led_chan || !rmt_led_encoder)
{
ESP_LOGE(TAG, "RMT Channel or Encoder not initialized!");
return;
}
size_t buffer_size = 3 * NUM_LEDS;
uint8_t led_data[buffer_size];
for(int i = 0; i < NUM_LEDS; i++) {
for (int i = 0; i < NUM_LEDS; i++)
{
led_data[i * 3 + 0] = g;
led_data[i * 3 + 1] = r;
led_data[i * 3 + 2] = b;
@@ -82,15 +86,16 @@ static void set_wled_color(uint8_t r, uint8_t g, uint8_t b) {
rmt_transmit_config_t tx_config = {
.loop_count = 0,
};
esp_err_t ret =
rmt_transmit(rmt_led_chan, rmt_led_encoder, led_data, sizeof(led_data), &tx_config);
if(ret != ESP_OK) {
esp_err_t ret = rmt_transmit(rmt_led_chan, rmt_led_encoder, led_data, sizeof(led_data), &tx_config);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "RMT Transmit failed: %s", esp_err_to_name(ret));
}
ESP_ERROR_CHECK(rmt_tx_wait_all_done(rmt_led_chan, pdMS_TO_TICKS(100)));
}
void setup(void) {
void setup(void)
{
setupButtons();
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
@@ -99,8 +104,7 @@ void setup(void) {
u8g2_esp32_hal.reset = PIN_RST;
u8g2_esp32_hal_init(u8g2_esp32_hal);
u8g2_Setup_sh1106_i2c_128x64_noname_f(
&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8x8_SetI2CAddress(&u8g2.u8x8, 0x3C * 2);
ESP_LOGI(TAG, "u8g2_InitDisplay");
@@ -115,7 +119,8 @@ void setup(void) {
ESP_LOGI(TAG, "Start of main loop. Waiting for button press...");
}
void loop(void) {
void loop(void)
{
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
u8g2_DrawStr(&u8g2, 5, 20, "Ready!");
@@ -124,7 +129,8 @@ void loop(void) {
u8g2_DrawStr(&u8g2, 5, 45, count_str);
u8g2_SendBuffer(&u8g2);
if(xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE) {
if (xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE)
{
ESP_LOGI(TAG, "Button event from Queue received!");
last_value = received_signal;
@@ -143,7 +149,8 @@ void loop(void) {
wled_turn_off_time = esp_timer_get_time() + (WLED_ON_DURATION_MS * 1000);
}
if(wled_is_on && esp_timer_get_time() >= wled_turn_off_time) {
if (wled_is_on && esp_timer_get_time() >= wled_turn_off_time)
{
ESP_LOGD(TAG, "Switch WLED OFF");
set_wled_color(0, 0, 0);
wled_is_on = false;

View File

@@ -1,10 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
void setup(void);
void loop(void);
void setup(void);
void loop(void);
#ifdef __cplusplus
}
#endif