Skip to content

Commit 99be8f4

Browse files
author
Timon Skerutsch
committed
Alligned mouse examples, added newer hid in/out example from main repo, added new composite example for ramdisk and hid in/out. Moved ramdisk.h to src.
1 parent 97b272b commit 99be8f4

File tree

12 files changed

+681
-309
lines changed

12 files changed

+681
-309
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check the example host application in `examples/HID/hid_generic_inout`
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 example demonstrate HID Generic raw Input & Output.
13+
* It will receive data from Host (In endpoint) and echo back (Out endpoint).
14+
* HID Report descriptor use vendor for usage page (using template TUD_HID_REPORT_DESC_GENERIC_INOUT)
15+
*
16+
* There are 2 ways to test the sketch
17+
* 1. Using nodejs
18+
* - Install nodejs and nmp to your PC
19+
* - Install execellent node-hid (https://github.com/node-hid/node-hid) by
20+
* $ npm install node-hid
21+
* - Run provided hid test script
22+
* $ node hid_test.js
23+
*
24+
* 2. Using python hidRun
25+
* - Python and `hid` package is required, for installation please follow https://pypi.org/project/hid/
26+
* - Run provided hid test script to send and receive data to this device.
27+
* $ python3 hid_test.py
28+
*/
29+
30+
#include "Adafruit_TinyUSB.h"
31+
32+
// 8KB is the smallest size that windows allow to mount
33+
#define DISK_BLOCK_NUM 16
34+
#define DISK_BLOCK_SIZE 512
35+
#include "ramdisk.h"
36+
37+
// HID report descriptor using TinyUSB's template
38+
// Generic In Out with 64 bytes report (max)
39+
uint8_t const desc_hid_report[] =
40+
{
41+
TUD_HID_REPORT_DESC_GENERIC_INOUT(64)
42+
};
43+
44+
Adafruit_USBD_HID usb_hid;
45+
Adafruit_USBD_MSC usb_msc;
46+
47+
// the setup function runs once when you press reset or power the board
48+
void setup()
49+
{
50+
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
51+
52+
// Set disk size
53+
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
54+
55+
// Set callback
56+
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
57+
58+
// Set Lun ready (RAM disk is always ready)
59+
usb_msc.setUnitReady(true);
60+
61+
usb_msc.begin();
62+
63+
usb_hid.enableOutEndpoint(true);
64+
usb_hid.setPollInterval(2);
65+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
66+
usb_hid.setReportCallback(get_report_callback, set_report_callback);
67+
68+
usb_hid.begin();
69+
70+
Serial.begin(115200);
71+
Serial.println("Waiting for USBDevice mount");
72+
// wait until device mounted
73+
while( !USBDevice.mounted() ) delay(1);
74+
75+
Serial.println("Adafruit TinyUSB HID Generic In Out example");
76+
}
77+
78+
void loop()
79+
{
80+
// nothing to do
81+
}
82+
83+
// Invoked when received GET_REPORT control request
84+
// Application must fill buffer report's content and return its length.
85+
// Return zero will cause the stack to STALL request
86+
uint16_t get_report_callback (uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
87+
{
88+
// not used in this example
89+
return 0;
90+
}
91+
92+
// Invoked when received SET_REPORT control request or
93+
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
94+
void set_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
95+
{
96+
// This example doesn't use multiple report and report ID
97+
(void) report_id;
98+
(void) report_type;
99+
100+
// echo back anything we received from host
101+
usb_hid.sendReport(0, buffer, bufsize);
102+
}
103+
104+
// Callback invoked when received READ10 command.
105+
// Copy disk's data to buffer (up to bufsize) and
106+
// return number of copied bytes (must be multiple of block size)
107+
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
108+
{
109+
uint8_t const* addr = msc_disk[lba];
110+
memcpy(buffer, addr, bufsize);
111+
112+
return bufsize;
113+
}
114+
115+
// Callback invoked when received WRITE10 command.
116+
// Process data in buffer to disk's storage and
117+
// return number of written bytes (must be multiple of block size)
118+
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
119+
{
120+
uint8_t* addr = msc_disk[lba];
121+
memcpy(addr, buffer, bufsize);
122+
123+
return bufsize;
124+
}
125+
126+
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
127+
// used to flush any pending cache.
128+
void msc_flush_cb (void)
129+
{
130+
// nothing to do
131+
}

examples/Composite/mouse_external_flash/mouse_external_flash.ino

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ uint8_t const desc_hid_report[] =
4242

4343
Adafruit_USBD_HID usb_hid;
4444

45-
const int pin = 7;
45+
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
46+
const int pin = 4; // Left Button
47+
bool activeState = true;
48+
#elif defined ARDUINO_NRF52840_FEATHER
49+
const int pin = 7; // UserSw
50+
bool activeState = false;
51+
#else
52+
const int pin = 12;
53+
bool activeState = false;
54+
#endif
4655

4756
// the setup function runs once when you press reset or power the board
4857
void setup()
@@ -67,7 +76,7 @@ void setup()
6776

6877

6978
// Set up button
70-
pinMode(pin, INPUT_PULLUP);
79+
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
7180

7281
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
7382
usb_hid.begin();
@@ -84,7 +93,7 @@ void loop()
8493
delay(10);
8594

8695
// button is active low
87-
uint32_t const btn = 1 - digitalRead(pin);
96+
uint32_t const btn = (digitalRead(pin) == activeState);
8897

8998
// Remote wakeup
9099
if ( USBDevice.suspended() && btn )
@@ -133,4 +142,4 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
133142
void msc_flush_cb (void)
134143
{
135144
flash.syncBlocks();
136-
}
145+
}

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ uint8_t const desc_hid_report[] =
3131
Adafruit_USBD_HID usb_hid;
3232
Adafruit_USBD_MSC usb_msc;
3333

34-
const int pin = 7;
34+
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
35+
const int pin = 4; // Left Button
36+
bool activeState = true;
37+
#elif defined ARDUINO_NRF52840_FEATHER
38+
const int pin = 7; // UserSw
39+
bool activeState = false;
40+
#else
41+
const int pin = 12;
42+
bool activeState = false;
43+
#endif
3544

3645
// the setup function runs once when you press reset or power the board
3746
void setup()
@@ -51,13 +60,13 @@ void setup()
5160
usb_msc.begin();
5261

5362
// Set up button
54-
pinMode(pin, INPUT_PULLUP);
63+
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
5564

5665
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
5766
usb_hid.begin();
5867

5968
Serial.begin(115200);
60-
while ( !Serial ) delay(10); // wait for native usb
69+
while( !USBDevice.mounted() ) delay(1); // wait for native usb
6170

6271
Serial.println("Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example");
6372
}
@@ -68,7 +77,7 @@ void loop()
6877
delay(10);
6978

7079
// button is active low
71-
uint32_t const btn = 1 - digitalRead(pin);
80+
uint32_t const btn = (digitalRead(pin) == activeState);
7281

7382
// Remote wakeup
7483
if ( USBDevice.suspended() && btn )
@@ -119,4 +128,4 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
119128
void msc_flush_cb (void)
120129
{
121130
// nothing to do
122-
}
131+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Instructions for node.js based example
2+
3+
===Setup===
4+
5+
1. Upload example code to your board
6+
2. Install node.js if you haven't already
7+
3. Run `npm install` to install the dependencies
8+
4. If this should fail on windows try installing the build tools via `npm i -g windows-build-tools`
9+
5. While the board is connected run `node hid_test.js`
10+
6. If this should fail make sure the VID and PID of your board is listed in boards.js
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
"Feather_nRF52840":[0X239A,0X8029],
3+
"Metro_nRF52840":[0X239A,0X803F],
4+
"Circuit_Playground_Express":[0X239A,0X8018],
5+
}

examples/HID/hid_generic_inout/hid_test.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// IMPORTANT: install dependency via 'npm i node-hid' in the same location as the script
1+
// IMPORTANT: install the dependency via 'npm i node-hid' in the same location as the script
22
// If the install fails on windows you may need to run 'npm i -g windows-build-tools' first to be able to compile native code needed for this library
33

44
var HID = require('node-hid');
55
var os = require('os')
6+
// list of supported devices
7+
var boards = require('./boards.js')
68

79
var isWin = (os.platform() === 'win32');
810
var devices = HID.devices();
911

10-
// choose either of the following supported devices:
11-
// Metro_nRF52840, Feather_nRF52840
12-
13-
var deviceInfo = devices.find(Feather_nRF52840);
12+
// this will choose any device found in the boards.js file
13+
var deviceInfo = devices.find(anySupportedBoard);
1414
var reportLen = 64;
1515

1616
var message = "Hello World!"
@@ -51,17 +51,21 @@ if( deviceInfo ) {
5151
}
5252

5353

54-
55-
56-
function Feather_nRF52840(d) {
57-
return isDevice(0X239A,0X8029,d)
54+
function anySupportedBoard(d) {
55+
56+
for (var key in boards) {
57+
if (boards.hasOwnProperty(key)) {
58+
if (isDevice(boards[key],d)) {
59+
console.log("Found " + d.product);
60+
return true;
61+
}
62+
}
63+
}
64+
return false;
5865
}
5966

60-
function Metro_nRF52840(d) {
61-
return isDevice(0X239A,0X803F,d)
62-
}
6367

68+
function isDevice(board,d){
69+
return d.vendorId==board[0] && d.productId==board[1];
70+
}
6471

65-
function isDevice(vid,pid,d){
66-
return d.vendorId==vid && d.productId==pid;
67-
}

0 commit comments

Comments
 (0)