nulla

Dependencies:   mbed mcp2515

Revision:
2:67223774f668
Parent:
1:fc463dd04a7f
--- a/main.cpp	Mon Jul 30 09:39:42 2018 +0000
+++ b/main.cpp	Mon Jul 30 12:44:42 2018 +0000
@@ -1,12 +1,20 @@
 #include "mbed.h"
+#include "CAN3.h"
+#include "mcp2515.h"
 #include <string>
+#include <sstream>
 #include <algorithm>
+#define DIM_STORE 6000
+#define DIM_PARAMS 10
 using namespace std;
 
 RawSerial pc(USBTX,USBRX);
 DigitalOut myled(LED2);
-//SPI spi(SPI_MOSI,SPI_MISO,SPI_SCK);
-//CAN3 can(spi,SPI_CS,PA_1);
+SPI spi(SPI_MOSI,SPI_MISO,SPI_SCK);
+CAN3 can(spi,SPI_CS,PA_1);
+
+
+CANMessage msg1;
 
 char data='A';
 char vrx[1000]; //memorizzo i caratteri ricevuti dopo il comando di start '+'
@@ -19,11 +27,25 @@
 int i = 0;
 int j = 0;
 int k = 0;
+int index_data;
 int i_row = 0;
 const char* FromStrToChar;
 size_t n,pos;
 string delimiter = ",";
-string paramlist[10];
+string paramlist[DIM_PARAMS];
+string StoreData[DIM_STORE]; 
+
+void clear_StoreData(){
+    int it;
+    for(it = 0; it < DIM_STORE; it++)
+        StoreData[it].clear();    
+}
+
+void clear_paramlist(){
+    int it;
+    for(it = 0; it < DIM_PARAMS; it++)
+        paramlist[it].clear();    
+}
 
 void rxCallback(){
     data = char(pc.getc());
@@ -32,6 +54,8 @@
             i=0;
             j=0;
             start=true;
+            clear_paramlist();
+            clear_StoreData();
     }else if(data == '-'){
             myled=0;
             stop=true;
@@ -53,12 +77,11 @@
                         //faccio il parsing della stringa, salvando i diversi Id ricevuti in un vettore di stringhe
                         while((pos = strrx.find(',')) != string::npos){
                                 paramlist[i_row] = strrx.substr(0,pos);
-                                //temp[j] = paramlist[i_row];
                                 strrx.erase(0,pos+1);
                                 i_row++;         
                           }
                           canread=true; //abilito la lettura da CAN
-                    
+                          index_data = 0;
                     }else { //altrimenti memorizzo il carattere ricevuto
                             vrx[i]=data;
                             i++;
@@ -74,18 +97,36 @@
 }
 
 int main() {
+    int index;
+    can.frequency(125000);
     pc.attach(&rxCallback, RawSerial::RxIrq);
-    //TODO: trova un modo per uscire dal ciclo while della lettura CAN
     while(true){
         if(canread){
             //ToDo: devo leggere da CAN
+            if(can.read(&msg1)){
+                for(index = 0; index < i_row-1; index++){
+                    if(msg1.id == atoi(paramlist[index].c_str())){
+                         string tmpData = string(msg1.data, find(msg1.data, msg1.data + 8, '\0'));
+                         stringstream ss;
+                         ss << msg1.id;
+                         StoreData[index_data] = ss.str()+","+tmpData;
+                         index_data++;                                          
+                    }
+            }
             }else if (stop){
                 //ToDo: devo mandare tutto quello che ho letto sulla seriale
-                pc.puts("101,20|101,20|101,20|101,20|101,20|259,1|101,20|101,20|101,20|101,20|101,20|259,3.0?");
+                //pc.puts("101,20|101,20|101,20|101,20|101,20|259,1|101,20|101,20|101,20|101,20|101,20|259,3.0?");
+                for(int z = 0; z < index_data-2; z++){
+                    pc.puts(StoreData[z].c_str()+'|');
+                    //pc.putc('|');       
+                }
+                pc.puts(StoreData[index_data-1].c_str()+'?');
+                
                 stop=false;
                 start=false;
                 canread=false;
                 }
             
         }
+    }
 }