315 lines
8.9 KiB
Arduino
315 lines
8.9 KiB
Arduino
#include <TFT_eSPI.h>
|
|
#include <lvgl.h>
|
|
#include <Arduino.h>
|
|
#define TOUCH_MODULES_GT911
|
|
#include "TouchLib.h"
|
|
#include "utilities.h"
|
|
|
|
static void slider_event_cb(lv_event_t *e);
|
|
static lv_obj_t *slider_label;
|
|
|
|
TFT_eSPI tft;
|
|
TouchLib *touch = NULL;
|
|
uint8_t touchAddress = GT911_SLAVE_ADDRESS2;
|
|
|
|
void scanDevices(TwoWire *w)
|
|
{
|
|
uint8_t err, addr;
|
|
int nDevices = 0;
|
|
uint32_t start = 0;
|
|
for (addr = 1; addr < 127; addr++) {
|
|
start = millis();
|
|
w->beginTransmission(addr); delay(2);
|
|
err = w->endTransmission();
|
|
if (err == 0) {
|
|
nDevices++;
|
|
Serial.print("I2C device found at address 0x");
|
|
if (addr < 16) {
|
|
Serial.print("0");
|
|
}
|
|
Serial.print(addr, HEX);
|
|
Serial.println(" !");
|
|
|
|
if (addr == GT911_SLAVE_ADDRESS2) {
|
|
touchAddress = GT911_SLAVE_ADDRESS2;
|
|
Serial.println("Find GT911 Drv Slave address: 0x14");
|
|
} else if (addr == GT911_SLAVE_ADDRESS1) {
|
|
touchAddress = GT911_SLAVE_ADDRESS1;
|
|
Serial.println("Find GT911 Drv Slave address: 0x5D");
|
|
}
|
|
} else if (err == 4) {
|
|
Serial.print("Unknow error at address 0x");
|
|
if (addr < 16) {
|
|
Serial.print("0");
|
|
}
|
|
Serial.println(addr, HEX);
|
|
}
|
|
}
|
|
if (nDevices == 0)
|
|
Serial.println("No I2C devices found\n");
|
|
}
|
|
|
|
|
|
// LilyGo T-Deck control backlight chip has 16 levels of adjustment range
|
|
// The adjustable range is 0~15, 0 is the minimum brightness, 15 is the maximum brightness
|
|
void setBrightness(uint8_t value)
|
|
{
|
|
static uint8_t level = 0;
|
|
static uint8_t steps = 16;
|
|
if (value == 0) {
|
|
digitalWrite(BOARD_BL_PIN, 0);
|
|
delay(3);
|
|
level = 0;
|
|
return;
|
|
}
|
|
if (level == 0) {
|
|
digitalWrite(BOARD_BL_PIN, 1);
|
|
level = steps;
|
|
delayMicroseconds(30);
|
|
}
|
|
int from = steps - level;
|
|
int to = steps - value;
|
|
int num = (steps + to - from) % steps;
|
|
for (int i = 0; i < num; i++) {
|
|
digitalWrite(BOARD_BL_PIN, 0);
|
|
digitalWrite(BOARD_BL_PIN, 1);
|
|
}
|
|
level = value;
|
|
}
|
|
|
|
|
|
// !!! LVGL !!!
|
|
// !!! LVGL !!!
|
|
// !!! LVGL !!!
|
|
static void disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p )
|
|
{
|
|
uint32_t w = ( area->x2 - area->x1 + 1 );
|
|
uint32_t h = ( area->y2 - area->y1 + 1 );
|
|
tft.startWrite();
|
|
tft.setAddrWindow( area->x1, area->y1, w, h );
|
|
tft.pushColors( ( uint16_t * )&color_p->full, w * h, false );
|
|
tft.endWrite();
|
|
lv_disp_flush_ready( disp );
|
|
}
|
|
|
|
|
|
static bool getTouch(int16_t &x, int16_t &y)
|
|
{
|
|
if (! digitalRead(BOARD_TOUCH_INT))return false;
|
|
uint8_t rotation = tft.getRotation();
|
|
if (!touch->read()) {
|
|
return false;
|
|
}
|
|
TP_Point t = touch->getPoint(0);
|
|
switch (rotation) {
|
|
case 1:
|
|
x = t.y;
|
|
y = tft.height() - t.x;
|
|
break;
|
|
case 2:
|
|
x = tft.width() - t.x;
|
|
y = tft.height() - t.y;
|
|
break;
|
|
case 3:
|
|
x = tft.width() - t.y;
|
|
y = t.x;
|
|
break;
|
|
case 0:
|
|
default:
|
|
x = t.x;
|
|
y = t.y;
|
|
}
|
|
Serial.printf("R:%d X:%d Y:%d\n", rotation, x, y);
|
|
return true;
|
|
}
|
|
|
|
/*Read the touchpad*/
|
|
static void touchpad_read( lv_indev_drv_t *indev_driver, lv_indev_data_t *data )
|
|
{
|
|
data->state = getTouch(data->point.x, data->point.y) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
|
|
}
|
|
|
|
|
|
void setupLvgl()
|
|
{
|
|
#define LVGL_BUFFER_SIZE (TFT_WIDTH * TFT_HEIGHT * sizeof(lv_color_t))
|
|
|
|
static lv_disp_draw_buf_t draw_buf;
|
|
static lv_color_t *buf = (lv_color_t *)ps_malloc(LVGL_BUFFER_SIZE);
|
|
if (!buf) {
|
|
Serial.println("menory alloc failed!");
|
|
delay(5000);
|
|
assert(buf);
|
|
}
|
|
String LVGL_Arduino = "Hello Arduino! ";
|
|
LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
|
|
Serial.println( LVGL_Arduino );
|
|
Serial.println( "I am LVGL_Arduino" );
|
|
|
|
lv_init();
|
|
|
|
lv_group_set_default(lv_group_create());
|
|
|
|
lv_disp_draw_buf_init( &draw_buf, buf, NULL, LVGL_BUFFER_SIZE );
|
|
|
|
/*Initialize the display*/
|
|
static lv_disp_drv_t disp_drv;
|
|
lv_disp_drv_init( &disp_drv );
|
|
|
|
/*Change the following line to your display resolution*/
|
|
disp_drv.hor_res = TFT_HEIGHT;
|
|
disp_drv.ver_res = TFT_WIDTH;
|
|
disp_drv.flush_cb = disp_flush;
|
|
disp_drv.draw_buf = &draw_buf;
|
|
disp_drv.full_refresh = 1;
|
|
lv_disp_drv_register( &disp_drv );
|
|
|
|
/*Initialize the input device driver*/
|
|
|
|
/*Register a touchscreen input device*/
|
|
static lv_indev_drv_t indev_touchpad;
|
|
lv_indev_drv_init( &indev_touchpad );
|
|
indev_touchpad.type = LV_INDEV_TYPE_POINTER;
|
|
indev_touchpad.read_cb = touchpad_read;
|
|
lv_indev_drv_register( &indev_touchpad );
|
|
}
|
|
|
|
static void slider_event_cb(lv_event_t *e)
|
|
{
|
|
lv_obj_t *slider = lv_event_get_target(e);
|
|
char buf[8];
|
|
lv_snprintf(buf, sizeof(buf), "%d%%", (int)lv_slider_get_value(slider));
|
|
lv_label_set_text(slider_label, buf);
|
|
lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
|
|
}
|
|
|
|
static void set_angle(void *obj, int32_t v)
|
|
{
|
|
lv_arc_set_value((lv_obj_t *)obj, v);
|
|
}
|
|
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(115200);
|
|
|
|
Serial.println("lvgl example");
|
|
|
|
//! The board peripheral power control pin needs to be set to HIGH when using the peripheral
|
|
pinMode(BOARD_POWERON, OUTPUT);
|
|
digitalWrite(BOARD_POWERON, HIGH);
|
|
|
|
//! Set CS on all SPI buses to high level during initialization
|
|
pinMode(BOARD_SDCARD_CS, OUTPUT);
|
|
pinMode(RADIO_CS_PIN, OUTPUT);
|
|
pinMode(BOARD_TFT_CS, OUTPUT);
|
|
|
|
digitalWrite(BOARD_SDCARD_CS, HIGH);
|
|
digitalWrite(RADIO_CS_PIN, HIGH);
|
|
digitalWrite(BOARD_TFT_CS, HIGH);
|
|
|
|
pinMode(BOARD_SPI_MISO, INPUT_PULLUP);
|
|
SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); //SD
|
|
|
|
pinMode(BOARD_BOOT_PIN, INPUT_PULLUP);
|
|
pinMode(BOARD_TBOX_G02, INPUT_PULLUP);
|
|
pinMode(BOARD_TBOX_G01, INPUT_PULLUP);
|
|
pinMode(BOARD_TBOX_G04, INPUT_PULLUP);
|
|
pinMode(BOARD_TBOX_G03, INPUT_PULLUP);
|
|
|
|
//Wakeup touch chip
|
|
pinMode(BOARD_TOUCH_INT, OUTPUT);
|
|
digitalWrite(BOARD_TOUCH_INT, HIGH);
|
|
|
|
Serial.print("Init display id:");
|
|
Serial.println(USER_SETUP_ID);
|
|
|
|
tft.begin();
|
|
tft.setRotation( 1 );
|
|
tft.fillScreen(TFT_BLACK);
|
|
|
|
// Set touch int input
|
|
pinMode(BOARD_TOUCH_INT, INPUT);
|
|
delay(20);
|
|
|
|
Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
|
|
|
|
|
|
// Two touch screens, the difference between them is the device address,
|
|
// use ScanDevices to get the existing I2C address
|
|
scanDevices(&Wire);
|
|
|
|
touch = new TouchLib(Wire, BOARD_I2C_SDA, BOARD_I2C_SCL, touchAddress);
|
|
|
|
touch->init();
|
|
|
|
setupLvgl();
|
|
|
|
lv_obj_t *tv = lv_tileview_create(lv_scr_act());
|
|
|
|
/*Tile1: just a label*/
|
|
lv_obj_t *tile1 = lv_tileview_add_tile(tv, 0, 0, LV_DIR_BOTTOM | LV_DIR_HOR);
|
|
lv_obj_t *label = lv_label_create(tile1);
|
|
lv_label_set_text(label, "Scroll down");
|
|
lv_obj_center(label);
|
|
|
|
|
|
/*Tile2: a slider*/
|
|
lv_obj_t *tile2 = lv_tileview_add_tile(tv, 0, 1, LV_DIR_TOP | LV_DIR_RIGHT);
|
|
|
|
/*Create a slider in the center of the display*/
|
|
lv_obj_t *slider = lv_slider_create(tile2);
|
|
lv_obj_center(slider);
|
|
lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
|
|
/*Create a label below the slider*/
|
|
slider_label = lv_label_create(tile2);
|
|
lv_label_set_text(slider_label, "0%");
|
|
lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
|
|
|
|
/*Tile3: a list*/
|
|
lv_obj_t *tile3 = lv_tileview_add_tile(tv, 1, 1, LV_DIR_LEFT);
|
|
lv_obj_t *list = lv_list_create(tile3);
|
|
lv_obj_set_size(list, LV_PCT(100), LV_PCT(100));
|
|
|
|
lv_list_add_btn(list, NULL, "One");
|
|
lv_list_add_btn(list, NULL, "Two");
|
|
lv_list_add_btn(list, NULL, "Three");
|
|
lv_list_add_btn(list, NULL, "Four");
|
|
lv_list_add_btn(list, NULL, "Five");
|
|
lv_list_add_btn(list, NULL, "Six");
|
|
lv_list_add_btn(list, NULL, "Seven");
|
|
lv_list_add_btn(list, NULL, "Eight");
|
|
lv_list_add_btn(list, NULL, "Nine");
|
|
lv_list_add_btn(list, NULL, "Ten");
|
|
|
|
/*Tile4: a arc*/
|
|
lv_obj_t *tile4 = lv_tileview_add_tile(tv, 1, 0, LV_DIR_HOR);
|
|
|
|
/*Create an Arc*/
|
|
lv_obj_t *arc = lv_arc_create(tile4);
|
|
lv_arc_set_rotation(arc, 270);
|
|
lv_arc_set_bg_angles(arc, 0, 360);
|
|
lv_obj_remove_style(arc, NULL, LV_PART_KNOB); /*Be sure the knob is not displayed*/
|
|
lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE); /*To not allow adjusting by click*/
|
|
lv_obj_center(arc);
|
|
|
|
lv_anim_t a;
|
|
lv_anim_init(&a);
|
|
lv_anim_set_var(&a, arc);
|
|
lv_anim_set_exec_cb(&a, set_angle);
|
|
lv_anim_set_time(&a, 1000);
|
|
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE); /*Just for the demo*/
|
|
lv_anim_set_repeat_delay(&a, 500);
|
|
lv_anim_set_values(&a, 0, 100);
|
|
lv_anim_start(&a);
|
|
|
|
// Adjust backlight
|
|
pinMode(BOARD_BL_PIN, OUTPUT);
|
|
setBrightness(16);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
lv_timer_handler();
|
|
delay(5);
|
|
} |