|
| 1 | +/********************************************************************* |
| 2 | + Adafruit invests time and resources providing this open source code, |
| 3 | + please support Adafruit and open-source hardware by purchasing |
| 4 | + products from Adafruit! |
| 5 | +
|
| 6 | + MIT license, check LICENSE for more information |
| 7 | + Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 8 | + All text above, and the splash screen below must be included in |
| 9 | + any redistribution |
| 10 | +*********************************************************************/ |
| 11 | + |
| 12 | +/* This sketch demonstrates USB HID mouse using Joystick Feather Wing |
| 13 | + * https://www.adafruit.com/product/3632 |
| 14 | + * |
| 15 | + * Following library is required |
| 16 | + * - Adafruit_seesaw |
| 17 | + */ |
| 18 | + |
| 19 | +#include "Adafruit_TinyUSB.h" |
| 20 | +#include "Adafruit_seesaw.h" |
| 21 | + |
| 22 | +#define BUTTON_RIGHT 6 |
| 23 | +#define BUTTON_DOWN 7 |
| 24 | +#define BUTTON_LEFT 9 |
| 25 | +#define BUTTON_UP 10 |
| 26 | +#define BUTTON_SEL 14 |
| 27 | +uint32_t button_mask = (1 << BUTTON_RIGHT) | (1 << BUTTON_DOWN) | |
| 28 | + (1 << BUTTON_LEFT) | (1 << BUTTON_UP) | (1 << BUTTON_SEL); |
| 29 | + |
| 30 | +Adafruit_seesaw ss; |
| 31 | + |
| 32 | +// HID report descriptor using TinyUSB's template |
| 33 | +// Single Report (no ID) descriptor |
| 34 | +uint8_t const desc_hid_report[] = |
| 35 | +{ |
| 36 | + TUD_HID_REPORT_DESC_MOUSE() |
| 37 | +}; |
| 38 | + |
| 39 | +// USB HID object |
| 40 | +Adafruit_USBD_HID usb_hid; |
| 41 | + |
| 42 | +int last_x, last_y; |
| 43 | + |
| 44 | +// the setup function runs once when you press reset or power the board |
| 45 | +void setup() |
| 46 | +{ |
| 47 | + usb_hid.setPollInterval(2); |
| 48 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 49 | + |
| 50 | + usb_hid.begin(); |
| 51 | + |
| 52 | + Serial.begin(115200); |
| 53 | + Serial.println("Adafruit TinyUSB HID Mouse with Joy FeatherWing example"); |
| 54 | + |
| 55 | + if(!ss.begin(0x49)){ |
| 56 | + Serial.println("ERROR! seesaw not found"); |
| 57 | + while(1); |
| 58 | + } else { |
| 59 | + Serial.println("seesaw started"); |
| 60 | + Serial.print("version: "); |
| 61 | + Serial.println(ss.getVersion(), HEX); |
| 62 | + } |
| 63 | + ss.pinModeBulk(button_mask, INPUT_PULLUP); |
| 64 | + ss.setGPIOInterrupts(button_mask, 1); |
| 65 | + |
| 66 | + last_y = ss.analogRead(2); |
| 67 | + last_x = ss.analogRead(3); |
| 68 | +} |
| 69 | + |
| 70 | +void loop() |
| 71 | +{ |
| 72 | + // poll gpio once each 10 ms |
| 73 | + delay(10); |
| 74 | + |
| 75 | + int y = ss.analogRead(2); |
| 76 | + int x = ss.analogRead(3); |
| 77 | + |
| 78 | + int dx = x - last_x; |
| 79 | + int dy = y - last_y; |
| 80 | + |
| 81 | + if ( (abs(dx) > 3) || (abs(dy) > 30) ) |
| 82 | + { |
| 83 | + // Remote wakeup if PC is suspended |
| 84 | + if ( tud_suspended() ) |
| 85 | + { |
| 86 | + // Wake up host if we are in suspend mode |
| 87 | + // and REMOTE_WAKEUP feature is enabled by host |
| 88 | + USBDevice.remoteWakeup(); |
| 89 | + } |
| 90 | + |
| 91 | + /*------------- Mouse -------------*/ |
| 92 | + if ( usb_hid.ready() ) |
| 93 | + { |
| 94 | + usb_hid.mouseMove(0, dx, dy); // no ID: right + down |
| 95 | + |
| 96 | + last_x = x; |
| 97 | + last_y = y; |
| 98 | + |
| 99 | + // delay a bit before attempt to send keyboard report |
| 100 | + delay(10); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments