|
| 1 | +/* |
| 2 | + Opta - DirList |
| 3 | +
|
| 4 | + The sketch shows how to mount an usb storage device and how to |
| 5 | + get a list of the existing folders and files. |
| 6 | + It will print debugging output to the RS485 port. |
| 7 | +
|
| 8 | + The circuit: |
| 9 | + - Arduino Opta RS485 or WiFi |
| 10 | +
|
| 11 | + This example code is in the public domain. |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <Arduino_USBHostMbed5.h> |
| 15 | +#include <DigitalOut.h> |
| 16 | +#include <FATFileSystem.h> |
| 17 | + |
| 18 | +USBHostMSD msd; |
| 19 | +mbed::FATFileSystem usb("usb"); |
| 20 | + |
| 21 | +#include <ArduinoRS485.h> |
| 22 | +constexpr auto baudrate { 115200 }; |
| 23 | + |
| 24 | +void setup() |
| 25 | +{ |
| 26 | + configureRS485(baudrate); |
| 27 | + printlnRS485("Starting USB Dir List example..."); |
| 28 | + |
| 29 | + |
| 30 | + printRS485("Please, connect the USB device..."); |
| 31 | + while (!msd.connect()) { |
| 32 | + printRS485("."); |
| 33 | + delay(1000); |
| 34 | + } |
| 35 | + |
| 36 | + char buf[256] {}; |
| 37 | + |
| 38 | + printlnRS485(""); |
| 39 | + printRS485("Mounting USB device... "); |
| 40 | + int err = usb.mount(&msd); |
| 41 | + |
| 42 | + if (err) { |
| 43 | + printRS485("Error mounting USB device "); |
| 44 | + snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", -err == EINVAL ? "Invalid Filesystem" : "Unknown Error", -err); |
| 45 | + printlnRS485(buf); |
| 46 | + while (1) |
| 47 | + ; |
| 48 | + } |
| 49 | + printlnRS485("done."); |
| 50 | + |
| 51 | + // Display the root directory |
| 52 | + printRS485("Opening the root directory... "); |
| 53 | + DIR* d = opendir("/usb/"); |
| 54 | + if (!d) { |
| 55 | + snprintf(buf, sizeof(buf), "error: %s (%d)", strerror(errno), -errno); |
| 56 | + printRS485(buf); |
| 57 | + } |
| 58 | + printlnRS485("done."); |
| 59 | + |
| 60 | + printlnRS485("Root directory:"); |
| 61 | + unsigned int count { 0 }; |
| 62 | + while (true) { |
| 63 | + struct dirent* e = readdir(d); |
| 64 | + if (!e) { |
| 65 | + break; |
| 66 | + } |
| 67 | + count++; |
| 68 | + snprintf(buf, sizeof(buf), " %s\r\n", e->d_name); |
| 69 | + printRS485(buf); |
| 70 | + } |
| 71 | + printRS485(count); |
| 72 | + printlnRS485(" files found!"); |
| 73 | + |
| 74 | + snprintf(buf, sizeof(buf), "Closing the root directory... "); |
| 75 | + printRS485(buf); |
| 76 | + fflush(stdout); |
| 77 | + err = closedir(d); |
| 78 | + snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
| 79 | + printRS485(buf); |
| 80 | + if (err < 0) { |
| 81 | + snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno); |
| 82 | + printRS485(buf); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void loop() |
| 87 | +{ |
| 88 | + delay(1000); |
| 89 | + // handle disconnection and reconnection |
| 90 | + if (!msd.connected()) { |
| 91 | + msd.connect(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +size_t printRS485(char* message) |
| 96 | +{ |
| 97 | + RS485.beginTransmission(); |
| 98 | + auto len = strlen(message); |
| 99 | + RS485.write(message, len); |
| 100 | + RS485.endTransmission(); |
| 101 | +} |
| 102 | + |
| 103 | +size_t printRS485(int num) |
| 104 | +{ |
| 105 | + char message[256] {}; |
| 106 | + sprintf(message, "%d", num); |
| 107 | + RS485.beginTransmission(); |
| 108 | + auto len = strlen(message); |
| 109 | + RS485.write(message, len); |
| 110 | + RS485.endTransmission(); |
| 111 | +} |
| 112 | + |
| 113 | +size_t printlnRS485(char* message) |
| 114 | +{ |
| 115 | + printRS485(message); |
| 116 | + RS485.beginTransmission(); |
| 117 | + RS485.write('\r'); |
| 118 | + RS485.write('\n'); |
| 119 | + RS485.endTransmission(); |
| 120 | +} |
| 121 | + |
| 122 | +void configureRS485(const int baudrate) |
| 123 | +{ |
| 124 | + const auto bitduration { 1.f / baudrate }; |
| 125 | + const auto wordlen { 9.6f }; // OR 10.0f depending on the channel configuration |
| 126 | + const auto preDelayBR { bitduration * wordlen * 3.5f * 1e6 }; |
| 127 | + const auto postDelayBR { bitduration * wordlen * 3.5f * 1e6 }; |
| 128 | + |
| 129 | + RS485.begin(baudrate); |
| 130 | + RS485.setDelays(preDelayBR, postDelayBR); |
| 131 | + RS485.noReceive(); |
| 132 | +} |
0 commit comments