Declan Gordon / Mbed 2 deprecated LPC-SD-35

Dependencies:   SDFileSystem SoftSerial ds3231 mbed

Revision:
6:facc3b44ab4e
Parent:
5:159a0b7172c8
Child:
7:31cf052463a0
diff -r 159a0b7172c8 -r facc3b44ab4e main.cpp
--- a/main.cpp	Thu Dec 03 10:30:23 2015 +0000
+++ b/main.cpp	Tue Dec 08 22:54:10 2015 +0000
@@ -80,7 +80,8 @@
 int setPre0=0;
 int setM=0;
 int setC=0;
-
+int strN;
+int setSend=0;
 void serial_t0_RxInt()
 {
     while ((serial_t0.readable()) && (((rxSoftIn + 1) % buffer_size) != rxSoftOut)) {
@@ -153,6 +154,50 @@
 int msgCode;
 int msgValue;
 int lineN;
+char * pch;
+
+
+void SendSet(){
+    FILE *fr  = fopen("/sd/set.txt", "r");
+    if(fr == NULL) {
+        serial_t0.printf("Could not open file for read");
+    } else {
+        lineN=0;
+        if( fgets(rBuffer , 200 , fr) != NULL ) {
+            pch = strtok (softBuffer,",");
+            strN=0;
+            while (pch != NULL)
+            {
+                if(strN==1){setT0 = atoi(pch);  }
+                if(strN==2){setPre0 =atoi(pch); }
+                if(strN==3){setM=atoi(pch);  }
+                if(strN==4){setC =atoi(pch); }
+                pch = strtok (NULL, ",");
+                strN++;
+            }
+            softBuffer[0] = setT0/255; //LSB
+            softBuffer[1] = setT0%255; //MSB
+            softBuffer[2] = setPre0/255; //LSB
+            softBuffer[3] = setPre0%255; //MSB
+            softBuffer[4] = setM/255; //LSB
+            softBuffer[5] = setM%255; //MSB
+            softBuffer[6] = setC/255; //LSB
+            softBuffer[7] = setC%255; //MSB
+            softBuffer[8] = '\0';
+            fpga.putc(softBuffer[0]);
+            fpga.putc(softBuffer[1]);
+            fpga.putc(softBuffer[2]);
+            fpga.putc(softBuffer[3]);
+            fpga.putc(softBuffer[4]);
+            fpga.putc(softBuffer[5]);
+            fpga.putc(softBuffer[6]);
+            fpga.putc(softBuffer[7]);
+        }
+    }
+    fclose(fr);
+    
+}
+
 int main()
 {
     //Comment below to test without RTC and SD
@@ -168,7 +213,7 @@
         createFile=1;
     }
     fclose(fpo);
-    
+
     FILE *fp  = fopen("/sd/d.txt", "a");
     if(fp == NULL) {
         fpga.printf("file system failed\r\n");
@@ -231,7 +276,7 @@
             string str(softBuffer);
 
             if(softBuffer[0]=='S'&&softBuffer[1]=='D') {
-             FILE *fr  = fopen("/sd/d.txt", "r");
+                FILE *fr  = fopen("/sd/d.txt", "r");
                 if(fr == NULL) {
                     serial_t0.printf("Could not open file for read");
                 } else {
@@ -241,42 +286,43 @@
                             lineN++;
                         } else {
                             serial_t0.printf("%s",rBuffer);
-                            wait(0.040);
+                            //wait(0.040);
                         }
                     }
                 }
-            } else {
+            } else if(softBuffer[0]=='P'&&softBuffer[1]=='A') {
 
-                s1="";
-                s2="";
-                strings = strtok(softBuffer,",");
-                if(strings!=NULL) {
-                    s1 = strings;
-                    strings = strtok(NULL,",");
-                    if(strings!=NULL) {
-                        s2= strings;
-                        serial_t0.printf("Received code %s, value %s ",s1,s2);
-                        msgCode = atoi(s1.c_str());
-                        if(msgCode==1||msgCode==2||msgCode==3||msgCode==4) {
-                            msgValue = atoi(s2.c_str());
-                            softBuffer[0] = msgCode;
-                            softBuffer[1] = msgValue/255; //LSB
-                            softBuffer[2] = msgValue%255; //MSB
-                            softBuffer[3] = '\0';
-                            serial_t0.printf("Relayed datagram: %x %x %x",softBuffer[0],softBuffer[1],softBuffer[2]); //DEBUG
-                            //fpga.printf(softBuffer[0]);fpga.printf(softBuffer[1]);fpga.printf(softBuffer[3]);
-                            fpga.putc(softBuffer[0]);
-                            fpga.putc(softBuffer[1]);
-                            fpga.putc(softBuffer[2]);///sned the datagram to fpga
+                pch = strtok (softBuffer,",");
+                strN=0;
+                while (pch != NULL)
+                {
+                    if(strN==1){setT0 = atoi(pch);  }
+                    if(strN==2){setPre0 =atoi(pch); }
+                    if(strN==3){setM=atoi(pch);  }
+                    if(strN==4){setC =atoi(pch); }
+                    pch = strtok (NULL, ",");
+                    strN++;
+                }
+                    ///serial_t0.printf (" setT0: %d Pre0 %d M %d C %d  \n",setT0,setPre0,setM,setC);
+                    
+                FILE *fpo = fopen("/sd/set.txt", "w");
+                if(fpo != NULL) {
+                    fprintf(fpo,"%d,%d,%d,%d\n",setT0,setPre0,setM,setC);
+                }
+                fclose(fpo);
+                    
+                    SendSet();
+                    
+            }
 
-                        } else if(msgCode==5) {
-                            ///Parse date time write to rtc.
-                        }
-                    }
-                }
-            }
-            
+        }
+        
+        setSend++;
+        
+        if(setSend>60){
+            setSend=0;  
+            SendSet();
         }
         
     }
-}
+}
\ No newline at end of file