clean up example

This commit is contained in:
hathach 2022-05-11 15:40:02 +07:00
parent 8bd923f5f1
commit f09df55ab9
No known key found for this signature in database
GPG Key ID: F5D50C6D51D17CBA
2 changed files with 17 additions and 13 deletions

View File

@ -23,10 +23,8 @@
* *
*/ */
// This example runs both host and device concurrently. The USB host looks for // This example runs both host and device concurrently. The USB host receive
// any HID device with reports that are 8 bytes long and then assumes they are // reports from HID device and print it out over USB Device CDC interface.
// keyboard reports. It translates the keypresses of the reports to ASCII and
// transmits it over CDC to the device's host.
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -157,7 +155,11 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
uint16_t vid, pid; uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid); tuh_vid_pid_get(dev_addr, &vid, &pid);
printf("[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); char tempbuf[256];
int count = sprintf(tempbuf, "[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
// Receive report from boot keyboard & mouse only // Receive report from boot keyboard & mouse only
// tuh_hid_report_received_cb() will be invoked when report is available // tuh_hid_report_received_cb() will be invoked when report is available
@ -165,7 +167,7 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
{ {
if ( !tuh_hid_receive_report(dev_addr, instance) ) if ( !tuh_hid_receive_report(dev_addr, instance) )
{ {
printf("Error: cannot request report\r\n"); tud_cdc_write_str("Error: cannot request report\r\n");
} }
} }
} }
@ -173,7 +175,10 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
// Invoked when device with hid interface is un-mounted // Invoked when device with hid interface is un-mounted
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
{ {
printf("[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance); char tempbuf[256];
int count = sprintf(tempbuf, "[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
} }
// look up new key in previous keys // look up new key in previous keys
@ -234,16 +239,14 @@ static void process_kbd_report(hid_keyboard_report_t const *report)
// send mouse report to usb device CDC // send mouse report to usb device CDC
static void process_mouse_report(hid_mouse_report_t const * report) static void process_mouse_report(hid_mouse_report_t const * report)
{ {
char tempbuf[32];
int count;
//------------- button state -------------// //------------- button state -------------//
//uint8_t button_changed_mask = report->buttons ^ prev_report.buttons; //uint8_t button_changed_mask = report->buttons ^ prev_report.buttons;
char l = report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-'; char l = report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-';
char m = report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-'; char m = report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-';
char r = report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-'; char r = report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-';
count = sprintf(tempbuf, " %c%c%c %d %d %d\r\n", l, m, r, report->x, report->y, report->wheel); char tempbuf[32];
int count = sprintf(tempbuf, "%c%c%c %d %d %d\r\n", l, m, r, report->x, report->y, report->wheel);
tud_cdc_write(tempbuf, count); tud_cdc_write(tempbuf, count);
tud_cdc_write_flush(); tud_cdc_write_flush();
@ -252,6 +255,7 @@ static void process_mouse_report(hid_mouse_report_t const * report)
// Invoked when received report from device via interrupt endpoint // Invoked when received report from device via interrupt endpoint
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len)
{ {
(void) len;
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
switch(itf_protocol) switch(itf_protocol)
@ -270,7 +274,7 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
// continue to request to receive report // continue to request to receive report
if ( !tuh_hid_receive_report(dev_addr, instance) ) if ( !tuh_hid_receive_report(dev_addr, instance) )
{ {
printf("Error: cannot request report\r\n"); tud_cdc_write_str("Error: cannot request report\r\n");
} }
} }

@ -1 +1 @@
Subproject commit 9d2b7dae4261e0f6a336a125972e16aab65be722 Subproject commit 53ec09168c5a3cd30d8dcb577fa97dc328d769d4