Skip to content

Commit

Permalink
Restore original lualilka_sdcard, few fixes of fileutils
Browse files Browse the repository at this point in the history
  • Loading branch information
frostmorn committed Mar 30, 2024
1 parent 1972b99 commit ae845e2
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 39 deletions.
2 changes: 1 addition & 1 deletion firmware/doom/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void setup() {
name.toLowerCase();
lilka::serial_log("Checking file: %s\n", name.c_str());
if (name.startsWith("doom") && name.endsWith(".wad")) {
strcpy(arg3, (String("/sd") + firmwareDir + "/" + file.name()).c_str());
strcpy(arg3, (lilka::fileutils.getSDRoot() + firmwareDir + "/" + file.name()).c_str());
lilka::serial_log("Found .WAD file: %s\n", arg3);
found = true;
file.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local file_dir = fileutils.ls("/sd")
local file_dir = sdcard.ls("/")

print(#file_dir)

for i = 0, #file_dir do
print(file_dir[i])
end

file = file("/sd/test.txt", "a+")
file = file("/test.txt", "a+")

file:write("HELLOWORLD")

Expand Down
4 changes: 3 additions & 1 deletion firmware/keira/src/apps/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ void LauncherApp::sdBrowserMenu(FS* fSysDriver, const String& path) {
void LauncherApp::selectFile(String path) {
String lowerCasedPath = path;
lowerCasedPath.toLowerCase();
//lilka::serial_log("FileBrowser : Selected path %s", path.c_str());
if (lowerCasedPath.endsWith(".rom") || lowerCasedPath.endsWith(".nes")) {
AppManager::getInstance()->runApp(new NesApp(path));
} else if (lowerCasedPath.endsWith(".bin")) {
Expand Down Expand Up @@ -382,7 +383,8 @@ void LauncherApp::settingsMenu() {
const uint32_t workSize = FF_MAX_SS * 4;
void* work = ps_malloc(workSize
); // Buffer (4 sectors), otherwise f_mkfs tries to allocate in stack and fails due to task stack size
FRESULT result = f_mkfs("/sd", FM_ANY, 0, work, workSize); // TODO - hardcoded mountpoint
FRESULT result =
f_mkfs(lilka::fileutils.getSDRoot().c_str(), FM_ANY, 0, work, workSize); // TODO - hardcoded mountpoint
free(work);
if (result != FR_OK) {
this->alert("Помилка", "Не вдалося сформатувати SD-карту, код помилки: " + String(result));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "lualilka_fileutils.h"
#include "lualilka_sdcard.h"
#include "lilka.h"

static int lualilka_create_object_file(lua_State* L) {
String path = luaL_checkstring(L, 1);
String mode = luaL_checkstring(L, 2);
*reinterpret_cast<FILE**>(lua_newuserdata(L, sizeof(FILE*))) = fopen((path).c_str(), mode.c_str());
*reinterpret_cast<FILE**>(lua_newuserdata(L, sizeof(FILE*))) =
fopen((lilka::fileutils.getSDRoot() + path).c_str(), mode.c_str());
luaL_setmetatable(L, FILE_OBJECT);
return 1;
}
Expand Down Expand Up @@ -72,27 +73,27 @@ static int lualilka_file_write(lua_State* L) {
return luaL_error(L, "write error");
}

int lualilka_list_dir(lua_State* L) {
int lualilka_sdcard_list_dir(lua_State* L) {
int n = lua_gettop(L);

if (n != 1) {
return luaL_error(L, "Очікується 1 аргумент, отримано %d", n);
}

if (!lilka::fileutils.isSDAvailable()) {
return luaL_error(L, "SD card not found");
}

String path = lua_tostring(L, 1);

size_t _numEntries = lilka::fileutils.getEntryCount(
lilka::fileutils.getFSysDriverByFullPath(path), lilka::fileutils.getRelativePath(path)
);
size_t _numEntries = lilka::fileutils.getEntryCount(&SD, path);
if (_numEntries == 0) {
return luaL_error(L, "Директорія порожня, або сталася помилка читання директорії");
}

lilka::Entry* entries = new lilka::Entry[_numEntries];

int numEntries = lilka::fileutils.listDir(
lilka::fileutils.getFSysDriverByFullPath(path), lilka::fileutils.getRelativePath(path), entries
);
int numEntries = lilka::fileutils.listDir(&SD, path, entries);
std::unique_ptr<lilka::Entry[]> entriesPtr(entries);

if (_numEntries != numEntries) {
Expand All @@ -109,16 +110,20 @@ int lualilka_list_dir(lua_State* L) {
return 1;
}

int lualilka_remove(lua_State* L) {
int lualilka_sdcard_remove(lua_State* L) {
int n = lua_gettop(L);

if (n != 1) {
return luaL_error(L, "Очікується 1 аргумент, отримано %d", n);
}

if (!lilka::fileutils.isSDAvailable()) {
return luaL_error(L, "SD card not found");
}

String path = lua_tostring(L, 1);

int ret = remove(path.c_str());
int ret = remove((lilka::fileutils.getSDRoot() + path).c_str());

if (ret != 0) {
return luaL_error(L, "Error remove file: %d", ret);
Expand All @@ -127,17 +132,22 @@ int lualilka_remove(lua_State* L) {
return 0;
}

int lualilka_rename(lua_State* L) {
int lualilka_sdcard_rename(lua_State* L) {
int n = lua_gettop(L);

if (n != 2) {
return luaL_error(L, "Очікується 1 аргумент, отримано %d", n);
}

if (!lilka::fileutils.isSDAvailable()) {
return luaL_error(L, "SD card not found");
}

String old_name = lua_tostring(L, 1);
String new_name = lua_tostring(L, 2);

int ret = rename((old_name).c_str(), (new_name).c_str());
int ret =
rename((lilka::fileutils.getSDRoot() + old_name).c_str(), (lilka::fileutils.getSDRoot() + new_name).c_str());

if (ret != 0) {
return luaL_error(L, "Error renaming file: %d", ret);
Expand All @@ -147,13 +157,13 @@ int lualilka_rename(lua_State* L) {
}

static const luaL_Reg lualilka_sdcard[] = {
{"ls", lualilka_list_dir},
{"remove", lualilka_remove},
{"rename", lualilka_rename},
{"ls", lualilka_sdcard_list_dir},
{"remove", lualilka_sdcard_remove},
{"rename", lualilka_sdcard_rename},
{NULL, NULL},
};

int lualilka_fileutils_register(lua_State* L) {
int lualilka_sdcard_register(lua_State* L) {
lua_register(L, FILE_OBJECT, lualilka_create_object_file);
luaL_newmetatable(L, FILE_OBJECT);
lua_pushcfunction(L, lualilka_delete_object_file);
Expand All @@ -173,7 +183,7 @@ int lualilka_fileutils_register(lua_State* L) {
lua_pop(L, 1);

luaL_newlib(L, lualilka_sdcard);
lua_setglobal(L, "fileutils");
lua_setglobal(L, "sdcard");

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <lua.hpp>
#include <Arduino.h>

#include <SD.h>
#define FILE_OBJECT "file"

int lualilka_fileutils_register(lua_State* L);
int lualilka_sdcard_register(lua_State* L);
12 changes: 7 additions & 5 deletions firmware/keira/src/apps/lua/luarunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "lualilka_util.h"
#include "lualilka_buzzer.h"
#include "lualilka_state.h"
#include "lualilka_fileutils.h"
#include "lualilka_sdcard.h"
#include "lualilka_wifi.h"
#include "lualilka_imageTransform.h"

Expand Down Expand Up @@ -158,7 +158,7 @@ void AbstractLuaRunnerApp::luaSetup(const char* dir) {
lualilka_gpio_register(L);
lualilka_util_register(L);
lualilka_buzzer_register(L);
lualilka_fileutils_register(L);
lualilka_sdcard_register(L);
lualilka_wifi_register(L);
lualilka_imageTransform_register(L);

Expand Down Expand Up @@ -285,7 +285,7 @@ LuaFileRunnerApp::LuaFileRunnerApp(String path) : AbstractLuaRunnerApp("Lua file
void LuaFileRunnerApp::run() {
#ifndef LILKA_NO_LUA
// Get dir name from path (without the trailing slash)
String dir = path.substring(0, path.lastIndexOf('/'));
String dir = lilka::fileutils.stripPath(path);

luaSetup(dir.c_str());

Expand Down Expand Up @@ -408,6 +408,7 @@ void LuaLiveRunnerApp::run() {
}

// TODO: This is a temporary fix: https://github.com/espressif/arduino-esp32/issues/9221
lilka::fileutils.isSDAvailable();

execSource(code);

Expand All @@ -423,7 +424,7 @@ void LuaLiveRunnerApp::run() {

void LuaLiveRunnerApp::execSource(String source) {
#ifndef LILKA_NO_LUA
luaSetup("/sd"); // TODO: hard-coded
luaSetup(lilka::fileutils.getSDRoot().c_str());

lilka::serial_log("lua: run source");

Expand All @@ -449,7 +450,7 @@ LuaReplApp::LuaReplApp() : AbstractLuaRunnerApp("Lua REPL") {

void LuaReplApp::run() {
#ifndef LILKA_NO_LUA
luaSetup("/sd"); // TODO: hard-coded
luaSetup(lilka::fileutils.getSDRoot().c_str()); // TODO: hard-coded

canvas->setFont(FONT_10x20);
canvas->setCursor(8, 48);
Expand All @@ -462,6 +463,7 @@ void LuaReplApp::run() {
lilka::serial_log("lua: start REPL");

// TODO: This is a temporary fix: https://github.com/espressif/arduino-esp32/issues/9221
lilka::fileutils.initSD();

bool quit = false;
while (!quit) {
Expand Down
12 changes: 6 additions & 6 deletions sdk/lib/lilka/src/lilka/fileutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ size_t FileUtils::listDir(FS* fSysDriver, const String& path, Entry entries[]) {

FS* FileUtils::getFSysDriverByFullPath(const String& path) {
FS* fs = NULL;
if (path.startsWith(LILKA_SDROOT)) {
if (path.startsWith(LILKA_SDROOT LILKA_SLASH)) {
fs = static_cast<FS*>(sdfs);
} else if (path.startsWith(LILKA_SPIFFS_ROOT)) {
} else if (path.startsWith(LILKA_SPIFFS_ROOT LILKA_SLASH)) {
fs = static_cast<FS*>(spiffs);
}
return fs;
}
const String FileUtils::getFullPath(const FS* fSysDriver, const String& path) {
// Allready okay
if (path.startsWith(LILKA_SDROOT) || path.startsWith(LILKA_SPIFFS_ROOT)) return path;
if (path.startsWith(LILKA_SDROOT LILKA_SLASH) || path.startsWith(LILKA_SPIFFS_ROOT LILKA_SLASH)) return path;

if (fSysDriver == sdfs) {
return String(LILKA_SDROOT) + path;
Expand All @@ -172,9 +172,9 @@ const String FileUtils::getFullPath(const FS* fSysDriver, const String& path) {

const String FileUtils::getRelativePath(const String& path) {
String relativePath;
if (path.startsWith(LILKA_SDROOT)) {
if (path.startsWith(LILKA_SDROOT LILKA_SLASH)) {
relativePath = "/" + path.substring(LILKA_SDROOT_LEN);
} else if (path.startsWith(LILKA_SPIFFS_ROOT)) {
} else if (path.startsWith(LILKA_SPIFFS_ROOT LILKA_SLASH)) {
relativePath = "/" + path.substring(LILKA_SPIFFS_ROOT_LEN);
} else
// Maybe path is allready relative?
Expand All @@ -183,7 +183,7 @@ const String FileUtils::getRelativePath(const String& path) {
}

bool FileUtils::isSDAvailable() {
return (sdfs->cardType() == CARD_NONE || sdfs->cardType() == CARD_UNKNOWN);
return !(sdfs->cardType() == CARD_NONE || sdfs->cardType() == CARD_UNKNOWN);
}

bool FileUtils::isSPIFFSAvailable() {
Expand Down
1 change: 1 addition & 0 deletions sdk/lib/lilka/src/lilka/fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "config.h"
#define LILKA_SDROOT "/sd"
#define LILKA_SPIFFS_ROOT "/fs"
#define LILKA_SLASH "/"
#define LILKA_SDROOT_LEN 3
#define LILKA_SPIFFS_ROOT_LEN 3
#define H_FILE_SIZE 6
Expand Down
5 changes: 2 additions & 3 deletions sdk/lib/lilka/src/lilka/multiboot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ int MultiBoot::start(String path) {
// Remove "/sd" prefix
// TODO: Maybe we should use absolute path (including "/sd")?
// TODO: Store arg in RAM?
if (arg.startsWith("/sd/")) {
arg = arg.substring(3);
}
arg = lilka::fileutils.getRelativePath(arg);

prefs.putString(MULTIBOOT_PATH_KEY, arg);
prefs.end();

Expand Down

0 comments on commit ae845e2

Please sign in to comment.