23
23
// 8KB is the smallest size that windows allow to mount
24
24
#define DISK_BLOCK_NUM 16
25
25
#define DISK_BLOCK_SIZE 512
26
+
26
27
#include " ramdisk.h"
27
28
28
29
Adafruit_USBD_MSC usb_msc;
@@ -41,37 +42,41 @@ uint8_t const desc_hid_report[] = {
41
42
Adafruit_USBD_HID usb_hid;
42
43
43
44
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
44
- const int pin = 4 ; // Left Button
45
- bool activeState = true ;
45
+ const int pin = 4 ; // Left Button
46
+ bool activeState = true ;
46
47
47
48
#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
48
- const int pin = BUTTON_DOWN;
49
- bool activeState = true ;
49
+ const int pin = BUTTON_DOWN;
50
+ bool activeState = true ;
50
51
51
52
#elif defined PIN_BUTTON1
52
- const int pin = PIN_BUTTON1;
53
- bool activeState = false ;
53
+ const int pin = PIN_BUTTON1;
54
+ bool activeState = false ;
54
55
55
56
#elif defined(ARDUINO_ARCH_ESP32)
56
- const int pin = 0 ;
57
- bool activeState = false ;
57
+ const int pin = 0 ;
58
+ bool activeState = false ;
59
+
60
+ #elif defined(ARDUINO_ARCH_RP2040)
61
+ const int pin = D0;
62
+ bool activeState = false ;
58
63
59
64
#else
60
- const int pin = 12 ;
61
- bool activeState = false ;
65
+ const int pin = A0 ;
66
+ bool activeState = false ;
62
67
#endif
63
68
69
+
64
70
// the setup function runs once when you press reset or power the board
65
- void setup ()
66
- {
67
- #if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
68
- // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
69
- TinyUSB_Device_Init (0 );
70
- #endif
71
+ void setup () {
72
+ // Manual begin() is required on core without built-in support e.g. mbed rp2040
73
+ if (!TinyUSBDevice.isInitialized ()) {
74
+ TinyUSBDevice.begin (0 );
75
+ }
71
76
72
77
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
73
78
usb_msc.setID (" Adafruit" , " Mass Storage" , " 1.0" );
74
-
79
+
75
80
// Set disk size
76
81
usb_msc.setCapacity (DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
77
82
@@ -80,7 +85,7 @@ void setup()
80
85
81
86
// Set Lun ready (RAM disk is always ready)
82
87
usb_msc.setUnitReady (true );
83
-
88
+
84
89
usb_msc.begin ();
85
90
86
91
// Set up button
@@ -93,32 +98,23 @@ void setup()
93
98
usb_hid.begin ();
94
99
95
100
Serial.begin (115200 );
96
- while ( !TinyUSBDevice.mounted () ) delay (1 ); // wait for native usb
97
-
98
101
Serial.println (" Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example" );
99
102
}
100
103
101
- void loop ()
102
- {
103
- // poll gpio once each 10 ms
104
- delay (10 );
105
-
104
+ void process_hid () {
106
105
// button is active low
107
106
uint32_t const btn = (digitalRead (pin) == activeState);
108
107
109
108
// Remote wakeup
110
- if ( TinyUSBDevice.suspended () && btn )
111
- {
109
+ if (TinyUSBDevice.suspended () && btn) {
112
110
// Wake up host if we are in suspend mode
113
111
// and REMOTE_WAKEUP feature is enabled by host
114
112
tud_remote_wakeup ();
115
113
}
116
114
117
115
/* ------------- Mouse -------------*/
118
- if ( usb_hid.ready () )
119
- {
120
- if ( btn )
121
- {
116
+ if (usb_hid.ready ()) {
117
+ if (btn) {
122
118
int8_t const delta = 5 ;
123
119
usb_hid.mouseMove (0 , delta, delta); // no ID: right + down
124
120
@@ -128,11 +124,29 @@ void loop()
128
124
}
129
125
}
130
126
127
+ void loop () {
128
+ #ifdef TINYUSB_NEED_POLLING_TASK
129
+ // Manual call tud_task since it isn't called by Core's background
130
+ TinyUSBDevice.task ();
131
+ #endif
132
+
133
+ // not enumerated()/mounted() yet: nothing to do
134
+ if (!TinyUSBDevice.mounted ()) {
135
+ return ;
136
+ }
137
+
138
+ // poll gpio once each 10 ms
139
+ static uint32_t ms = 0 ;
140
+ if (millis () - ms > 10 ) {
141
+ ms = millis ();
142
+ process_hid ();
143
+ }
144
+ }
145
+
131
146
// Callback invoked when received READ10 command.
132
147
// Copy disk's data to buffer (up to bufsize) and
133
148
// return number of copied bytes (must be multiple of block size)
134
- int32_t msc_read_cb (uint32_t lba, void * buffer, uint32_t bufsize)
135
- {
149
+ int32_t msc_read_cb (uint32_t lba, void * buffer, uint32_t bufsize) {
136
150
uint8_t const * addr = msc_disk[lba];
137
151
memcpy (buffer, addr, bufsize);
138
152
@@ -142,8 +156,7 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
142
156
// Callback invoked when received WRITE10 command.
143
157
// Process data in buffer to disk's storage and
144
158
// return number of written bytes (must be multiple of block size)
145
- int32_t msc_write_cb (uint32_t lba, uint8_t * buffer, uint32_t bufsize)
146
- {
159
+ int32_t msc_write_cb (uint32_t lba, uint8_t * buffer, uint32_t bufsize) {
147
160
uint8_t * addr = msc_disk[lba];
148
161
memcpy (addr, buffer, bufsize);
149
162
@@ -152,7 +165,6 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
152
165
153
166
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
154
167
// used to flush any pending cache.
155
- void msc_flush_cb (void )
156
- {
168
+ void msc_flush_cb (void ) {
157
169
// nothing to do
158
170
}
0 commit comments