Files
webkeyboard/hid.c
T
2025-05-28 18:16:00 -04:00

137 lines
3.9 KiB
C

/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
* sekigon-gonnoc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "tusb.h"
#include "usb_descriptors.h"
#include "server.h"
#include "main.h"
/*------------- MAIN -------------*/
int run_hid_device(void) {
// init device stack on native usb (roothub port0)
tud_init(0);
while (true) {
tud_task(); // tinyusb device task
//tud_cdc_write_flush();
}
return 0;
}
// Invoked when received SET_REPORT control request or
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
{
if (instance == 0 && report_type==HID_REPORT_TYPE_OUTPUT && report_id==REPORT_ID_KEYBOARD && bufsize==1) {
//received keyboard LED status, so update
set_indicator(buffer);
}
char tempbuf[128];
size_t count;
if(bufsize>0) {
count = sprintf(tempbuf, "\nReceived device set report type %u on interface %u with ID %u (%u)\n", report_type, instance, report_id, bufsize);
tud_cdc_write(tempbuf, count);
for(int i=0;i<bufsize;i++){
count=sprintf(tempbuf,"%02X ",buffer[i]);
tud_cdc_write(tempbuf,count);
}
count = sprintf(tempbuf, "\n");
tud_cdc_write(tempbuf,count);
tud_cdc_write_flush();
}
}
// Invoked when received GET_REPORT control request
// Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
{
// TODO not Implemented
(void) instance;
(void) report_id;
(void) report_type;
(void) buffer;
(void) reqlen;
char tempbuf[128];
size_t count;
if(reqlen>0) {
count = sprintf(tempbuf, "\nReceived device get report type %u on interface %u with ID %u (%u)\n", report_type, instance, report_id, reqlen);
tud_cdc_write(tempbuf, count);
for(int i=0;i<reqlen;i++){
count=sprintf(tempbuf,"%02X ",buffer[i]);
tud_cdc_write(tempbuf,count);
}
count = sprintf(tempbuf, "\n");
tud_cdc_write(tempbuf,count);
tud_cdc_write_flush();
}
return 0;
}
// Invoked when sent REPORT successfully to host
// Application can use this to send the next report
// Note: For composite reports, report[0] is report ID
/*
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len)
{
(void) instance;
(void) len;
char tempbuf[128];
size_t count;
if(len>0) {
count = sprintf(tempbuf, "\nDevice sent report on interface %u (%u)\n", instance, len);
tud_cdc_write(tempbuf, count);
for(int i=0;i<len;i++){
count=sprintf(tempbuf,"%02X ",report[i]);
tud_cdc_write(tempbuf,count);
}
count = sprintf(tempbuf, "\n");
tud_cdc_write(tempbuf,count);
tud_cdc_write_flush();
}
}
*/