|
| 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 and Keyboard with Joy Feather Wing. |
| 13 | + * - The analog stick move mouse cursor |
| 14 | + * - Button A, B, X, Y will send character a, b, x, y |
| 15 | + * - Any actions will wake up PC host if it is in suspended (standby) mode. |
| 16 | + * |
| 17 | + * Joy Feather Wing: https://www.adafruit.com/product/3632 |
| 18 | + * |
| 19 | + * Following library is required |
| 20 | + * - Adafruit_seesaw |
| 21 | + */ |
| 22 | + |
| 23 | +#include "Adafruit_TinyUSB.h" |
| 24 | +#include "Adafruit_seesaw.h" |
| 25 | + |
| 26 | +#define BUTTON_A 6 |
| 27 | +#define BUTTON_B 7 |
| 28 | +#define BUTTON_Y 9 |
| 29 | +#define BUTTON_X 10 |
| 30 | +uint32_t button_mask = (1 << BUTTON_A) | (1 << BUTTON_B) | |
| 31 | + (1 << BUTTON_Y) | (1 << BUTTON_X); |
| 32 | + |
| 33 | +Adafruit_seesaw ss; |
| 34 | + |
| 35 | +// Report ID |
| 36 | +enum |
| 37 | +{ |
| 38 | + RID_KEYBOARD = 1, |
| 39 | + RID_MOUSE |
| 40 | +}; |
| 41 | + |
| 42 | +// HID report descriptor using TinyUSB's template |
| 43 | +uint8_t const desc_hid_report[] = |
| 44 | +{ |
| 45 | + TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD), ), |
| 46 | + TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE), ) |
| 47 | +}; |
| 48 | + |
| 49 | +// USB HID object |
| 50 | +Adafruit_USBD_HID usb_hid; |
| 51 | + |
| 52 | +int last_x, last_y; |
| 53 | + |
| 54 | +// the setup function runs once when you press reset or power the board |
| 55 | +void setup() |
| 56 | +{ |
| 57 | + usb_hid.setPollInterval(2); |
| 58 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 59 | + |
| 60 | + usb_hid.begin(); |
| 61 | + |
| 62 | + Serial.begin(115200); |
| 63 | + Serial.println("Adafruit TinyUSB HID Mouse with Joy FeatherWing example"); |
| 64 | + |
| 65 | + if(!ss.begin(0x49)){ |
| 66 | + Serial.println("ERROR! seesaw not found"); |
| 67 | + while(1); |
| 68 | + } else { |
| 69 | + Serial.println("seesaw started"); |
| 70 | + Serial.print("version: "); |
| 71 | + Serial.println(ss.getVersion(), HEX); |
| 72 | + } |
| 73 | + ss.pinModeBulk(button_mask, INPUT_PULLUP); |
| 74 | + ss.setGPIOInterrupts(button_mask, 1); |
| 75 | + |
| 76 | + last_y = ss.analogRead(2); |
| 77 | + last_x = ss.analogRead(3); |
| 78 | + |
| 79 | + // wait until device mounted |
| 80 | + while( !USBDevice.mounted() ) delay(1); |
| 81 | +} |
| 82 | + |
| 83 | +void loop() |
| 84 | +{ |
| 85 | + // poll gpio once each 10 ms |
| 86 | + delay(10); |
| 87 | + |
| 88 | + // If either analog stick or any buttons is pressed |
| 89 | + bool has_action = false; |
| 90 | + |
| 91 | + /*------------- Mouse -------------*/ |
| 92 | + int y = ss.analogRead(2); |
| 93 | + int x = ss.analogRead(3); |
| 94 | + |
| 95 | + // reduce the delta by half to slow down the cursor move |
| 96 | + int dx = (x - last_x) / 2; |
| 97 | + int dy = (y - last_y) / 2; |
| 98 | + |
| 99 | + if ( (abs(dx) > 3) || (abs(dy) > 3) ) |
| 100 | + { |
| 101 | + has_action = true; |
| 102 | + |
| 103 | + if ( usb_hid.ready() ) |
| 104 | + { |
| 105 | + usb_hid.mouseMove(RID_MOUSE, dx, dy); // no ID: right + down |
| 106 | + |
| 107 | + last_x = x; |
| 108 | + last_y = y; |
| 109 | + |
| 110 | + // delay a bit before attempt to send keyboard report |
| 111 | + delay(10); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + /*------------- Keyboard -------------*/ |
| 117 | + // button is active low, invert read value for convenience |
| 118 | + uint32_t buttons = ~ss.digitalReadBulk(button_mask); |
| 119 | + |
| 120 | + if ( usb_hid.ready() ) |
| 121 | + { |
| 122 | + // use to prevent sending multiple consecutive zero report |
| 123 | + static bool has_key = false; |
| 124 | + |
| 125 | + if ( buttons & button_mask ) |
| 126 | + { |
| 127 | + has_action = true; |
| 128 | + has_key = true; |
| 129 | + |
| 130 | + uint8_t keycode[6] = { 0 }; |
| 131 | + |
| 132 | + if ( buttons & (1 << BUTTON_A) ) keycode[0] = HID_KEY_A; |
| 133 | + if ( buttons & (1 << BUTTON_B) ) keycode[0] = HID_KEY_B; |
| 134 | + if ( buttons & (1 << BUTTON_X) ) keycode[0] = HID_KEY_X; |
| 135 | + if ( buttons & (1 << BUTTON_Y) ) keycode[0] = HID_KEY_Y; |
| 136 | + |
| 137 | + usb_hid.keyboardReport(RID_KEYBOARD, 0, keycode); |
| 138 | + }else |
| 139 | + { |
| 140 | + // send empty key report if previously has key pressed |
| 141 | + if (has_key) usb_hid.keyboardRelease(RID_KEYBOARD); |
| 142 | + has_key = false; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /*------------- Remote Wakeup -------------*/ |
| 147 | + // Remote wakeup if PC is suspended and we has user interaction with joy feather wing |
| 148 | + if ( has_action && USBDevice.suspended() ) |
| 149 | + { |
| 150 | + // Wake up only works if REMOTE_WAKEUP feature is enable by host |
| 151 | + // Usually this is the case with Mouse/Keyboard device |
| 152 | + USBDevice.remoteWakeup(); |
| 153 | + } |
| 154 | +} |
0 commit comments