PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoDisk.cpp
00001 /**************************************************************************/ 00002 /*! 00003 @file Pokitto_disk.cpp 00004 @author Jonne Valola 00005 00006 @section LICENSE 00007 00008 Pokitto development stage library 00009 Software License Agreement 00010 00011 Copyright (c) 2015, Jonne Valola ("Author") 00012 All rights reserved. 00013 00014 This library is intended solely for the purpose of Pokitto development. 00015 00016 Redistribution and use in source and binary forms, with or without 00017 modification requires written permission from Author. 00018 */ 00019 /**************************************************************************/ 00020 00021 #include "Pokitto.h " 00022 00023 #define SD_MOSI_PORT 0 00024 #define SD_MISO_PORT 0 00025 #define SD_SCK_PORT 0 00026 #define SD_CS_PORT 0 00027 #define SD_MOSI_PIN 9 00028 #define SD_MISO_PIN 8 00029 #define SD_SCK_PIN 6 00030 #define SD_CS_PIN 7 00031 00032 #if POK_ENABLE_SD > 0 00033 PFFS::BYTE res; 00034 PFFS::FATFS fs; /* File system object */ 00035 PFFS::FATDIR dir; /* Directory object */ 00036 PFFS::FILINFO fno; /* File information */ 00037 00038 //static FATFS *FatFs; /* Pointer to the file system object (logical drive) */ 00039 00040 bool diropened=false; 00041 00042 #define SPEAKER 3 00043 //#define BUFFER_SIZE 256 // was 128 00044 #define SONGLENGTH 0x1BFBCD // 1072223 00045 #define FILESIZE 0x1BFBCD 00046 00047 uint8_t filemode = FILE_MODE_UNINITIALIZED; 00048 char currentfile[15]; // holds current file's name 00049 00050 SPI device(CONNECT_MOSI,CONNECT_MISO,CONNECT_SCK); 00051 //DigitalOut mmccs(CONNECT_CS); 00052 00053 const char *get_filename_ext(const char *filename) { 00054 const char *dot = strrchr(filename, '.'); 00055 if(!dot || dot == filename) return ""; 00056 return dot + 1; 00057 } 00058 00059 //__attribute__((section(".SD_Code"))) 00060 void initSDGPIO() { 00061 LPC_GPIO_PORT->DIR[SD_MOSI_PORT] |= (1 << SD_MOSI_PIN ); 00062 LPC_GPIO_PORT->DIR[SD_MISO_PORT] |= (1 << SD_MISO_PIN ); 00063 LPC_GPIO_PORT->DIR[SD_SCK_PORT] |= (1 << SD_SCK_PIN ); 00064 LPC_GPIO_PORT->DIR[SD_CS_PORT] |= (1 << SD_CS_PIN ); 00065 } 00066 00067 //__attribute__((section(".SD_Code"))) 00068 int pokInitSD() { 00069 initSDGPIO(); 00070 res = PFFS::disk_initialize(); 00071 //res = disk_initialize(0); 00072 res = (pf_mount(&fs)); 00073 res = pf_opendir(&dir,""); 00074 if (res) diropened=false; 00075 else diropened=true; 00076 return res; 00077 } 00078 00079 00080 void emptyFname() { 00081 for (int i=0; i<13; i++) fno.fname[i]=NULL; 00082 } 00083 00084 /** PUBLIC FUNCTIONS **/ 00085 00086 char* getFirstDirEntry(char* path) { 00087 res=0; 00088 if (!diropened) { 00089 pokInitSD(); 00090 } 00091 res = pf_opendir(&dir,path); 00092 emptyFname(); 00093 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK 00094 if (res) return 0; 00095 while (res==0) { //while res is ok 00096 if ((fno.fattrib & 0x02)==0) { 00097 if (fno.fattrib & 0x10) { 00098 fno.fname[8]='.'; 00099 fno.fname[9]='D'; 00100 fno.fname[10]='I'; 00101 fno.fname[11]='R'; 00102 fno.fname[12]='\0'; 00103 } 00104 return fno.fname; 00105 } 00106 emptyFname(); 00107 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK 00108 if (res==0 && dir.index==0) break; 00109 } 00110 return 0; 00111 } 00112 00113 char* getFirstDirEntry() { 00114 return getFirstDirEntry(""); 00115 } 00116 00117 char* getNextDirEntry() { 00118 if (!diropened) pokInitSD(); 00119 emptyFname(); 00120 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK 00121 if (res==0) { 00122 while (fno.fattrib & 0x02 && !res) {emptyFname(); res = pf_readdir(&dir,&fno);} //system/hidden file 00123 if (fno.fattrib & 0x10) { 00124 int a=12; 00125 while (a) { 00126 fno.fname[a] = fno.fname[a-1]; 00127 a--; 00128 } 00129 if (fno.fname[0]) { 00130 fno.fname[0]='/'; 00131 a=0; 00132 while (fno.fname[a]) a++; 00133 fno.fname[a]='/'; 00134 } 00135 00136 /*fno.fname[a++]='.'; 00137 fno.fname[a++]='D'; 00138 fno.fname[a++]='I'; 00139 fno.fname[a++]='R'; 00140 fno.fname[a]='\0';*/ 00141 } 00142 return fno.fname; 00143 } 00144 return NULL; 00145 } 00146 00147 char* getNextFile (char* ext){ 00148 00149 if (!diropened) pokInitSD(); 00150 int a=1; 00151 emptyFname(); 00152 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK 00153 while (res==0 || a) { //while there are entries and 00154 if (dir.index==0) return 0; //end of list 00155 a = strcmp((const char*)get_filename_ext(fno.fname),(const char*)ext); // returns 0 if strings are identical 00156 if (strcmp(ext,"")==0 && (fno.fattrib & 0x10) == 0) a=0; 00157 if (a == 0 && (fno.fattrib & 0x10) == 0) return fno.fname; 00158 if (fno.fname[0]==NULL) return NULL; //end of files 00159 //if (fno.fattrib&0x10) return NULL; //its a directory 00160 emptyFname(); 00161 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK 00162 } 00163 return 0; 00164 } 00165 00166 00167 char* getNextFile() { 00168 return getNextFile(""); 00169 } 00170 00171 char* getFirstFile(char* ext, char* path) { 00172 res=0; 00173 if (!diropened) { 00174 pokInitSD(); 00175 } 00176 res = pf_opendir(&dir,path); 00177 emptyFname(); 00178 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK 00179 if (res) return 0; 00180 while (res==0 || (fno.fattrib & 0x10) == 0) { 00181 int a=0; 00182 a = strcmp((const char*)get_filename_ext(fno.fname),(const char*)ext); // returns 0 if strings are identical 00183 if (!strcmp(ext,"")) a=0; 00184 if ( a == 0 && (fno.fattrib & 0x10) == 0) return fno.fname; 00185 emptyFname(); 00186 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK 00187 if (fno.fname[0]==NULL) break; //end of directory reached, no files found 00188 if (res==0 && dir.index==0) break; 00189 } 00190 return 0; 00191 } 00192 00193 char* getFirstFile(char* ext) { 00194 return getFirstFile(ext, ""); 00195 } 00196 00197 char* getFirstFile() { 00198 return getFirstFile(""); 00199 } 00200 00201 int isThisFileOpen(char* buffer){ 00202 int a=0; 00203 a = strcmp((const char*)buffer,(const char*)currentfile); // returns 0 if strings are identical 00204 if ( a == 0 && filemode != FILE_MODE_FAILED) return 1; 00205 return 0; 00206 } 00207 00208 int fileOK() { 00209 if (filemode != FILE_MODE_FAILED) return 1; 00210 return 0; 00211 } 00212 00213 uint8_t fileOpen(char* buffer, char fmode) { 00214 int err; 00215 if (filemode == FILE_MODE_UNINITIALIZED) { 00216 int a = pf_mount(&fs); 00217 if (a) return 1; // 1 means error in this context 00218 } 00219 00220 filemode = fmode; 00221 err = PFFS::pf_open(buffer); 00222 if (err==0) { 00223 strcpy(currentfile,(const char*)buffer); 00224 return 0; // 0 means all clear 00225 } 00226 // file open failed 00227 filemode = FILE_MODE_FAILED; 00228 return 1; // 1 means failed 00229 } 00230 00231 void fileClose() { 00232 filemode = FILE_MODE_UNINITIALIZED; 00233 for (uint8_t i=0; i<15; i++) currentfile[i]=0; 00234 } 00235 00236 char fileGetChar() { 00237 PFFS::BYTE buff[1]; 00238 PFFS::WORD br; 00239 int err = PFFS::pf_read(buff, 1, &br); /* Read data to the buff[] */ 00240 return buff[0]; 00241 } 00242 00243 void filePutChar(char c) { 00244 PFFS::WORD bw; 00245 PFFS::pf_write((const void*)&c, 1, &bw); 00246 PFFS::pf_write(0, 0, &bw); 00247 } 00248 00249 void fileWriteBytes(uint8_t * b, uint16_t n) { 00250 PFFS::WORD bw; 00251 PFFS::pf_write((const void*)&b, n, &bw); 00252 PFFS::pf_write(0, 0, &bw); 00253 } 00254 00255 uint16_t fileReadBytes(uint8_t * b, uint16_t n) { 00256 PFFS::WORD br; 00257 PFFS::pf_read(b, n, &br); /* Read data to the buff[] */ 00258 return br; /* Return number of bytes read */ 00259 } 00260 00261 void fileSeekAbsolute(long n) { 00262 res = PFFS::pf_lseek(n); 00263 } 00264 00265 void fileSeekRelative(long n) { 00266 if (n<0) if (fs.fptr < -n) n=-fs.fptr; 00267 else if (n>0) if (fs.fptr+n > fs.fsize) n=fs.fsize-fs.fptr; 00268 res = PFFS::pf_lseek(fs.fptr + n); 00269 } 00270 00271 void fileRewind() { 00272 res = PFFS::pf_lseek(0); 00273 } 00274 00275 void fileEnd() { 00276 res = PFFS::pf_lseek(fs.fsize); 00277 } 00278 00279 long int fileGetPosition() { 00280 return fs.fptr; 00281 } 00282 00283 uint8_t filePeek(long n) { 00284 PFFS::pf_lseek(n); 00285 return fileGetChar(); 00286 } 00287 00288 void filePoke(long n, uint8_t c) { 00289 PFFS::pf_lseek(n); 00290 filePutChar(c); 00291 } 00292 00293 int fileReadLine(char* destination, int maxchars) { 00294 int n=0; 00295 char c=1; 00296 char linebuf[80]; 00297 fileReadBytes((uint8_t*)linebuf,80); 00298 int index=0; 00299 while (c!=NULL) { 00300 c = linebuf[index++]; 00301 if (n == 0) { 00302 while (c == '\n' || c == '\r') c = linebuf[index++]; // skip empty lines 00303 } 00304 n++; 00305 if (c=='\n' || c=='\r' || n==maxchars-1) c=NULL; //prevent buffer overflow 00306 *destination++ = c; 00307 } 00308 fileSeekRelative(-80+index); //rewind 00309 return n; //number of characters read 00310 } 00311 00312 int dirOpen() { 00313 return PFFS::pf_opendir(&dir,""); 00314 } 00315 00316 int dirUp() { 00317 00318 return 0; 00319 } 00320 #endif // POK_ENABLE_SD 00321 00322 00323
Generated on Tue Jul 12 2022 11:20:36 by 1.7.2