PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
Pokitto
Date:
Mon Apr 02 22:37:22 2018 +0000
Revision:
36:771321e70814
Synced with Github repo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 36:771321e70814 1 /**************************************************************************/
Pokitto 36:771321e70814 2 /*!
Pokitto 36:771321e70814 3 @file Pokitto_disk.cpp
Pokitto 36:771321e70814 4 @author Jonne Valola
Pokitto 36:771321e70814 5
Pokitto 36:771321e70814 6 @section LICENSE
Pokitto 36:771321e70814 7
Pokitto 36:771321e70814 8 Pokitto development stage library
Pokitto 36:771321e70814 9 Software License Agreement
Pokitto 36:771321e70814 10
Pokitto 36:771321e70814 11 Copyright (c) 2015, Jonne Valola ("Author")
Pokitto 36:771321e70814 12 All rights reserved.
Pokitto 36:771321e70814 13
Pokitto 36:771321e70814 14 This library is intended solely for the purpose of Pokitto development.
Pokitto 36:771321e70814 15
Pokitto 36:771321e70814 16 Redistribution and use in source and binary forms, with or without
Pokitto 36:771321e70814 17 modification requires written permission from Author.
Pokitto 36:771321e70814 18 */
Pokitto 36:771321e70814 19 /**************************************************************************/
Pokitto 36:771321e70814 20
Pokitto 36:771321e70814 21 #include "Pokitto.h"
Pokitto 36:771321e70814 22
Pokitto 36:771321e70814 23 #define SD_MOSI_PORT 0
Pokitto 36:771321e70814 24 #define SD_MISO_PORT 0
Pokitto 36:771321e70814 25 #define SD_SCK_PORT 0
Pokitto 36:771321e70814 26 #define SD_CS_PORT 0
Pokitto 36:771321e70814 27 #define SD_MOSI_PIN 9
Pokitto 36:771321e70814 28 #define SD_MISO_PIN 8
Pokitto 36:771321e70814 29 #define SD_SCK_PIN 6
Pokitto 36:771321e70814 30 #define SD_CS_PIN 7
Pokitto 36:771321e70814 31
Pokitto 36:771321e70814 32 #if POK_ENABLE_SD > 0
Pokitto 36:771321e70814 33 #ifndef NOPETITFATFS
Pokitto 36:771321e70814 34 BYTE res;
Pokitto 36:771321e70814 35 FATFS fs; /* File system object */
Pokitto 36:771321e70814 36 FATDIR dir; /* Directory object */
Pokitto 36:771321e70814 37 FILINFO fno; /* File information */
Pokitto 36:771321e70814 38
Pokitto 36:771321e70814 39 //static FATFS *FatFs; /* Pointer to the file system object (logical drive) */
Pokitto 36:771321e70814 40
Pokitto 36:771321e70814 41 bool diropened=false;
Pokitto 36:771321e70814 42
Pokitto 36:771321e70814 43 #define SPEAKER 3
Pokitto 36:771321e70814 44 //#define BUFFER_SIZE 256 // was 128
Pokitto 36:771321e70814 45 #define SONGLENGTH 0x1BFBCD // 1072223
Pokitto 36:771321e70814 46 #define FILESIZE 0x1BFBCD
Pokitto 36:771321e70814 47
Pokitto 36:771321e70814 48 uint8_t filemode = FILE_MODE_UNINITIALIZED;
Pokitto 36:771321e70814 49 char currentfile[15]; // holds current file's name
Pokitto 36:771321e70814 50
Pokitto 36:771321e70814 51 SPI device(CONNECT_MOSI,CONNECT_MISO,CONNECT_SCK);
Pokitto 36:771321e70814 52 //DigitalOut mmccs(CONNECT_CS);
Pokitto 36:771321e70814 53
Pokitto 36:771321e70814 54 const char *get_filename_ext(const char *filename) {
Pokitto 36:771321e70814 55 const char *dot = strrchr(filename, '.');
Pokitto 36:771321e70814 56 if(!dot || dot == filename) return "";
Pokitto 36:771321e70814 57 return dot + 1;
Pokitto 36:771321e70814 58 }
Pokitto 36:771321e70814 59
Pokitto 36:771321e70814 60 __attribute__((section(".SD_Code"))) void initSDGPIO() {
Pokitto 36:771321e70814 61 LPC_GPIO_PORT->DIR[SD_MOSI_PORT] |= (1 << SD_MOSI_PIN );
Pokitto 36:771321e70814 62 LPC_GPIO_PORT->DIR[SD_MISO_PORT] |= (1 << SD_MISO_PIN );
Pokitto 36:771321e70814 63 LPC_GPIO_PORT->DIR[SD_SCK_PORT] |= (1 << SD_SCK_PIN );
Pokitto 36:771321e70814 64 LPC_GPIO_PORT->DIR[SD_CS_PORT] |= (1 << SD_CS_PIN );
Pokitto 36:771321e70814 65 }
Pokitto 36:771321e70814 66
Pokitto 36:771321e70814 67 __attribute__((section(".SD_Code"))) int pokInitSD() {
Pokitto 36:771321e70814 68 initSDGPIO();
Pokitto 36:771321e70814 69 #ifndef NOPETITFATFS
Pokitto 36:771321e70814 70 res = disk_initialize();
Pokitto 36:771321e70814 71 #else
Pokitto 36:771321e70814 72 res = disk_initialize(0);
Pokitto 36:771321e70814 73 #endif
Pokitto 36:771321e70814 74 res = (pf_mount(&fs));
Pokitto 36:771321e70814 75 res = pf_opendir(&dir,"");
Pokitto 36:771321e70814 76 if (res) diropened=false;
Pokitto 36:771321e70814 77 else diropened=true;
Pokitto 36:771321e70814 78 return res;
Pokitto 36:771321e70814 79 }
Pokitto 36:771321e70814 80
Pokitto 36:771321e70814 81
Pokitto 36:771321e70814 82 void emptyFname() {
Pokitto 36:771321e70814 83 for (int i=0; i<13; i++) fno.fname[i]=NULL;
Pokitto 36:771321e70814 84 }
Pokitto 36:771321e70814 85
Pokitto 36:771321e70814 86 /** PUBLIC FUNCTIONS **/
Pokitto 36:771321e70814 87
Pokitto 36:771321e70814 88 char* getFirstDirEntry() {
Pokitto 36:771321e70814 89 res=0;
Pokitto 36:771321e70814 90 if (!diropened) {
Pokitto 36:771321e70814 91 pokInitSD();
Pokitto 36:771321e70814 92 }
Pokitto 36:771321e70814 93 res = pf_opendir(&dir,"");
Pokitto 36:771321e70814 94 emptyFname();
Pokitto 36:771321e70814 95 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK
Pokitto 36:771321e70814 96 if (res) return 0;
Pokitto 36:771321e70814 97 while (res==0) { //while res is ok
Pokitto 36:771321e70814 98 if ((fno.fattrib & 0x02)==0) {
Pokitto 36:771321e70814 99 if (fno.fattrib & 0x10) {
Pokitto 36:771321e70814 100 fno.fname[8]='.';
Pokitto 36:771321e70814 101 fno.fname[9]='D';
Pokitto 36:771321e70814 102 fno.fname[10]='I';
Pokitto 36:771321e70814 103 fno.fname[11]='R';
Pokitto 36:771321e70814 104 fno.fname[12]='\0';
Pokitto 36:771321e70814 105 }
Pokitto 36:771321e70814 106 return fno.fname;
Pokitto 36:771321e70814 107 }
Pokitto 36:771321e70814 108 emptyFname();
Pokitto 36:771321e70814 109 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK
Pokitto 36:771321e70814 110 if (res==0 && dir.index==0) break;
Pokitto 36:771321e70814 111 }
Pokitto 36:771321e70814 112 return 0;
Pokitto 36:771321e70814 113 }
Pokitto 36:771321e70814 114
Pokitto 36:771321e70814 115 char* getNextDirEntry() {
Pokitto 36:771321e70814 116 if (!diropened) pokInitSD();
Pokitto 36:771321e70814 117 emptyFname();
Pokitto 36:771321e70814 118 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK
Pokitto 36:771321e70814 119 if (res==0) {
Pokitto 36:771321e70814 120 while (fno.fattrib & 0x02 && !res) {emptyFname(); res = pf_readdir(&dir,&fno);} //system/hidden file
Pokitto 36:771321e70814 121 if (fno.fattrib & 0x10) {
Pokitto 36:771321e70814 122 int a=12;
Pokitto 36:771321e70814 123 while (a) {
Pokitto 36:771321e70814 124 fno.fname[a] = fno.fname[a-1];
Pokitto 36:771321e70814 125 a--;
Pokitto 36:771321e70814 126 }
Pokitto 36:771321e70814 127 if (fno.fname[0]) {
Pokitto 36:771321e70814 128 fno.fname[0]='/';
Pokitto 36:771321e70814 129 a=0;
Pokitto 36:771321e70814 130 while (fno.fname[a]) a++;
Pokitto 36:771321e70814 131 fno.fname[a]='/';
Pokitto 36:771321e70814 132 }
Pokitto 36:771321e70814 133
Pokitto 36:771321e70814 134 /*fno.fname[a++]='.';
Pokitto 36:771321e70814 135 fno.fname[a++]='D';
Pokitto 36:771321e70814 136 fno.fname[a++]='I';
Pokitto 36:771321e70814 137 fno.fname[a++]='R';
Pokitto 36:771321e70814 138 fno.fname[a]='\0';*/
Pokitto 36:771321e70814 139 }
Pokitto 36:771321e70814 140 return fno.fname;
Pokitto 36:771321e70814 141 }
Pokitto 36:771321e70814 142 return NULL;
Pokitto 36:771321e70814 143 }
Pokitto 36:771321e70814 144
Pokitto 36:771321e70814 145 char* getNextFile (char* ext){
Pokitto 36:771321e70814 146
Pokitto 36:771321e70814 147 if (!diropened) pokInitSD();
Pokitto 36:771321e70814 148 int a=1;
Pokitto 36:771321e70814 149 emptyFname();
Pokitto 36:771321e70814 150 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK
Pokitto 36:771321e70814 151 while (res==0 || a) { //while there are entries and
Pokitto 36:771321e70814 152 if (dir.index==0) return 0; //end of list
Pokitto 36:771321e70814 153 a = strcmp((const char*)get_filename_ext(fno.fname),(const char*)ext); // returns 0 if strings are identical
Pokitto 36:771321e70814 154 if (strcmp(ext,"")==0 && (fno.fattrib & 0x10) == 0) a=0;
Pokitto 36:771321e70814 155 if (a == 0 && (fno.fattrib & 0x10) == 0) return fno.fname;
Pokitto 36:771321e70814 156 if (fno.fname[0]==NULL) return NULL; //end of files
Pokitto 36:771321e70814 157 //if (fno.fattrib&0x10) return NULL; //its a directory
Pokitto 36:771321e70814 158 emptyFname();
Pokitto 36:771321e70814 159 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK
Pokitto 36:771321e70814 160 }
Pokitto 36:771321e70814 161 return 0;
Pokitto 36:771321e70814 162 }
Pokitto 36:771321e70814 163
Pokitto 36:771321e70814 164
Pokitto 36:771321e70814 165 char* getNextFile() {
Pokitto 36:771321e70814 166 return getNextFile("");
Pokitto 36:771321e70814 167 }
Pokitto 36:771321e70814 168
Pokitto 36:771321e70814 169 char* getFirstFile(char* ext) {
Pokitto 36:771321e70814 170 res=0;
Pokitto 36:771321e70814 171 if (!diropened) {
Pokitto 36:771321e70814 172 pokInitSD();
Pokitto 36:771321e70814 173 }
Pokitto 36:771321e70814 174 res = pf_opendir(&dir,"");
Pokitto 36:771321e70814 175 emptyFname();
Pokitto 36:771321e70814 176 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK
Pokitto 36:771321e70814 177 if (res) return 0;
Pokitto 36:771321e70814 178 while (res==0 || (fno.fattrib & 0x10) == 0) {
Pokitto 36:771321e70814 179 int a=0;
Pokitto 36:771321e70814 180 a = strcmp((const char*)get_filename_ext(fno.fname),(const char*)ext); // returns 0 if strings are identical
Pokitto 36:771321e70814 181 if (!strcmp(ext,"")) a=0;
Pokitto 36:771321e70814 182 if ( a == 0 && (fno.fattrib & 0x10) == 0) return fno.fname;
Pokitto 36:771321e70814 183 emptyFname();
Pokitto 36:771321e70814 184 res = pf_readdir(&dir,&fno); //returns 0 if everything is OK
Pokitto 36:771321e70814 185 if (fno.fname[0]==NULL) break; //end of directory reached, no files found
Pokitto 36:771321e70814 186 if (res==0 && dir.index==0) break;
Pokitto 36:771321e70814 187 }
Pokitto 36:771321e70814 188 return 0;
Pokitto 36:771321e70814 189 }
Pokitto 36:771321e70814 190
Pokitto 36:771321e70814 191 char* getFirstFile() {
Pokitto 36:771321e70814 192 return getFirstFile("");
Pokitto 36:771321e70814 193 }
Pokitto 36:771321e70814 194
Pokitto 36:771321e70814 195 int isThisFileOpen(char* buffer){
Pokitto 36:771321e70814 196 int a=0;
Pokitto 36:771321e70814 197 a = strcmp((const char*)buffer,(const char*)currentfile); // returns 0 if strings are identical
Pokitto 36:771321e70814 198 if ( a == 0 && filemode != FILE_MODE_FAILED) return 1;
Pokitto 36:771321e70814 199 return 0;
Pokitto 36:771321e70814 200 }
Pokitto 36:771321e70814 201
Pokitto 36:771321e70814 202 int fileOK() {
Pokitto 36:771321e70814 203 if (filemode != FILE_MODE_FAILED) return 1;
Pokitto 36:771321e70814 204 return 0;
Pokitto 36:771321e70814 205 }
Pokitto 36:771321e70814 206
Pokitto 36:771321e70814 207 uint8_t fileOpen(char* buffer, char fmode) {
Pokitto 36:771321e70814 208 int err;
Pokitto 36:771321e70814 209 if (filemode == FILE_MODE_UNINITIALIZED) {
Pokitto 36:771321e70814 210 int a = pf_mount(&fs);
Pokitto 36:771321e70814 211 if (a) return 1; // 1 means error in this context
Pokitto 36:771321e70814 212 }
Pokitto 36:771321e70814 213
Pokitto 36:771321e70814 214 filemode = fmode;
Pokitto 36:771321e70814 215 err = pf_open(buffer);
Pokitto 36:771321e70814 216 if (err==0) {
Pokitto 36:771321e70814 217 strcpy(currentfile,(const char*)buffer);
Pokitto 36:771321e70814 218 return 0; // 0 means all clear
Pokitto 36:771321e70814 219 }
Pokitto 36:771321e70814 220 // file open failed
Pokitto 36:771321e70814 221 filemode = FILE_MODE_FAILED;
Pokitto 36:771321e70814 222 return 1; // 1 means failed
Pokitto 36:771321e70814 223 }
Pokitto 36:771321e70814 224
Pokitto 36:771321e70814 225 void fileClose() {
Pokitto 36:771321e70814 226 filemode = FILE_MODE_UNINITIALIZED;
Pokitto 36:771321e70814 227 for (uint8_t i=0; i<15; i++) currentfile[i]=0;
Pokitto 36:771321e70814 228 }
Pokitto 36:771321e70814 229
Pokitto 36:771321e70814 230 char fileGetChar() {
Pokitto 36:771321e70814 231 BYTE buff[1];
Pokitto 36:771321e70814 232 WORD br;
Pokitto 36:771321e70814 233 int err = pf_read(buff, 1, &br); /* Read data to the buff[] */
Pokitto 36:771321e70814 234 return buff[0];
Pokitto 36:771321e70814 235 }
Pokitto 36:771321e70814 236
Pokitto 36:771321e70814 237 void filePutChar(char c) {
Pokitto 36:771321e70814 238 WORD bw;
Pokitto 36:771321e70814 239 pf_write((const void*)&c, 1, &bw);
Pokitto 36:771321e70814 240 pf_write(0, 0, &bw);
Pokitto 36:771321e70814 241 }
Pokitto 36:771321e70814 242
Pokitto 36:771321e70814 243 void fileWriteBytes(uint8_t * b, uint16_t n) {
Pokitto 36:771321e70814 244 WORD bw;
Pokitto 36:771321e70814 245 pf_write((const void*)&b, n, &bw);
Pokitto 36:771321e70814 246 pf_write(0, 0, &bw);
Pokitto 36:771321e70814 247 }
Pokitto 36:771321e70814 248
Pokitto 36:771321e70814 249 uint16_t fileReadBytes(uint8_t * b, uint16_t n) {
Pokitto 36:771321e70814 250 WORD br;
Pokitto 36:771321e70814 251 pf_read(b, n, &br); /* Read data to the buff[] */
Pokitto 36:771321e70814 252 return br; /* Return number of bytes read */
Pokitto 36:771321e70814 253 }
Pokitto 36:771321e70814 254
Pokitto 36:771321e70814 255 void fileSeekAbsolute(long n) {
Pokitto 36:771321e70814 256 res = pf_lseek(n);
Pokitto 36:771321e70814 257 }
Pokitto 36:771321e70814 258
Pokitto 36:771321e70814 259 void fileSeekRelative(long n) {
Pokitto 36:771321e70814 260 if (n<0) if (fs.fptr < -n) n=-fs.fptr;
Pokitto 36:771321e70814 261 else if (n>0) if (fs.fptr+n > fs.fsize) n=fs.fsize-fs.fptr;
Pokitto 36:771321e70814 262 res = pf_lseek(fs.fptr + n);
Pokitto 36:771321e70814 263 }
Pokitto 36:771321e70814 264
Pokitto 36:771321e70814 265 void fileRewind() {
Pokitto 36:771321e70814 266 res = pf_lseek(0);
Pokitto 36:771321e70814 267 }
Pokitto 36:771321e70814 268
Pokitto 36:771321e70814 269 void fileEnd() {
Pokitto 36:771321e70814 270 res = pf_lseek(fs.fsize);
Pokitto 36:771321e70814 271 }
Pokitto 36:771321e70814 272
Pokitto 36:771321e70814 273 long int fileGetPosition() {
Pokitto 36:771321e70814 274 return fs.fptr;
Pokitto 36:771321e70814 275 }
Pokitto 36:771321e70814 276
Pokitto 36:771321e70814 277 uint8_t filePeek(long n) {
Pokitto 36:771321e70814 278 pf_lseek(n);
Pokitto 36:771321e70814 279 return fileGetChar();
Pokitto 36:771321e70814 280 }
Pokitto 36:771321e70814 281
Pokitto 36:771321e70814 282 void filePoke(long n, uint8_t c) {
Pokitto 36:771321e70814 283 pf_lseek(n);
Pokitto 36:771321e70814 284 filePutChar(c);
Pokitto 36:771321e70814 285 }
Pokitto 36:771321e70814 286
Pokitto 36:771321e70814 287 int fileReadLine(char* destination, int maxchars) {
Pokitto 36:771321e70814 288 int n=0;
Pokitto 36:771321e70814 289 char c=1;
Pokitto 36:771321e70814 290 char linebuf[80];
Pokitto 36:771321e70814 291 fileReadBytes((uint8_t*)linebuf,80);
Pokitto 36:771321e70814 292 int index=0;
Pokitto 36:771321e70814 293 while (c!=NULL) {
Pokitto 36:771321e70814 294 c = linebuf[index++];
Pokitto 36:771321e70814 295 if (n == 0) {
Pokitto 36:771321e70814 296 while (c == '\n' || c == '\r') c = linebuf[index++]; // skip empty lines
Pokitto 36:771321e70814 297 }
Pokitto 36:771321e70814 298 n++;
Pokitto 36:771321e70814 299 if (c=='\n' || c=='\r' || n==maxchars-1) c=NULL; //prevent buffer overflow
Pokitto 36:771321e70814 300 *destination++ = c;
Pokitto 36:771321e70814 301 }
Pokitto 36:771321e70814 302 fileSeekRelative(-80+index); //rewind
Pokitto 36:771321e70814 303 return n; //number of characters read
Pokitto 36:771321e70814 304 }
Pokitto 36:771321e70814 305
Pokitto 36:771321e70814 306 int dirOpen() {
Pokitto 36:771321e70814 307 return pf_opendir(&dir,"");
Pokitto 36:771321e70814 308 }
Pokitto 36:771321e70814 309
Pokitto 36:771321e70814 310 int dirUp() {
Pokitto 36:771321e70814 311
Pokitto 36:771321e70814 312 return 0;
Pokitto 36:771321e70814 313 }
Pokitto 36:771321e70814 314 #endif // NOPETITFATFS
Pokitto 36:771321e70814 315 #endif // POK_ENABLE_SD
Pokitto 36:771321e70814 316
Pokitto 36:771321e70814 317
Pokitto 36:771321e70814 318