123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include <stdint.h>
- #include "USBHID.h"
- #include "USBHID_Types.h"
- #include "USBDescriptor.h"
- #include "HIDKeyboard.h"
- #define DEFAULT_CONFIGURATION (1)
- HIDKeyboard::HIDKeyboard(uint16_t vendor_id, uint16_t product_id, uint16_t product_release) : USBDevice(vendor_id, product_id, product_release) { USBDevice::connect(); }
- bool HIDKeyboard::sendReport(report_keyboard_t report) {
- USBDevice::write(EP1IN, report.raw, sizeof(report), MAX_PACKET_SIZE_EP1);
- return true;
- }
- uint8_t HIDKeyboard::leds() { return led_state; }
- bool HIDKeyboard::USBCallback_setConfiguration(uint8_t configuration) {
- if (configuration != DEFAULT_CONFIGURATION) {
- return false;
- }
-
- addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
-
-
-
- return true;
- }
- uint8_t *HIDKeyboard::stringImanufacturerDesc() {
- static uint8_t stringImanufacturerDescriptor[] = {
- 0x18,
- STRING_DESCRIPTOR,
- 't',
- 0,
- 'm',
- 0,
- 'k',
- 0,
- '-',
- 0,
- 'k',
- 0,
- 'b',
- 0,
- 'd',
- 0,
- '.',
- 0,
- 'c',
- 0,
- 'o',
- 0,
- 'm',
- 0
- };
- return stringImanufacturerDescriptor;
- }
- uint8_t *HIDKeyboard::stringIproductDesc() {
- static uint8_t stringIproductDescriptor[] = {
- 0x0a,
- STRING_DESCRIPTOR,
- 'm',
- 0,
- 'b',
- 0,
- 'e',
- 0,
- 'd',
- 0
- };
- return stringIproductDescriptor;
- }
- uint8_t *HIDKeyboard::stringIserialDesc() {
- static uint8_t stringIserialDescriptor[] = {
- 0x04,
- STRING_DESCRIPTOR,
- '0', 0
- };
- return stringIserialDescriptor;
- }
- uint8_t *HIDKeyboard::reportDesc() {
- static uint8_t reportDescriptor[] = {
- USAGE_PAGE(1), 0x01,
- USAGE(1), 0x06,
- COLLECTION(1), 0x01,
- USAGE_PAGE(1), 0x07,
- USAGE_MINIMUM(1), 0xE0, USAGE_MAXIMUM(1), 0xE7, LOGICAL_MINIMUM(1), 0x00, LOGICAL_MAXIMUM(1), 0x01, REPORT_SIZE(1), 0x01, REPORT_COUNT(1), 0x08, INPUT(1), 0x02,
- REPORT_COUNT(1), 0x01, REPORT_SIZE(1), 0x08, INPUT(1), 0x01,
- REPORT_COUNT(1), 0x05, REPORT_SIZE(1), 0x01, USAGE_PAGE(1), 0x08,
- USAGE_MINIMUM(1), 0x01, USAGE_MAXIMUM(1), 0x05, OUTPUT(1), 0x02,
- REPORT_COUNT(1), 0x01, REPORT_SIZE(1), 0x03, OUTPUT(1), 0x01,
- REPORT_COUNT(1), 0x06, REPORT_SIZE(1), 0x08, LOGICAL_MINIMUM(1), 0x00, LOGICAL_MAXIMUM(1), 0xFF, USAGE_PAGE(1), 0x07,
- USAGE_MINIMUM(1), 0x00, USAGE_MAXIMUM(1), 0xFF, INPUT(1), 0x00,
- END_COLLECTION(0),
- };
- reportLength = sizeof(reportDescriptor);
- return reportDescriptor;
- }
- uint16_t HIDKeyboard::reportDescLength() {
- reportDesc();
- return reportLength;
- }
- #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) + (1 * INTERFACE_DESCRIPTOR_LENGTH) + (1 * HID_DESCRIPTOR_LENGTH) + (1 * ENDPOINT_DESCRIPTOR_LENGTH))
- uint8_t *HIDKeyboard::configurationDesc() {
- static uint8_t configurationDescriptor[] = {
- CONFIGURATION_DESCRIPTOR_LENGTH,
- CONFIGURATION_DESCRIPTOR,
- LSB(TOTAL_DESCRIPTOR_LENGTH),
- MSB(TOTAL_DESCRIPTOR_LENGTH),
- 0x01,
- DEFAULT_CONFIGURATION,
- 0x00,
- C_RESERVED | C_REMOTE_WAKEUP,
- C_POWER(100),
- INTERFACE_DESCRIPTOR_LENGTH,
- INTERFACE_DESCRIPTOR,
- 0x00,
- 0x00,
- 0x01,
- HID_CLASS,
- 1,
- 1,
- 0x00,
- HID_DESCRIPTOR_LENGTH,
- HID_DESCRIPTOR,
- LSB(HID_VERSION_1_11),
- MSB(HID_VERSION_1_11),
- 0x00,
- 0x01,
- REPORT_DESCRIPTOR,
- (uint8_t)(LSB(reportDescLength())),
- (uint8_t)(MSB(reportDescLength())),
- ENDPOINT_DESCRIPTOR_LENGTH,
- ENDPOINT_DESCRIPTOR,
- PHY_TO_DESC(EP1IN),
- E_INTERRUPT,
- LSB(MAX_PACKET_SIZE_EPINT),
- MSB(MAX_PACKET_SIZE_EPINT),
- 1,
- };
- return configurationDescriptor;
- }
- #if 0
- uint8_t * HIDKeyboard::deviceDesc() {
- static uint8_t deviceDescriptor[] = {
- DEVICE_DESCRIPTOR_LENGTH,
- DEVICE_DESCRIPTOR,
- LSB(USB_VERSION_2_0),
- MSB(USB_VERSION_2_0),
- 0x00,
- 0x00,
- 0x00,
- MAX_PACKET_SIZE_EP0,
- (uint8_t)(LSB(0xfeed)),
- (uint8_t)(MSB(0xfeed)),
- (uint8_t)(LSB(0x1bed)),
- (uint8_t)(MSB(0x1bed)),
- (uint8_t)(LSB(0x0002)),
- (uint8_t)(MSB(0x0002)),
- 0,
- 0,
- 0,
- 0x01
- };
- return deviceDescriptor;
- }
- #endif
- bool HIDKeyboard::USBCallback_request() {
- bool success = false;
- CONTROL_TRANSFER *transfer = getTransferPtr();
- uint8_t * hidDescriptor;
-
- if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE)) {
- switch (transfer->setup.bRequest) {
- case GET_DESCRIPTOR:
- switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) {
- case REPORT_DESCRIPTOR:
- if ((reportDesc() != NULL) && (reportDescLength() != 0)) {
- transfer->remaining = reportDescLength();
- transfer->ptr = reportDesc();
- transfer->direction = DEVICE_TO_HOST;
- success = true;
- }
- break;
- case HID_DESCRIPTOR:
-
- hidDescriptor = findDescriptor(HID_DESCRIPTOR);
- if (hidDescriptor != NULL) {
- transfer->remaining = HID_DESCRIPTOR_LENGTH;
- transfer->ptr = hidDescriptor;
- transfer->direction = DEVICE_TO_HOST;
- success = true;
- }
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- }
-
- if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
- switch (transfer->setup.bRequest) {
- case SET_REPORT:
-
-
-
-
- transfer->remaining = 1;
-
- transfer->direction = HOST_TO_DEVICE;
- transfer->notify = true;
- success = true;
- default:
- break;
- }
- }
- return success;
- }
- void HIDKeyboard::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) {
- if (length > 0) {
- CONTROL_TRANSFER *transfer = getTransferPtr();
- if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
- switch (transfer->setup.bRequest) {
- case SET_REPORT:
- led_state = buf[0];
- break;
- default:
- break;
- }
- }
- }
- }
|