Fabio Fumi / Mbed 2 deprecated Sharp_ce140f_emul

Dependencies:   mbed SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers commands.cpp Source File

commands.cpp

00001 #include "commands.h"
00002 #include "SDFileSystem.h"
00003 
00004 volatile char     inDataBuf[BUF_SIZE];
00005 volatile char     outDataBuf[BUF_SIZE];
00006 volatile uint16_t inBufPosition;
00007 volatile uint16_t outBufPosition;
00008 volatile uint8_t  checksum = 0;
00009 
00010 extern void debug_log(const char *fmt, ...);
00011 extern RawSerial         pc;
00012 
00013 int fileCount;
00014 
00015 // SD Card
00016 SDFileSystem sd(PB_5, PB_4, PB_3, PA_10, "sd"); // mosi, miso, sclk, cs
00017 
00018 
00019 char CheckSum(char b) {
00020     checksum = (checksum + b) & 0xff;
00021     return b;
00022 }
00023 
00024 void outDataAppend(char b) {
00025     // NB in case we're going to implement a circular output buffer...
00026     //    I expect functions appending data to be faster than the
00027     //    consumer (outNibbleSend, increasing outDataPointer every about 10 ms).
00028     //    We should wait here, when outBufPosition reaches outDataPointer,
00029     //    for outDataPointer to increase again
00030     outDataBuf[ outBufPosition++ ] = b;
00031 }
00032 
00033 void sendString(char* s) {
00034     for (int i=0;i<strlen(s);i++) {
00035         outDataAppend(CheckSum(s[i]));
00036     }
00037 }
00038 
00039 /*
00040 QString cleanFileName(QString s) {
00041     QString r = "X:";
00042     r = r + s.left(s.indexOf(".")).leftJustified(8,' ',true) + s.mid(s.indexOf(".")).rightJustified(4,' ',true);
00043     return r;
00044 }
00045 */
00046 
00047 
00048 void process_FILES_LIST(int cmd) {
00049     // QString fname;
00050     
00051     pc.putc('l');
00052     pc.putc(0x30+cmd);
00053     debug_log ("FILES_LIST %d\n", cmd);
00054     outDataAppend(0x00);
00055     checksum=0;
00056 
00057     switch (cmd) {
00058     case 0:
00059             fileCount++;
00060             //fname = fileList.at(fileCount);
00061             //debug_log ("*"+cleanFileName(fname)+"*\n");
00062             // Send (cleaned-up) filenames
00063             //sendString(cleanFileName(fname));
00064             sendString("X:TEST    .BAS ");
00065             sendString(" ");
00066             // Send CheckSum
00067             outDataAppend(checksum);
00068             break;
00069 
00070     case 1:
00071             fileCount--;
00072             //fname = fileList.at(fileCount);
00073             //debug_log ("*"+cleanFileName(fname)+"*\n");
00074             // Send filenames
00075             //sendString(cleanFileName(fname));
00076             sendString("X:TEST    .BAS ");
00077             sendString(" ");
00078             // Send CheckSum
00079             outDataAppend(checksum);
00080             break;
00081     }
00082 }
00083 
00084 
00085 void process_FILES(void) {
00086     
00087     struct dirent* ent;
00088     DIR *dir;
00089     char n_files = 0x00;
00090     char *ext_pos;
00091     
00092     debug_log ( "FILES\n" ); 
00093     pc.putc('f');
00094     outDataAppend(CheckSum(0x00));
00095     // Send nb files, from specified dir (with wildcards)
00096     /* 
00097     QString s ="";
00098     for (int i =3;i< 15;i++) {
00099         s.append(QChar(data.at(i)));
00100     }
00101     //    s="*.BAS"; 
00102     fileList = directory.entryList( QStringList() << s.replace(" ",""),QDir::Files);
00103     outDataAppend(CheckSum(fileList.size()));
00104     fileCount = -1;
00105     */
00106     if ((dir = opendir ("/sd/")) != NULL) { // ignore files other than BASIC
00107         // print all the files and directories within directory
00108         while ((ent = readdir (dir)) != NULL
00109             && n_files < 0xFF ) { // max 255 files
00110             debug_log("<%s>\n", ent->d_name);
00111             // ignore files other than BASIC (for the timebeing...)
00112             if ( (ext_pos = strstr (ent->d_name, ".BAS")) != 0 ) {
00113                 n_files++; 
00114             }
00115         }
00116         closedir (dir);
00117         fileCount = -1;
00118         debug_log("%d files\n", n_files);
00119         outDataAppend(CheckSum(n_files));
00120         outDataAppend(checksum);
00121         
00122         // process_FILES_LIST ( 0x00 ); // append first file name by default ?
00123         
00124     } else {
00125         // could not open directory
00126         pc.putc('x');
00127         pc.printf("Could not open SD directory\n");
00128     }
00129 }
00130 
00131 
00132 void ProcessCommand ( void ) {
00133 
00134     checksum=0;
00135     outBufPosition = 0;
00136 
00137     switch (inDataBuf[0]) {
00138         
00139     case 0x05: process_FILES();break;
00140     case 0x06: process_FILES_LIST(0);break;
00141     case 0x07: process_FILES_LIST(1);break;
00142             
00143     case 0x1D:
00144         // case 0x1D: process_DSKF();
00145         debug_log ( "DSKF\n" ); 
00146         outDataAppend(CheckSum(0x00));
00147         outDataAppend(CheckSum(0x02));  // number of byte
00148         outDataAppend(CheckSum(0x50));  // number of 256Bytes free sectors
00149         outDataAppend(CheckSum(0x00));
00150         outDataAppend(0x52);  // don't know yet. Perhaps a checksum ?     
00151         break;
00152     
00153     default:
00154         debug_log ( "Unsupported command (yet...)" ); 
00155         break;
00156     }
00157         
00158 }