|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 hathach for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <string.h> |
| 28 | +#include "tick.h" |
| 29 | +#include "common-hal/usb_hid/Device.h" |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "shared-bindings/usb_hid/Device.h" |
| 32 | +#include "tusb.h" |
| 33 | + |
| 34 | +uint8_t common_hal_usb_hid_device_get_usage_page(usb_hid_device_obj_t *self) { |
| 35 | + return self->usage_page; |
| 36 | +} |
| 37 | + |
| 38 | +uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self) { |
| 39 | + return self->usage; |
| 40 | +} |
| 41 | + |
| 42 | +void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* report, uint8_t len) { |
| 43 | + if (len != self->report_length) { |
| 44 | + mp_raise_ValueError_varg("Buffer incorrect size. Should be %d bytes.", self->report_length); |
| 45 | + } |
| 46 | + |
| 47 | + // Wait until interface is ready, timeout = 2 seconds |
| 48 | + uint64_t end_ticks = ticks_ms + 2000; |
| 49 | + while ( (ticks_ms < end_ticks) && !tud_hid_generic_ready() ) { } |
| 50 | + |
| 51 | + if ( !tud_hid_generic_ready() ) { |
| 52 | + mp_raise_msg(&mp_type_OSError, "USB Busy"); |
| 53 | + } |
| 54 | + |
| 55 | + memcpy(self->report_buffer, report, len); |
| 56 | + |
| 57 | + if ( !tud_hid_generic_report(self->report_id, self->report_buffer, len) ) { |
| 58 | + mp_raise_msg(&mp_type_OSError, "USB Error"); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Callbacks invoked when receive Get_Report request through control endpoint |
| 63 | +uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) { |
| 64 | + // only support Input Report |
| 65 | + if ( report_type != HID_REPORT_TYPE_INPUT ) return 0; |
| 66 | + |
| 67 | + // index is ID-1 |
| 68 | + uint8_t idx = ( report_id ? (report_id-1) : 0 ); |
| 69 | + |
| 70 | + // fill buffer with current report |
| 71 | + memcpy(buffer, usb_hid_devices[idx].report_buffer, reqlen); |
| 72 | + return reqlen; |
| 73 | +} |
| 74 | + |
| 75 | +// Callbacks invoked when receive Set_Report request through control endpoint |
| 76 | +void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { |
| 77 | + // index is ID-1 |
| 78 | + uint8_t idx = ( report_id ? (report_id-1) : 0 ); |
| 79 | + |
| 80 | + if ( report_type == HID_REPORT_TYPE_OUTPUT ) { |
| 81 | + // Check if it is Keyboard device |
| 82 | + if ( (usb_hid_devices[idx].usage_page == HID_USAGE_PAGE_DESKTOP) && (usb_hid_devices[idx].usage == HID_USAGE_DESKTOP_KEYBOARD) ) { |
| 83 | + // This is LED indicator (CapsLock, NumLock) |
| 84 | + // TODO Light up some LED here |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
0 commit comments