drumz/src/ui/display.cpp

129 lines
3.0 KiB
C++

#include <lvgl.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <Arduino.h>
#include <input/encoder.hpp>
#include <ui/views/main_view.hpp>
#include <input/user_input_manager.hpp>
#include <vector>
#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
{
using std::vector;
using UserInputManager::InputEvent;
const uint8_t MAIN_VIEW = 1;
const String USER_INPUT_HANDLE = "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 handleReadUserInput(lv_indev_t *indev, lv_indev_data_t *data);
void setCurrentView(uint8_t view);
lv_timer_t *update_values_timer;
lv_indev_t *indev;
uint8_t current_view = 0;
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);
UserInputManager::registerForEvents(USER_INPUT_HANDLE);
indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_KEYPAD);
lv_indev_set_read_cb(indev, handleReadUserInput);
setCurrentView(MAIN_VIEW);
}
void update()
{
lv_timer_handler();
}
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 setCurrentView(uint8_t view)
{
current_view = view;
lv_obj_t *parent = lv_screen_active();
lv_obj_clean(parent);
switch (view)
{
case MAIN_VIEW:
MainView::render(parent, indev);
break;
}
}
void handleUpdateValues(lv_timer_t *timer)
{
switch (current_view)
{
case MAIN_VIEW:
MainView::update();
break;
}
}
void handleReadUserInput(lv_indev_t *indev, lv_indev_data_t *data)
{
InputEvent event = UserInputManager::getNextEvent(USER_INPUT_HANDLE);
switch (event)
{
case InputEvent::EncoderButtonRelease:
data->key = LV_KEY_ENTER;
data->state = LV_INDEV_STATE_RELEASED;
Serial.println("indev event EncoderButtonRelease");
break;
case InputEvent::EncoderButtonPress:
data->key = LV_KEY_ENTER;
data->state = LV_INDEV_STATE_PRESSED;
Serial.println("indev event EncoderButtonPress");
break;
case InputEvent::EncoderRotateLeft:
data->key = LV_KEY_PREV;
data->state = LV_INDEV_STATE_PRESSED;
Serial.println("indev event EncoderRotateLeft");
break;
case InputEvent::EncoderRotateRight:
data->key = LV_KEY_NEXT;
data->state = LV_INDEV_STATE_PRESSED;
Serial.println("indev event EncoderRotateRight");
break;
}
}
}