Skip to content

Commit 17f3704

Browse files
committed
wrap up hid_composite exmaple
1 parent 1e26788 commit 17f3704

File tree

5 files changed

+188
-2
lines changed

5 files changed

+188
-2
lines changed

examples/hid_composite/hid_composite.ino

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach for Adafruit Industries
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+
125
#include "Adafruit_TinyUSB.h"
226

27+
/* This sketch demonstrates multiple report USB HID.
28+
* Press button pin will move
29+
* - mouse toward bottom right of monitor
30+
* - send 'a' key
31+
*/
32+
33+
// Report ID
334
enum
435
{
536
RID_KEYBOARD = 1,
@@ -15,13 +46,19 @@ uint8_t const desc_hid_report[] =
1546

1647
Adafruit_USBD_HID usb_hid;
1748

49+
const int pin = 7;
50+
1851
// the setup function runs once when you press reset or power the board
1952
void setup()
2053
{
21-
usb_hid.setPollInterval(10);
54+
usb_hid.setPollInterval(2);
2255
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
56+
2357
usb_hid.begin();
2458

59+
// Set up button
60+
pinMode(pin, INPUT_PULLUP);
61+
2562
Serial.begin(115200);
2663
while ( !Serial ) delay(10); // wait for native usb
2764

@@ -30,5 +67,52 @@ void setup()
3067

3168
void loop()
3269
{
33-
// nothing to do
70+
// poll gpio once each 10 ms
71+
delay(10);
72+
73+
// button is active low
74+
uint32_t const btn = 1 - digitalRead(pin);
75+
76+
// Remote wakeup
77+
if ( tud_suspended() && btn )
78+
{
79+
// Wake up host if we are in suspend mode
80+
// and REMOTE_WAKEUP feature is enabled by host
81+
tud_remote_wakeup();
82+
}
83+
84+
/*------------- Mouse -------------*/
85+
if ( usb_hid.ready() )
86+
{
87+
if ( btn )
88+
{
89+
int8_t const delta = 5;
90+
usb_hid.mouseMove(RID_MOUSE, delta, delta); // right + down
91+
92+
// delay a bit before attempt to send keyboard report
93+
delay(2);
94+
}
95+
}
96+
97+
/*------------- Keyboard -------------*/
98+
if ( usb_hid.ready() )
99+
{
100+
// use to avoid send multiple consecutive zero report for keyboard
101+
static bool has_key = false;
102+
103+
if ( btn )
104+
{
105+
uint8_t keycode[6] = { 0 };
106+
keycode[0] = HID_KEY_A;
107+
108+
usb_hid.keyboadReport(RID_KEYBOARD, 0, keycode);
109+
110+
has_key = true;
111+
}else
112+
{
113+
// send empty key report if previously has key pressed
114+
if (has_key) usb_hid.keyboardKeyRelease(RID_KEYBOARD);
115+
has_key = false;
116+
}
117+
}
34118
}

examples/msc_ramdisk/msc_ramdisk.ino

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach for Adafruit Industries
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+
125
#include "Adafruit_TinyUSB.h"
226
#include "ramdisk.h"
327

examples/msc_ramdisk_dual/msc_ramdisk_dual.ino

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach for Adafruit Industries
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+
125
#include "Adafruit_TinyUSB.h"
226
#include "ramdisk.h"
327

src/Adafruit_USBD_HID.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,46 @@ bool Adafruit_USBD_HID::sendReport(uint8_t report_id, void const* report, uint8_
8888
return tud_hid_report(report_id, report, len);
8989
}
9090

91+
//--------------------------------------------------------------------+
92+
// Keyboard
93+
//--------------------------------------------------------------------+
94+
95+
bool Adafruit_USBD_HID::keyboadReport(uint8_t report_id, uint8_t modifier, uint8_t keycode[6])
96+
{
97+
return tud_hid_keyboard_report(report_id, modifier, keycode);
98+
}
99+
100+
bool Adafruit_USBD_HID::keyboardKeyRelease(uint8_t report_id)
101+
{
102+
return tud_hid_keyboard_key_release(report_id);
103+
}
104+
105+
//--------------------------------------------------------------------+
106+
// Mouse
107+
//--------------------------------------------------------------------+
108+
109+
bool Adafruit_USBD_HID::mouseReport(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan)
110+
{
111+
return tud_hid_mouse_report(report_id, buttons, x, y, scroll, pan);
112+
}
113+
114+
bool Adafruit_USBD_HID::mouseMove(uint8_t report_id, int8_t x, int8_t y)
115+
{
116+
return tud_hid_mouse_move(report_id, x, y);
117+
}
118+
119+
bool Adafruit_USBD_HID::mouseScroll(uint8_t report_id, int8_t scroll, int8_t pan)
120+
{
121+
return tud_hid_mouse_scroll(report_id, scroll, pan);
122+
}
123+
124+
bool Adafruit_USBD_HID::mouseButtonPress(uint8_t report_id, uint8_t buttons)
125+
{
126+
return tud_hid_mouse_report(report_id, buttons, 0, 0, 0, 0);
127+
}
128+
129+
bool Adafruit_USBD_HID::mouseButtonRelease(uint8_t report_id)
130+
{
131+
return tud_hid_mouse_report(report_id, 0, 0, 0, 0, 0);
132+
}
91133

src/Adafruit_USBD_HID.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ class Adafruit_USBD_HID : Adafruit_USBD_Interface
4545
bool ready(void);
4646
bool sendReport(uint8_t report_id, void const* report, uint8_t len);
4747

48+
//------------- Keyboard API -------------//
49+
bool keyboadReport(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
50+
bool keyboardKeyPress(uint8_t report_id, char ch);
51+
bool keyboardKeyRelease(uint8_t report_id);
52+
53+
//------------- Mouse API -------------//
54+
bool mouseReport(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan);
55+
bool mouseMove(uint8_t report_id, int8_t x, int8_t y);
56+
bool mouseScroll(uint8_t report_id, int8_t scroll, int8_t pan);
57+
bool mouseButtonPress(uint8_t report_id, uint8_t buttons);
58+
static inline bool mouseButtonRelease(uint8_t report_id);
59+
4860
// from Adafruit_USBD_Interface
4961
virtual uint16_t getDescriptor(uint8_t* buf, uint16_t bufsize);
5062

0 commit comments

Comments
 (0)