Write a hello world of sorts for writing to SD card.

This just writes a file sd://helloworld.txt to the sd card.

It will form the basis for implementing save backup
This commit is contained in:
Philippe Symons 2024-09-02 22:18:39 +02:00
parent c0141cba9d
commit a5a6f620d4
6 changed files with 90 additions and 0 deletions

View File

@ -20,10 +20,29 @@ enum class NavigationInputSourceType
BOTH
};
extern bool sdcard_mounted;
/**
* This function determines whether the joypad_inputs_t has analog or dpad positions/presses that could be considered for UI navigation.
* If so, it will return the most prominent direction.
*/
const UINavigationDirection determineUINavigationDirection(joypad_inputs_t inputs, NavigationInputSourceType sourceType);
/**
* @brief mounts the SD card (if possible)
*
*/
bool mountSDCard();
/**
* @brief Writes the specified buffer to a file at the specified path.
* This should be a path starting with sd:/
*
* @param path path starting with sd:/
* @param buffer buffer to write
* @param bufferSize size of the buffer
* @return size_t number of bytes written
*/
size_t writeBufferToFile(const char* path, const uint8_t* buffer, size_t bufferSize);
#endif

View File

@ -30,5 +30,8 @@ void gen1TeachPikachu(void* context, const void* param);
void gen2ReceiveGSBall(void* context, const void* param);
void gen2SetEventFlag(void* context, const void* param);
//Temporary function.
// TODO: should be removed!
void writeFileToSD(void* context, const void* param);
#endif

View File

@ -1,5 +1,6 @@
#include "core/Application.h"
#include "scenes/IScene.h"
#include "core/DragonUtils.h"
static Application* appInstance = nullptr;
static void resetInterruptHandler()
@ -40,6 +41,7 @@ void Application::init()
{
// Based on example code https://github.com/DragonMinded/libdragon/wiki/OpenGL-on-N64
debug_init_isviewer();
mountSDCard();
//console_set_debug(true);
joypad_init();

View File

@ -1,5 +1,7 @@
#include "core/DragonUtils.h"
bool sdcard_mounted = false;
static uint8_t ANALOG_STICK_THRESHOLD = 30;
const UINavigationDirection determineUINavigationDirection(joypad_inputs_t inputs, NavigationInputSourceType sourceType)
@ -45,4 +47,30 @@ const UINavigationDirection determineUINavigationDirection(joypad_inputs_t input
}
}
return UINavigationDirection::MAX;
}
bool mountSDCard()
{
sdcard_mounted = debug_init_sdfs("sd:/", -1);
return sdcard_mounted;
}
size_t writeBufferToFile(const char* path, const uint8_t* buffer, size_t bufferSize)
{
size_t ret;
if(!sdcard_mounted)
{
return 0;
}
FILE* f = fopen(path, "w");
if(!f)
{
return 0;
}
ret = fwrite(buffer, sizeof(char), bufferSize, f);
fclose(f);
return ret;
}

View File

@ -20,6 +20,10 @@ MenuItemData gen1MenuEntries[] = {
.title = "Gen 3 Transfer Info",
.onConfirmAction = goToPokeTransporterGBRef
},
{
.title = "Test SD Write",
.onConfirmAction = writeFileToSD
},
{
.title = "About",
.onConfirmAction = goToAboutScene
@ -45,6 +49,10 @@ MenuItemData gen2MenuEntries[] = {
.title = "Gen 3 Transfer Info",
.onConfirmAction = goToPokeTransporterGBRef
},
{
.title = "Test SD Write",
.onConfirmAction = writeFileToSD
},
{
.title = "About",
.onConfirmAction = goToAboutScene
@ -74,6 +82,10 @@ MenuItemData gen2CrystalMenuEntries[] = {
.title = "Gen 3 Transfer Info",
.onConfirmAction = goToPokeTransporterGBRef
},
{
.title = "Test SD Write",
.onConfirmAction = writeFileToSD
},
{
.title = "About",
.onConfirmAction = goToAboutScene

View File

@ -414,3 +414,29 @@ void gen2SetEventFlag(void* context, const void* param)
tpakManager.setRAMEnabled(false);
scene->showDialog(messageData);
}
void writeFileToSD(void* context, const void* param)
{
MenuScene* scene = static_cast<MenuScene*>(context);
DialogData* msg = new DialogData{
.shouldDeleteWhenDone = true
};
if(sdcard_mounted)
{
const char* test = "Hello Phil 2!";
size_t ret = writeBufferToFile("sd:/helloworld.txt", reinterpret_cast<const uint8_t*>(test), strlen(test));
if(!ret)
{
setDialogDataText(*msg, "ERROR: Could not write to file sd:/helloworld.txt!");
}
else
{
setDialogDataText(*msg, "SUCCESS: Check the SD! sd:/helloworld.txt should exist!");
}
}
scene->showDialog(msg);
}