Skip to content

Commit f24496f

Browse files
authored
Merge pull request #19 from PTS93/master
A little cleanup, new example
2 parents 97b272b + 4b2d1fa commit f24496f

File tree

10 files changed

+782
-24
lines changed

10 files changed

+782
-24
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 `../../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+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
25+
#ifndef RAMDISK_H_
26+
#define RAMDISK_H_
27+
28+
#define README_CONTENTS \
29+
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
30+
31+
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
32+
{
33+
//------------- Block0: Boot Sector -------------//
34+
// byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
35+
// sector_per_cluster = 1; reserved_sectors = 1;
36+
// fat_num = 1; fat12_root_entry_num = 16;
37+
// sector_per_fat = 1; sector_per_track = 1; head_num = 1; hidden_sectors = 0;
38+
// drive_number = 0x80; media_type = 0xf8; extended_boot_signature = 0x29;
39+
// filesystem_type = "FAT12 "; volume_serial_number = 0x1234; volume_label = "TinyUSB MSC";
40+
// FAT magic code at offset 510-511
41+
{
42+
0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
43+
0x01, 0x10, 0x00, 0x10, 0x00, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
44+
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0x34, 0x12, 0x00, 0x00, 'T' , 'i' , 'n' , 'y' , 'U' ,
45+
'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
46+
47+
// Zero up to 2 last bytes of FAT magic code
48+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56+
57+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65+
66+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74+
75+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
79+
},
80+
81+
//------------- Block1: FAT12 Table -------------//
82+
{
83+
0xF8, 0xFF, 0xFF, 0xFF, 0x0F // // first 2 entries must be F8FF, third entry is cluster end of readme file
84+
},
85+
86+
//------------- Block2: Root Directory -------------//
87+
{
88+
// first entry is volume label
89+
'T' , 'i' , 'n' , 'y' , 'U' , 'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 0x08, 0x00, 0x00, 0x00, 0x00,
90+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x6D, 0x65, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91+
// second entry is readme file
92+
'R' , 'E' , 'A' , 'D' , 'M' , 'E' , ' ' , ' ' , 'T' , 'X' , 'T' , 0x20, 0x00, 0xC6, 0x52, 0x6D,
93+
0x65, 0x43, 0x65, 0x43, 0x00, 0x00, 0x88, 0x6D, 0x65, 0x43, 0x02, 0x00,
94+
sizeof(README_CONTENTS)-1, 0x00, 0x00, 0x00 // readme's files size (4 Bytes)
95+
},
96+
97+
//------------- Block3: Readme Content -------------//
98+
README_CONTENTS
99+
};
100+
101+
#endif /* RAMDISK_H_ */

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+
}

0 commit comments

Comments
 (0)