|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 3 | + |
| 4 | +#include "Adafruit_TinyUSB.h" |
| 5 | + |
| 6 | +/* This sketch demonstrates USB HID keyboard. |
| 7 | + * - PIN A0-A5 is used to send digit '0' to '5' respectively |
| 8 | + * - LED will be turned on when one of Caplock/NumLock/ScrollLock/Compose/Kana is set by Host |
| 9 | + */ |
| 10 | + |
| 11 | + |
| 12 | +// HID report descriptor using TinyUSB's template |
| 13 | +// Single Report (no ID) decriptor |
| 14 | +uint8_t const desc_hid_report[] = |
| 15 | +{ |
| 16 | + TUD_HID_REPORT_DESC_KEYBOARD(), |
| 17 | +}; |
| 18 | + |
| 19 | +Adafruit_USBD_HID usb_hid; |
| 20 | + |
| 21 | +// Array of pins and its keycode |
| 22 | +// For keycode definition see BLEHidGeneric.h |
| 23 | +uint8_t pins[] = { A0, A1, A2, A3, A4, A5 }; |
| 24 | +uint8_t hidcode[] = { HID_KEY_0, HID_KEY_1, HID_KEY_2, HID_KEY_3 , HID_KEY_4, HID_KEY_5 }; |
| 25 | + |
| 26 | +uint8_t pincount = sizeof(pins)/sizeof(pins[0]); |
| 27 | + |
| 28 | +// the setup function runs once when you press reset or power the board |
| 29 | +void setup() |
| 30 | +{ |
| 31 | + usb_hid.setPollInterval(2); |
| 32 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 33 | + usb_hid.setReportCallback(NULL, set_report_callback_t set_report); |
| 34 | + |
| 35 | + usb_hid.begin(); |
| 36 | + |
| 37 | + // led pin |
| 38 | + pinMode(PIN_LED, OUTPUT); |
| 39 | + digitalWrite(PIN_LED, 0); |
| 40 | + |
| 41 | + // Set up pin as input |
| 42 | + for (uint8_t i=0; i<pincount; i++) |
| 43 | + { |
| 44 | + pinMode(pins[i], INPUT_PULLUP); |
| 45 | + } |
| 46 | + |
| 47 | + Serial.begin(115200); |
| 48 | + while ( !Serial ) delay(10); // wait for native usb |
| 49 | + |
| 50 | + Serial.println("Adafruit TinyUSB HID Keyboard example"); |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +void loop() |
| 55 | +{ |
| 56 | + // poll gpio once each 2 ms |
| 57 | + delay(2); |
| 58 | + |
| 59 | +// // Remote wakeup |
| 60 | +// if ( tud_suspended() && btn ) |
| 61 | +// { |
| 62 | +// // Wake up host if we are in suspend mode |
| 63 | +// // and REMOTE_WAKEUP feature is enabled by host |
| 64 | +// tud_remote_wakeup(); |
| 65 | +// } |
| 66 | + |
| 67 | + if ( !usb_hid.ready() ) return; |
| 68 | + |
| 69 | + static bool keyPressedPreviously = false; |
| 70 | + bool anyKeyPressed = false; |
| 71 | + |
| 72 | + uint8_t count=0; |
| 73 | + uint8_t keycode[6] = { 0 }; |
| 74 | + |
| 75 | + // scan normal key and send report |
| 76 | + for(uint8_t i=0; i < pincount; i++) |
| 77 | + { |
| 78 | + if ( 0 == digitalRead(pins[i]) ) |
| 79 | + { |
| 80 | + // if pin is active (low), add its hid code to key report |
| 81 | + keycode[count++] = hidcode[i]; |
| 82 | + |
| 83 | + // 6 is max keycode per report |
| 84 | + if (count == 6) |
| 85 | + { |
| 86 | + usb_hid.keyboadReport(0, 0, keycode); |
| 87 | + delay(2); // delay for report to send out |
| 88 | + |
| 89 | + // reset report |
| 90 | + count = 0; |
| 91 | + memset(keycode, 0, 6); |
| 92 | + } |
| 93 | + |
| 94 | + // used later |
| 95 | + anyKeyPressed = true; |
| 96 | + keyPressedPreviously = true; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Send any remaining keys (not accumulated up to 6) |
| 101 | + if ( count ) |
| 102 | + { |
| 103 | + usb_hid.keyboadReport(0, 0, keycode); |
| 104 | + } |
| 105 | + |
| 106 | + // Send All-zero report to indicate there is no keys pressed |
| 107 | + // Most of the time, it is, though we don't need to send zero report |
| 108 | + // every loop(), only a key is pressed in previous loop() |
| 109 | + if ( !anyKeyPressed && keyPressedPreviously ) |
| 110 | + { |
| 111 | + keyPressedPreviously = false; |
| 112 | + usb_hid.keyboardRelease(0); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Output report callback for LED indicator such as Caplocks |
| 117 | +void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) |
| 118 | +{ |
| 119 | + // LED indicator is output report with only 1 byte length |
| 120 | + if ( report_type != HID_REPORT_TYPE_OUTPUT ) return; |
| 121 | + |
| 122 | + // The LED bit map is as follows: (also defined by KEYBOARD_LED_* ) |
| 123 | + // Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0) |
| 124 | + uint8_t ledIndicator = buffer[0]; |
| 125 | + |
| 126 | + // turn on LED if any indicator is set |
| 127 | + digitalWrite(PIN_LED, ledIndicator); |
| 128 | +} |
0 commit comments