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

Dependencies:   MODSERIAL Nanopb

Committer:
omatthews
Date:
Sun Aug 18 21:38:31 2019 +0000
Revision:
3:49faa91b696e
Parent:
2:89156c267f7a
Child:
4:4d4890afa9cb
Trying to get rid of #

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 0:abf096b1334e 1 #include "mbed.h"
omatthews 0:abf096b1334e 2 #include "pb.h"
omatthews 0:abf096b1334e 3 #include "pb_decode.h"
omatthews 0:abf096b1334e 4 #include "pb_encode.h"
omatthews 0:abf096b1334e 5 #include "LEX_Initialisation.pb.h"
omatthews 0:abf096b1334e 6 #include "MODSERIAL.h"
omatthews 0:abf096b1334e 7
omatthews 0:abf096b1334e 8 DigitalOut led1(LED1);
omatthews 0:abf096b1334e 9 MODSERIAL pc(USBTX, USBRX, 512);
omatthews 3:49faa91b696e 10 uint8_t buffer_i[512];
omatthews 3:49faa91b696e 11 uint8_t buffer_o[512];
omatthews 3:49faa91b696e 12 size_t message_length_o;
omatthews 3:49faa91b696e 13 size_t message_length_i;
omatthews 0:abf096b1334e 14 bool status;
omatthews 0:abf096b1334e 15 int i = 0;
omatthews 0:abf096b1334e 16 char c = 0;
omatthews 0:abf096b1334e 17 char cn = 0;
omatthews 0:abf096b1334e 18 char cnn = 0;
omatthews 0:abf096b1334e 19 volatile bool sent_flag = false;
omatthews 2:89156c267f7a 20 Configuration config_o;
omatthews 2:89156c267f7a 21 Configuration config_i;
omatthews 0:abf096b1334e 22
omatthews 0:abf096b1334e 23
omatthews 0:abf096b1334e 24 //indicator LEDs
omatthews 0:abf096b1334e 25 DigitalOut signal1(p26); //Green
omatthews 0:abf096b1334e 26 DigitalOut signal2(p25); //Red
omatthews 0:abf096b1334e 27
omatthews 2:89156c267f7a 28 //Parameters
omatthews 2:89156c267f7a 29 float r_set_array[5] = {0.5,0.53,0.55,0.56,0.5};
omatthews 2:89156c267f7a 30 int time_array[5] = {500,1000,1500,2500,3000};
omatthews 2:89156c267f7a 31 int trig_array[5] = {50,250,0,100,100};
omatthews 0:abf096b1334e 32
omatthews 3:49faa91b696e 33 void read_message(){
omatthews 3:49faa91b696e 34 if (pc.scanf("%d",&message_length_i) < 0){pc.printf("Error in reading message length");}
omatthews 3:49faa91b696e 35 i = 0;
omatthews 3:49faa91b696e 36 while (i < message_length_i) {
omatthews 3:49faa91b696e 37 scanf("%d", &i);
omatthews 3:49faa91b696e 38 scanf("%c", &buffer_i[i]);
omatthews 3:49faa91b696e 39 i++;
omatthews 3:49faa91b696e 40 wait_us(10);
omatthews 1:5752892425a3 41 }
omatthews 1:5752892425a3 42 }
omatthews 0:abf096b1334e 43
omatthews 3:49faa91b696e 44 void write_message(){
omatthews 3:49faa91b696e 45 pc.printf("%d ",message_length_o);
omatthews 3:49faa91b696e 46 for (int i = 0; i < message_length_o; i++) {
omatthews 3:49faa91b696e 47 printf("%d%c", i, buffer_o[i]);
omatthews 3:49faa91b696e 48 wait_us(10);
omatthews 0:abf096b1334e 49 }
omatthews 3:49faa91b696e 50 }
omatthews 0:abf096b1334e 51
omatthews 0:abf096b1334e 52
omatthews 3:49faa91b696e 53 template <typename T>
omatthews 3:49faa91b696e 54 void decode_message(T * message_ptr, char type){
omatthews 0:abf096b1334e 55 // Create a stream that reads from the buffer.
omatthews 0:abf096b1334e 56
omatthews 3:49faa91b696e 57 pb_istream_t istream = pb_istream_from_buffer(buffer_i, message_length_i);
omatthews 0:abf096b1334e 58
omatthews 0:abf096b1334e 59 //Now we are ready to decode the message.
omatthews 3:49faa91b696e 60 if (type == 'c'){
omatthews 3:49faa91b696e 61 status = pb_decode(&istream, Configuration_fields, message_ptr);
omatthews 3:49faa91b696e 62 }
omatthews 3:49faa91b696e 63 else{
omatthews 3:49faa91b696e 64 status = pb_decode(&istream, SetPoint_fields, message_ptr);
omatthews 3:49faa91b696e 65 }
omatthews 0:abf096b1334e 66
omatthews 0:abf096b1334e 67 // Check for errors...
omatthews 0:abf096b1334e 68 if (!status)
omatthews 0:abf096b1334e 69 {
omatthews 0:abf096b1334e 70 pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
omatthews 0:abf096b1334e 71 }
omatthews 0:abf096b1334e 72 }
omatthews 0:abf096b1334e 73
omatthews 3:49faa91b696e 74 template <typename T>
omatthews 3:49faa91b696e 75 void encode_message(T * message_ptr, char type){
omatthews 3:49faa91b696e 76 pb_ostream_t ostream = pb_ostream_from_buffer(buffer_o, sizeof(buffer_o));
omatthews 3:49faa91b696e 77 if (type == 'c'){
omatthews 3:49faa91b696e 78 status = pb_encode(&ostream, Configuration_fields, message_ptr);
omatthews 3:49faa91b696e 79 }
omatthews 3:49faa91b696e 80 else{
omatthews 3:49faa91b696e 81 status = pb_encode(&ostream, SetPoint_fields, message_ptr);
omatthews 3:49faa91b696e 82 }
omatthews 3:49faa91b696e 83 message_length_o = ostream.bytes_written;
omatthews 3:49faa91b696e 84
omatthews 3:49faa91b696e 85 if (!status)
omatthews 3:49faa91b696e 86 {
omatthews 3:49faa91b696e 87 pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
omatthews 3:49faa91b696e 88 }
omatthews 3:49faa91b696e 89 }
omatthews 0:abf096b1334e 90
omatthews 0:abf096b1334e 91 // main() runs in its own thread in the OS
omatthews 0:abf096b1334e 92 int main()
omatthews 0:abf096b1334e 93 {
omatthews 0:abf096b1334e 94 /* This is the buffer where we will store our message. */
omatthews 0:abf096b1334e 95 pc.baud(115200);
omatthews 3:49faa91b696e 96 int n_points = sizeof(r_set_array)/sizeof(float);
omatthews 0:abf096b1334e 97
omatthews 3:49faa91b696e 98 SetPoint set_point_o[n_points];
omatthews 0:abf096b1334e 99
omatthews 3:49faa91b696e 100 //Set set points
omatthews 3:49faa91b696e 101
omatthews 3:49faa91b696e 102 config_o.n_set_points = n_points;
omatthews 3:49faa91b696e 103 encode_message<Configuration>(&config_o,'c');
omatthews 3:49faa91b696e 104 write_message();
omatthews 0:abf096b1334e 105
omatthews 3:49faa91b696e 106 for (int point = 0; point < n_points ; point++){
omatthews 3:49faa91b696e 107 set_point_o[point].r_set_point = r_set_array[point];
omatthews 3:49faa91b696e 108 set_point_o[point].time_point = time_array[point];
omatthews 3:49faa91b696e 109 set_point_o[point].trig_time = trig_array[point];
omatthews 3:49faa91b696e 110 encode_message<SetPoint>(&set_point_o[point],'s');
omatthews 3:49faa91b696e 111 write_message();
omatthews 3:49faa91b696e 112 }
omatthews 3:49faa91b696e 113
omatthews 0:abf096b1334e 114
omatthews 0:abf096b1334e 115
omatthews 0:abf096b1334e 116
omatthews 0:abf096b1334e 117 pc.getc();
omatthews 0:abf096b1334e 118 pc.printf("\n Input your set point:\n");
omatthews 3:49faa91b696e 119 read_message();
omatthews 3:49faa91b696e 120 /*for (i = 0; i < message_length_i; i++){
omatthews 3:49faa91b696e 121 pc.putc(buffer_i[i]);
omatthews 3:49faa91b696e 122 pc.putc(buffer_o[i]);
omatthews 3:49faa91b696e 123 if (buffer_i[i] == buffer_o[i]){pc.putc('y');}
omatthews 3:49faa91b696e 124 else {pc.putc('n');}
omatthews 3:49faa91b696e 125 }*/
omatthews 3:49faa91b696e 126 decode_message<Configuration>(&config_i,'c');
omatthews 3:49faa91b696e 127 printf("Config decoded - there are %d points \n",config_i.n_set_points);
omatthews 3:49faa91b696e 128
omatthews 3:49faa91b696e 129 SetPoint set_point_i[config_i.n_set_points];
omatthews 3:49faa91b696e 130 for (int point = 0; point < config_i.n_set_points; point++){
omatthews 3:49faa91b696e 131 read_message();
omatthews 3:49faa91b696e 132 decode_message<SetPoint>(&set_point_i[i],'c');
omatthews 3:49faa91b696e 133 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);
omatthews 3:49faa91b696e 134 }
omatthews 3:49faa91b696e 135
omatthews 3:49faa91b696e 136
omatthews 3:49faa91b696e 137
omatthews 3:49faa91b696e 138 while(1);
omatthews 0:abf096b1334e 139 return 0;
omatthews 0:abf096b1334e 140 }
omatthews 0:abf096b1334e 141
omatthews 0:abf096b1334e 142