Skip to content

Commit

Permalink
ReplaysLED 'yes' flash off to indicate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlee337 committed Feb 28, 2024
1 parent 4f27681 commit 2035b53
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions kernel/SlippiFileWriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// double that, making our read buffer 10000 bytes
#define READ_BUF_SIZE 10000
#define THREAD_CYCLE_TIME_MS 100
#define THREAD_ERROR_TIME_MS 1000
#define LED_FLASH_TIME_MS 1000

#define FOOTER_BUFFER_LENGTH 200

Expand All @@ -35,6 +37,9 @@ u32 gameStartTime;
// timer for drive led
u32 driveTimer;

// flag for drive led timer
bool driveTimerSet;

// replays LED setting, 0: always on, 1: flash on insert and file end, 2: do not use
u32 replaysLED;

Expand Down Expand Up @@ -163,17 +168,21 @@ void completeFile(FIL *file, SlpGameReader *reader, u32 writtenByteCount)

// Write footer
u32 wrote;
u32 res;
f_write(file, footer, writePos, &wrote);
f_sync(file);

f_lseek(file, 11);
f_write(file, &writtenByteCount, 4, &wrote);
res = f_sync(file);
if (replaysLED == 1 && res == 0) {
set32(HW_GPIO_OUT, GPIO_SLOT_LED);
FRESULT fileWriteResult = f_write(file, &writtenByteCount, 4, &wrote);
if ((replaysLED == 0 && fileWriteResult != FR_OK) || (replaysLED == 1 && fileWriteResult == FR_OK))
{
if (replaysLED == 0)
clear32(HW_GPIO_OUT, GPIO_SLOT_LED);
else
set32(HW_GPIO_OUT, GPIO_SLOT_LED);
driveTimer = read32(HW_TIMER);
driveTimerSet = true;
}
f_sync(file);
}

static u32 SlippiHandlerThread(void *arg)
Expand All @@ -186,6 +195,7 @@ static u32 SlippiHandlerThread(void *arg)

u32 writtenByteCount = 0;
driveTimer = read32(HW_TIMER);
driveTimerSet = false;

FATFS device;
bool failedToMount = false;
Expand All @@ -200,10 +210,6 @@ static u32 SlippiHandlerThread(void *arg)
// Cycle time, look at const definition for more info
mdelay(THREAD_CYCLE_TIME_MS);

if (replaysLED == 1 && TimerDiffMs(driveTimer) > 1000) {
clear32(HW_GPIO_OUT, GPIO_SLOT_LED);
}

if (use_usb)
{
if (!USBStorage_IsInserted_SlippiThread())
Expand Down Expand Up @@ -234,7 +240,10 @@ static u32 SlippiHandlerThread(void *arg)
{
set32(HW_GPIO_OUT, GPIO_SLOT_LED);
if (replaysLED == 1)
{
driveTimer = read32(HW_TIMER); // flash drive LED on successful insertion.
driveTimerSet = true;
}
}
}
else
Expand All @@ -247,14 +256,32 @@ static u32 SlippiHandlerThread(void *arg)
continue;
}

if (driveTimerSet && TimerDiffMs(driveTimer) >= LED_FLASH_TIME_MS)
{
if (mounted && replaysLED == 0)
set32(HW_GPIO_OUT, GPIO_SLOT_LED);
else if (replaysLED == 1)
clear32(HW_GPIO_OUT, GPIO_SLOT_LED);

driveTimerSet = false;
}

// Read from memory and write to file
SlpMemError err = SlippiMemoryRead(&reader, readBuf, READ_BUF_SIZE, memReadPos);
if (err)
{
if (err == SLP_READ_OVERFLOW)
{
memReadPos = SlippiRestoreReadPos();
if (replaysLED == 0)
{
clear32(HW_GPIO_OUT, GPIO_SLOT_LED);
driveTimer = read32(HW_TIMER); // flash LED off because this means a replay has been lost/corrupted.
driveTimerSet = true;
}
}

mdelay(1000);
mdelay(THREAD_ERROR_TIME_MS);

// For specific errors, bytes will still be read. Not continueing to deal with those
}
Expand All @@ -272,8 +299,14 @@ static u32 SlippiHandlerThread(void *arg)
FRESULT fileOpenResult = f_open_secondary_drive(&currentFile, fileName, FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
if (fileOpenResult != FR_OK)
{
if (replaysLED == 0)
{
clear32(HW_GPIO_OUT, GPIO_SLOT_LED);
driveTimer = read32(HW_TIMER); // flash LED off on fs error.
driveTimerSet = true;
}
dbgprintf("Slippi: failed to open file: %s, errno: %d\r\n", fileName, fileOpenResult);
mdelay(1000);
mdelay(THREAD_ERROR_TIME_MS);
continue;
}

Expand All @@ -296,7 +329,13 @@ static u32 SlippiHandlerThread(void *arg)
}

UINT wrote;
f_write(&currentFile, readBuf, reader.lastReadResult.bytesRead, &wrote);
FRESULT writeResult = f_write(&currentFile, readBuf, reader.lastReadResult.bytesRead, &wrote);
if (replaysLED == 0 && writeResult != FR_OK)
{
clear32(HW_GPIO_OUT, GPIO_SLOT_LED);
driveTimer = read32(HW_TIMER); // flash LED off on fs error.
driveTimerSet = true;
}
f_sync(&currentFile);

if (wrote == 0)
Expand Down

0 comments on commit 2035b53

Please sign in to comment.