@gordsh Oh great thanks a lot, I actually got it working,
and after seeing your code i tried to implement the button part with encoder but the dial seems to crash repeatedly because of it. For now that is not my focus so i will try it again later.
Also , do you have any idea how the implementation of encoder is eez studio is.
i understood that the group is to be made but didnt quite understand how to append/impelment the encoder events to it
If anyone has any idea or reference, Thank you in advance.
also this is the code for how i implemented the button press with encoder
// Encoder read function
void encoder_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
long newPosition = M5Dial.Encoder.read() / 4; // Normalize encoder position
if (newPosition != oldPosition) {
int encoderDiff = newPosition - oldPosition;
oldPosition = newPosition;
// React to encoder rotation
if (encoderDiff > 0) {
lv_group_focus_next(group);
} else if (encoderDiff < 0) {
lv_group_focus_prev(group);
}
}
// Check button press
if (M5Dial.BtnA.wasPressed()) {
data->state = LV_INDEV_STATE_PR;
Serial.println("M5.BtnA Pressed");
} else {
data->state = LV_INDEV_STATE_REL;
}
}
// Display flush function
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
uint32_t width = (area->x2 - area->x1 + 1);
uint32_t height = (area->y2 - area->y1 + 1);
M5.Display.startWrite();
M5.Display.setAddrWindow(area->x1, area->y1, width, height);
M5.Display.pushPixels((uint16_t *)&color_p->full, width * height, true);
M5.Display.endWrite();
lv_disp_flush_ready(disp);
}
void setup() {
auto cfg = M5.config();
M5Dial.begin(cfg, true, false);
Serial.begin(115200);
// LVGL initialization
lv_init();
lv_disp_draw_buf_init(&drawBuffer, buffer, NULL, SCREEN_WIDTH * SCREEN_HEIGHT / 10);
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = SCREEN_WIDTH;
disp_drv.ver_res = SCREEN_HEIGHT;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &drawBuffer;
lv_disp_drv_register(&disp_drv);
static lv_indev_drv_t encoder_drv;
lv_indev_drv_init(&encoder_drv);
encoder_drv.type = LV_INDEV_TYPE_ENCODER;
encoder_drv.read_cb = encoder_read;
encoder_indev = lv_indev_drv_register(&encoder_drv);
// Create a group and assign it to the encoder input device
group = lv_group_create();
lv_indev_set_group(encoder_indev, group);
// Create focusable widgets
lv_obj_t *btn1 = lv_btn_create(lv_scr_act());
lv_obj_align(btn1, LV_ALIGN_CENTER, -60, 0);
lv_obj_t *label1 = lv_label_create(btn1);
lv_label_set_text(label1, "Button 1");
lv_obj_t *btn2 = lv_btn_create(lv_scr_act());
lv_obj_align(btn2, LV_ALIGN_CENTER, 60, 0);
lv_obj_t *label2 = lv_label_create(btn2);
lv_label_set_text(label2, "Button 2");
lv_obj_t *btn3 = lv_btn_create(lv_scr_act());
lv_obj_align(btn3, LV_ALIGN_CENTER, 0, 50);
lv_obj_t *label3 = lv_label_create(btn3);
lv_label_set_text(label3, "Button 3");
// Add widgets to the group
lv_group_add_obj(group, btn1);
lv_group_add_obj(group, btn2);
lv_group_add_obj(group, btn3);
}