drumz/src/ui/display.cpp

82 lines
1.7 KiB
C++

#include <lvgl.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <Arduino.h>
#include <input/encoder.hpp>
#ifndef SCREEN_WIDTH
#define SCREEN_WIDTH 320
#endif
#ifndef SCREEN_HEIGHT
#define SCREEN_HEIGHT 240
#endif
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
namespace Display
{
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
uint32_t getTime();
void handleLvglLogs(lv_log_level_t level, const char *buf);
void handleUpdateValues(lv_timer_t *timer);
void handleEnterButton();
String label_text;
lv_obj_t *label;
lv_timer_t *update_values_timer;
void init()
{
lv_init();
lv_log_register_print_cb(handleLvglLogs);
lv_tick_set_cb(getTime);
update_values_timer = lv_timer_create(handleUpdateValues, 5, NULL);
lv_timer_ready(update_values_timer);
lv_display_t *display;
display = lv_tft_espi_create(SCREEN_HEIGHT, SCREEN_WIDTH, draw_buf, sizeof(draw_buf));
lv_display_set_rotation(display, LV_DISPLAY_ROTATION_90);
label = lv_label_create(lv_screen_active());
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
Encoder::onButtonRelease(handleEnterButton);
}
void update()
{
lv_timer_handler();
}
void setLabelText(String text)
{
label_text = text;
}
void handleLvglLogs(lv_log_level_t level, const char *buf)
{
Serial.printf("LVGL: %s\r\n", buf);
}
uint32_t getTime()
{
return esp_timer_get_time() / 1000;
}
void handleUpdateValues(lv_timer_t *timer)
{
if (label != NULL && lv_label_get_text(label) != label_text.c_str())
{
lv_label_set_text(label, label_text.c_str());
}
}
void handleEnterButton()
{
setLabelText("Button pressed.");
}
}