Skip to content

Commit

Permalink
Merge pull request #250 from espressif/example/display_usb_hid
Browse files Browse the repository at this point in the history
examples: Add LVGL USB HID input (keyboard + mouse) example.
  • Loading branch information
espzav authored Nov 24, 2023
2 parents cccb91f + 0014a5c commit caaaafc
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 19 deletions.
3 changes: 0 additions & 3 deletions examples/audio/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions examples/display_audio_photo/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions examples/display_rotation/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions examples/display_sensors/.gitignore

This file was deleted.

9 changes: 9 additions & 0 deletions examples/display_usb_hid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

set(COMPONENTS main) # "Trim" the build. Include the minimal set of components; main and anything it depends on.
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(display-usb-hid)
58 changes: 58 additions & 0 deletions examples/display_usb_hid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# BSP: Display USB HID Example

This example demonstrates usage of the USB HID (keyboard, mouse or GamePad) with Board Support Package.

## How to use the example

### Hardware Required

* Board ESP32-S3-USB-OTG

### Compile and flash

```
idf.py -p COMx flash monitor
```

### Example outputs

```
I (0) cpu_start: App cpu up.
I (235) cpu_start: Pro cpu start user code
I (235) cpu_start: cpu freq: 160000000 Hz
I (235) cpu_start: Application information:
I (238) cpu_start: Project name: display-usb-hid
I (244) cpu_start: App version: squareline-latest-21-g87b0e54
I (251) cpu_start: Compile time: Apr 5 2023 14:15:08
I (257) cpu_start: ELF file SHA256: a74d139d1fcf326b...
I (263) cpu_start: ESP-IDF: v5.0-dev-8835-g29737e1bc8a-dirt
I (270) cpu_start: Min chip rev: v0.0
I (275) cpu_start: Max chip rev: v0.99
I (279) cpu_start: Chip rev: v0.1
I (284) heap_init: Initializing. RAM available for dynamic allocation:
I (291) heap_init: At 3FC976B8 len 00052058 (328 KiB): DRAM
I (298) heap_init: At 3FCE9710 len 00005724 (21 KiB): STACK/DRAM
I (304) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (310) heap_init: At 600FE010 len 00001FF0 (7 KiB): RTCRAM
I (318) spi_flash: detected chip: gd
I (321) spi_flash: flash io: dio
W (325) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (338) sleep: Configure to isolate all GPIO pins in sleep state
I (345) sleep: Enable automatic switching of GPIO sleep configuration
I (352) app_start: Starting scheduler on CPU0
I (357) app_start: Starting scheduler on CPU1
I (357) main_task: Started on CPU0
I (367) main_task: Calling app_main()
I (367) gpio: GPIO[18]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (377) gpio: GPIO[12]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (387) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (397) gpio: GPIO[17]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (467) USB-OTG: Installing USB Host
I (497) LVGL: Starting LVGL task
I (497) gpio: GPIO[4]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (497) gpio: GPIO[8]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (627) USB-OTG: Setting LCD backlight: 100%
I (667) example: Keyboard input device group was set.
I (667) example: Example initialization done.
I (667) main_task: Returned from app_main()
```
2 changes: 2 additions & 0 deletions examples/display_usb_hid/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
idf_component_register(SRCS "main.c"
INCLUDE_DIRS ".")
7 changes: 7 additions & 0 deletions examples/display_usb_hid/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
description: BSP Display USB HID Example
dependencies:
idf: ">=4.4"
usb_host_hid: "*"
esp32_s3_usb_otg:
version: "*"
override_path: "../../../bsp/esp32_s3_usb_otg"
159 changes: 159 additions & 0 deletions examples/display_usb_hid/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/

#include "esp_log.h"
#include "bsp/esp-bsp.h"
#include "lvgl.h"
#include "esp_lvgl_port.h"

static const char *TAG = "example";

static lv_disp_t *display;
static lv_indev_t *kb_indev = NULL;
static lv_obj_t *result = NULL;
static lv_obj_t *ta_name = NULL;
static lv_obj_t *ta_email = NULL;
static lv_group_t *kb_group = NULL;

/*******************************************************************************
* Private functions
*******************************************************************************/
static void app_lvgl_btn_back_cb(lv_event_t *e)
{
if (result) {
lv_obj_clean(result);
lv_obj_del(result);
result = NULL;
}
if (kb_indev) {
lv_indev_set_group(kb_indev, kb_group);
}
}

static void app_lvgl_btn_ok_cb(lv_event_t *e)
{
lv_obj_t *scr = lv_scr_act();
const char *name = lv_textarea_get_text(ta_name);
const char *email = lv_textarea_get_text(ta_email);

result = lv_obj_create(scr);
lv_obj_set_size(result, BSP_LCD_V_RES, BSP_LCD_H_RES);
lv_obj_align(result, LV_ALIGN_TOP_MID, 0, 0);
lv_obj_set_flex_flow(result, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(result, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

lv_obj_t *lbl = lv_label_create(result);
lv_label_set_text_fmt(lbl, "Your name: %s", name);
lbl = lv_label_create(result);
lv_label_set_text_fmt(lbl, "Your email: %s", email);

lv_obj_t *back_btn = lv_btn_create(result);
lbl = lv_label_create(back_btn);
lv_label_set_text_static(lbl, "BACK");
/* Button event */
lv_obj_add_event_cb(back_btn, app_lvgl_btn_back_cb, LV_EVENT_CLICKED, NULL);

if (kb_indev) {
lv_group_t *group = lv_group_create();
lv_group_add_obj(group, back_btn);
lv_indev_set_group(kb_indev, group);
}
}

static void app_lvgl_display(void)
{
lv_obj_t *scr = lv_scr_act();
lv_obj_t *lbl;
bsp_display_lock(0);

lv_obj_t *col = lv_obj_create(scr);
lv_obj_set_size(col, BSP_LCD_V_RES, BSP_LCD_H_RES);
lv_obj_align(col, LV_ALIGN_TOP_MID, 0, 0);
lv_obj_set_flex_flow(col, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(col, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

lbl = lv_label_create(col);
lv_label_set_text(lbl, "USB HID Example");

lv_obj_t *cont_row = lv_obj_create(col);
lv_obj_set_size(cont_row, BSP_LCD_V_RES - 10, 50);
lv_obj_align(cont_row, LV_ALIGN_TOP_MID, 0, 20);
lv_obj_set_flex_flow(cont_row, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_top(cont_row, 5, 0);
lv_obj_set_style_pad_bottom(cont_row, 5, 0);
lv_obj_set_style_pad_left(cont_row, 5, 0);
lv_obj_set_style_pad_right(cont_row, 5, 0);
lv_obj_set_flex_align(cont_row, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

lbl = lv_label_create(cont_row);
lv_label_set_text(lbl, "Name:");
ta_name = lv_textarea_create(cont_row);
lv_obj_set_width(ta_name, BSP_LCD_V_RES - 100);
lv_textarea_set_one_line(ta_name, true);

cont_row = lv_obj_create(col);
lv_obj_set_size(cont_row, BSP_LCD_V_RES - 10, 50);
lv_obj_align(cont_row, LV_ALIGN_TOP_MID, 0, 20);
lv_obj_set_flex_flow(cont_row, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_top(cont_row, 5, 0);
lv_obj_set_style_pad_bottom(cont_row, 5, 0);
lv_obj_set_style_pad_left(cont_row, 5, 0);
lv_obj_set_style_pad_right(cont_row, 5, 0);
lv_obj_set_flex_align(cont_row, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

lbl = lv_label_create(cont_row);
lv_label_set_text(lbl, "E-mail:");
ta_email = lv_textarea_create(cont_row);
lv_obj_set_width(ta_email, BSP_LCD_V_RES - 100);
lv_textarea_set_one_line(ta_email, true);

/* Button OK */
lv_obj_t *btn_ok = lv_btn_create(col);
lbl = lv_label_create(btn_ok);
lv_label_set_text_static(lbl, "OK");
/* Button event */
lv_obj_add_event_cb(btn_ok, app_lvgl_btn_ok_cb, LV_EVENT_CLICKED, NULL);

if (kb_indev) {
kb_group = lv_group_create();
lv_group_add_obj(kb_group, ta_name);
lv_group_add_obj(kb_group, ta_email);
lv_group_add_obj(kb_group, btn_ok);
lv_indev_set_group(kb_indev, kb_group);
ESP_LOGI(TAG, "Keyboard input device group was set.");
}

bsp_display_unlock();
}

void app_main(void)
{
/* Initialize USB */
bsp_usb_host_start(BSP_USB_HOST_POWER_MODE_USB_DEV, true);

/* Initialize display and LVGL */
display = bsp_display_start();

/* Set display brightness to 100% */
bsp_display_backlight_on();

const lvgl_port_hid_mouse_cfg_t mouse_cfg = {
.disp = display,
.sensitivity = 1,
};
lvgl_port_add_usb_hid_mouse_input(&mouse_cfg);

const lvgl_port_hid_keyboard_cfg_t kb_cfg = {
.disp = display,
};
kb_indev = lvgl_port_add_usb_hid_keyboard_input(&kb_cfg);

/* Add and show objects on display */
app_lvgl_display();

ESP_LOGI(TAG, "Example initialization done.");

}
8 changes: 8 additions & 0 deletions examples/display_usb_hid/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated using idf.py save-defconfig. It can be edited manually.
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
#
CONFIG_IDF_TARGET="esp32s3"
CONFIG_LV_COLOR_16_SWAP=y
CONFIG_LV_MEM_CUSTOM=y
CONFIG_LV_MEMCPY_MEMSET_STD=y
CONFIG_LV_USE_PERF_MONITOR=y
3 changes: 0 additions & 3 deletions examples/mqtt_example/.gitignore

This file was deleted.

0 comments on commit caaaafc

Please sign in to comment.