I am only a hobbyist, I grew up with FORTRAN 4, in the early 70s! I have had occasion to use JavaScript, Python, VBA but I am not a professional programmer. I would classify myself as a tinkerer. I learn from example. That being said, I am enclosing the program that resides on both WIO #1 and WIO #2. I have no need to be overclocking. I am sure my code is not efficient.
Here is the code that resides on BOTH WIOs:
//Background:
//tft.setRotation®; This code rotates the screen. 0=0 , 1=90, 2=180, 3=270
// tft.setFreeFont(FSSBO24)
//
// Where the letters mean:
// F = Free font
// M = Mono
// SS = Sans Serif (double S to distinguish is form serif fonts)
// S = Serif
// B = Bold
// O = Oblique (letter O not zero)
// I = Italic
// # = point size, either 9, 12, 18 or 24
//
#include <Wire.h>
#include"TFT_eSPI.h"
#include"Free_Fonts.h" //include the header file
#include <SPI.h>
#include <Seeed_FS.h>
#define DEV SD
#include “SD/Seeed_SD.h”
//#include “SFUD/Seeed_SFUD.h”
#define SERIAL Serial
File root;
#define BUZZER_PIN WIO_BUZZER
TFT_eSPI tft;
int state = 0;
int xpos = 6; //xpos = 6;
int ypos = 2; // ypos = 2;
int xPrtArea = xpos; // xpos currently 6
int yPrtArea = ypos+27;
int borderColor = TFT_WHITE;
int menuDelay = 2000; //Set Delay for Menu/Button presses
int i5sD = 0;
char* fileList[10]; //MAX of 10 members/Filenames
String fPick;
int iCount = 0;
void showBorders() {
//bC == border Color
tft.drawLine(0,0,320,0,borderColor); //drawing a black line from (0,0) to (0,320) Top Horizantal
tft.drawLine(0,25,315,25,borderColor);
tft.drawLine(0,238,320,238,borderColor); //Bottom horizantal line
tft.drawLine(0,0,0,240,borderColor); //Left Vertical Barely visible must tilt screen to see all the way to the left
tft.drawLine(2,0,2,240,borderColor); //Left Hand Vertical line
tft.drawLine(318,0,318,240,borderColor); //Vertical line Right hand side, cannot push further right to 320
}
void setup() {
Wire.begin();
//Initialise SD card
if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) {
while (1);
}
tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
showBorders(); //Show Border and Heading
//tft.setFreeFont(FSSBO24); //FS12 or FS9 == FreeFont Serif 9pt
//Define Header Text
tft.setFreeFont(FS12); //12pt Text for Header
tft.setCursor(xpos, ypos); //For Header Text
tft.drawString(“Header Text here”, xpos, ypos, GFXFF);
Serial.begin(115200);
pinMode(WIO_KEY_A, INPUT_PULLUP);
pinMode(WIO_KEY_B, INPUT_PULLUP);
pinMode(WIO_KEY_C, INPUT_PULLUP);
pinMode(WIO_5S_UP, INPUT_PULLUP); //Cursor Up
pinMode(WIO_5S_DOWN, INPUT_PULLUP); //Cursor Down
pinMode(WIO_5S_LEFT, INPUT_PULLUP); //Up Directory/ Folder
pinMode(WIO_5S_RIGHT, INPUT_PULLUP); //Down Directory/ Floder
pinMode(WIO_5S_PRESS, INPUT_PULLUP); //Select File
pinMode(BUZZER_PIN, OUTPUT);
SERIAL.print(“Initializing SD card…”);
#ifdef SFUD_USING_QSPI
while (!DEV.begin(104000000UL)) {
SERIAL.println(“Card Mount Failed”);
return;
}
#else
while (!DEV.begin(SDCARD_SS_PIN,SDCARD_SPI,4000000UL)) {
SERIAL.println(“Card Mount Failed”);
return;
}
#endif
SERIAL.println(“initialization done.”);
//listDir(DEV, "/", 0);
SERIAL.println("done!");
}
/*
-
Visible Rectangle Boundary
-
(0,0) to (0,320) TOP Line
-
(0,238) to (320,238) BOTTOM Line
-
(2,0) to (2,240) LEFT Vertical Line
-
(318,0) to (318,240) RIGHT Vertical Line
-
HEADER TOP HORIZANTAL:
-
(0,25) to (315,25) 25 pixels from Top
-
HEADER TEXT 12pt fits between TOP two HORIZANTAL Lines
-
HEADER CURSOR POSITION: (6,2)
-
BODY TEXT: 9pt
-
BODY TEXT Position: (2,29) (2,(2+27))
*/
void loop() {
// defined above: xpos = 6; ypos = 2;
//showBorders();
if (digitalRead(WIO_KEY_A) == LOW) {
state = 1;
//tft.fillScreen(TFT_BLACK);
//showBorders(TFT_WHITE); //See setup
//tft.drawString(“C”,140,100);
playTone(1915, 500);
tft.setFreeFont(FS12); //12pt Text for Header
tft.setCursor(xpos, ypos); //For Header Text
tft.drawString("Listing directory: ", xpos, ypos, GFXFF);
tft.setFreeFont(FS9);
tft.drawString(“WIO_KEY_A-Pressed”, xPrtArea, yPrtArea, GFXFF);
tft.drawString(" ", xPrtArea, yPrtArea, GFXFF);
tft.setCursor(xPrtArea, yPrtArea);
listDir(DEV, “/”, 3);
delay(menuDelay);
}
else if (digitalRead(WIO_KEY_B) == LOW) {
//tft.fillScreen(TFT_BLACK);
//tft.drawString(“D”,140,100);
playTone(1700, 500);
tft.setFreeFont(FS9);
tft.drawString("WIO_KEY_B-Pressed ", xpos, ypos+27, GFXFF);
// “123456789A123456789B123456789C1”
delay(menuDelay);
}
else if (digitalRead(WIO_KEY_C) == LOW) {
state = 3;
//tft.fillScreen(TFT_BLACK);
//tft.drawString(“E”,140,100);
playTone(1519, 500);
tft.setFreeFont(FS9); //31 Characters fit between the 2 vertical boundary lines
tft.drawString("WIO_KEY_C-Pressed ", xpos, ypos+27, GFXFF);
// “123456789A123456789B123456789C1”
delay(menuDelay);
}else if (digitalRead(WIO_5S_UP) == LOW) {
state = 4;
//tft.fillScreen(TFT_BLACK);
//tft.drawString(“F”,140,100);
playTone(1432, 500);
tft.setFreeFont(FS12); //12pt Text for Header
tft.setCursor(xpos, ypos); //For Header Text
tft.drawString(“WIO_5S_UP: Pick File”, xpos, ypos, GFXFF);
// “123456789A123456789B123456789C1”
delay(menuDelay);
i5sD = i5sD + 1; //how many times the UP is pressed
// == how many times to move file pick cursor DOWN
if (i5sD < 0) { //Keep cursor between “1” and the total number of Files
i5sD = 1;
}
if (i5sD > iCount) { // set to last file
i5sD = iCount;
}
//iCount = iCount + 1;
tft.setFreeFont(FS9);
//tft.drawString("WIO_5S_DWN-Pressed ", xpos, ypos+27, GFXFF);
// “123456789A123456789B123456789C1”
//Start Cursor at default starting position: xPrtArea = xpos; 6 AND yPrtArea = ypos+27;
xPrtArea = xpos; // ==6
yPrtArea = ypos+27; //Start at top of printable area
int i = 251; //len of color
tft.startWrite();
tft.setAddrWindow(xPrtArea,(i5sD21)*yPrtArea-15,25,1);
tft.writeColor(TFT_BLACK, i);
tft.endWrite();
delay(menuDelay); //
tft.startWrite();
tft.setAddrWindow(xPrtArea,(i5sD*21)*yPrtArea-15,25,1);
tft.writeColor(TFT_GREEN, i);
tft.endWrite();
} //End press UP
else if (digitalRead(WIO_5S_DOWN) == LOW) { //****** DOWN
state = 5;
i5sD = i5sD + 1; //how many times the DOWN is pressed
// == how many times to move file pick cursor DOWN
if (i5sD < 0) { //Keep cursor between “1” and the total number of Files
i5sD = 1;
}
if (i5sD > iCount) { // set to last file
i5sD = iCount;
}
//tft.drawLine(0,238,320,238,borderColor); //Bottom horizantal line
tft.drawString("i5sD:= " + String(i5sD), 2, 228, GFXFF);
//iCount = iCount + 1;
tft.setFreeFont(FS12); //12pt Text for Header
tft.setCursor(xpos, ypos); //For Header Text
tft.drawString(“WIO_5S_DWN: Pick File”, xpos, ypos, GFXFF);
playTone(1275, 500);
tft.setFreeFont(FS9);
//tft.drawString("WIO_5S_DWN-Pressed ", xpos, ypos+27, GFXFF);
// “123456789A123456789B123456789C1”
//Start Cursor at default starting position: xPrtArea = xpos; 6 AND yPrtArea = ypos+27;
xPrtArea = xpos; // ==6
yPrtArea = ypos+27; //Start at top of printable area
int i = 251; //len of color
tft.startWrite();
tft.setAddrWindow(xPrtArea,(i5sD21)*yPrtArea+15,25,1);
tft.writeColor(TFT_BLACK, i);
tft.endWrite();
delay(menuDelay); //
tft.startWrite();
tft.setAddrWindow(xPrtArea,(i5sD*21)*yPrtArea+15,25,1);
tft.writeColor(TFT_GREEN, i);
tft.endWrite();
}
else if (digitalRead(WIO_5S_LEFT) == LOW) {
state = 6;
//tft.fillScreen(TFT_BLACK);
tft.drawString(“A”,140,100);
playTone(1136, 500);
tft.setFreeFont(FS9);
tft.drawString("WIO_5S_L-Pressed ", xpos, ypos+27, GFXFF);
// “123456789A123456789B123456789C1”
delay(menuDelay);
}
else if (digitalRead(WIO_5S_RIGHT) == LOW) {
state = 7;
//tft.fillScreen(TFT_BLACK);
//tft.drawString(“B”,140,100);
playTone(1014, 500);
tft.setFreeFont(FS9);
tft.drawString("WIO_5S_R-Pressed ", xpos, ypos+27, GFXFF);
// “123456789A123456789B123456789C1”
delay(menuDelay);
}
else if (digitalRead(WIO_5S_PRESS) == LOW) { //Selection Button
state = 8;
//tft.fillScreen(TFT_BLACK);
//tft.drawString(“C”,140,100);
playTone(956, 500);
tft.setFreeFont(FS9);
tft.drawString("WIO_5S_B-Pressed ", xpos, ypos+27, GFXFF);
// “123456789A123456789B123456789C1”
delay(menuDelay);
String fPick = fileList[i5sD]; //Store file name chosen
}
else{
//tft.fillScreen(TFT_BLACK);
}
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(tone);
}
} //End PlayTone
void listDir(fs::FS& fs, const char* dirname, uint8_t levels) {
//Main Header is at Location (6,2)
//Set Cursor within the printable area
String myDir; //Note: File name includes Directory name
String myFile;
//stringOne.trim();Remove white space
//stringOne.length();
SERIAL.print("Listing directory: ");
SERIAL.println(dirname);
tft.setFreeFont(FS9);
// "123456789A123456789B123456789C1"
//tft.drawString(" ", xPrtArea, yPrtArea, GFXFF);
//tft.drawString("Dir:=" + String(dirname), xPrtArea, yPrtArea, GFXFF);
File root = fs.open(dirname);
if (!root) {
SERIAL.println("Failed to open directory");
tft.println("Failed to open directory");
//tft.drawString("Failed to open directory", xPrtArea, yPrtArea, GFXFF);
return;
}
if (!root.isDirectory()) {
SERIAL.println("Not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
SERIAL.print(" DIR : ");
SERIAL.println(file.name());
myDir = file.name(); // myDir Filename includes DIR so no need to print this out
if (levels) {
listDir(fs, file.name(), levels - 1);
}
} else {
SERIAL.print(" FILE: ");
SERIAL.print(file.name());
SERIAL.print(" SIZE: ");
SERIAL.println(file.size());
iCount++; //iCount = iCount + 1; //Count each found File
myFile = file.name(); // myFile
//String sFileName = String(file.name());
char* temp = file.name(); //sFileName;
String sFileName = String(temp); //char* fileList[10]; //MAX of 10 members or Filenames
sFileName.toCharArray(temp, sFileName.length()+1); //inString, converted it back to char array for it to be stored in char* plantList
fileList[iCount] = strdup(temp);
//could free up memory for each
// free(fileList[iCount]);
//myFile = myFile.trim(); does not work
//Remove "/System Volume Information"
//if ( sFileName.substring(1,6) == "/System") { // Volume Information") {
//if(sFileName.indexOf("/System") >= 0) { //This works
if (sFileName.indexOf("/System") >= 0) {
//Skip the volume information
}
else {
tft.drawString(" ", xPrtArea, yPrtArea, GFXFF);
tft.drawString(“F:=” + sFileName, xPrtArea, yPrtArea, GFXFF);
yPrtArea = yPrtArea+21;
//tft.println();
}
}
file = root.openNextFile();
}
} //End listDir