void feedDataStream(void) { uint8_t readBuf[32]; uint8_t res = ERR_OK; UINT bytesRead = 0; //FRTOS1_xSemaphoreTakeRecursive(feedSem, portMAX_DELAY); if (!PlaybackIsSet()) { return; // paused or stopped } // Feed the hungry buffer! :) if (VS_Ready()) { //for (;;) { /* breaks */ if (FAT1_read(&fp, readBuf, sizeof(readBuf), &bytesRead) != FR_OK) { //FRTOS1_xSemaphoreGiveRecursive(feedSem); PLR_StopPlayback(); return; } if (bytesRead == 0) { /* end of file? */ // must be at the end of the file, wrap it up! //FRTOS1_xSemaphoreGiveRecursive(feedSem); PLR_StopPlayback(); return; } VS_SendData(readBuf, sizeof(readBuf)); } //FRTOS1_xSemaphoreGiveRecursive(feedSem); }
static uint8_t Test(CLS1_ConstStdIOTypePtr io) { static FIL fp; UINT bw; uint8_t read_buf[16]; uint8_t write_buf[10]; uint8_t i; if (FAT1_isWriteProtected()) { CLS1_SendStr((unsigned char*)"disk is write protected!\r\n", io->stdErr); return ERR_FAILED; } /* write file */ CLS1_SendStr((const unsigned char*)"Creating test.txt...\r\n", io->stdOut); if (FAT1_open(&fp, "./test.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed creating file!\r\n", io->stdErr); return ERR_FAILED; } /* write text */ if (FAT1_write(&fp, "Hello world ", sizeof("Hello world ")-1, &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr); (void)FAT1_close(&fp); return ERR_FAILED; } write_buf[0] = '\0'; for(i=0;i<4;i++) { UTIL1_strcatNum8u(write_buf, sizeof(write_buf), i); UTIL1_chcat(write_buf, sizeof(write_buf), ' '); } if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr); (void)FAT1_close(&fp); return ERR_FAILED; } /* closing file */ (void)FAT1_close(&fp); /* read from file */ CLS1_SendStr((const unsigned char*)"Read from file...\r\n", io->stdOut); if (FAT1_open(&fp, "./test.txt", FA_READ)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed opening file!\r\n", io->stdErr); return ERR_FAILED; } if (FAT1_read(&fp, &read_buf[0], sizeof(read_buf)-2, &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed reading file!\r\n", io->stdErr); (void)FAT1_close(&fp); return ERR_FAILED; } read_buf[sizeof(read_buf)-2] = '\0'; /* terminate string */ UTIL1_strcat(read_buf, sizeof(read_buf), (unsigned char*)"\r\n"); CLS1_SendStr(read_buf, io->stdOut); CLS1_SendStr((const unsigned char*)"\r\n", io->stdOut); /* close file */ (void)FAT1_close(&fp); return ERR_OK; }
uint8_t VS_PlaySong(const uint8_t *fileName, const CLS1_StdIOType *io) { UINT bytesRead; uint8_t readBuf[32]; uint8_t res = ERR_OK; static FIL fp; if (io!=NULL) { CLS1_SendStr("Playing file '", io->stdOut); CLS1_SendStr(fileName, io->stdOut); CLS1_SendStr("'\r\n", io->stdOut); } if (FAT1_open(&fp, fileName, FA_READ)!=FR_OK) { if (io!=NULL) { CLS1_SendStr("ERR: Failed to open song file\r\n", io->stdErr); } return ERR_FAILED; } for(;;) { /* breaks */ bytesRead = 0; if (FAT1_read(&fp, readBuf, sizeof(readBuf), &bytesRead)!=FR_OK) { if (io!=NULL) { CLS1_SendStr("ERR: Failed to read file\r\n", io->stdErr); } res = ERR_FAILED; break; } if (bytesRead==0) { /* end of file? */ break; } while(!VS_Ready()) { FRTOS1_vTaskDelay(10/portTICK_RATE_MS); } VS_SendData(readBuf, sizeof(readBuf)); } /* closing file */ (void)FAT1_close(&fp); VS_StartSong(); return res; }
/*! \brief Simple benchmark function: first we are going to write a file, then we will copy it */ static void benchmark(const CLS1_StdIOType *io) { static FIL fp; uint16_t i; UINT bw; uint8_t read_buf[10]; TIMEREC time, startTime; int32_t start_mseconds, mseconds; /* write benchmark */ CLS1_SendStr((const unsigned char*)"Benchmark: open file, write 10k times 10 bytes (100'000 bytes), close file:\r\n", io->stdOut); CLS1_SendStr((const unsigned char*)"Deleting any existing files...\r\n", io->stdOut); (void)FAT1_DeleteFile((const unsigned char*)"./bench.txt", io); (void)FAT1_DeleteFile((const unsigned char*)"./copy.txt", io); CLS1_SendStr((const unsigned char*)"Creating benchmark file...\r\n", io->stdOut); (void)TmDt1_GetTime(&startTime); if (FAT1_open(&fp, "./bench.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed opening benchmark file!\r\n", io->stdErr); return; } for(i=0;i<10000;i++) { if (FAT1_write(&fp, "benchmark ", sizeof("benchmark ")-1, &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed writing file!\r\n", io->stdErr); (void)FAT1_close(&fp); return; } } (void)FAT1_close(&fp); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; CLS1_SendNum32s(mseconds, io->stdOut); CLS1_SendStr((const unsigned char*)" mseconds needed for command.\r\n", io->stdOut); /* read benchmark */ CLS1_SendStr((const unsigned char*)"Reading benchmark file...\r\n", io->stdOut); (void)TmDt1_GetTime(&startTime); if (FAT1_open(&fp, "./bench.txt", FA_READ)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed opening benchmark file!\r\n", io->stdErr); return; } for(i=0;i<10000;i++) { if (FAT1_read(&fp, &read_buf[0], sizeof(read_buf), &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed reading file!\r\n", io->stdErr); (void)FAT1_close(&fp); return; } } (void)FAT1_close(&fp); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; CLS1_SendNum32s(mseconds, io->stdOut); CLS1_SendStr((const unsigned char*)" mseconds needed for command.\r\n", io->stdOut); /* copy benchmark */ CLS1_SendStr((const unsigned char*)"Benchmark: copy file (100'000 bytes):\r\n", io->stdOut); CLS1_SendStr((const unsigned char*)"Going to copy file...\r\n", io->stdOut); (void)TmDt1_GetTime(&startTime); (void)FAT1_CopyFile((const unsigned char*)"./bench.txt", (const unsigned char*)"./copy.txt", io); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; CLS1_SendNum32s(mseconds, io->stdOut); CLS1_SendStr((const unsigned char*)" mseconds needed for command.\r\n", io->stdOut); CLS1_SendStr((const unsigned char*)"done!\r\n", io->stdOut); }
/*! * \brief Copy the source file to a destination file * \param[in] srcFileName Source file name * \param[in] dstFileName Destination file name * \param[in] io IO handler for output * \return Error code, ERR_OK for success. */ byte UFFS_FAT_CopyFile(const byte *srcFileName, const byte *dstFileName, const CLS1_StdIOType *io) { bool sourceUffs = pdTRUE; ///< source is uffs (otherwise FAT) bool destUffs = pdTRUE; ///< destination is uffs (otherwise FAT) int fd1=-1, fd2=-1; // uffs file pointers FAT1_FIL fsrc, fdst; // FAT file objects FAT1_FRESULT fres; // FAT result uint8_t buffer[32]; /* copy buffer */ UINT br, bw, bt=0; /* file read/write counters */ byte res = ERR_OK; if( srcFileName[1] == ':') sourceUffs = pdFALSE; if( dstFileName[1] == ':') { destUffs = pdFALSE; if (FAT1_isWriteProtected() || FAT1_FS_READONLY) { MSGLN("destination FAT disk is write protected!"); return ERR_FAILED; } } /* open source file */ if(sourceUffs) { fd1 = uffs_open((char*)srcFileName, UO_RDONLY); if (fd1 < 0) { MSGLN("open source file failed"); return ERR_FAILED; } } else { fres = FAT1_open(&fsrc, (char*)srcFileName, FA_OPEN_EXISTING | FA_READ); if (fres != FR_OK) { MSGLN("open source file failed. Result=%d", fres); return ERR_FAILED; } } /* create destination file */ if(destUffs) { fd2 = uffs_open((char*)dstFileName, UO_CREATE|UO_WRONLY); if (fd2 < 0) { MSGLN("open destination file failed"); return ERR_FAILED; } } else { fres = FAT1_open(&fdst, (char*)dstFileName, FA_CREATE_ALWAYS | FA_WRITE); if (fres != FR_OK) { MSGLN("open destination file failed. Result=%d", fres); return ERR_FAILED; } } /* now copy source to destination */ for (;;) { if(sourceUffs) { br = uffs_read(fd1, buffer, sizeof(buffer)); } else { fres = FAT1_read(&fsrc, buffer, sizeof(buffer), &br); if (fres != FR_OK) { MSGLN("reading source file failed. Result=%d", fres); res = ERR_FAILED; break; } } bt += br; if (br == 0) { /* EOF */ break; /* get out of loop */ } if(destUffs) { bw = uffs_write(fd2, buffer, br); } else { fres = FAT1_write(&fdst, buffer, br, &bw); if (fres != ERR_OK) { MSGLN("writing destination file failed. Result=%d", fres); res = ERR_FAILED; break; } } if (bw < br) { MSGLN("failed writing destination file, or disk full"); res = ERR_FAILED; break; } } /* for */ /* close all files */ if(sourceUffs) uffs_close(fd1); else { fres = FAT1_close(&fsrc); if (fres != FR_OK) { MSGLN("closing source file failed. Result=%d", fres); res = ERR_FAILED; } } if(destUffs) uffs_close(fd2); else { fres = FAT1_close(&fdst); if (fres != FR_OK) { MSGLN("closing destination file failed. Result=%d", fres); res = ERR_FAILED; } } MSGLN("%u bytes copied.", bt); return res; }