mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-10-17 10:55:06 +00:00
- Create `USB_HOST_TYPE` base driver in TactilityKernel - Refactor esp32_usbhost dts format, and create child devices dynamically - Use PSRAM for task stacks where possible: saving 20 kB from just the task stacks, but roughly 30 kB when nothing is connected. - Allow the user to disable USB host interface in the USB settings app, saving an additionaly 20 kB of IRAM when nothing was connected. - Change listener-based APIs to subscribe and poll/await pattern. This avoids callstacks of apps and services leaking into the USB API This results in big IRAM savings: 30 kB when USB host is on and nothing is connected, and 50 kB total when USB host is disabled. This brings the total free IRAM after boot from roughly 170 kB to 200 kB (host enabled) or 220 kB (host disabled).
93 lines
3.1 KiB
C
93 lines
3.1 KiB
C
// SPDX-License-Identifier: Apache-2.0
|
|
#pragma once
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
struct Device;
|
|
struct DeviceType;
|
|
|
|
typedef void* UsbHidQueueHandle;
|
|
|
|
/** Key values — numerically identical to lv_key_t so no translation is needed. */
|
|
typedef enum {
|
|
USB_HID_KEY_UP = 17,
|
|
USB_HID_KEY_DOWN = 18,
|
|
USB_HID_KEY_RIGHT = 19,
|
|
USB_HID_KEY_LEFT = 20,
|
|
USB_HID_KEY_ESC = 27,
|
|
USB_HID_KEY_DEL = 127,
|
|
USB_HID_KEY_BACKSPACE = 8,
|
|
USB_HID_KEY_ENTER = 10,
|
|
USB_HID_KEY_NEXT = 9,
|
|
USB_HID_KEY_PREV = 11,
|
|
USB_HID_KEY_HOME = 2,
|
|
USB_HID_KEY_END = 3,
|
|
} UsbHidKey;
|
|
|
|
typedef enum {
|
|
USB_HID_EVENT_KEY,
|
|
USB_HID_EVENT_MOUSE_MOVE,
|
|
USB_HID_EVENT_MOUSE_BTN,
|
|
USB_HID_EVENT_SCROLL,
|
|
USB_HID_EVENT_CONSUMER,
|
|
USB_HID_EVENT_KEYBOARD_CONNECTED,
|
|
USB_HID_EVENT_KEYBOARD_DISCONNECTED,
|
|
USB_HID_EVENT_MOUSE_CONNECTED,
|
|
USB_HID_EVENT_MOUSE_DISCONNECTED,
|
|
} UsbHidEventType;
|
|
|
|
typedef struct {
|
|
UsbHidEventType type;
|
|
union {
|
|
/**
|
|
* @brief A key press or release.
|
|
*
|
|
* `ctrl` and `alt` report the modifiers separately rather than folding them into
|
|
* `key_code`, because the two encodings collide: the C0 control codes a terminal expects
|
|
* for Ctrl chords (Ctrl+C is 0x03, Ctrl+K is 0x0B) overlap the UsbHidKey constants above
|
|
* (USB_HID_KEY_END is 3, USB_HID_KEY_PREV is 11). A consumer wanting control codes derives
|
|
* them here, e.g.
|
|
* `((key_code >= 'a' && key_code <= 'z') || (key_code >= 'A' && key_code <= 'Z')) ?
|
|
* (key_code & 0x1F) : key_code`.
|
|
*/
|
|
struct { uint32_t key_code; bool pressed; bool ctrl; bool alt; } key;
|
|
struct { int32_t dx; int32_t dy; } mouse_move;
|
|
struct { bool button1; bool button2; } mouse_btn;
|
|
struct { int32_t delta; } scroll;
|
|
/**
|
|
* @brief A Consumer Control usage press or release (HID Usage Tables section 15),
|
|
* e.g. headset buttons. `usage` is the raw Consumer-page usage code, not a key code.
|
|
*/
|
|
struct { uint16_t usage; bool pressed; } consumer;
|
|
};
|
|
} UsbHidEvent;
|
|
|
|
struct UsbHidApi {
|
|
bool (*subscribe)(struct Device* device, UsbHidQueueHandle event_queue);
|
|
void (*unsubscribe)(struct Device* device, UsbHidQueueHandle event_queue);
|
|
};
|
|
|
|
/**
|
|
* A USB_HOST_HID_TYPE device exists while at least one HID interface (keyboard, mouse or consumer control) is connected.
|
|
*/
|
|
extern const struct DeviceType USB_HOST_HID_TYPE;
|
|
|
|
/**
|
|
* Subscribe a FreeRTOS queue to receive UsbHidEvent items from the HID driver.
|
|
* A new subscriber immediately receives a *_CONNECTED event for each device that is already connected.
|
|
* Subscriptions end when the device stops. Subscribe again on DEVICE_EVENT_STARTED of the next device.
|
|
*/
|
|
bool usb_host_hid_subscribe(struct Device* device, UsbHidQueueHandle event_queue);
|
|
|
|
/** Unsubscribe a previously subscribed queue. */
|
|
void usb_host_hid_unsubscribe(struct Device* device, UsbHidQueueHandle event_queue);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|