-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUSBFileReader.ino
More file actions
398 lines (329 loc) · 11.9 KB
/
Copy pathUSBFileReader.ino
File metadata and controls
398 lines (329 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// USB Reader Header Files
#include <SPI.h>
#include <UsbFat.h>
#include <masstorage.h>
// Display Header Files
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
// Misc Header Files
#include "Colours.hpp"
// Instantiating the display class
MCUFRIEND_kbv display;
// Touchscreen Variables
#define MINPRESSURE 1//200
#define MAXPRESSURE 1000
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x7575
const int TS_LEFT=886,TS_RT=120,TS_TOP=86,TS_BOT=903;
int touch_x, touch_y; //Touch_getXY() updates global vars
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// Instantiating the USB reader
USB usb;
BulkOnly bulk(&usb);
UsbFat key(&bulk);
// The file
File openedFile;
// Buttons
Adafruit_GFX_Button fileButtons[6];
Adafruit_GFX_Button backButton;
Adafruit_GFX_Button upButton;
Adafruit_GFX_Button downButton;
// Constants
const int TEXT_CHARACTERS_ON_SCREEN = 1100;
// Global variables
bool isTouched = false;
int currentFileLevel = 0;
int currentTextLevel = 0;
bool viewingFile = false;
bool usbConnected = false;
String currentDirectory = "/";
int filesInCurrentDirectoryView = 0;
void setup() {
Serial.begin(9600);
// Initialising the screen and USB
usbConnected = InitialiseUSB();
InitialiseDisplay();
display.setTextColor(TEXT_COLOUR);
DrawUniversalBackground();
// If a USB is connected, the directory view is drawn
if (usbConnected) {
DrawDirectoryView();
}
else {
// If a USB is not connected, text is drawn instructing the user to do so
display.setCursor(0, 85);
display.setTextSize(3);
display.println(" Please insert\n a USB drive\n and reset this\n device!");
}
}
void loop() {
// put your main code here, to run repeatedly:
isTouched = Touch_getXY();
if (!viewingFile) CheckButtonsDirectoryView();
// Checking the press logic for the navigation buttons
// Back button
backButton.press(isTouched && backButton.contains(touch_x, touch_y));
if (backButton.justPressed()) NavigateBack();
if (backButton.justReleased()) backButton.drawButton(false);
// Up button
upButton.press(isTouched && upButton.contains(touch_x, touch_y));
if (upButton.justPressed()) NavigateUp();
if (upButton.justReleased()) upButton.drawButton(false);
// Down button
downButton.press(isTouched && downButton.contains(touch_x, touch_y));
if (downButton.justPressed()) NavigateDown();
if (downButton.justReleased()) downButton.drawButton(false);
}
/*--MY FUNCTIONS--*/
// Function to run the USB initialisation functions
bool InitialiseUSB() {
if (!initUSB(&usb)) return false;
if (!key.begin()) return false;
return true;
}
// Function to initialise the display
void InitialiseDisplay() {
uint16_t ID = display.readID();
display.begin(ID);
display.setRotation(1);
display.fillScreen(BACKGROUND_COLOUR);
}
// Function from example to get whether the screen is touched and map the touch position
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z >= MINPRESSURE && p.z <= MAXPRESSURE);
if (pressed) {
touch_x = 320 - map(p.y, TS_LEFT, TS_RT, 0, 320);
touch_y = map(p.x, TS_TOP, TS_BOT, 0, 240);
}
return pressed;
}
// Function to get the files in a directory, sending it to an array
void GetFilesInDirectory(String path, String *fileNames, int arraySize) {
// Opening the folder
File dir = key.open(path.c_str());
int arrayIndex = 0;
int nameArraySize = 50;
while (true) {
// Opening the next file in the directory
File entry = dir.openNextFile();
if (!entry) break;
// Getting the name of the file as a string
char nameArr[nameArraySize];
entry.getName(nameArr, nameArraySize);
String name = String(nameArr);
if (entry.isDir()) name = "/" + name + "/";
// Returning if the array index is greater than the size of the array
if (arrayIndex >= arraySize) break;
// Adding the name to the array and incrementing the index
*(fileNames + arrayIndex) = name;
arrayIndex++;
}
}
// Function to get the amount of files in a directory
uint16_t GetAmountOfFilesInDirectory(String path) {
uint16_t amountOfFilesInDir = 0;
// Opening the directory path
File dir = key.open(path.c_str());
while (true) {
// Opening the next file in the directory, if it does not exist the loop is broken
File entry = dir.openNextFile();
if (!entry) break;
// Incrementing the amount of files in the directory
amountOfFilesInDir++;
}
return amountOfFilesInDir;
}
// Function to draw the universal background for each window type
void DrawUniversalBackground() {
// The white background
display.fillScreen(BACKGROUND_COLOUR);
// The top bar
display.fillRect(0, 0, 320, 30, BUTTON_COLOUR);
display.drawRect(0, 0, 160 * 2, 30, TEXT_COLOUR);
//display.drawRect(160, 0, 160, 30, BLACK);
// The side bar
display.fillRect(270, 29, 50, 211, BUTTON_COLOUR);
display.drawRect(270, 29, 50, 211, TEXT_COLOUR);
// Triangle test
//display.drawTriangle(304-11, 107, 293-11, 142, 316-11, 142, BLACK);
//display.fillTriangle(304-11, 107, 293-11, 142, 316-11, 142, WHITE);
// The directory and usb information
display.setTextColor(TEXT_COLOUR);
display.setCursor(5, 12);
display.print(currentDirectory);
// The bounding black box
display.drawRect(0, 0, 320, 240, TEXT_COLOUR);
}
// Function to initialise the navigation buttons
void InitialiseNavigationButtons() {
// The back button
backButton.initButton(&display, 295, 63, 50-2, 67, BUTTON_COLOUR, BUTTON_COLOUR, TEXT_COLOUR, "Back", 1);
backButton.drawButton(false);
// The up button
upButton.initButton(&display, 295, 130, 50-2, 67, BUTTON_COLOUR, BUTTON_COLOUR, TEXT_COLOUR, "Up", 1);
upButton.drawButton(false);
// The down button
downButton.initButton(&display, 295, 197, 50-2, 67, BUTTON_COLOUR, BUTTON_COLOUR, TEXT_COLOUR, "Down", 1);
downButton.drawButton(false);
}
/*--THE DIRECTORY VIEW FUNCTIONS--*/
// Function to draw the directory view window
void DrawDirectoryView() {
// Initialising the navigation buttons
InitialiseNavigationButtons();
viewingFile = false;
filesInCurrentDirectoryView = 0;
// Loading all files in directory into an array
uint16_t amountOfFiles = GetAmountOfFilesInDirectory(currentDirectory);
String files[amountOfFiles];
GetFilesInDirectory(currentDirectory, files, amountOfFiles);
// Initialising the file buttons
for (int i = 0; i < 6; i++) {
if (i + currentFileLevel * 6 >= amountOfFiles) break;
bool nextRow = i > 2;
fileButtons[i].initButton(&display, 45 + ((i % 3) * 90), 85 + (int) (85 * nextRow * 1.1),
80, 80, TEXT_COLOUR, BUTTON_COLOUR, TEXT_COLOUR,
files[i + currentFileLevel * 6].c_str(), 1);
fileButtons[i].drawButton(false);
filesInCurrentDirectoryView += 1;
}
}
// Function to check the button presses in the directory view window
void CheckButtonsDirectoryView() {
// Checking the press logic for all file buttons
for (int i = 0; i < 6; i++) {
// If the current button index is greater than the amount of buttons on screen, no press logic is run
if (i + 1 > filesInCurrentDirectoryView) break;
// Pressing the current button if the screen is touched and if the touch coords are in the button
fileButtons[i].press(isTouched && fileButtons[i].contains(touch_x, touch_y));
// Checking press and release logic for the buttons
if (fileButtons[i].justPressed()) OpenFile(i);
if (fileButtons[i].justReleased()) fileButtons[i].drawButton(false);
}
}
/*--THE DIRECTORY VIEW FUNCTIONS--*/
// Function to navigate up
void NavigateUp() {
upButton.drawButton(true);
if (!viewingFile) {
if (currentFileLevel <= 0) return;
currentFileLevel --;
display.fillScreen(BACKGROUND_COLOUR);
DrawUniversalBackground();
DrawDirectoryView();
}
else {
if (currentTextLevel <= 0) return;
currentTextLevel --;
display.fillScreen(BACKGROUND_COLOUR);
DrawUniversalBackground();
DrawTextInFile();
}
}
// Function to navigate down
void NavigateDown() {
downButton.drawButton(true);
if (!viewingFile) {
currentFileLevel ++;
display.fillScreen(BACKGROUND_COLOUR);
DrawUniversalBackground();
DrawDirectoryView();
}
else {
currentTextLevel ++;
display.fillScreen(BACKGROUND_COLOUR);
DrawUniversalBackground();
DrawTextInFile();
}
}
// Function to navigate back
void NavigateBack() {
backButton.drawButton(true);
// TODO: File navigation
if (viewingFile) {
// Getting the name of the current file and removing it from the directory
int fileNameBufferSize = 128;
char fileName[fileNameBufferSize];
openedFile.getName(fileName, fileNameBufferSize);
currentDirectory = currentDirectory.substring(0, currentDirectory.length() - String(fileName).length());
// Closing the file and moving to the directory view
openedFile.close();
}
else {
// Finding the index of the slash separator between directories and slicing the current directory string to remove anything beyond it
int indexOfDirectorySlash = currentDirectory.lastIndexOf('/', currentDirectory.length() - 2);
currentDirectory = currentDirectory.substring(0, indexOfDirectorySlash);
// Refreshing the directory view
currentFileLevel = 0;
}
display.fillScreen(BACKGROUND_COLOUR);
DrawUniversalBackground();
DrawDirectoryView();
}
/*--THE FILE VIEW FUNCTIONS--*/
// Function to open the file view
void OpenFile(int buttonIndex) {
// Loading all files in directory into an array
uint16_t amountOfFiles = GetAmountOfFilesInDirectory(currentDirectory);
String files[amountOfFiles];
GetFilesInDirectory(currentDirectory, files, amountOfFiles);
// Opening the file
String fileToOpen = currentDirectory + files[buttonIndex + currentFileLevel * 6];
openedFile = key.open(fileToOpen.c_str());
// Redrawing the screen to prepare for text display
currentDirectory = fileToOpen;
display.fillScreen(BACKGROUND_COLOUR);
DrawUniversalBackground();
InitialiseNavigationButtons();
if (!openedFile.isDir()) {
// If the file is a file then we run the draw text in file function
currentTextLevel = 0;
viewingFile = true;
DrawTextInFile();
}
else {
// If the file is a directory, we instead open that directory
currentFileLevel = 0;
DrawDirectoryView();
}
}
// Function to draw the text in a file
void DrawTextInFile() {
// Initialising the navigation buttons
InitialiseNavigationButtons();
// Setting text position settings
int yPos = 35;
display.setCursor(5, yPos);
int textCharactersOnScreenTemp = TEXT_CHARACTERS_ON_SCREEN;
// Finding the start of the file based on scroll amount and seeking to it
int startOfFile = currentTextLevel * TEXT_CHARACTERS_ON_SCREEN;
openedFile.seek(startOfFile);
// Looping through each character in the file
for (int i = startOfFile; i < ((currentTextLevel + 1) * textCharactersOnScreenTemp); i++) {
// If the file is at the end of the line, another line is added
if (i % 44 == 0 && i != startOfFile) {
yPos += 8;
display.setCursor(5, yPos);
}
// Reading the next character of the file
char currentCharacter = (char) openedFile.read();
// If the current character is a newline, a character is printed to show a newline
if (currentCharacter == '\n')
display.print((char) 176); // Long Term TODO: Add newlines that actually work (The character displayed is '░')
// If not then the character is printed
else
display.print(currentCharacter);
}
}
/*--THE FILE VIEW FUNCTIONS--*/
/*
As of 26/08/2022 12:32pm, the coding for my major project is complete!
There are no bugs that I have been able to encounter with this current codebase
The only future thing I could add is proper newline support, as right now newlines are replaced with '░' due to it breaking my text system
*/