Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@1:fc463dd04a7f, 2018-07-30 (annotated)
- Committer:
- ironidio
- Date:
- Mon Jul 30 09:39:42 2018 +0000
- Revision:
- 1:fc463dd04a7f
- Parent:
- 0:b40deb141f99
- Child:
- 2:67223774f668
AcquiringSystem 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giuseppe_guida | 0:b40deb141f99 | 1 | #include "mbed.h" |
giuseppe_guida | 0:b40deb141f99 | 2 | #include <string> |
giuseppe_guida | 0:b40deb141f99 | 3 | #include <algorithm> |
giuseppe_guida | 0:b40deb141f99 | 4 | using namespace std; |
giuseppe_guida | 0:b40deb141f99 | 5 | |
giuseppe_guida | 0:b40deb141f99 | 6 | RawSerial pc(USBTX,USBRX); |
ironidio | 1:fc463dd04a7f | 7 | DigitalOut myled(LED2); |
ironidio | 1:fc463dd04a7f | 8 | //SPI spi(SPI_MOSI,SPI_MISO,SPI_SCK); |
ironidio | 1:fc463dd04a7f | 9 | //CAN3 can(spi,SPI_CS,PA_1); |
ironidio | 1:fc463dd04a7f | 10 | |
ironidio | 1:fc463dd04a7f | 11 | char data='A'; |
ironidio | 1:fc463dd04a7f | 12 | char vrx[1000]; //memorizzo i caratteri ricevuti dopo il comando di start '+' |
ironidio | 1:fc463dd04a7f | 13 | |
ironidio | 1:fc463dd04a7f | 14 | bool start=false; // true quando ho ricevuto il comando di start '+' |
ironidio | 1:fc463dd04a7f | 15 | bool stop=false; // true quando ho ricevuto il comando di stop '-' |
giuseppe_guida | 0:b40deb141f99 | 16 | |
ironidio | 1:fc463dd04a7f | 17 | bool canread=false; //true per abilitare la lettura da can |
ironidio | 1:fc463dd04a7f | 18 | |
ironidio | 1:fc463dd04a7f | 19 | int i = 0; |
ironidio | 1:fc463dd04a7f | 20 | int j = 0; |
ironidio | 1:fc463dd04a7f | 21 | int k = 0; |
ironidio | 1:fc463dd04a7f | 22 | int i_row = 0; |
ironidio | 1:fc463dd04a7f | 23 | const char* FromStrToChar; |
ironidio | 1:fc463dd04a7f | 24 | size_t n,pos; |
ironidio | 1:fc463dd04a7f | 25 | string delimiter = ","; |
ironidio | 1:fc463dd04a7f | 26 | string paramlist[10]; |
giuseppe_guida | 0:b40deb141f99 | 27 | |
giuseppe_guida | 0:b40deb141f99 | 28 | void rxCallback(){ |
ironidio | 1:fc463dd04a7f | 29 | data = char(pc.getc()); |
ironidio | 1:fc463dd04a7f | 30 | if (data == '+'){ |
ironidio | 1:fc463dd04a7f | 31 | myled = 1; |
ironidio | 1:fc463dd04a7f | 32 | i=0; |
ironidio | 1:fc463dd04a7f | 33 | j=0; |
ironidio | 1:fc463dd04a7f | 34 | start=true; |
ironidio | 1:fc463dd04a7f | 35 | }else if(data == '-'){ |
ironidio | 1:fc463dd04a7f | 36 | myled=0; |
ironidio | 1:fc463dd04a7f | 37 | stop=true; |
ironidio | 1:fc463dd04a7f | 38 | start=false; |
ironidio | 1:fc463dd04a7f | 39 | canread=false; //disabilito la lettura da CAN |
ironidio | 1:fc463dd04a7f | 40 | //pc.puts("123?"); |
ironidio | 1:fc463dd04a7f | 41 | }else{ |
ironidio | 1:fc463dd04a7f | 42 | if(start){ |
ironidio | 1:fc463dd04a7f | 43 | //quando ricevo il terminatore del messaggio '?' |
ironidio | 1:fc463dd04a7f | 44 | if(data == '?'){//quando ricevo il terminatore del messaggio '?' |
ironidio | 1:fc463dd04a7f | 45 | |
ironidio | 1:fc463dd04a7f | 46 | //converto il vettore di char in string |
ironidio | 1:fc463dd04a7f | 47 | string strrx(vrx); |
ironidio | 1:fc463dd04a7f | 48 | //contro il numero di id che sono stati ricevuti contando le ',' |
ironidio | 1:fc463dd04a7f | 49 | n = count(strrx.begin(),strrx.end(),','); |
ironidio | 1:fc463dd04a7f | 50 | |
ironidio | 1:fc463dd04a7f | 51 | pos = 0; |
ironidio | 1:fc463dd04a7f | 52 | |
ironidio | 1:fc463dd04a7f | 53 | //faccio il parsing della stringa, salvando i diversi Id ricevuti in un vettore di stringhe |
ironidio | 1:fc463dd04a7f | 54 | while((pos = strrx.find(',')) != string::npos){ |
ironidio | 1:fc463dd04a7f | 55 | paramlist[i_row] = strrx.substr(0,pos); |
ironidio | 1:fc463dd04a7f | 56 | //temp[j] = paramlist[i_row]; |
ironidio | 1:fc463dd04a7f | 57 | strrx.erase(0,pos+1); |
ironidio | 1:fc463dd04a7f | 58 | i_row++; |
ironidio | 1:fc463dd04a7f | 59 | } |
ironidio | 1:fc463dd04a7f | 60 | canread=true; //abilito la lettura da CAN |
ironidio | 1:fc463dd04a7f | 61 | |
ironidio | 1:fc463dd04a7f | 62 | }else { //altrimenti memorizzo il carattere ricevuto |
ironidio | 1:fc463dd04a7f | 63 | vrx[i]=data; |
ironidio | 1:fc463dd04a7f | 64 | i++; |
ironidio | 1:fc463dd04a7f | 65 | } |
ironidio | 1:fc463dd04a7f | 66 | |
ironidio | 1:fc463dd04a7f | 67 | |
ironidio | 1:fc463dd04a7f | 68 | } |
ironidio | 1:fc463dd04a7f | 69 | |
ironidio | 1:fc463dd04a7f | 70 | |
ironidio | 1:fc463dd04a7f | 71 | |
ironidio | 1:fc463dd04a7f | 72 | |
ironidio | 1:fc463dd04a7f | 73 | } |
giuseppe_guida | 0:b40deb141f99 | 74 | } |
giuseppe_guida | 0:b40deb141f99 | 75 | |
giuseppe_guida | 0:b40deb141f99 | 76 | int main() { |
giuseppe_guida | 0:b40deb141f99 | 77 | pc.attach(&rxCallback, RawSerial::RxIrq); |
giuseppe_guida | 0:b40deb141f99 | 78 | //TODO: trova un modo per uscire dal ciclo while della lettura CAN |
giuseppe_guida | 0:b40deb141f99 | 79 | while(true){ |
ironidio | 1:fc463dd04a7f | 80 | if(canread){ |
ironidio | 1:fc463dd04a7f | 81 | //ToDo: devo leggere da CAN |
ironidio | 1:fc463dd04a7f | 82 | }else if (stop){ |
ironidio | 1:fc463dd04a7f | 83 | //ToDo: devo mandare tutto quello che ho letto sulla seriale |
ironidio | 1:fc463dd04a7f | 84 | 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?"); |
ironidio | 1:fc463dd04a7f | 85 | stop=false; |
ironidio | 1:fc463dd04a7f | 86 | start=false; |
ironidio | 1:fc463dd04a7f | 87 | canread=false; |
ironidio | 1:fc463dd04a7f | 88 | } |
ironidio | 1:fc463dd04a7f | 89 | |
giuseppe_guida | 0:b40deb141f99 | 90 | } |
giuseppe_guida | 0:b40deb141f99 | 91 | } |