2
main/CMakeLists.txt
Executable file
2
main/CMakeLists.txt
Executable file
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
8
main/idf_component.yml
Executable file
8
main/idf_component.yml
Executable file
@@ -0,0 +1,8 @@
|
||||
dependencies:
|
||||
lvgl/lvgl: ~8.3.0
|
||||
esp_lvgl_port: ^1.4.0
|
||||
tny-robotics/sh1106-esp-idf: ^1.0.0
|
||||
u8g2:
|
||||
git: https://github.com/olikraus/u8g2.git
|
||||
u8g2_hal:
|
||||
git: https://github.com/mkfrey/u8g2-hal-esp-idf.git
|
107
main/lcd_lvgl_main.c
Executable file
107
main/lcd_lvgl_main.c
Executable file
@@ -0,0 +1,107 @@
|
||||
#include <stdio.h>
|
||||
#include "driver/i2c_master.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_lcd_panel_io.h"
|
||||
#include "esp_lcd_panel_ops.h"
|
||||
#include "esp_lcd_panel_sh1106.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
static const char* TAG = "example";
|
||||
|
||||
#define I2C_BUS_PORT 0
|
||||
#define EXAMPLE_PIN_NUM_SDA 35
|
||||
#define EXAMPLE_PIN_NUM_SCL 36
|
||||
|
||||
// The pixel number in horizontal and vertical
|
||||
#define EXAMPLE_LCD_H_RES 128
|
||||
#define EXAMPLE_LCD_V_RES 64
|
||||
|
||||
void example_lvgl_demo_ui(lv_disp_t* disp) {
|
||||
lv_obj_t* scr = lv_disp_get_scr_act(disp);
|
||||
lv_obj_t* label = lv_label_create(scr);
|
||||
lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
||||
lv_label_set_text(label, "Hello Espressif, Hello LVGL.");
|
||||
/* Size of the screen (if you use rotation 90 or 270, please set
|
||||
* disp->driver->ver_res) */
|
||||
lv_obj_set_width(label, disp->driver->hor_res);
|
||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
ESP_LOGI(TAG, "Initialize I2C bus");
|
||||
i2c_master_bus_config_t bus_config = {
|
||||
.i2c_port = I2C_BUS_PORT,
|
||||
.sda_io_num = EXAMPLE_PIN_NUM_SDA,
|
||||
.scl_io_num = EXAMPLE_PIN_NUM_SCL,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.intr_priority = 0,
|
||||
.trans_queue_depth = 0,
|
||||
.flags =
|
||||
{
|
||||
.enable_internal_pullup = true,
|
||||
.allow_pd = false,
|
||||
},
|
||||
};
|
||||
i2c_master_bus_handle_t i2c_bus_handle = NULL;
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &i2c_bus_handle));
|
||||
|
||||
ESP_LOGI(TAG, "Install panel IO");
|
||||
esp_lcd_panel_io_handle_t io_handle = NULL;
|
||||
esp_lcd_panel_io_i2c_config_t io_config = ESP_SH1106_DEFAULT_IO_CONFIG;
|
||||
ESP_ERROR_CHECK(
|
||||
esp_lcd_new_panel_io_i2c(i2c_bus_handle, &io_config, &io_handle));
|
||||
|
||||
esp_lcd_panel_dev_config_t panel_config = {
|
||||
.reset_gpio_num = -1,
|
||||
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
|
||||
.data_endian = LCD_RGB_DATA_ENDIAN_LITTLE,
|
||||
.bits_per_pixel = SH1106_PIXELS_PER_BYTE / 8,
|
||||
.flags =
|
||||
{
|
||||
.reset_active_high = false,
|
||||
},
|
||||
.vendor_config = NULL,
|
||||
};
|
||||
|
||||
esp_lcd_panel_handle_t panel_handle = NULL;
|
||||
ESP_ERROR_CHECK(
|
||||
esp_lcd_new_panel_sh1106(io_handle, &panel_config, &panel_handle));
|
||||
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
|
||||
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
|
||||
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
|
||||
|
||||
ESP_LOGI(TAG, "Initialize LVGL");
|
||||
const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
||||
lvgl_port_init(&lvgl_cfg);
|
||||
|
||||
const lvgl_port_display_cfg_t disp_cfg = {
|
||||
.io_handle = io_handle,
|
||||
.panel_handle = panel_handle,
|
||||
.buffer_size = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES,
|
||||
.double_buffer = true,
|
||||
.hres = EXAMPLE_LCD_H_RES,
|
||||
.vres = EXAMPLE_LCD_V_RES,
|
||||
.monochrome = true,
|
||||
.rotation = {
|
||||
.swap_xy = true,
|
||||
.mirror_x = false,
|
||||
.mirror_y = false,
|
||||
}};
|
||||
lv_disp_t* disp = lvgl_port_add_disp(&disp_cfg);
|
||||
|
||||
ESP_LOGI(TAG, "Display LVGL Scroll Text");
|
||||
// Lock the mutex due to the LVGL APIs are not thread-safe
|
||||
if (lvgl_port_lock(0)) {
|
||||
example_lvgl_demo_ui(disp);
|
||||
// Release the mutex
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
}
|
204
main/main.c
Normal file
204
main/main.c
Normal file
@@ -0,0 +1,204 @@
|
||||
#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 "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "driver/rmt_encoder.h"
|
||||
#include "driver/rmt_tx.h"
|
||||
|
||||
#include "u8g2.h"
|
||||
#include "u8g2_esp32_hal.h"
|
||||
|
||||
#define PIN_SDA GPIO_NUM_35
|
||||
#define PIN_SCL GPIO_NUM_36
|
||||
#define PIN_RST GPIO_NUM_NC
|
||||
|
||||
#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 DEBOUNCE_TIME_MS (500)
|
||||
|
||||
#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 BUTTON_QUEUE_LENGTH 5
|
||||
#define BUTTON_QUEUE_ITEM_SIZE sizeof(uint8_t)
|
||||
|
||||
static const char* TAG = "U8G2_BUTTON_EXAMPLE";
|
||||
|
||||
QueueHandle_t buttonQueue = NULL;
|
||||
volatile int counter = 0;
|
||||
volatile int64_t last_interrupt_time = 0;
|
||||
|
||||
rmt_channel_handle_t rmt_led_chan = NULL;
|
||||
rmt_encoder_handle_t rmt_led_encoder = NULL;
|
||||
bool wled_is_on = false;
|
||||
int64_t wled_turn_off_time = 0;
|
||||
|
||||
static void IRAM_ATTR button_isr_handler(void* arg) {
|
||||
int64_t now = esp_timer_get_time();
|
||||
if ((now - last_interrupt_time) > (DEBOUNCE_TIME_MS * 1000)) {
|
||||
last_interrupt_time = now;
|
||||
|
||||
uint8_t press_signal = 1;
|
||||
BaseType_t higherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
xQueueSendFromISR(buttonQueue, &press_signal, &higherPriorityTaskWoken);
|
||||
|
||||
if (higherPriorityTaskWoken) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void init_rmt_ws2812b(void) {
|
||||
ESP_LOGI(TAG, "Installiere RMT TX Kanal für WS2812B auf GPIO %d", WLED_GPIO);
|
||||
rmt_tx_channel_config_t tx_chan_config = {
|
||||
.gpio_num = WLED_GPIO,
|
||||
.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||
.resolution_hz = WLED_RESOLUTION_HZ,
|
||||
.mem_block_symbols = 64,
|
||||
.trans_queue_depth = 4,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &rmt_led_chan));
|
||||
|
||||
ESP_LOGI(TAG, "Installiere RMT Bytes Encoder");
|
||||
rmt_bytes_encoder_config_t bytes_encoder_config = {
|
||||
.bit0 = {.duration0 = 4, .level0 = 1, .duration1 = 8, .level1 = 0},
|
||||
.bit1 = {.duration0 = 8, .level0 = 1, .duration1 = 4, .level1 = 0},
|
||||
.flags = {.msb_first = 1}};
|
||||
ESP_ERROR_CHECK(
|
||||
rmt_new_bytes_encoder(&bytes_encoder_config, &rmt_led_encoder));
|
||||
|
||||
ESP_LOGI(TAG, "Aktiviere RMT TX Kanal");
|
||||
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) {
|
||||
ESP_LOGE(TAG, "RMT Kanal oder Encoder nicht initialisiert!");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t buffer_size = 3 * NUM_LEDS;
|
||||
uint8_t led_data[buffer_size];
|
||||
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;
|
||||
}
|
||||
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_LOGE(TAG, "RMT Transmit fehlgeschlagen: %s", esp_err_to_name(ret));
|
||||
}
|
||||
ESP_ERROR_CHECK(rmt_tx_wait_all_done(rmt_led_chan, pdMS_TO_TICKS(100)));
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
buttonQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, BUTTON_QUEUE_ITEM_SIZE);
|
||||
if (buttonQueue == NULL) {
|
||||
ESP_LOGE(TAG, "Fehler beim Erstellen der Queue!");
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "Button Queue erstellt.");
|
||||
|
||||
gpio_config_t gpio_button_up;
|
||||
gpio_button_up.intr_type = GPIO_INTR_NEGEDGE;
|
||||
gpio_button_up.pin_bit_mask = (1ULL << BUTTON_UP);
|
||||
gpio_button_up.mode = GPIO_MODE_INPUT;
|
||||
gpio_button_up.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_button_up.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
gpio_config(&gpio_button_up);
|
||||
|
||||
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) {
|
||||
ESP_LOGE(TAG, "Fehler bei gpio_install_isr_service: %s",
|
||||
esp_err_to_name(isr_service_err));
|
||||
}
|
||||
|
||||
esp_err_t add_isr_err =
|
||||
gpio_isr_handler_add(BUTTON_UP, button_isr_handler, (void*)BUTTON_UP);
|
||||
if (add_isr_err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Fehler bei gpio_isr_handler_add: %s",
|
||||
esp_err_to_name(add_isr_err));
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Button-Interrupt konfiguriert für GPIO %d", BUTTON_UP);
|
||||
|
||||
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
|
||||
u8g2_esp32_hal.bus.i2c.sda = PIN_SDA;
|
||||
u8g2_esp32_hal.bus.i2c.scl = PIN_SCL;
|
||||
u8g2_esp32_hal.reset = PIN_RST;
|
||||
u8g2_esp32_hal_init(u8g2_esp32_hal);
|
||||
|
||||
u8g2_t u8g2;
|
||||
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");
|
||||
u8g2_InitDisplay(&u8g2);
|
||||
|
||||
ESP_LOGI(TAG, "u8g2_SetPowerSave");
|
||||
u8g2_SetPowerSave(&u8g2, 0);
|
||||
|
||||
init_rmt_ws2812b();
|
||||
set_wled_color(0, 0, 0);
|
||||
|
||||
ESP_LOGI(TAG, "Start der Hauptschleife. Warte auf Button-Druck...");
|
||||
|
||||
uint8_t received_signal;
|
||||
while (1) {
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
|
||||
u8g2_DrawStr(&u8g2, 5, 20, "Bereit!");
|
||||
char count_str[12];
|
||||
snprintf(count_str, sizeof(count_str), "Zahl: %d", counter);
|
||||
u8g2_DrawStr(&u8g2, 5, 45, count_str);
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
|
||||
if (xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) ==
|
||||
pdTRUE) {
|
||||
ESP_LOGI(TAG, "Button-Event aus Queue empfangen!");
|
||||
|
||||
counter++;
|
||||
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
u8g2_DrawStr(&u8g2, 5, 20, "Gedrueckt!");
|
||||
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
|
||||
snprintf(count_str, sizeof(count_str), "Zahl: %d", counter);
|
||||
u8g2_DrawStr(&u8g2, 5, 45, count_str);
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
ESP_LOGD(TAG, "Display aktualisiert mit Zähler: %d", counter);
|
||||
|
||||
ESP_LOGD(TAG, "Schalte WLED Magenta EIN");
|
||||
set_wled_color(255, 0, 255);
|
||||
wled_is_on = true;
|
||||
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) {
|
||||
ESP_LOGD(TAG, "Schalte WLED AUS");
|
||||
set_wled_color(0, 0, 0);
|
||||
wled_is_on = false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user