Trying to encode a configuration file and a set of instructions to be passed to a microcontroller

Dependencies:   MODSERIAL Nanopb

Revision:
3:49faa91b696e
Parent:
2:89156c267f7a
Child:
4:4d4890afa9cb
--- a/main.cpp	Sun Aug 18 19:14:59 2019 +0000
+++ b/main.cpp	Sun Aug 18 21:38:31 2019 +0000
@@ -7,10 +7,10 @@
 
 DigitalOut led1(LED1);
 MODSERIAL pc(USBTX, USBRX, 512);
-uint8_t buffer_in[128];
-uint8_t buffer[128];
-size_t set_point_length;
-size_t set_point_in_length;
+uint8_t buffer_i[512];
+uint8_t buffer_o[512];
+size_t message_length_o;
+size_t message_length_i;
 bool status;
 int i = 0;
 char c = 0;
@@ -19,10 +19,7 @@
 volatile bool sent_flag = false;
 Configuration config_o;
 Configuration config_i;
-SetPoint set_point;
-SetPoint set_point_in;
 
-Thread thread;
 
 //indicator LEDs
 DigitalOut signal1(p26);       //Green
@@ -33,89 +30,112 @@
 int time_array[5] = {500,1000,1500,2500,3000};
 int trig_array[5] = {50,250,0,100,100};
     
-void read_setpoint(){
-    if (pc.scanf("%d",&set_point_in_length) < 0){pc.printf("Error in reading message length");}
-    for (int i = 0; i < set_point_length; i++) {
-        i = i % sizeof(buffer_in);
-        c = pc.getc();
-        if (c == '#'){
-            }
-        else{
-            buffer_in[i] = c;
-            }  
-        pc.putc(buffer_in[i]);
+void read_message(){
+    if (pc.scanf("%d",&message_length_i) < 0){pc.printf("Error in reading message length");}
+    i = 0;
+    while (i < message_length_i) {
+        scanf("%d", &i);
+        scanf("%c", &buffer_i[i]);
+        i++;
+        wait_us(10);
         }
     }      
 
-void write_setpoint(){
-    pc.printf("%d ",set_point_length);
-    for (int i = 0; i < set_point_length; i++) {
-        if (buffer[i] == NULL){
-            pc.putc('#');
-            }
-        else{
-            pc.putc(buffer[i]);
-            }
+void write_message(){
+    pc.printf("%d ",message_length_o);
+    for (int i = 0; i < message_length_o; i++) {
+        printf("%d%c", i, buffer_o[i]);
+        wait_us(10);
     }
-    }
+}
        
        
-
-void decode_setpoint(){
+template <typename T>
+void decode_message(T * message_ptr, char type){
             // Create a stream that reads from the buffer. 
                 
-                pb_istream_t istream = pb_istream_from_buffer(buffer_in, set_point_in_length);
+                pb_istream_t istream = pb_istream_from_buffer(buffer_i, message_length_i);
         
                 //Now we are ready to decode the message. 
-                status = pb_decode(&istream, SetPoint_fields, &set_point_in);
+                if (type == 'c'){
+                    status = pb_decode(&istream, Configuration_fields, message_ptr);
+                }
+                else{
+                    status = pb_decode(&istream, SetPoint_fields, message_ptr);
+                }
         
                 // Check for errors... 
                 if (!status)
                 {
                     pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
                 }
-        
-                // Print the data contained in the message. 
-                pc.printf("Your set point was %f,%d,%d!\n", set_point_in.r_set_point,set_point_in.time_point,set_point_in.trig_time);
             }
     
-
-
+template <typename T>
+void encode_message(T * message_ptr, char type){
+    pb_ostream_t ostream = pb_ostream_from_buffer(buffer_o, sizeof(buffer_o));
+    if (type == 'c'){
+        status = pb_encode(&ostream, Configuration_fields, message_ptr);
+    }
+    else{
+        status = pb_encode(&ostream, SetPoint_fields, message_ptr);
+    }
+    message_length_o = ostream.bytes_written;
+    
+    if (!status)
+    {
+        pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
+    }
+}
 
 // main() runs in its own thread in the OS
 int main()
 {
     /* This is the buffer where we will store our message. */
     pc.baud(115200);
-    
-
+    int n_points = sizeof(r_set_array)/sizeof(float);
 
-    
-    
+    SetPoint set_point_o[n_points];
     
-    pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
-    
-    set_point.r_set_point = 0.5;
-    set_point.time_point = 100;
-    set_point.trig_time = 101;
+    //Set set points  
+
+    config_o.n_set_points = n_points;
+    encode_message<Configuration>(&config_o,'c');
+    write_message();
     
-    status = pb_encode(&stream, SetPoint_fields, &set_point);
-    set_point_length = stream.bytes_written;
+    for (int point = 0; point < n_points ; point++){
+        set_point_o[point].r_set_point = r_set_array[point];
+        set_point_o[point].time_point = time_array[point];
+        set_point_o[point].trig_time = trig_array[point];
+        encode_message<SetPoint>(&set_point_o[point],'s');
+        write_message();
+        }
+       
 
     
-    if (!status)
-    {
-        pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
-        return 1;
-    }
-    
-    write_setpoint();
-    
     
     pc.getc();
     pc.printf("\n Input your set point:\n");
-    read_setpoint();
-    decode_setpoint();
+    read_message();
+    /*for (i = 0; i < message_length_i; i++){
+        pc.putc(buffer_i[i]);
+        pc.putc(buffer_o[i]);
+        if (buffer_i[i] == buffer_o[i]){pc.putc('y');}
+        else {pc.putc('n');}
+        }*/
+    decode_message<Configuration>(&config_i,'c');
+    printf("Config decoded - there are %d points \n",config_i.n_set_points);
+    
+    SetPoint set_point_i[config_i.n_set_points];
+    for (int point = 0; point < config_i.n_set_points; point++){
+        read_message();
+        decode_message<SetPoint>(&set_point_i[i],'c');
+        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);
+    }
+    
+    
+    
+    while(1);
     return 0;
 }