Hello, i am trying to connect a waveshare 7inch LCD HDMI(B) to my portenta. I have managed to get the screen working but i am so far unable to get the touchscreen to work. I have connected the portenta with a usb-c to a usb hub similarly to the GUI Tutorial, in addition to a mini usb from the display to the hub. I do not know what i should add/change in my code or even if i need different libraries. I tried most of the libraries for touchscreens but since it's a usb connection, im at loss. Everyone reading this thank you for your time.
#include "Portenta_LittleVGL.h"
static lv_indev_drv_t indev_drv;
static void touchpad_init(void);
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static bool touchpad_is_pressed(void);
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);
lv_indev_t * indev_touchpad;
static int32_t encoder_diff;
static lv_indev_state_t encoder_state;
/*
// Enable usage of EtherClass, to set static IP address and other
#include <PortentaEthernet.h>
arduino::EthernetClass eth(&m_netInterface);
*/
typedef uint8_t lv_menu_builder_variant_t;
static lv_obj_t * slider_label;
static lv_obj_t * create_text(lv_obj_t * parent, const char * icon, const char * txt, lv_menu_builder_variant_t builder_variant);
static lv_obj_t * create_slider(lv_obj_t * parent, const char * icon, const char * txt, int32_t min, int32_t max, int32_t val);
static lv_obj_t * create_switch(lv_obj_t * parent, const char * icon, const char * txt, bool chk);
static void slider_event_cb(lv_event_t * e);
static void double_slider_event_cb(lv_event_t * e);
lv_obj_t * root_page;
enum {
LV_MENU_ITEM_BUILDER_VARIANT_1,
LV_MENU_ITEM_BUILDER_VARIANT_2
};
void setup()
{
portenta_init_video();
lv_init();
/*
// Configure static IP address
IPAddress ip(192, 168, 1, 1);
IPAddress dns(8, 8, 8, 8);
IPAddress gateway(192, 168, 1, 0);
IPAddress subnet(255, 255, 255, 0);
// If cable is not connected this will block the start of PLC with about 60s of timeout!
eth.begin(ip, dns, gateway, subnet);
*/
lv_port_indev_init();
lv_obj_t * menu = lv_menu_create(lv_scr_act());
lv_color_t bg_color = lv_obj_get_style_bg_color(menu, 0);
if(lv_color_brightness(bg_color) > 127) {
lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 10), 0);
}
else {
lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 50), 0);
}
lv_obj_set_size(menu, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
lv_obj_center(menu);
lv_obj_t * cont;
lv_obj_t * section;
/*Create sub pages*/
lv_obj_t * sub_variables_page = lv_menu_page_create(menu, NULL);
lv_obj_set_style_pad_hor(sub_variables_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
lv_menu_separator_create(sub_variables_page);
section = lv_menu_section_create(sub_variables_page);
create_slider(section, LV_SYMBOL_SETTINGS, "Brightness", 0, 100, 60);
create_slider(section, LV_SYMBOL_SETTINGS, "Air Flow", 0, 2, 1);
create_double_slider(section, LV_SYMBOL_SETTINGS, " ", 0, 3600, 240, 60);
lv_obj_t * sub_sensors_page = lv_menu_page_create(menu, NULL);
lv_obj_set_style_pad_hor(sub_sensors_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
lv_menu_separator_create(sub_sensors_page);
section = lv_menu_section_create(sub_sensors_page);
lv_obj_t * sub_data_page = lv_menu_page_create(menu, NULL);
lv_obj_set_style_pad_hor(sub_data_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
lv_menu_separator_create(sub_data_page);
section = lv_menu_section_create(sub_data_page);
/*Create a root page*/
root_page = lv_menu_page_create(menu, "Settings");
lv_obj_set_style_pad_hor(root_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
section = lv_menu_section_create(root_page);
cont = create_text(section, LV_SYMBOL_SETTINGS, "Adjustments", LV_MENU_ITEM_BUILDER_VARIANT_1);
lv_menu_set_load_page_event(menu, cont, sub_variables_page);
cont = create_text(section, LV_SYMBOL_USB, "Sensors", LV_MENU_ITEM_BUILDER_VARIANT_1);
lv_menu_set_load_page_event(menu, cont, sub_sensors_page);
cont = create_text(section, LV_SYMBOL_DIRECTORY, "Data", LV_MENU_ITEM_BUILDER_VARIANT_1);
lv_menu_set_load_page_event(menu, cont, sub_data_page);
lv_menu_set_sidebar_page(menu, root_page);
lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(menu), 0), 0), LV_EVENT_CLICKED,
NULL);
}
void loop()
{
lv_task_handler();
delay(5);
}
static void slider_event_cb(lv_event_t * e)
{
lv_obj_t * slider = lv_event_get_target(e);
char buf[8];
/*Refresh the text*/
lv_snprintf(buf, sizeof(buf), "%d%%", (int)lv_slider_get_value(slider));
lv_label_set_text(slider_label, buf);
/*Align top of the slider*/
lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_TOP_MID, 0, 10);
}
static void double_slider_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
/*Provide some extra space for the value*/
if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
lv_event_set_ext_draw_size(e, 50);
}
else if(code == LV_EVENT_DRAW_PART_END) {
lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
if(dsc->part == LV_PART_INDICATOR) {
char buf[30];
lv_snprintf(buf, sizeof(buf), "%dsec on - \r%dsec off", (int)lv_slider_get_left_value(obj), (int)lv_slider_get_value(obj));
lv_point_t label_size;
lv_txt_get_size(&label_size, buf, LV_FONT_DEFAULT, 0, 0, 100, 0);
lv_area_t label_area;
label_area.x1 = dsc->draw_area->x1 + lv_area_get_width(dsc->draw_area) / 2 - label_size.x / 2;
label_area.x2 = label_area.x1 + label_size.x;
label_area.y2 = dsc->draw_area->y1 - 10;
label_area.y1 = label_area.y2 - label_size.y;
lv_draw_label_dsc_t label_draw_dsc;
lv_draw_label_dsc_init(&label_draw_dsc);
lv_draw_label(dsc->draw_ctx, &label_draw_dsc, &label_area, buf, NULL);
}
}
}
static lv_obj_t * create_text(lv_obj_t * parent, const char * icon, const char * txt, lv_menu_builder_variant_t builder_variant)
{
lv_obj_t * obj = lv_menu_cont_create(parent);
lv_obj_t * img = NULL;
lv_obj_t * label = NULL;
if(icon) {
img = lv_img_create(obj);
lv_img_set_src(img, icon);
}
if(txt) {
label = lv_label_create(obj);
lv_label_set_text(label, txt);
lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
lv_obj_set_flex_grow(label, 1);
}
if(builder_variant == LV_MENU_ITEM_BUILDER_VARIANT_2 && icon && txt) {
lv_obj_add_flag(img, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
lv_obj_swap(img, label);
}
return obj;
}
static lv_obj_t * create_slider(lv_obj_t * parent, const char * icon, const char * txt, int32_t min, int32_t max, int32_t val)
{
lv_obj_t * obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_2);
lv_obj_t * slider = lv_slider_create(obj);
lv_obj_set_flex_grow(slider, 1);
lv_slider_set_range(slider, min, max);
lv_slider_set_value(slider, val, LV_ANIM_OFF);
if(icon == NULL) {
lv_obj_add_flag(slider, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
}
return obj;
}
static lv_obj_t * create_double_slider(lv_obj_t * parent, const char * icon, const char * txt, int32_t min, int32_t max, int val, int left_val)
{
lv_obj_t * obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_2);
lv_obj_t * slider;
slider = lv_slider_create(obj);
lv_obj_center(obj);
lv_slider_set_mode(slider, LV_SLIDER_MODE_RANGE);
lv_slider_set_range(slider, min, max);
lv_slider_set_value(slider, val, LV_ANIM_OFF);
lv_slider_set_left_value(slider, left_val, LV_ANIM_OFF);
lv_obj_add_event_cb(slider, double_slider_event_cb, LV_EVENT_ALL, NULL);
lv_obj_refresh_ext_draw_size(slider);
}
static void touchpad_init(void)
{
/*Your code comes here*/
}
void lv_port_indev_init(void)
{
touchpad_init();
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = touchpad_read;
lv_indev_drv_register(&indev_drv);
}
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
// static lv_coord_t last_x = 0;
// static lv_coord_t last_y = 0;
uint16_t last_x, last_y;
// /*Save the pressed coordinates and the state*/
if(touchpad_is_pressed()) {
touchpad_get_xy(&last_x, &last_y);
data->state = LV_INDEV_STATE_PR;
}
else {
data->state = LV_INDEV_STATE_REL;
}
/*Set the last pressed coordinates*/
data->point.x = last_x;
data->point.y = last_y;
// data->state = LV_INDEV_STATE_PR or LV_INDEV_STATE_RELEASED;
}
static bool touchpad_is_pressed(void)
{
/*Your code comes here*/
return false;
}
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
/*Your code comes here*/
(*x) = 0;
(*y) = 0;
}