Hi, I have been struggling for a while to make lvgl to work on my wt32-sc01-plus, I finally good or bad I am getting some results, I can draw and press buttons and a few more things, but, now I am having problems with external buttons, or an external variable changing like a serial read.
I want to be able to press a button on the screen and get an output to turn on, or change the state on something, and when I receive an external input, like from a webserver or a physical button I need the output to change state and the button to change to the checked state on display to show that the output is on.
I have tested many examples but I am missing something and I am not getting the display button pressed when I change the external inputs, this is what I am trying to do, sorry I know it might be messy but, remember I am learning, any extra tip would be appreciated, also, I am getting a white line at the top of my screen and I have no idea why.
In my code all appear to work, except the buttons not getting pressed on the screen when I apply external input.
// for external inputs
int button0_pin = 10;
int button1_pin = 11;
int button2_pin = 12;
int button3_pin = 13;
int button4_pin = 14;
int button5_pin = 21;
#define LGFX_USE_V1
#include <LovyanGFX.hpp>
/* ALL DISPLAY CONFIGURATIONS REMOVED */
#include <lvgl.h>
LGFX tft;
#define screenWidth 480
#define screenHeight 320
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * 10];
void my_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);
//============================ here set the color - no idea why the white top line is showing
lv_obj_set_style_bg_color(lv_scr_act(), LV_COLOR_MAKE(0, 0, 0), LV_STATE_DEFAULT);
lv_obj_set_style_text_color(lv_scr_act(), lv_palette_lighten(LV_PALETTE_GREY, 5), 0);
//============================
tft.startWrite();
tft.setAddrWindow(area->x1, area->y1, w, h);
tft.writePixels((lgfx::rgb565_t *)&color_p->full, w * h);
tft.setBrightness(backlight);
tft.endWrite();
lv_disp_flush_ready(disp);
}
void my_touch_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
uint16_t touchX, touchY;
bool touched = tft.getTouch(&touchX, &touchY);
if (!touched) { data->state = LV_INDEV_STATE_REL; }
else {
data->state = LV_INDEV_STATE_PR;
data->point.x = touchX;
data->point.y = touchY;
Serial.print( "Data x " );
Serial.println( touchX );
Serial.print( "Data y " );
Serial.println( touchY );
}
}
// start lv stuff
//*********-------------**********
// Buttons Section
//*********-------------**********
#define NUM_BUTTONS 6
static lv_obj_t *buttons[NUM_BUTTONS];
static void btn_cb( lv_event_t *event ) {
lv_obj_t *btn = lv_event_get_target(event);
lv_obj_t *label = lv_obj_get_child(btn, 0); // Get a pointer to the label object containing button text
if( btn == buttons[0] ) { // Compare the object pointers to discover which object was clicked
printf( "1 Clicked!\n");
} else if( btn == buttons[1] ) {
printf( "2 Clicked!\n");
} else if( btn == buttons[2] ) {
printf( "3 Clicked!\n");
} else if( btn == buttons[3] ) {
printf( "4 Clicked!\n");
} else if( btn == buttons[4] ) {
printf( "5 Clicked!\n");
} else if( btn == buttons[5] ) {
printf( "6 Clicked!\n");
}
printf( "Button with label %s Clicked!\n", lv_label_get_text(label)); // Get the label text
}
static void create_screen( lv_obj_t *parent ) {
lv_obj_t *btn_txt;
buttons[0] = lv_btn_create( parent );
btn_txt = lv_label_create( buttons[0] );
lv_obj_set_align(btn_txt, LV_ALIGN_CENTER);
lv_label_set_text_fmt( btn_txt, "LED BAR", 1 );
lv_obj_set_pos( buttons[0], 20, 50 );
lv_obj_set_size(buttons[0], 160, 50);
lv_obj_add_flag(buttons[0], LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(buttons[0], btn_cb, LV_EVENT_CLICKED, NULL);
buttons[1] = lv_btn_create( parent );
btn_txt = lv_label_create( buttons[1] );
lv_obj_set_align(btn_txt, LV_ALIGN_CENTER);
lv_label_set_text_fmt( btn_txt, "LED FRONT", 2 );
lv_obj_set_pos( buttons[1], 200, 50 );
lv_obj_set_size(buttons[1], 160, 50);
lv_obj_add_flag(buttons[1], LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(buttons[1], btn_cb, LV_EVENT_CLICKED, NULL);
buttons[2] = lv_btn_create( parent );
btn_txt = lv_label_create( buttons[2] );
lv_obj_set_align(btn_txt, LV_ALIGN_CENTER);
lv_label_set_text_fmt( btn_txt, "LED REAR", 3 );
lv_obj_set_pos( buttons[2], 20, 110 );
lv_obj_set_size(buttons[2], 160, 50);
lv_obj_add_flag(buttons[2], LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(buttons[2], btn_cb, LV_EVENT_CLICKED, NULL);
buttons[3] = lv_btn_create( parent );
btn_txt = lv_label_create( buttons[3] );
lv_obj_set_align(btn_txt, LV_ALIGN_CENTER);
lv_label_set_text_fmt( btn_txt, "LED UNDER", 4 );
lv_obj_set_pos(buttons[3], 200, 110 );
// lv_obj_set_size(buttons[3], new_width, new_height)
lv_obj_set_size(buttons[3], 160, 50);
lv_obj_add_flag(buttons[3], LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(buttons[3], btn_cb, LV_EVENT_CLICKED, NULL);
buttons[4] = lv_btn_create( parent );
btn_txt = lv_label_create( buttons[4] );
lv_obj_set_align(btn_txt, LV_ALIGN_CENTER);
lv_label_set_text_fmt( btn_txt, "LED CUBES", 5 );
lv_obj_set_pos( buttons[4], 20, 170 );
lv_obj_set_size(buttons[4], 160, 50);
lv_obj_add_flag(buttons[4], LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(buttons[4], btn_cb, LV_EVENT_CLICKED, NULL);
buttons[5] = lv_btn_create( parent );
btn_txt = lv_label_create( buttons[5] );
lv_obj_set_align(btn_txt, LV_ALIGN_CENTER);
lv_label_set_text_fmt( btn_txt, "LED SIDE", 6 );
lv_obj_set_pos(buttons[5], 200, 170 );
// lv_obj_set_size(buttons[5], new_width, new_height)
lv_obj_set_size(buttons[5], 160, 50);
lv_obj_add_flag(buttons[5], LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(buttons[5], btn_cb, LV_EVENT_CLICKED, NULL);
}
//************-------------*************
// External Input buttons(or commands)
//************-------------*************
static void button_read(lv_indev_drv_t * indev_drv1, lv_indev_data_t*data)
{
// static uint32_t btn_id = 0;
static uint32_t last_btn = 0; //Store the last pressed button
int btn_pr = my_btn_read(); //Get the ID (0,1,2...) of the pressed button
if(btn_pr >= 0) { //Is there a button press? (E.g. -1 indicated no button was pressed)
last_btn = btn_pr; //Save the ID of the pressed button
//Serial.print(" Last btn: "); Serial.println(last_btn);
data->state = LV_INDEV_STATE_PR; //ESSED; //Set the pressed state
Serial.print(" pressed button: "); Serial.println(last_btn);
} else {
data->state = LV_INDEV_STATE_REL; //EASED; //Set the released state
Serial.print(" released button: "); Serial.println(last_btn);
}
data->btn_id = last_btn; //Save the last button
// Serial.print(" Last btn: "); Serial.println(last_btn);
}
//*External Button lv_port_Indev functions
//Initialize your buttons
static void inputs_init(void)
{
pinMode(button0_pin, INPUT_PULLUP);
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
pinMode(button3_pin, INPUT_PULLUP);
pinMode(button4_pin, INPUT_PULLUP);
pinMode(button5_pin, INPUT_PULLUP);
}
//Get ID (0, 1, 2 ..) of the pressed button
static int8_t my_btn_read(void)
{
uint8_t i;
//Check to buttons see which is being pressed (assume there are 6 buttons)
for(i = 0; i < 6; i++) {
//Return the pressed button's ID
if(button_is_pressed_1(i)) {
return i;
}
}
Serial.println(" no button pressed ");
//No button pressed
return -1;
}
//Test if `id` button is pressed or not
static bool button_is_pressed_1(uint8_t id)
{
if(id == 0)
{
if(digitalRead(button0_pin) == LOW)
{
Serial.println("0-0");
return true;
}else
{
Serial.println("0-1");
return false;
}
}
if(id == 1)
{
if(digitalRead(button1_pin) == LOW)
{
Serial.println("1-0");
return true;
}else
{
Serial.println("1-1");
return false;
}
}
if(id == 2)
{
if(digitalRead(button2_pin) == LOW)
{
Serial.println("2-0");
return true;
}else
{
Serial.println("2-1");
return false;
}
}
if(id == 3)
{
if(digitalRead(button3_pin) == LOW)
{
Serial.println("3-0");
return true;
} else
{
Serial.println("3-1");
return false;
}
}
if(id == 4)
{
if(digitalRead(button4_pin) == LOW)
{
Serial.println("4-0");
return true;
}else
{
Serial.println("4-1");
return false;
}
}
if(id == 5)
{
if(digitalRead(button5_pin) == LOW)
{
Serial.println("5-0");
return true;
}else
{
Serial.println("5-1");
return false;
}
}
return false;
}
//Initialization function:-
void lv_port_indev_init_1(void)
{
const lv_point_t points_array[6] = { {20,50},{200,50},{20,110},{200,110},{20,170},{200,170} };
static lv_indev_drv_t indev_drv1;
//Initialize your button if you have
inputs_init(); // will be used only if adding external inputs *******************
lv_indev_drv_init(&indev_drv1); //Basic initialization
indev_drv1.type = LV_INDEV_TYPE_BUTTON;
indev_drv1.read_cb = button_read;
//Register the driver in LVGL and save the created input device object
lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv1);
//Assign buttons to points on the screen
lv_indev_set_button_points(my_indev, points_array);
}
//************-------------*************
// end external inputs
//************-------------*************
void setup()
{
Serial.begin( 115200 ); /* prepare for possible serial debug */
lv_init();
tft.begin(); /* TFT init */
tft.setRotation( 3 ); /* Landscape orientation, flipped */
lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * 10 );
/*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 = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register( &disp_drv );
/*Initialize the (dummy) input device driver*/
static lv_indev_drv_t indev_drv;
lv_indev_drv_init( &indev_drv );
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touch_read;
lv_indev_drv_register( &indev_drv );
//init all stuff
/*External Buttons input Driver Initialization*/
lv_port_indev_init_1(); //**************************
create_screen( lv_scr_act() ); // create all buttons
}
void loop()
{
lv_timer_handler(); /* let the GUI do its work */
delay( 5 );
}
Thank you