Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdBrowserMenu + spiffsBrowserMenu -> fileBrowserMenu #14

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 60 additions & 71 deletions firmware/keira/src/apps/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,29 @@ void LauncherApp::run() {
menu.addItem("Налаштування", &settings, lilka::display.color565(255, 200, 224));

while (1) {
// NOTE: most likely we'll have a needness in inserting here
// taskYIELD() to prevent some problems...
while (!menu.isFinished()) {
menu.update();
menu.draw(canvas);
queueDraw();
}
queueDraw();
int16_t index = menu.getCursor();
if (index == 0) {
appsMenu();
} else if (index == 1) {
sdBrowserMenu("/");
} else if (index == 2) {
spiffsBrowserMenu();
} else if (index == 3) {
// dev_menu();
devMenu();
} else if (index == 4) {
settingsMenu();
if (index != -1) {
if (index == 0) {
appsMenu();
} else if (index == 1) {
fileBrowserMenu("/", FB_SD);
} else if (index == 2) {
fileBrowserMenu("/", FB_SPIFFS);
} else if (index == 3) {
// dev_menu();
devMenu();
} else if (index == 4) {
settingsMenu();
}
}
taskYIELD();
}
}

Expand Down Expand Up @@ -127,20 +132,39 @@ const uint16_t get_file_color(const String& filename) {
return lilka::display.color565(200, 200, 200);
}
}

void LauncherApp::sdBrowserMenu(String path) {
if (!lilka::sdcard.available()) {
alert("Помилка", "SD-карта не знайдена");
}
size_t _numEntries = lilka::sdcard.getEntryCount(path);
if (_numEntries == 0) {
alert("Помилка", "Директорія пуста або сталася помилка читання директорії");
void LauncherApp::fileBrowserMenu(String path, int fbrowserType) {
size_t _numEntries = 0;
if (fbrowserType == FB_SD) {
if (!lilka::sdcard.available()) {
alert("Помилка", "SD-карта не знайдена");
return;
}
_numEntries = lilka::sdcard.getEntryCount(path);
if (_numEntries == 0) {
alert("Помилка", "Директорія пуста або сталася помилка читання директорії");
return;
}
} else if (fbrowserType == FB_SPIFFS) {
if (!lilka::filesystem.available()) {
alert("Помилка", "SPIFFS не підтримується");
return;
}
_numEntries = lilka::filesystem.getEntryCount(path);
if (_numEntries == 0) {
alert("Помилка", "Директорія пуста або сталася помилка читання директорії");
return;
}
} else {
alert("Помилка", "Цей тип носія не підтримується");
return;
}

lilka::Entry* entries = new lilka::Entry[_numEntries];
int numEntries = -1;

if (fbrowserType == FB_SD) numEntries = lilka::sdcard.listDir(path, entries);
else numEntries = lilka::filesystem.listDir(path, entries);

int numEntries = lilka::sdcard.listDir(path, entries);
std::unique_ptr<lilka::Entry[]> entriesPtr(entries);

// Так як listDir має повертати -1 в разі помилки
Expand All @@ -150,7 +174,12 @@ void LauncherApp::sdBrowserMenu(String path) {
return;
}

lilka::Menu menu("SD: " + path);
String menuLabel;
if (fbrowserType == FB_SD) menuLabel = "SD: " + path;
else menuLabel = "SPIFFS: " + path;

lilka::Menu menu(menuLabel);

for (int i = 0; i < numEntries; i++) {
String filename = entries[i].name;
const menu_icon_t* icon =
Expand All @@ -168,56 +197,16 @@ void LauncherApp::sdBrowserMenu(String path) {
queueDraw();
}
int16_t index = menu.getCursor();
if (index == numEntries) break;
if (entries[index].type == lilka::EntryType::ENT_DIRECTORY) {
sdBrowserMenu(path + entries[index].name + "/");
} else {
selectFile(lilka::sdcard.abspath(path + entries[index].name));
}
}

return;
}

void LauncherApp::spiffsBrowserMenu() {
if (!lilka::filesystem.available()) {
alert("Помилка", "SPIFFS не підтримується");
return;
}

String filenames
[32]; // TODO - allocate dynamically (increasing to 64 causes task stack overflow which is limited by ARDUINO_LOOP_STACK_SIZE)
int numEntries = lilka::filesystem.readdir(filenames);

if (numEntries == -1) {
alert("Помилка", "Не вдалося прочитати корінь SPIFFS");
return;
}
const menu_icon_t* icons[32];
uint16_t colors[32];
for (int i = 0; i < numEntries; i++) {
icons[i] = get_file_icon(filenames[i]);
colors[i] = get_file_color(filenames[i]);
}
filenames[numEntries++] = "<< Назад";
icons[numEntries - 1] = 0;
colors[numEntries - 1] = 0;

lilka::Menu menu("SPIFFS");
for (int i = 0; i < numEntries; i++) {
menu.addItem(filenames[i], icons[i], colors[i]);
}
while (1) {
while (!menu.isFinished()) {
menu.update();
menu.draw(canvas);
queueDraw();
}
int16_t index = menu.getCursor();
if (index == numEntries - 1) {
return;
if (index != -1) {
if (index >= numEntries - 1) break;
if (entries[index].type == lilka::EntryType::ENT_DIRECTORY) {
fileBrowserMenu(path + entries[index].name + "/", fbrowserType);
} else {
if (fbrowserType == FB_SD) selectFile(lilka::sdcard.abspath(path + entries[index].name));
else selectFile(lilka::filesystem.abspath(path + entries[index].name));
}
}
selectFile(lilka::filesystem.abspath(filenames[index]));
taskYIELD();
}
}

Expand Down
7 changes: 5 additions & 2 deletions firmware/keira/src/apps/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ typedef struct {
{ \
name, []() { return new className(); } \
}
typedef enum {
FB_SD,
FB_SPIFFS,
} FileBrowserType;

class LauncherApp : public App {
public:
Expand All @@ -20,8 +24,7 @@ class LauncherApp : public App {
private:
void run() override;
void appsMenu();
void sdBrowserMenu(String path);
void spiffsBrowserMenu();
void fileBrowserMenu(String path, int fbrowserType);
void devMenu();
void settingsMenu();

Expand Down
100 changes: 65 additions & 35 deletions sdk/lib/lilka/src/lilka/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace lilka {

Filesystem::Filesystem() : _available(false), _filesystem(NULL) {
Filesystem::Filesystem() : _available(false), fs(NULL) {
}

void Filesystem::begin() {
Expand All @@ -18,56 +18,86 @@ void Filesystem::begin() {
serial_log("SPIFFS ok");
_available = true;
}
_filesystem = &SPIFFS;
fs = &SPIFFS;
}

bool Filesystem::available() {
return _available;
}

int Filesystem::readdir(String filenames[]) {
File _root = _filesystem->open("/");
int count = 0;
File file = _root.openNextFile();
int Filesystem::listDir(String path, Entry entries[]) {
while (!path.equals("/") && path.endsWith("/")) {
// Strip trailing slashes, unless it's the root directory
path.remove(path.length() - 1);
}
File root = fs->open(path);
if (!root) {
serial_err("listDir: failed to open directory: %s", path.c_str());
return -1;
}
if (!root.isDirectory()) {
serial_err("listDir: not a directory: %s", path.c_str());
return -1;
}

int i = 0;
File file = root.openNextFile();
while (file) {
entries[i].name = file.name();
if (file.isDirectory()) {
file.close();
file = _root.openNextFile();
continue;
entries[i].type = ENT_DIRECTORY;
} else {
entries[i].type = ENT_FILE;
}
filenames[count++] = file.name();
entries[i].size = file.size();
file.close();
file = _root.openNextFile();
file = root.openNextFile();
i++;
}
_root.close();
// Sort filenames
qsort(filenames, count, sizeof(String), [](const void* a, const void* b) -> int {
const String* ea = static_cast<const String*>(a);
const String* eb = static_cast<const String*>(b);
return ea->compareTo(*eb);
root.close();
// Sort filenames, but keep directories first
qsort(entries, i, sizeof(Entry), [](const void* a, const void* b) -> int {
const Entry* ea = static_cast<const Entry*>(a);
const Entry* eb = static_cast<const Entry*>(b);
if (ea->type == ENT_DIRECTORY && eb->type != ENT_DIRECTORY) {
return -1;
} else if (ea->type != ENT_DIRECTORY && eb->type == ENT_DIRECTORY) {
return 1;
}
return ea->name.compareTo(eb->name);
});
return count;
return i;
}

int Filesystem::readdir(String filenames[], String extension) {
File _root = _filesystem->open("/");
int count = 0;
File file = _root.openNextFile();
size_t Filesystem::getEntryCount(String path) {
size_t countFiles = 0;

while (!path.equals("/") && path.endsWith("/")) {
// Strip trailing slashes, unless it's the root directory
path.remove(path.length() - 1);
}
File root = fs->open(path);
// Below we assume if folder can't be open then it has zero files
// Btw we will show this error using serial
if (!root) {
serial_err("%s:%d:getEntryCount: failed to open directory: %s", __FILE__, __LINE__, path.c_str());
return 0;
}
if (!root.isDirectory()) {
serial_err("%s:%d:getEntryCount: not a directory: %s", __FILE__, __LINE__, path.c_str());
return 0;
}

File file = root.openNextFile();

while (file) {
if (file.isDirectory()) {
file.close();
file = _root.openNextFile();
continue;
}
String name = file.name();
if (name.endsWith(extension)) {
filenames[count++] = name;
}
file.close();
file = _root.openNextFile();
file = root.openNextFile();
countFiles++;
}
_root.close();
return count;

root.close();

return countFiles;
}

String Filesystem::abspath(String filename) {
Expand Down
10 changes: 5 additions & 5 deletions sdk/lib/lilka/src/lilka/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
#define LILKA_FILESYSTEM_H

#include <SPIFFS.h>

#include "fsdefs.h"
namespace lilka {

class Filesystem {
public:
Filesystem();
void begin();
bool available();
int readdir(String filenames[]);
int readdir(String filenames[], String extension);

int listDir(String path, Entry entries[]);
size_t getEntryCount(String path);
String abspath(String filename);

private:
FS* _filesystem;
FS* fs;
bool _available;
};

Expand Down
18 changes: 18 additions & 0 deletions sdk/lib/lilka/src/lilka/fsdefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
/*
This file should be a place to store common for all filesystems
parts
*/
// definitions required for SPIFFS and SDCard Browsers
namespace lilka {
typedef enum {
ENT_FILE,
ENT_DIRECTORY,
} EntryType;

typedef struct {
String name;
EntryType type;
size_t size;
} Entry;
} // namespace lilka
Loading
Loading