2021-10-13 20:08:35 +00:00
|
|
|
#include "displayapp/LittleVgl.h"
|
2022-08-16 05:21:23 +00:00
|
|
|
#include "displayapp/InfiniTimeTheme.h"
|
2020-11-15 15:49:36 +00:00
|
|
|
|
2020-02-10 20:05:33 +00:00
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
2020-11-15 15:49:36 +00:00
|
|
|
#include "drivers/St7789.h"
|
2023-02-26 19:33:34 +00:00
|
|
|
#include "littlefs/lfs.h"
|
|
|
|
#include "components/fs/FS.h"
|
2020-02-10 20:05:33 +00:00
|
|
|
|
|
|
|
using namespace Pinetime::Components;
|
|
|
|
|
2023-01-23 23:17:32 +00:00
|
|
|
namespace {
|
|
|
|
void InitTheme() {
|
|
|
|
lv_theme_t* theme = lv_pinetime_theme_init();
|
|
|
|
lv_theme_set_act(theme);
|
|
|
|
}
|
2023-02-26 19:33:34 +00:00
|
|
|
|
|
|
|
lv_fs_res_t lvglOpen(lv_fs_drv_t* drv, void* file_p, const char* path, lv_fs_mode_t /*mode*/) {
|
|
|
|
lfs_file_t* file = static_cast<lfs_file_t*>(file_p);
|
|
|
|
Pinetime::Controllers::FS* filesys = static_cast<Pinetime::Controllers::FS*>(drv->user_data);
|
|
|
|
int res = filesys->FileOpen(file, path, LFS_O_RDONLY);
|
|
|
|
if (res == 0) {
|
|
|
|
if (file->type == 0) {
|
|
|
|
return LV_FS_RES_FS_ERR;
|
|
|
|
} else {
|
|
|
|
return LV_FS_RES_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LV_FS_RES_NOT_EX;
|
|
|
|
}
|
|
|
|
|
|
|
|
lv_fs_res_t lvglClose(lv_fs_drv_t* drv, void* file_p) {
|
|
|
|
Pinetime::Controllers::FS* filesys = static_cast<Pinetime::Controllers::FS*>(drv->user_data);
|
|
|
|
lfs_file_t* file = static_cast<lfs_file_t*>(file_p);
|
|
|
|
filesys->FileClose(file);
|
|
|
|
|
|
|
|
return LV_FS_RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
lv_fs_res_t lvglRead(lv_fs_drv_t* drv, void* file_p, void* buf, uint32_t btr, uint32_t* br) {
|
|
|
|
Pinetime::Controllers::FS* filesys = static_cast<Pinetime::Controllers::FS*>(drv->user_data);
|
|
|
|
lfs_file_t* file = static_cast<lfs_file_t*>(file_p);
|
|
|
|
filesys->FileRead(file, static_cast<uint8_t*>(buf), btr);
|
|
|
|
*br = btr;
|
|
|
|
return LV_FS_RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
lv_fs_res_t lvglSeek(lv_fs_drv_t* drv, void* file_p, uint32_t pos) {
|
|
|
|
Pinetime::Controllers::FS* filesys = static_cast<Pinetime::Controllers::FS*>(drv->user_data);
|
|
|
|
lfs_file_t* file = static_cast<lfs_file_t*>(file_p);
|
|
|
|
filesys->FileSeek(file, pos);
|
|
|
|
return LV_FS_RES_OK;
|
|
|
|
}
|
2023-01-23 23:17:32 +00:00
|
|
|
}
|
2020-07-03 19:37:40 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
static void disp_flush(lv_disp_drv_t* disp_drv, const lv_area_t* area, lv_color_t* color_p) {
|
2020-02-10 20:05:33 +00:00
|
|
|
auto* lvgl = static_cast<LittleVgl*>(disp_drv->user_data);
|
|
|
|
lvgl->FlushDisplay(area, color_p);
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:21:35 +00:00
|
|
|
static void rounder(lv_disp_drv_t* disp_drv, lv_area_t* area) {
|
|
|
|
auto* lvgl = static_cast<LittleVgl*>(disp_drv->user_data);
|
|
|
|
if (lvgl->GetFullRefresh()) {
|
|
|
|
area->x1 = 0;
|
|
|
|
area->x2 = LV_HOR_RES - 1;
|
|
|
|
area->y1 = 0;
|
|
|
|
area->y2 = LV_VER_RES - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
bool touchpad_read(lv_indev_drv_t* indev_drv, lv_indev_data_t* data) {
|
2020-02-16 17:32:36 +00:00
|
|
|
auto* lvgl = static_cast<LittleVgl*>(indev_drv->user_data);
|
|
|
|
return lvgl->GetTouchPadInfo(data);
|
|
|
|
}
|
|
|
|
|
2023-02-26 19:33:34 +00:00
|
|
|
LittleVgl::LittleVgl(Pinetime::Drivers::St7789& lcd, Pinetime::Controllers::FS& filesystem) : lcd {lcd}, filesystem {filesystem} {
|
2021-06-12 08:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LittleVgl::Init() {
|
2020-02-10 20:05:33 +00:00
|
|
|
lv_init();
|
2020-03-01 14:59:17 +00:00
|
|
|
InitTheme();
|
2020-02-16 17:32:36 +00:00
|
|
|
InitDisplay();
|
|
|
|
InitTouchpad();
|
2023-02-26 19:33:34 +00:00
|
|
|
InitFileSystem();
|
2020-02-16 17:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LittleVgl::InitDisplay() {
|
2021-04-18 17:28:14 +00:00
|
|
|
lv_disp_buf_init(&disp_buf_2, buf2_1, buf2_2, LV_HOR_RES_MAX * 4); /*Initialize the display buffer*/
|
|
|
|
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
|
2020-02-10 20:05:33 +00:00
|
|
|
|
|
|
|
/*Set up the functions to access to your display*/
|
|
|
|
|
|
|
|
/*Set the resolution of the display*/
|
|
|
|
disp_drv.hor_res = 240;
|
|
|
|
disp_drv.ver_res = 240;
|
|
|
|
|
|
|
|
/*Used to copy the buffer's content to the display*/
|
|
|
|
disp_drv.flush_cb = disp_flush;
|
|
|
|
/*Set a display buffer*/
|
|
|
|
disp_drv.buffer = &disp_buf_2;
|
|
|
|
disp_drv.user_data = this;
|
2022-04-20 09:21:35 +00:00
|
|
|
disp_drv.rounder_cb = rounder;
|
2020-02-10 20:05:33 +00:00
|
|
|
|
|
|
|
/*Finally register the driver*/
|
|
|
|
lv_disp_drv_register(&disp_drv);
|
2020-02-16 17:32:36 +00:00
|
|
|
}
|
2020-02-10 20:05:33 +00:00
|
|
|
|
2020-02-16 17:32:36 +00:00
|
|
|
void LittleVgl::InitTouchpad() {
|
|
|
|
lv_indev_drv_t indev_drv;
|
2020-02-10 20:05:33 +00:00
|
|
|
|
2020-02-16 17:32:36 +00:00
|
|
|
lv_indev_drv_init(&indev_drv);
|
|
|
|
indev_drv.type = LV_INDEV_TYPE_POINTER;
|
|
|
|
indev_drv.read_cb = touchpad_read;
|
|
|
|
indev_drv.user_data = this;
|
|
|
|
lv_indev_drv_register(&indev_drv);
|
2020-02-10 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 19:33:34 +00:00
|
|
|
void LittleVgl::InitFileSystem() {
|
|
|
|
lv_fs_drv_t fs_drv;
|
|
|
|
lv_fs_drv_init(&fs_drv);
|
|
|
|
|
|
|
|
fs_drv.file_size = sizeof(lfs_file_t);
|
|
|
|
fs_drv.letter = 'F';
|
|
|
|
fs_drv.open_cb = lvglOpen;
|
|
|
|
fs_drv.close_cb = lvglClose;
|
|
|
|
fs_drv.read_cb = lvglRead;
|
|
|
|
fs_drv.seek_cb = lvglSeek;
|
|
|
|
|
|
|
|
fs_drv.user_data = &filesystem;
|
|
|
|
|
|
|
|
lv_fs_drv_register(&fs_drv);
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:29:12 +00:00
|
|
|
void LittleVgl::SetFullRefresh(FullRefreshDirections direction) {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (scrollDirection == FullRefreshDirections::None) {
|
2020-03-10 19:21:41 +00:00
|
|
|
scrollDirection = direction;
|
2021-04-04 02:08:51 +00:00
|
|
|
if (scrollDirection == FullRefreshDirections::Down) {
|
2020-03-10 19:21:41 +00:00
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 1);
|
2021-04-04 02:08:51 +00:00
|
|
|
} else if (scrollDirection == FullRefreshDirections::Right) {
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 2);
|
|
|
|
} else if (scrollDirection == FullRefreshDirections::Left) {
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 3);
|
|
|
|
} else if (scrollDirection == FullRefreshDirections::RightAnim) {
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 5);
|
|
|
|
} else if (scrollDirection == FullRefreshDirections::LeftAnim) {
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 4);
|
|
|
|
}
|
2020-03-10 19:21:41 +00:00
|
|
|
}
|
2022-04-20 09:21:35 +00:00
|
|
|
fullRefresh = true;
|
2020-03-08 20:46:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
void LittleVgl::FlushDisplay(const lv_area_t* area, lv_color_t* color_p) {
|
2021-03-18 19:38:19 +00:00
|
|
|
uint16_t y1, y2, width, height = 0;
|
|
|
|
|
2021-04-04 02:08:51 +00:00
|
|
|
ulTaskNotifyTake(pdTRUE, 200);
|
2022-05-31 19:17:36 +00:00
|
|
|
// Notification is still needed (even if there is a mutex on SPI) because of the DataCommand pin
|
|
|
|
// which cannot be set/clear during a transfer.
|
2021-04-18 17:28:14 +00:00
|
|
|
|
|
|
|
if ((scrollDirection == LittleVgl::FullRefreshDirections::Down) && (area->y2 == visibleNbLines - 1)) {
|
2021-03-18 19:38:19 +00:00
|
|
|
writeOffset = ((writeOffset + totalNbLines) - visibleNbLines) % totalNbLines;
|
2021-04-18 17:28:14 +00:00
|
|
|
} else if ((scrollDirection == FullRefreshDirections::Up) && (area->y1 == 0)) {
|
2021-03-18 19:38:19 +00:00
|
|
|
writeOffset = (writeOffset + visibleNbLines) % totalNbLines;
|
|
|
|
}
|
2020-03-01 14:57:58 +00:00
|
|
|
|
2021-03-18 19:38:19 +00:00
|
|
|
y1 = (area->y1 + writeOffset) % totalNbLines;
|
|
|
|
y2 = (area->y2 + writeOffset) % totalNbLines;
|
2020-03-08 20:46:25 +00:00
|
|
|
|
2021-03-18 19:38:19 +00:00
|
|
|
width = (area->x2 - area->x1) + 1;
|
|
|
|
height = (area->y2 - area->y1) + 1;
|
2020-03-08 20:46:25 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (scrollDirection == LittleVgl::FullRefreshDirections::Down) {
|
2021-04-04 02:08:51 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (area->y2 < visibleNbLines - 1) {
|
2020-03-09 20:29:12 +00:00
|
|
|
uint16_t toScroll = 0;
|
2021-04-18 17:28:14 +00:00
|
|
|
if (area->y1 == 0) {
|
2021-03-18 19:38:19 +00:00
|
|
|
toScroll = height * 2;
|
2020-03-09 20:29:12 +00:00
|
|
|
scrollDirection = FullRefreshDirections::None;
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 0);
|
|
|
|
} else {
|
|
|
|
toScroll = height;
|
|
|
|
}
|
2021-04-04 02:08:51 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (scrollOffset >= toScroll)
|
2020-03-09 20:29:12 +00:00
|
|
|
scrollOffset -= toScroll;
|
|
|
|
else {
|
|
|
|
toScroll -= scrollOffset;
|
2021-04-18 17:28:14 +00:00
|
|
|
scrollOffset = (totalNbLines) -toScroll;
|
2020-03-08 20:46:25 +00:00
|
|
|
}
|
2020-03-09 20:29:12 +00:00
|
|
|
lcd.VerticalScrollStartAddress(scrollOffset);
|
|
|
|
}
|
2020-03-08 20:46:25 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
} else if (scrollDirection == FullRefreshDirections::Up) {
|
2020-03-09 20:29:12 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (area->y1 > 0) {
|
|
|
|
if (area->y2 == visibleNbLines - 1) {
|
2020-03-09 20:29:12 +00:00
|
|
|
scrollOffset += (height * 2);
|
|
|
|
scrollDirection = FullRefreshDirections::None;
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 0);
|
|
|
|
} else {
|
|
|
|
scrollOffset += height;
|
|
|
|
}
|
|
|
|
scrollOffset = scrollOffset % totalNbLines;
|
|
|
|
lcd.VerticalScrollStartAddress(scrollOffset);
|
2020-03-08 20:46:25 +00:00
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
} else if (scrollDirection == FullRefreshDirections::Left or scrollDirection == FullRefreshDirections::LeftAnim) {
|
|
|
|
if (area->x2 == visibleNbLines - 1) {
|
2021-04-04 02:08:51 +00:00
|
|
|
scrollDirection = FullRefreshDirections::None;
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 0);
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
} else if (scrollDirection == FullRefreshDirections::Right or scrollDirection == FullRefreshDirections::RightAnim) {
|
|
|
|
if (area->x1 == 0) {
|
2021-04-04 02:08:51 +00:00
|
|
|
scrollDirection = FullRefreshDirections::None;
|
|
|
|
lv_disp_set_direction(lv_disp_get_default(), 0);
|
|
|
|
}
|
2021-03-18 19:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (y2 < y1) {
|
|
|
|
height = totalNbLines - y1;
|
2020-03-09 20:29:12 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (height > 0) {
|
|
|
|
lcd.DrawBuffer(area->x1, y1, width, height, reinterpret_cast<const uint8_t*>(color_p), width * height * 2);
|
2021-04-04 02:08:51 +00:00
|
|
|
ulTaskNotifyTake(pdTRUE, 100);
|
2020-03-09 20:29:12 +00:00
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2021-03-18 19:38:19 +00:00
|
|
|
uint16_t pixOffset = width * height;
|
|
|
|
height = y2 + 1;
|
2021-04-18 17:28:14 +00:00
|
|
|
lcd.DrawBuffer(area->x1, 0, width, height, reinterpret_cast<const uint8_t*>(color_p + pixOffset), width * height * 2);
|
2021-03-18 19:38:19 +00:00
|
|
|
|
2021-04-04 02:08:51 +00:00
|
|
|
} else {
|
2021-04-18 17:28:14 +00:00
|
|
|
lcd.DrawBuffer(area->x1, y1, width, height, reinterpret_cast<const uint8_t*>(color_p), width * height * 2);
|
2020-03-08 20:46:25 +00:00
|
|
|
}
|
2020-02-10 20:05:33 +00:00
|
|
|
|
2021-03-18 19:38:19 +00:00
|
|
|
// IMPORTANT!!!
|
|
|
|
// Inform the graphics library that you are ready with the flushing
|
2020-02-10 20:05:33 +00:00
|
|
|
lv_disp_flush_ready(&disp_drv);
|
|
|
|
}
|
2020-03-09 20:29:12 +00:00
|
|
|
|
2023-02-23 11:28:04 +00:00
|
|
|
void LittleVgl::SetNewTouchPoint(int16_t x, int16_t y, bool contact) {
|
|
|
|
if (contact) {
|
|
|
|
if (!isCancelled) {
|
|
|
|
touchPoint = {x, y};
|
|
|
|
tapped = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isCancelled) {
|
|
|
|
touchPoint = {-1, -1};
|
|
|
|
tapped = false;
|
|
|
|
isCancelled = false;
|
|
|
|
} else {
|
|
|
|
touchPoint = {x, y};
|
|
|
|
tapped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LittleVgl::CancelTap() {
|
|
|
|
if (tapped) {
|
|
|
|
isCancelled = true;
|
|
|
|
touchPoint = {-1, -1};
|
|
|
|
}
|
2020-03-09 20:29:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
bool LittleVgl::GetTouchPadInfo(lv_indev_data_t* ptr) {
|
2023-02-23 11:28:04 +00:00
|
|
|
ptr->point.x = touchPoint.x;
|
|
|
|
ptr->point.y = touchPoint.y;
|
2021-04-18 17:28:14 +00:00
|
|
|
if (tapped) {
|
2020-03-09 20:29:12 +00:00
|
|
|
ptr->state = LV_INDEV_STATE_PR;
|
|
|
|
} else {
|
|
|
|
ptr->state = LV_INDEV_STATE_REL;
|
|
|
|
}
|
|
|
|
return false;
|
2020-02-16 17:32:36 +00:00
|
|
|
}
|