Export Interface
Last updated
Last updated
Após o desenvolvimento da UI, podemos exportar os arquivos UIs para a pasta principal do nosso projeto, isto é, para a pasta main.
Após a exportação dos arquivos, podemos abrir a pasta com o VSCode para configuração dos arquivos gerados.
Serão criados as seguites pastas:
images (para as imagens adicionadas na interface);
fonts (para as fontes adicionadas na interface);
screens ( para as telas criadas);
components (para os componentes criados);
Temos que adicionar cada pasta/arquivos no arquivo CMakeList.txt para a compilação.
Após a configuração do CMake, devemos chamar a função ui_init();
para carregar a interface criada no SquareLine Studio.
// Some code
```c
#include <stdio.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <freertos/queue.h>
#include <esp_lcd_types.h>
#include <driver/gpio.h>
#include <esp_err.h>
#include <esp_log.h>
#include "ESPMI.h"
#include <usb/hid_host.h>
#include <usb/usb_host.h>
#include "HDMI.h"
#include "lvgl.h"
#include "ui.h"
int x_coord = 0;
int y_coord = 0;
int touchscreen_interrupt = 0;
int isPressed = 0;
static const char TAG [] = "Main";
QueueHandle_t hid_host_event_queue;
inline int map_coord(int coord, int max_touchscreen, int max_screen) {
return ((float)coord / max_touchscreen) * max_screen;
}
void touchscreen_read(struct _lv_indev_drv_t * indev_drv, lv_indev_data_t * data) {
if (touchscreen_interrupt) {
data->point.x = x_coord;
data->point.y = y_coord;
data->state = isPressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
touchscreen_interrupt = false;
}
}
/**
* @brief HID Host event
*
* This event is used for delivering the HID Host event from callback to a task.
*/
typedef struct {
hid_host_device_handle_t hid_device_handle;
hid_host_driver_event_t event;
void *arg;
} hid_host_event_queue_t;
/**
* @brief HID Protocol string names
*/
static const char *hid_proto_name_str[] = {
"NONE",
"KEYBOARD",
"MOUSE"
};
/**
* @brief USB HID Host Generic Interface report callback handler
*
* 'generic' means anything else than mouse or keyboard
*
* @param[in] data Pointer to input report data buffer
* @param[in] length Length of input report data buffer
*/
static void hid_host_generic_report_callback(const uint8_t *const data, const int length){
uint16_t x = ((uint8_t)data[3] << 8)| data[2];
uint16_t y = ((uint8_t)data[5] << 8)| data[4];
x_coord = map_coord(x, 16383, 640);
y_coord = map_coord(y, 16282, 480);
touchscreen_interrupt = 1;
isPressed = data[1] >> 6;
}
/**
* @brief USB HID Host interface callback
*
* @param[in] hid_device_handle HID Device handle
* @param[in] event HID Host interface event
* @param[in] arg Pointer to arguments, does not used
*/
void hid_host_interface_callback(hid_host_device_handle_t hid_device_handle, const hid_host_interface_event_t event, void *arg) {
uint8_t data[64] = { 0 };
size_t data_length = 0;
hid_host_dev_params_t dev_params;
ESP_ERROR_CHECK( hid_host_device_get_params(hid_device_handle, &dev_params));
switch (event) {
case HID_HOST_INTERFACE_EVENT_INPUT_REPORT:
ESP_ERROR_CHECK( hid_host_device_get_raw_input_report_data(hid_device_handle, data, 64, &data_length));
if (HID_SUBCLASS_BOOT_INTERFACE == dev_params.sub_class) {
if (HID_PROTOCOL_KEYBOARD == dev_params.proto) {
} else if (HID_PROTOCOL_MOUSE == dev_params.proto) {
}
} else {
hid_host_generic_report_callback(data, data_length);
}
break;
case HID_HOST_INTERFACE_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HID Device, protocol '%s' DISCONNECTED", hid_proto_name_str[dev_params.proto]);
ESP_ERROR_CHECK( hid_host_device_close(hid_device_handle) );
break;
case HID_HOST_INTERFACE_EVENT_TRANSFER_ERROR:
ESP_LOGI(TAG, "HID Device, protocol '%s' TRANSFER_ERROR", hid_proto_name_str[dev_params.proto]);
break;
default:
ESP_LOGE(TAG, "HID Device, protocol '%s' Unhandled event", hid_proto_name_str[dev_params.proto]);
break;
}
}
/**
* @brief USB HID Host Device event
*
* @param[in] hid_device_handle HID Device handle
* @param[in] event HID Host Device event
* @param[in] arg Pointer to arguments, does not used
*/
void hid_host_device_event(hid_host_device_handle_t hid_device_handle, const hid_host_driver_event_t event, void *arg) {
hid_host_dev_params_t dev_params;
ESP_ERROR_CHECK( hid_host_device_get_params(hid_device_handle, &dev_params));
switch (event) {
case HID_HOST_DRIVER_EVENT_CONNECTED:
ESP_LOGI(TAG, "HID Device, protocol '%s' CONNECTED", hid_proto_name_str[dev_params.proto]);
const hid_host_device_config_t dev_config = {
.callback = hid_host_interface_callback,
.callback_arg = NULL
};
ESP_ERROR_CHECK( hid_host_device_open(hid_device_handle, &dev_config) );
if (HID_SUBCLASS_BOOT_INTERFACE == dev_params.sub_class) {
ESP_ERROR_CHECK( hid_class_request_set_protocol(hid_device_handle, HID_REPORT_PROTOCOL_BOOT));
if (HID_PROTOCOL_KEYBOARD == dev_params.proto) {
ESP_ERROR_CHECK( hid_class_request_set_idle(hid_device_handle, 0, 0));
}
}
ESP_ERROR_CHECK( hid_host_device_start(hid_device_handle) );
break;
default:
break;
}
}
/**
* @brief Start USB Host install and handle common USB host library events while app pin not low
*
* @param[in] arg Not used
*/
static void usb_lib_task(void *arg){
const usb_host_config_t host_config = {
.skip_phy_setup = false,
.intr_flags = ESP_INTR_FLAG_LEVEL1,
};
ESP_ERROR_CHECK( usb_host_install(&host_config) );
xTaskNotifyGive(arg);
for(;;){
uint32_t event_flags;
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
// Release devices once all clients has deregistered
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
usb_host_device_free_all();
ESP_LOGI(TAG, "USB Event flags: NO_CLIENTS");
}
// All devices were removed
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
ESP_LOGI(TAG, "USB Event flags: ALL_FREE");
}
}
}
/**
* @brief HID Host Device callback
*
* Puts new HID Device event to the queue
*
* @param[in] hid_device_handle HID Device handle
* @param[in] event HID Device event
* @param[in] arg Not used
*/
void hid_host_device_callback(hid_host_device_handle_t hid_device_handle, const hid_host_driver_event_t event, void *arg){
const hid_host_event_queue_t evt_queue = {
.hid_device_handle = hid_device_handle,
.event = event,
.arg = arg
};
xQueueSend(hid_host_event_queue, &evt_queue, 0);
}
/**
* @brief HID Host main task
*
* Creates queue and get new event from the queue
*
* @param[in] pvParameters Not used
*/
void hid_host_task(void *pvParameters){
hid_host_event_queue_t evt_queue;
// Create queue
hid_host_event_queue = xQueueCreate(10, sizeof(hid_host_event_queue_t));
for(;;) {
if (xQueueReceive(hid_host_event_queue, &evt_queue, pdMS_TO_TICKS(50))) {
hid_host_device_event(evt_queue.hid_device_handle, evt_queue.event, evt_queue.arg);
}
}
}
void app_main(void){
uint32_t time_till_next = 0;
xTaskCreatePinnedToCore(usb_lib_task, "usb_events", 4096, xTaskGetCurrentTaskHandle(), 2, NULL, 0);
ESPMI_ConfigI2C(ESPMI_I2C_MASTER_NUM);
ulTaskNotifyTake(false, 1000);
/*
* HID host driver configuration
* - create background task for handling low level event inside the HID driver
* - provide the device callback to get new HID Device connection event
*/
const hid_host_driver_config_t hid_host_driver_config = {
.create_background_task = true,
.task_priority = 5,
.stack_size = 4096,
.core_id = 0,
.callback = hid_host_device_callback,
.callback_arg = NULL
};
ESP_ERROR_CHECK( hid_host_install(&hid_host_driver_config) );
/*
* Create HID Host task process for handle events
* IMPORTANT: Task is necessary here while there is no possibility to interact
* with USB device from the callback.
*/
xTaskCreate(&hid_host_task, "hid_task", 4 * 1024, NULL, 2, NULL);
esp_lcd_panel_handle_t panel = HDMI_Initialize(xTaskGetCurrentTaskHandle());
HDMI_LVGLConfig(panel, touchscreen_read);
ui_init();
while (1) {
time_till_next = lv_timer_handler();
vTaskDelay(time_till_next);
}
}
```