PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
spinal
Date:
Sun Oct 15 18:03:02 2017 +0000
Revision:
11:02ad9c807a21
Parent:
2:968589ca3484
fixed 4color refreshRegion code

Who changed what in which revision?

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