Skip to content

Commit 7a143f2

Browse files
author
Adafruit Adabot
committed
startin' arcadify - filesystem and imagereader moved
1 parent e77f906 commit 7a143f2

3 files changed

Lines changed: 33 additions & 126 deletions

File tree

M4_Eyes/M4_Eyes.ino

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636

3737
#define GLOBAL_VAR
3838
#include "globals.h"
39-
extern Adafruit_ImageReader reader;
39+
40+
Adafruit_Arcada arcada;
4041

4142
// Global eye state that applies to all eyes (not per-eye):
4243
bool eyeInMotion = false;
@@ -133,13 +134,17 @@ void fatal(const char *message, uint16_t blinkDelay) {
133134
// SETUP FUNCTION - CALLED ONCE AT PROGRAM START ---------------------------
134135

135136
void setup() {
136-
pinMode(LED_BUILTIN, OUTPUT);
137-
digitalWrite(LED_BUILTIN, LOW);
138-
137+
if (!arcada.arcadaBegin()) {
138+
while (1);
139+
}
139140
// MUST set up flash filesystem before Serial.begin() or seesaw.begin()
140-
int i = file_setup();
141+
arcada.filesysBeginMSD();
142+
143+
// if(i == 1) fatal("Flash init fail", 100);
144+
//else if(i == 2) fatal("No filesys", 500);
141145

142-
Serial.begin(9600);
146+
147+
Serial.begin(115200);
143148
//while(!Serial) delay(10);
144149

145150
Serial.printf("Available RAM at start: %d\n", availableRAM());
@@ -157,8 +162,6 @@ void setup() {
157162
priorButtonState = seesaw.digitalReadBulk(0b111000000000);
158163
#endif
159164

160-
if(i == 1) fatal("Flash init fail", 100);
161-
else if(i == 2) fatal("No filesys", 500);
162165

163166
#if NUM_EYES > 1
164167
seesaw.pinMode(SEESAW_TFT_RESET_PIN, OUTPUT);
@@ -181,12 +184,12 @@ void setup() {
181184
}
182185

183186
yield();
184-
if (reader.drawBMP("/splash.bmp", *(eye[0].display), 0, 0) == IMAGE_SUCCESS) {
187+
if (arcada.drawBMP("/splash.bmp", 0, 0, (eye[0].display)) == IMAGE_SUCCESS) {
185188
Serial.println("Splashing");
186189
#if NUM_EYES > 1
187190
// other eye
188191
yield();
189-
reader.drawBMP("/splash.bmp", *(eye[1].display), 0, 0);
192+
arcada.drawBMP("/splash.bmp", 0, 0, (eye[1].display));
190193
#endif
191194
// backlight on for a bit
192195
for (int bl=0; bl<=250; bl+=10) {
@@ -426,7 +429,7 @@ void setup() {
426429
yield();
427430
if(boopPin >= 0) {
428431
boopThreshold = 0;
429-
for(i=0; i<240; i++) {
432+
for(int i=0; i<240; i++) {
430433
boopThreshold += readBoop();
431434
}
432435
boopThreshold = boopThreshold * 110 / 100; // 10% overhead

M4_Eyes/file.cpp

Lines changed: 18 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,9 @@
11
//34567890123456789012345678901234567890123456789012345678901234567890123456
22

3-
#include <SdFat.h> // SD card & FAT filesystem library
4-
#include <Adafruit_SPIFlash.h> // SPI / QSPI flash library
53
#include <ArduinoJson.h> // JSON config file functions
6-
#include "Adafruit_TinyUSB.h"
74
#include "globals.h"
85

9-
#if defined(__SAMD51__) || defined(NRF52840_XXAA)
10-
Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS,
11-
PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
12-
#else
13-
#if (SPI_INTERFACES_COUNT == 1)
14-
Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
15-
#else
16-
Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
17-
#endif
18-
#endif
19-
Adafruit_SPIFlash flash(&flashTransport);
20-
FatFileSystem filesys;
21-
Adafruit_USBD_MSC usb_msc; // USB Mass Storage object
22-
Adafruit_ImageReader reader(filesys); // Image-reader for flash filesys
23-
24-
// MASS STORAGE SETUP ------------------------------------------------------
25-
26-
// Callback invoked when READ10 command received.
27-
// Copy disk's data to buffer (up to bufsize) and
28-
// return number of copied bytes (must be multiple of block size)
29-
static int32_t msc_read_cb(uint32_t lba, void* buffer, uint32_t bufsize) {
30-
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
31-
// already include 4K sector caching internally, no need to cache here.
32-
return flash.readBlocks(lba, (uint8_t*) buffer, bufsize / 512) ? bufsize : -1;
33-
}
34-
35-
// Callback invoked when WRITE10 command received.
36-
// Process data in buffer to disk's storage and
37-
// return number of written bytes (must be multiple of block size)
38-
static int32_t msc_write_cb(uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
39-
digitalWrite(LED_BUILTIN, HIGH);
40-
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
41-
// already include 4K sector caching internally, no need to cache here.
42-
return flash.writeBlocks(lba, buffer, bufsize / 512) ? bufsize : -1;
43-
}
44-
45-
// Callback invoked when WRITE10 command is completed (status received
46-
// and accepted by host). Used to flush any pending cache.
47-
static void msc_flush_cb(void) {
48-
flash.syncBlocks(); // sync with flash
49-
filesys.cacheClear(); // clear file system's cache to force refresh
50-
filesystem_change_flag = true;
51-
digitalWrite(LED_BUILTIN, LOW);
52-
}
53-
54-
// Mass storage setup. This MUST be called before Serial.begin()!
55-
// Returns 0 on success. 1 = flash init failure, 2 = filesys init failure
56-
int file_setup(bool msc) {
57-
if(flash.begin()) {
58-
if(msc) { // Enable mass storage handler? (flash visible to host computer)
59-
// Set disk vendor id, product id and revision --
60-
// up to 8, 16, 4 characters respectively
61-
usb_msc.setID("Adafruit", "External Flash", "1.0");
62-
63-
// Set callbacks
64-
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
65-
66-
// Set disk size, block size should be 512 regardless of SPI flash page size
67-
usb_msc.setCapacity(flash.pageSize() * flash.numPages() / 512, 512);
68-
69-
usb_msc.setUnitReady(true); // MSC is ready for read/write
70-
usb_msc.begin();
71-
}
72-
73-
if(filesys.begin(&flash)) return 0; // Success
74-
return 2; // Filesys init failure
75-
}
76-
return 1; // Flash init failure
77-
}
78-
79-
// MASS STORAGE CHANGE HANDLER ---------------------------------------------
80-
81-
// Not sure -- should this just do a major reboot?
82-
// (want to turn off initial filesystem_change_flag = true in that case)
83-
84-
void handle_filesystem_change() {
85-
filesystem_change_flag = false;
86-
87-
FatFile root;
88-
FatFile file;
89-
90-
if(!root.open("/")) {
91-
Serial.println("open root failed");
92-
return;
93-
}
94-
95-
Serial.println("Flash contents:");
96-
97-
// Open next file in root.
98-
// Warning, openNext starts at the current directory position
99-
// so a rewind of the directory may be required.
100-
while(file.openNext(&root, O_RDONLY)) {
101-
file.printFileSize(&Serial);
102-
Serial.write(' ');
103-
file.printName(&Serial);
104-
if(file.isDir()) {
105-
// Indicate a directory.
106-
Serial.write('/');
107-
}
108-
Serial.println();
109-
file.close();
110-
}
111-
112-
root.close();
113-
114-
Serial.println();
115-
}
6+
extern Adafruit_Arcada arcada;
1167

1178
// CONFIGURATION FILE HANDLING ---------------------------------------------
1189

@@ -187,7 +78,7 @@ void loadConfig(char *filename) {
18778
File file;
18879
uint8_t rotation = 3;
18980

190-
if(file = filesys.open(filename, FILE_READ)) {
81+
if(file = arcada.open(filename, FILE_READ)) {
19182
StaticJsonDocument<2048> doc;
19283

19384
yield();
@@ -431,12 +322,18 @@ ImageReturnCode loadEyelid(char *filename,
431322
uint32_t tempBytes;
432323
uint8_t *tempPtr = NULL;
433324
ImageReturnCode status;
325+
Adafruit_ImageReader *reader;
326+
327+
reader = arcada.getImageReader();
328+
if (!reader) {
329+
return IMAGE_ERR_FILE_NOT_FOUND;
330+
}
434331

435332
memset(minArray, init, 240); // Fill eyelid arrays with init value to
436333
memset(maxArray, init, 240); // mark 'no eyelid data for this column'
437334

438335
// This is the "booster seat" described in m4eyes.ino
439-
if(reader.bmpDimensions(filename, &w, &h) == IMAGE_SUCCESS) {
336+
if(reader->bmpDimensions(filename, &w, &h) == IMAGE_SUCCESS) {
440337
tempBytes = ((w + 7) / 8) * h; // Bitmap size in bytes
441338
if(tempPtr = (uint8_t *)malloc(maxRam - tempBytes)) {
442339
// Make SOME tempPtr reference, or optimizer removes the alloc!
@@ -448,7 +345,7 @@ ImageReturnCode loadEyelid(char *filename,
448345
}
449346

450347
yield();
451-
if((status = reader.loadBMP(filename, image)) == IMAGE_SUCCESS) {
348+
if((status = reader->loadBMP(filename, image)) == IMAGE_SUCCESS) {
452349
if(image.getFormat() == IMAGE_1) { // MUST be 1-bit image
453350
uint16_t *palette = image.getPalette();
454351
uint8_t white = (!palette || (palette[1] > palette[0]));
@@ -509,9 +406,15 @@ ImageReturnCode loadTexture(char *filename, uint16_t **data,
509406
uint32_t tempBytes;
510407
uint8_t *tempPtr = NULL;
511408
ImageReturnCode status;
409+
Adafruit_ImageReader *reader;
512410

411+
reader = arcada.getImageReader();
412+
if (!reader) {
413+
return IMAGE_ERR_FILE_NOT_FOUND;
414+
}
415+
513416
// This is the "booster seat" described in m4eyes.ino
514-
if(reader.bmpDimensions(filename, &w, &h) == IMAGE_SUCCESS) {
417+
if(reader->bmpDimensions(filename, &w, &h) == IMAGE_SUCCESS) {
515418
tempBytes = w * h * 2; // Image size in bytes (converted to 16bpp)
516419
if(tempPtr = (uint8_t *)malloc(maxRam - tempBytes)) {
517420
// Make SOME tempPtr reference, or optimizer removes the alloc!
@@ -523,7 +426,7 @@ ImageReturnCode loadTexture(char *filename, uint16_t **data,
523426
}
524427

525428
yield();
526-
if((status = reader.loadBMP(filename, image)) == IMAGE_SUCCESS) {
429+
if((status = reader->loadBMP(filename, image)) == IMAGE_SUCCESS) {
527430
if(image.getFormat() == IMAGE_16) { // MUST be 16-bit image
528431
Serial.println("Texture loaded!");
529432
GFXcanvas16 *canvas = (GFXcanvas16 *)image.getCanvas();

M4_Eyes/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <Adafruit_ST7789.h> // TFT-specific display library
55
#include <Adafruit_ZeroDMA.h> // SAMD-specific DMA library
66
#include <Adafruit_ImageReader.h> // ImageReturnCode type
7+
#include "Adafruit_Arcada.h"
78
#include "DMAbuddy.h" // DMA-bug-workaround class
89

910
#if defined(GLOBAL_VAR) // #defined in .ino file ONLY!

0 commit comments

Comments
 (0)