Skip to content

Commit 3f03adc

Browse files
committed
Added possibility to register a callback when MSD is detected
Former-commit-id: 59714fd
1 parent 675de20 commit 3f03adc

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

libraries/UsbHostMsd/UsbHostMsd.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,7 @@ extern "C" uint8_t usb_host_msd_get_default_lun();
2525
extern "C" uint8_t usb_host_msd_get_lun_num();
2626
extern "C" uint32_t usb_host_msd_get_num_of_blocks(uint8_t lun);
2727
extern "C" uint32_t usb_host_msd_get_block_size(uint8_t lun);
28-
29-
30-
#define CBW_SIGNATURE 0x43425355
31-
#define CSW_SIGNATURE 0x53425355
32-
33-
#define DEVICE_TO_HOST 0x80
34-
#define HOST_TO_DEVICE 0x00
35-
36-
#define GET_MAX_LUN (0xFE)
37-
#define BO_MASS_STORAGE_RESET (0xFF)
28+
extern "C" void usb_host_msd_attach_mnt_cbk(void (*fnc)(void));
3829

3930

4031
/* -------------------------------------------------------------------------- */
@@ -243,3 +234,6 @@ const char *USBHostMSD::get_type() const {
243234
return "USBMSD";
244235
}
245236

237+
bool USBHostMSD::attach_detected_callback(void (*cbk)()) {
238+
usb_host_msd_attach_mnt_cbk(cbk);
239+
}

libraries/UsbHostMsd/UsbHostMsd.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class USBHostMSD : public BlockDevice {
4646
virtual const char *get_type() const override;
4747
virtual bool select_lun(uint8_t lun);
4848
virtual uint8_t get_lun_num();
49+
bool attach_detected_callback(void (*cbk)());
4950

5051
private:
5152
uint8_t get_lun();

libraries/UsbHostMsd/examples/USB_HOST_MSD/USB_HOST_MSD.ino

+21-12
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@
2626
USBHostMSD block_device;
2727
FATFileSystem fs(TEST_FS_NAME);
2828

29-
extern "C" void log_init();
30-
extern "C" int mylogadd(const char *fmt, ...) ;
31-
extern "C" void empty_log();
32-
3329
std::string root_folder = std::string("/") + std::string(TEST_FS_NAME);
3430
std::string folder_test_name = root_folder + std::string("/") + std::string(TEST_FOLDER_NAME);
3531
std::string file_test_name = folder_test_name + std::string("/") + std::string(TEST_FILE);
3632

33+
/* this callback will be called when a Mass Storage Device is plugged in */
34+
void device_attached_callback(void) {
35+
Serial.println();
36+
Serial.println("++++ Mass Storage Device detected ++++");
37+
Serial.println();
38+
}
39+
3740
void setup() {
38-
log_init();
3941
/*
4042
* SERIAL INITIALIZATION
4143
*/
@@ -44,8 +46,13 @@ void setup() {
4446

4547
}
4648

49+
Serial.println();
4750
Serial.println("*** USB HOST Mass Storage Device example ***");
48-
51+
Serial.println();
52+
53+
/* attache the callback so that when the device is inserted the device_attached_callback
54+
will be automatically called */
55+
block_device.attach_detected_callback(device_attached_callback);
4956
/* list to store all directory in the root */
5057
std::vector<std::string> dir_list;
5158

@@ -62,7 +69,6 @@ void setup() {
6269
Serial.print(".");
6370
if(count % 30 == 0) {
6471
Serial.println();
65-
empty_log();
6672
}
6773
}
6874
count++;
@@ -105,7 +111,9 @@ void setup() {
105111
}
106112
else if(ent->d_type == DT_DIR) {
107113
Serial.print("- [Fold]: ");
108-
dir_list.push_back(ent->d_name);
114+
if(ent->d_name[0] != '.') { /* avoid hidden folders (.Trash might contain a lot of files) */
115+
dir_list.push_back(ent->d_name);
116+
}
109117
}
110118
Serial.println(ent->d_name);
111119
dirIndex++;
@@ -118,6 +126,10 @@ void setup() {
118126
while(1);
119127
}
120128

129+
if(dirIndex == 0) {
130+
Serial.println("Empty SDCARD");
131+
}
132+
121133
bool found_test_folder = false;
122134

123135
/*
@@ -144,7 +156,6 @@ void setup() {
144156
}
145157
else if(ent->d_type == DT_DIR) {
146158
Serial.print(" - [Fold]: ");
147-
dir_list.push_back(ent->d_name);
148159
}
149160
Serial.println(ent->d_name);
150161
}
@@ -291,9 +302,7 @@ void setup() {
291302
Serial.println(file_test_name.c_str());
292303
}
293304
}
294-
if(dirIndex == 0) {
295-
Serial.println("Empty SDCARD");
296-
}
305+
297306
}
298307

299308
void loop() {

libraries/UsbHostMsd/tu_msc.c

+8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ uint32_t usb_host_msd_get_block_size(uint8_t lun) {
6767
return 0;
6868
}
6969

70+
static void (*mount_fnc)(void) = NULL;
7071

72+
void usb_host_msd_attach_mnt_cbk(void (*fnc)(void)) {
73+
mount_fnc = fnc;
74+
}
7175

7276
//--------------------------------------------------------------------+
7377
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
@@ -121,6 +125,10 @@ void tuh_msc_mount_cb(uint8_t dev_addr) {
121125
}
122126
}
123127
device_address = dev_addr;
128+
129+
if(mount_fnc != NULL) {
130+
mount_fnc();
131+
}
124132
}
125133

126134

0 commit comments

Comments
 (0)