|
| 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 mouse |
| 7 | + * Press button pin will move |
| 8 | + * - mouse toward bottom right of monitor |
| 9 | + */ |
| 10 | + |
| 11 | +// HID report descriptor using TinyUSB's template |
| 12 | +// Single Report (no ID) descriptor |
| 13 | +uint8_t const desc_hid_report[] = |
| 14 | +{ |
| 15 | + TUD_HID_REPORT_DESC_MOUSE() |
| 16 | +}; |
| 17 | + |
| 18 | +Adafruit_USBD_HID usb_hid; |
| 19 | + |
| 20 | +const int pin = 7; |
| 21 | + |
| 22 | +// the setup function runs once when you press reset or power the board |
| 23 | +void setup() |
| 24 | +{ |
| 25 | + usb_hid.setPollInterval(2); |
| 26 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 27 | + |
| 28 | + usb_hid.begin(); |
| 29 | + |
| 30 | + // Set up button |
| 31 | + pinMode(pin, INPUT_PULLUP); |
| 32 | + |
| 33 | + Serial.begin(115200); |
| 34 | + while ( !Serial ) delay(10); // wait for native usb |
| 35 | + |
| 36 | + Serial.println("Adafruit TinyUSB HID Mouse example"); |
| 37 | + Serial.print("Wire pin "); Serial.print(pin); Serial.println(" to GND to move cursor to bottom right corner.") |
| 38 | +} |
| 39 | + |
| 40 | +void loop() |
| 41 | +{ |
| 42 | + // poll gpio once each 10 ms |
| 43 | + delay(10); |
| 44 | + |
| 45 | + // button is active low |
| 46 | + uint32_t const btn = 1 - digitalRead(pin); |
| 47 | + |
| 48 | + // Remote wakeup |
| 49 | + if ( tud_suspended() && btn ) |
| 50 | + { |
| 51 | + // Wake up host if we are in suspend mode |
| 52 | + // and REMOTE_WAKEUP feature is enabled by host |
| 53 | + tud_remote_wakeup(); |
| 54 | + } |
| 55 | + |
| 56 | + /*------------- Mouse -------------*/ |
| 57 | + if ( usb_hid.ready() ) |
| 58 | + { |
| 59 | + if ( btn ) |
| 60 | + { |
| 61 | + int8_t const delta = 5; |
| 62 | + usb_hid.mouseMove(0, delta, delta); // no ID: right + down |
| 63 | + |
| 64 | + // delay a bit before attempt to send keyboard report |
| 65 | + delay(10); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments