Kevin79611:
(PT09 WAV\n);
awesome Jim, the way they explain it, \n was for a new line. I think I can delete that and it should work.
Cheers
2 Likes
Jim
I don’t need to use any of the loop code as I an going to call it by
Serial1.write (PT09 WAV);
In the loop code what is needed for the FlushInput() function? Do I need it?
James46717:
Serial.println(F("What would you like to do?"));
Serial.println(F("[r] - reset"));
Serial.println(F("[+] - Vol +"));
Serial.println(F("[-] - Vol -"));
Serial.println(F("[L] - List files"));
Serial.println(F("[P] - play by file name"));
Serial.println(F("[#] - play by file number"));
Serial.println(F("[=] - pause playing"));
Serial.println(F("[>] - unpause playing"));
Serial.println(F("[q] - stop playing"));
Serial.println(F("[t] - playtime status"));
Serial.println(F("> "));
while (!Serial.available());
char cmd = Serial.read();
flushInput();
switch (cmd) {
case 'r': {
if (!sfx.reset()) {
Serial.println("Reset failed");
}
break;
}
case 'L': {
uint8_t files = sfx.listFiles();
Serial.println("File Listing");
Serial.println("========================");
Serial.println();
Serial.print("Found "); Serial.print(files); Serial.println(" Files");
for (uint8_t f=0; f<files; f++) {
Serial.print(f);
Serial.print("\tname: "); Serial.print(sfx.fileName(f));
Serial.print("\tsize: "); Serial.println(sfx.fileSize(f));
}
Serial.println("========================");
break;
}
case '#': {
Serial.print("Enter track #");
uint8_t n = readnumber();
Serial.print("\nPlaying track #"); Serial.println(n);
if (! sfx.playTrack((uint8_t)n) ) {
Serial.println("Failed to play track?");
}
break;
}
case 'P': {
Serial.print("Enter track name (full 12 character name!) >");
char name[20];
readline(name, 20);
Serial.print("\nPlaying track \""); Serial.print(name); Serial.print("\"");
if (! sfx.playTrack(name) ) {
Serial.println("Failed to play track?");
}
break;
}
case '+': {
Serial.println("Vol up...");
uint16_t v;
if (! (v = sfx.volUp()) ) {
Serial.println("Failed to adjust");
} else {
Serial.print("Volume: "); Serial.println(v);
}
break;
}
case '-': {
Serial.println("Vol down...");
uint16_t v;
if (! (v=sfx.volDown()) ) {
Serial.println("Failed to adjust");
} else {
Serial.print("Volume: ");
Serial.println(v);
}
break;
}
case '=': {
Serial.println("Pausing...");
if (! sfx.pause() ) Serial.println("Failed to pause");
break;
}
case '>': {
Serial.println("Unpausing...");
if (! sfx.unpause() ) Serial.println("Failed to unpause");
break;
}
case 'q': {
Serial.println("Stopping...");
if (! sfx.stop() ) Serial.println("Failed to stop");
break;
}
case 't': {
Serial.print("Track time: ");
uint32_t current, total;
if (! sfx.trackTime(¤t, &total) ) Serial.println("Failed to query");
Serial.print(current); Serial.println(" seconds");
break;
}
case 's': {
Serial.print("Track size (bytes remaining/total): ");
uint32_t remain, total;
if (! sfx.trackSize(&remain, &total) )
Serial.println("Failed to query");
Serial.print(remain); Serial.print("/"); Serial.println(total);
break;
}
}
}
/************************ MENU HELPERS ***************************/
void flushInput() {
// Read all available serial input to flush pending data.
uint16_t timeoutloop = 0;
while (timeoutloop++ < 40) {
while(Serial1.available()) {
Serial1.read();
timeoutloop = 0; // If char was received reset the timer
}
delay(1);
}
}
char readBlocking() {
while (!Serial.available());
return Serial.read();
}
uint16_t readnumber() {
uint16_t x = 0;
char c;
while (! isdigit(c = readBlocking())) {
//Serial.print(c);
}
Serial.print(c);
x = c - '0';
while (isdigit(c = readBlocking())) {
Serial.print(c);
x *= 10;
x += c - '0';
}
return x;
}
uint8_t readline(char *buff, uint8_t maxbuff) {
uint16_t buffidx = 0;
while (true) {
if (buffidx > maxbuff) {
break;
}
if (Serial.available()) {
char c = Serial.read();
//Serial.print(c, HEX); Serial.print("#"); Serial.println(c);
if (c == '\r') continue;
if (c == 0xA) {
if (buffidx == 0) { // the first 0x0A is ignored
continue;
}
buff[buffidx] = 0; // null term
return buffidx;
}
buff[buffidx] = c;
buffidx++;
}
}
buff[buffidx] = 0; // null term
return buffidx;
}
/************************ MENU HELPERS ***************************/
2 Likes
Hardware USARTs operate independent of the program. If valid serial data is received it will be stored in a small buffer. If the data is not read by the program in a timely manner it will be overwritten leading to potentially corrupt message.
My understanding of flushinput()
.
It reads any data in the buffer to clear it, ensuring the next received data will be part of a valid message.
It may or may not be needed. Suggest trying without it.
I think flushinput()
was part of the early development of the Arduino Compiler and is not really needed now but is kept in to be compatible with old code. I think handling of serial data is better in the current version of the Arduino.
Anyway …
Cheers
Jim
4 Likes
Ok thanks for the explanation. I will post what I find once finished.
1 Like
menucommands_not_using_software_serial:189:29: error: stray '\' in program
Serial1.write(PT09 WAV\n);
SOLUTION1: The Nano33BLE did not like this format. It works with Serial1.write("#9 "); the \n scares it too so delete that.
SOLUTION2: To upload files or to open the SFX board as a USB with your PC onceSFX board is wired up to an Arduino:
Put a switch inline with the GND to UG connection on the SFX and open the contacts to float the connection and this will allow USB connection to a PC, recognise the device as a USB drive and now you can upload your files. No need to disconnect the board from your Arduino board.
3 Likes