Skip to content

Commit b55a4c6

Browse files
committed
Fixed USB code in demo sketch
1 parent 7a25298 commit b55a4c6

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ animate(20);
8989

9090
Note: The Arduino_H7_Video library uses some shared SDRAM for the framebuffer, and `SDRAM.begin();` should **not** be called after initializing the display.
9191

92-
Scripts stored on USB flash devices plugged into the USB port on the Giga can be loaded with `load "script.lox"` at the prompt (some issues with this, currently).
92+
Scripts stored on USB flash devices plugged into the USB port on the Giga (or via an OTG cable on Portenta H7) can be loaded with `load "script.lox"` at the prompt (any file extension can be used).
9393

9494
## Adding Functions
9595

@@ -150,7 +150,7 @@ int Serial_vfprintf(FILE *dummy, const char *fmt, va_list args);
150150
#endif
151151
```
152152

153-
5. Create a script which defines these two functions, and includes the necessary C headers (as a minimum). Here is a possible outline:
153+
5. Create a script which defines these three functions, and includes the necessary C headers (as a minimum). Here is a possible outline:
154154

155155
```ino
156156
extern "C" {

examples/clox_gfx_demo/clox_gfx_demo.ino

+22-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Arduino_H7_Video Display(1024, 768, USBCVideo);
3333

3434
USBHostMSD msd;
3535
mbed::FATFileSystem usb("usb");
36+
#if ( defined(ARDUINO_PORTENTA_H7_M7) && defined(ARDUINO_ARCH_MBED) )
37+
mbed::DigitalOut otg(PB_14, 0);
38+
#endif
3639
#endif
3740

3841
#if CLOX_WEB_CONSOLE
@@ -117,7 +120,7 @@ void loop() {
117120
line = "";
118121
char ch = '\0';
119122
while (ch != '\n') {
120-
if (Serial.available()) {
123+
if (Serial.available() > 0) {
121124
ch = Serial.read();
122125
line += ch;
123126
}
@@ -154,7 +157,10 @@ void loop() {
154157

155158
digitalWrite(LEDB, LOW);
156159
if (line.startsWith("load")) {
157-
String filename = line.substring(line.indexOf('\"'), line.lastIndexOf('\"'));
160+
String filename;
161+
if (line.indexOf('\"') != line.lastIndexOf('\"')) {
162+
filename = line.substring(line.indexOf('\"') + 1, line.lastIndexOf('\"'));
163+
}
158164
if (filename.length()) {
159165
String program = readFile(filename);
160166
if (program.length()) {
@@ -172,7 +178,14 @@ void loop() {
172178
}
173179

174180
String readFile(String filename) {
181+
String fileData;
175182
#if CLOX_USB_HOST
183+
Serial_printf("Waiting for USB device...\n");
184+
long until = millis() + 10 * 1000;
185+
while (!msd.connected() && (until > millis())) {
186+
msd.connect();
187+
delay(3000);
188+
}
176189
Serial_printf("Mounting USB device...\n");
177190
int err = usb.mount(&msd);
178191
if (err) {
@@ -181,23 +194,24 @@ String readFile(String filename) {
181194
else {
182195
filename = String("/usb/") + filename;
183196
FILE *f = fopen(filename.c_str(), "r+");
184-
String fileData;
185-
char buf[256];
186197
if (f) {
198+
char buf[256];
187199
while (fgets(buf, 256, f) != NULL) {
188200
fileData += String(buf);
189201
}
190202
fclose(f);
191-
return fileData;
192203
}
193204
else {
194-
Serial_printf("Error reading file: %s\n", filename);
205+
Serial_printf("Error reading file: %s\n", filename.c_str());
206+
}
207+
if (!usb.unmount()) {
208+
Serial_printf("USB device dismounted.\n");
195209
}
196210
}
197211
#else
198212
Serial_printf("Error: USB support not available.\n");
199213
#endif
200-
return "";
214+
return fileData;
201215
}
202216

203217
#if CLOX_WEB_CONSOLE
@@ -206,7 +220,7 @@ int poll_webserver(unsigned long poll_time) {
206220
while (millis() < until) {
207221
WiFiClient webclient = webserver.available();
208222
if (webclient) {
209-
boolean currentLineIsBlank = true;
223+
bool currentLineIsBlank = true;
210224
while (webclient.connected()) {
211225
if (webclient.available()) {
212226
char c = webclient.read();

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=clox_gfx
2-
version=0.0.2
2+
version=0.0.3
33
author=Richard Spencer <cpptutor@outlook.com>
44
maintainer=Richard Spencer <cpptutor@outlook.com>
55
sentence=Port of the Lox interpreted programming language to Arduino

0 commit comments

Comments
 (0)