Ollie Matthews / Mbed OS nanopb_V2

Dependencies:   MODSERIAL Nanopb

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "pb.h"
00003 #include "pb_decode.h"
00004 #include "pb_encode.h"
00005 #include "LEX_Initialisation.pb.h"
00006 #include "MODSERIAL.h"
00007 
00008 DigitalOut led1(LED1);
00009 MODSERIAL pc(USBTX, USBRX, 512);
00010 uint8_t buffer_i[512];
00011 uint8_t buffer_o[512];
00012 size_t message_length_o;
00013 size_t message_length_i;
00014 bool status;
00015 int i = 0;
00016 char c = 0;
00017 char cn = 0;
00018 char cnn = 0;
00019 volatile bool sent_flag = false;
00020 Configuration config_o;
00021 Configuration config_i;
00022 
00023 
00024 //indicator LEDs
00025 DigitalOut signal1(p26);       //Green
00026 DigitalOut signal2(p25);         //Red
00027 
00028 //Parameters
00029 float r_set_array[5] = {0.5,0.53,0.55,0.56,0.5};
00030 int time_array[5] = {500,1000,1500,2500,3000};
00031 int trig_array[5] = {50,250,0,100,100};
00032     
00033 void read_message(){
00034     if (pc.scanf("%d",&message_length_i) < 0){pc.printf("Error in reading message length");}
00035     for (int i = 0; i < message_length_i; i++) {
00036         i = i % sizeof(buffer_i);
00037         c = pc.getc();
00038         if (c != '#'){
00039             buffer_i[i] = c;
00040             }  
00041         pc.putc(buffer_i[i]);
00042         }
00043     }      
00044 
00045 void write_message(){
00046     pc.printf("%d ",message_length_o);
00047     for (int i = 0; i < message_length_o; i++) {
00048         if (buffer_o[i] == NULL){
00049             pc.putc('#');
00050             }
00051         else{
00052             pc.putc(buffer_o[i]);
00053             }
00054     }
00055 }
00056        
00057        
00058 template <typename T>
00059 void decode_message(T * message_ptr, char type){
00060             // Create a stream that reads from the buffer. 
00061                 
00062                 pb_istream_t istream = pb_istream_from_buffer(buffer_i, message_length_i);
00063         
00064                 //Now we are ready to decode the message. 
00065                 if (type == 'c'){
00066                     status = pb_decode(&istream, Configuration_fields, message_ptr);
00067                 }
00068                 else{
00069                     status = pb_decode(&istream, SetPoint_fields, message_ptr);
00070                 }
00071         
00072                 // Check for errors... 
00073                 if (!status)
00074                 {
00075                     pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
00076                 }
00077             }
00078     
00079 template <typename T>
00080 void encode_message(T * message_ptr, char type){
00081     pb_ostream_t ostream = pb_ostream_from_buffer(buffer_o, sizeof(buffer_o));
00082     if (type == 'c'){
00083         status = pb_encode(&ostream, Configuration_fields, message_ptr);
00084     }
00085     else{
00086         status = pb_encode(&ostream, SetPoint_fields, message_ptr);
00087     }
00088     message_length_o = ostream.bytes_written;
00089     
00090     if (!status)
00091     {
00092         pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
00093     }
00094 }
00095 
00096 // main() runs in its own thread in the OS
00097 int main()
00098 {
00099     /* This is the buffer where we will store our message. */
00100     pc.baud(115200);
00101     int n_points = sizeof(r_set_array)/sizeof(float);
00102 
00103     SetPoint set_point_o[n_points];
00104     
00105     //Set set points  
00106 
00107     config_o.n_set_points = n_points;
00108     encode_message<Configuration>(&config_o,'c');
00109     write_message();
00110     
00111     for (int point = 0; point < n_points ; point++){
00112         set_point_o[point].r_set_point = r_set_array[point];
00113         set_point_o[point].time_point = time_array[point];
00114         set_point_o[point].trig_time = trig_array[point];
00115         encode_message<SetPoint>(&set_point_o[point],'s');
00116         write_message();
00117         }
00118        
00119 
00120     
00121     
00122     pc.getc();
00123     pc.printf("\n Input your set point:\n");
00124     read_message();
00125 
00126     decode_message<Configuration>(&config_i,'c');
00127     printf("Config decoded - there are %d points \n",config_i.n_set_points);
00128     
00129     SetPoint set_point_i[config_i.n_set_points];
00130     for (int point = 0; point < config_i.n_set_points; point++){
00131         read_message();
00132         for (i = 0; i < message_length_i; i++){
00133             pc.putc(buffer_i[i]);
00134             pc.putc(buffer_o[i]);
00135             if (buffer_i[i] == buffer_o[i]){pc.putc('y');}
00136             else {pc.putc('n');}
00137             pc.printf("\n");
00138             wait_us(50);
00139         }
00140         decode_message<SetPoint>(&set_point_i[i],'c');
00141         printf("Set point %d is %f,at %d, with a trigger at %d",i,set_point_i[i].r_set_point,set_point_i[i].time_point,set_point_i[i].trig_time);
00142     }
00143     
00144     
00145     
00146     while(1);
00147     return 0;
00148 }
00149 
00150