Fix SD card integration and Lua game loading
- Fix SD card MISO pin (was using display MISO instead of SD MISO) - Add FatFS mounting after SD card initialization - Restore SPI baudrate on all SD init failure paths - Add case-insensitive .lua file extension check - Filter out hidden 8.3 filename entries - Add SPI speed management functions for shared SPI bus - Wrap all FatFS operations with SPI speed switching - Restore display SPI speed (32 MHz) after SD operations - Add debug output to Lua game loader This fixes slow display refresh when SD card is present and enables reliable Lua game loading from SD card /games directory.
This commit is contained in:
BIN
games/.DS_Store
vendored
Normal file
BIN
games/.DS_Store
vendored
Normal file
Binary file not shown.
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "lua_game.h"
|
||||
#include "lua_bindings.h"
|
||||
#include "sd_card.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -54,9 +55,13 @@ bool LuaGame::load_script() {
|
||||
FIL fil;
|
||||
FRESULT fr;
|
||||
|
||||
// Set SPI speed for SD card operations
|
||||
uint prev_speed = sd_card_set_spi_speed();
|
||||
|
||||
// Open Lua script from SD card
|
||||
fr = f_open(&fil, script_path.c_str(), FA_READ);
|
||||
if (fr != FR_OK) {
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
error_message = "Failed to open file (FatFS error: ";
|
||||
error_message += std::to_string((int)fr);
|
||||
error_message += ")";
|
||||
@@ -67,6 +72,7 @@ bool LuaGame::load_script() {
|
||||
FSIZE_t file_size = f_size(&fil);
|
||||
if (file_size == 0 || file_size > 64 * 1024) { // Limit to 64KB
|
||||
f_close(&fil);
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
error_message = "Script file size invalid (0 or > 64KB)";
|
||||
return false;
|
||||
}
|
||||
@@ -75,6 +81,7 @@ bool LuaGame::load_script() {
|
||||
char* script_buffer = new char[file_size + 1];
|
||||
if (!script_buffer) {
|
||||
f_close(&fil);
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
error_message = "Failed to allocate memory for script";
|
||||
return false;
|
||||
}
|
||||
@@ -84,6 +91,9 @@ bool LuaGame::load_script() {
|
||||
fr = f_read(&fil, script_buffer, file_size, &bytes_read);
|
||||
f_close(&fil);
|
||||
|
||||
// Restore SPI speed immediately after file operations
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
|
||||
if (fr != FR_OK || bytes_read != file_size) {
|
||||
delete[] script_buffer;
|
||||
error_message = "Failed to read script file";
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "lua_game_loader.h"
|
||||
#include "lua_game.h"
|
||||
#include "sd_card.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
@@ -51,10 +52,14 @@ bool LuaGameLoader::parse_metadata(const char* script_path, char* name, char* de
|
||||
// Default empty description
|
||||
description[0] = '\0';
|
||||
|
||||
// Set SPI speed for SD card operations
|
||||
uint prev_speed = sd_card_set_spi_speed();
|
||||
|
||||
// Try to open file and parse metadata comments
|
||||
fr = f_open(&fil, script_path, FA_READ);
|
||||
if (fr != FR_OK) {
|
||||
printf("LuaGameLoader: Warning - could not open %s for metadata\n", script_path);
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -64,6 +69,9 @@ bool LuaGameLoader::parse_metadata(const char* script_path, char* name, char* de
|
||||
fr = f_read(&fil, buffer, sizeof(buffer) - 1, &bytes_read);
|
||||
f_close(&fil);
|
||||
|
||||
// Restore SPI speed
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
|
||||
if (fr != FR_OK) {
|
||||
return false;
|
||||
}
|
||||
@@ -109,25 +117,60 @@ int LuaGameLoader::register_all_games(GameLauncher* launcher) {
|
||||
|
||||
printf("LuaGameLoader: Scanning /games directory for .lua scripts...\n");
|
||||
|
||||
// Set SPI speed for SD card operations
|
||||
uint prev_speed = sd_card_set_spi_speed();
|
||||
|
||||
// Open /games directory
|
||||
fr = f_opendir(&dir, "/games");
|
||||
if (fr != FR_OK) {
|
||||
printf("LuaGameLoader: Could not open /games directory (error %d)\n", fr);
|
||||
printf("LuaGameLoader: Make sure SD card is mounted and /games exists\n");
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Scan for .lua files
|
||||
while (true) {
|
||||
fr = f_readdir(&dir, &fno);
|
||||
if (fr != FR_OK || fno.fname[0] == 0) break; // End of directory
|
||||
if (fr != FR_OK) {
|
||||
printf("LuaGameLoader: Error reading directory (error %d)\n", fr);
|
||||
break;
|
||||
}
|
||||
if (fno.fname[0] == 0) {
|
||||
printf("LuaGameLoader: End of directory reached\n");
|
||||
break; // End of directory
|
||||
}
|
||||
|
||||
printf("LuaGameLoader: Found entry: %s (attrib=0x%02X)\n", fno.fname, fno.fattrib);
|
||||
|
||||
// Skip directories
|
||||
if (fno.fattrib & AM_DIR) continue;
|
||||
if (fno.fattrib & AM_DIR) {
|
||||
printf("LuaGameLoader: Skipping directory\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for .lua extension
|
||||
// Skip hidden files (these are short 8.3 filename entries)
|
||||
if (fno.fattrib & AM_HID) {
|
||||
printf("LuaGameLoader: Skipping hidden file (short filename entry)\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for .lua extension (case-insensitive)
|
||||
size_t len = strlen(fno.fname);
|
||||
if (len < 5 || strcmp(fno.fname + len - 4, ".lua") != 0) continue;
|
||||
printf("LuaGameLoader: Filename length: %d\n", len);
|
||||
if (len < 5) {
|
||||
printf("LuaGameLoader: Filename too short\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Case-insensitive check for .lua or .LUA
|
||||
const char* ext = fno.fname + len - 4;
|
||||
if (strcasecmp(ext, ".lua") != 0) {
|
||||
printf("LuaGameLoader: Not a .lua file (ext=%s)\n", ext);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("LuaGameLoader: Valid .lua file!\n");
|
||||
|
||||
// Build full path
|
||||
char script_path[256];
|
||||
@@ -160,6 +203,9 @@ int LuaGameLoader::register_all_games(GameLauncher* launcher) {
|
||||
|
||||
f_closedir(&dir);
|
||||
|
||||
// Restore SPI speed
|
||||
sd_card_restore_spi_speed(prev_speed);
|
||||
|
||||
printf("LuaGameLoader: Registered %d Lua games\n", count);
|
||||
return count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user