|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +#include <stdlib.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "bsp/board.h" |
| 31 | +#include "tusb.h" |
| 32 | + |
| 33 | +//--------------------------------------------------------------------+ |
| 34 | +// MACRO CONSTANT TYPEDEF PROTYPES |
| 35 | +//--------------------------------------------------------------------+ |
| 36 | + |
| 37 | +// Interface index depends on the order in configuration descriptor |
| 38 | +enum { |
| 39 | + ITF_KEYBOARD = 0, |
| 40 | + ITF_MOUSE = 1 |
| 41 | +}; |
| 42 | + |
| 43 | +/* Blink pattern |
| 44 | + * - 250 ms : device not mounted |
| 45 | + * - 1000 ms : device mounted |
| 46 | + * - 2500 ms : device is suspended |
| 47 | + */ |
| 48 | +enum { |
| 49 | + BLINK_NOT_MOUNTED = 250, |
| 50 | + BLINK_MOUNTED = 1000, |
| 51 | + BLINK_SUSPENDED = 2500, |
| 52 | +}; |
| 53 | + |
| 54 | +static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; |
| 55 | + |
| 56 | +void led_blinking_task(void); |
| 57 | +void hid_task(void); |
| 58 | + |
| 59 | +/*------------- MAIN -------------*/ |
| 60 | +int main(void) |
| 61 | +{ |
| 62 | + board_init(); |
| 63 | + |
| 64 | + // init device stack on configured roothub port |
| 65 | + tud_init(BOARD_TUD_RHPORT); |
| 66 | + |
| 67 | + while (1) |
| 68 | + { |
| 69 | + tud_task(); // tinyusb device task |
| 70 | + led_blinking_task(); |
| 71 | + |
| 72 | + hid_task(); |
| 73 | + } |
| 74 | + |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +//--------------------------------------------------------------------+ |
| 79 | +// Device callbacks |
| 80 | +//--------------------------------------------------------------------+ |
| 81 | + |
| 82 | +// Invoked when device is mounted |
| 83 | +void tud_mount_cb(void) |
| 84 | +{ |
| 85 | + blink_interval_ms = BLINK_MOUNTED; |
| 86 | +} |
| 87 | + |
| 88 | +// Invoked when device is unmounted |
| 89 | +void tud_umount_cb(void) |
| 90 | +{ |
| 91 | + blink_interval_ms = BLINK_NOT_MOUNTED; |
| 92 | +} |
| 93 | + |
| 94 | +// Invoked when usb bus is suspended |
| 95 | +// remote_wakeup_en : if host allow us to perform remote wakeup |
| 96 | +// Within 7ms, device must draw an average of current less than 2.5 mA from bus |
| 97 | +void tud_suspend_cb(bool remote_wakeup_en) |
| 98 | +{ |
| 99 | + (void) remote_wakeup_en; |
| 100 | + blink_interval_ms = BLINK_SUSPENDED; |
| 101 | +} |
| 102 | + |
| 103 | +// Invoked when usb bus is resumed |
| 104 | +void tud_resume_cb(void) |
| 105 | +{ |
| 106 | + blink_interval_ms = BLINK_MOUNTED; |
| 107 | +} |
| 108 | + |
| 109 | +//--------------------------------------------------------------------+ |
| 110 | +// USB HID |
| 111 | +//--------------------------------------------------------------------+ |
| 112 | + |
| 113 | +void hid_task(void) |
| 114 | +{ |
| 115 | + // Poll every 10ms |
| 116 | + const uint32_t interval_ms = 10; |
| 117 | + static uint32_t start_ms = 0; |
| 118 | + |
| 119 | + if ( board_millis() - start_ms < interval_ms) return; // not enough time |
| 120 | + start_ms += interval_ms; |
| 121 | + |
| 122 | + uint32_t const btn = board_button_read(); |
| 123 | + |
| 124 | + // Remote wakeup |
| 125 | + if ( tud_suspended() && btn ) |
| 126 | + { |
| 127 | + // Wake up host if we are in suspend mode |
| 128 | + // and REMOTE_WAKEUP feature is enabled by host |
| 129 | + tud_remote_wakeup(); |
| 130 | + } |
| 131 | + |
| 132 | + /*------------- Keyboard -------------*/ |
| 133 | + if ( tud_hid_n_ready(ITF_KEYBOARD) ) |
| 134 | + { |
| 135 | + // use to avoid send multiple consecutive zero report for keyboard |
| 136 | + static bool has_key = false; |
| 137 | + |
| 138 | + if ( btn ) |
| 139 | + { |
| 140 | + uint8_t keycode[6] = { 0 }; |
| 141 | + keycode[0] = HID_KEY_A; |
| 142 | + |
| 143 | + tud_hid_n_keyboard_report(ITF_KEYBOARD, 0, 0, keycode); |
| 144 | + |
| 145 | + has_key = true; |
| 146 | + }else |
| 147 | + { |
| 148 | + // send empty key report if previously has key pressed |
| 149 | + if (has_key) tud_hid_n_keyboard_report(ITF_KEYBOARD, 0, 0, NULL); |
| 150 | + has_key = false; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /*------------- Mouse -------------*/ |
| 155 | + if ( tud_hid_n_ready(ITF_MOUSE) ) |
| 156 | + { |
| 157 | + if ( btn ) |
| 158 | + { |
| 159 | + int8_t const delta = 5; |
| 160 | + |
| 161 | + // no button, right + down, no scroll pan |
| 162 | + tud_hid_n_mouse_report(ITF_MOUSE, 0, 0x00, delta, delta, 0, 0); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +// Invoked when received GET_REPORT control request |
| 169 | +// Application must fill buffer report's content and return its length. |
| 170 | +// Return zero will cause the stack to STALL request |
| 171 | +uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) |
| 172 | +{ |
| 173 | + // TODO not Implemented |
| 174 | + (void) itf; |
| 175 | + (void) report_id; |
| 176 | + (void) report_type; |
| 177 | + (void) buffer; |
| 178 | + (void) reqlen; |
| 179 | + |
| 180 | + return 0; |
| 181 | +} |
| 182 | + |
| 183 | +// Invoked when received SET_REPORT control request or |
| 184 | +// received data on OUT endpoint ( Report ID = 0, Type = 0 ) |
| 185 | +void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) |
| 186 | +{ |
| 187 | + // TODO set LED based on CAPLOCK, NUMLOCK etc... |
| 188 | + (void) itf; |
| 189 | + (void) report_id; |
| 190 | + (void) report_type; |
| 191 | + (void) buffer; |
| 192 | + (void) bufsize; |
| 193 | +} |
| 194 | + |
| 195 | +//--------------------------------------------------------------------+ |
| 196 | +// BLINKING TASK |
| 197 | +//--------------------------------------------------------------------+ |
| 198 | +void led_blinking_task(void) |
| 199 | +{ |
| 200 | + static uint32_t start_ms = 0; |
| 201 | + static bool led_state = false; |
| 202 | + |
| 203 | + // Blink every interval ms |
| 204 | + if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time |
| 205 | + start_ms += blink_interval_ms; |
| 206 | + |
| 207 | + board_led_write(led_state); |
| 208 | + led_state = 1 - led_state; // toggle |
| 209 | +} |
0 commit comments