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:52:33 2019 +0000
Revision:
5:72408582aadf
Parent:
4:4d4890afa9cb
Can only encode config

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 4:4d4890afa9cb 35 for (int i = 0; i < message_length_i; i++) {
omatthews 4:4d4890afa9cb 36 i = i % sizeof(buffer_i);
omatthews 4:4d4890afa9cb 37 c = pc.getc();
omatthews 4:4d4890afa9cb 38 if (c != '#'){
omatthews 4:4d4890afa9cb 39 buffer_i[i] = c;
omatthews 4:4d4890afa9cb 40 }
omatthews 4:4d4890afa9cb 41 pc.putc(buffer_i[i]);
omatthews 1:5752892425a3 42 }
omatthews 1:5752892425a3 43 }
omatthews 0:abf096b1334e 44
omatthews 3:49faa91b696e 45 void write_message(){
omatthews 3:49faa91b696e 46 pc.printf("%d ",message_length_o);
omatthews 3:49faa91b696e 47 for (int i = 0; i < message_length_o; i++) {
omatthews 4:4d4890afa9cb 48 if (buffer_o[i] == NULL){
omatthews 4:4d4890afa9cb 49 pc.putc('#');
omatthews 4:4d4890afa9cb 50 }
omatthews 4:4d4890afa9cb 51 else{
omatthews 4:4d4890afa9cb 52 pc.putc(buffer_o[i]);
omatthews 4:4d4890afa9cb 53 }
omatthews 0:abf096b1334e 54 }
omatthews 3:49faa91b696e 55 }
omatthews 0:abf096b1334e 56
omatthews 0:abf096b1334e 57
omatthews 3:49faa91b696e 58 template <typename T>
omatthews 3:49faa91b696e 59 void decode_message(T * message_ptr, char type){
omatthews 0:abf096b1334e 60 // Create a stream that reads from the buffer.
omatthews 0:abf096b1334e 61
omatthews 3:49faa91b696e 62 pb_istream_t istream = pb_istream_from_buffer(buffer_i, message_length_i);
omatthews 0:abf096b1334e 63
omatthews 0:abf096b1334e 64 //Now we are ready to decode the message.
omatthews 3:49faa91b696e 65 if (type == 'c'){
omatthews 3:49faa91b696e 66 status = pb_decode(&istream, Configuration_fields, message_ptr);
omatthews 3:49faa91b696e 67 }
omatthews 3:49faa91b696e 68 else{
omatthews 3:49faa91b696e 69 status = pb_decode(&istream, SetPoint_fields, message_ptr);
omatthews 3:49faa91b696e 70 }
omatthews 0:abf096b1334e 71
omatthews 0:abf096b1334e 72 // Check for errors...
omatthews 0:abf096b1334e 73 if (!status)
omatthews 0:abf096b1334e 74 {
omatthews 0:abf096b1334e 75 pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
omatthews 0:abf096b1334e 76 }
omatthews 0:abf096b1334e 77 }
omatthews 0:abf096b1334e 78
omatthews 3:49faa91b696e 79 template <typename T>
omatthews 3:49faa91b696e 80 void encode_message(T * message_ptr, char type){
omatthews 3:49faa91b696e 81 pb_ostream_t ostream = pb_ostream_from_buffer(buffer_o, sizeof(buffer_o));
omatthews 3:49faa91b696e 82 if (type == 'c'){
omatthews 3:49faa91b696e 83 status = pb_encode(&ostream, Configuration_fields, message_ptr);
omatthews 3:49faa91b696e 84 }
omatthews 3:49faa91b696e 85 else{
omatthews 3:49faa91b696e 86 status = pb_encode(&ostream, SetPoint_fields, message_ptr);
omatthews 3:49faa91b696e 87 }
omatthews 3:49faa91b696e 88 message_length_o = ostream.bytes_written;
omatthews 3:49faa91b696e 89
omatthews 3:49faa91b696e 90 if (!status)
omatthews 3:49faa91b696e 91 {
omatthews 3:49faa91b696e 92 pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
omatthews 3:49faa91b696e 93 }
omatthews 3:49faa91b696e 94 }
omatthews 0:abf096b1334e 95
omatthews 0:abf096b1334e 96 // main() runs in its own thread in the OS
omatthews 0:abf096b1334e 97 int main()
omatthews 0:abf096b1334e 98 {
omatthews 0:abf096b1334e 99 /* This is the buffer where we will store our message. */
omatthews 0:abf096b1334e 100 pc.baud(115200);
omatthews 3:49faa91b696e 101 int n_points = sizeof(r_set_array)/sizeof(float);
omatthews 0:abf096b1334e 102
omatthews 3:49faa91b696e 103 SetPoint set_point_o[n_points];
omatthews 0:abf096b1334e 104
omatthews 3:49faa91b696e 105 //Set set points
omatthews 3:49faa91b696e 106
omatthews 3:49faa91b696e 107 config_o.n_set_points = n_points;
omatthews 3:49faa91b696e 108 encode_message<Configuration>(&config_o,'c');
omatthews 3:49faa91b696e 109 write_message();
omatthews 0:abf096b1334e 110
omatthews 3:49faa91b696e 111 for (int point = 0; point < n_points ; point++){
omatthews 3:49faa91b696e 112 set_point_o[point].r_set_point = r_set_array[point];
omatthews 3:49faa91b696e 113 set_point_o[point].time_point = time_array[point];
omatthews 3:49faa91b696e 114 set_point_o[point].trig_time = trig_array[point];
omatthews 3:49faa91b696e 115 encode_message<SetPoint>(&set_point_o[point],'s');
omatthews 3:49faa91b696e 116 write_message();
omatthews 3:49faa91b696e 117 }
omatthews 3:49faa91b696e 118
omatthews 0:abf096b1334e 119
omatthews 0:abf096b1334e 120
omatthews 0:abf096b1334e 121
omatthews 0:abf096b1334e 122 pc.getc();
omatthews 0:abf096b1334e 123 pc.printf("\n Input your set point:\n");
omatthews 3:49faa91b696e 124 read_message();
omatthews 4:4d4890afa9cb 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 4:4d4890afa9cb 132 for (i = 0; i < message_length_i; i++){
omatthews 4:4d4890afa9cb 133 pc.putc(buffer_i[i]);
omatthews 4:4d4890afa9cb 134 pc.putc(buffer_o[i]);
omatthews 4:4d4890afa9cb 135 if (buffer_i[i] == buffer_o[i]){pc.putc('y');}
omatthews 4:4d4890afa9cb 136 else {pc.putc('n');}
omatthews 4:4d4890afa9cb 137 pc.printf("\n");
omatthews 4:4d4890afa9cb 138 wait_us(50);
omatthews 4:4d4890afa9cb 139 }
omatthews 3:49faa91b696e 140 decode_message<SetPoint>(&set_point_i[i],'c');
omatthews 3:49faa91b696e 141 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 142 }
omatthews 3:49faa91b696e 143
omatthews 3:49faa91b696e 144
omatthews 3:49faa91b696e 145
omatthews 3:49faa91b696e 146 while(1);
omatthews 0:abf096b1334e 147 return 0;
omatthews 0:abf096b1334e 148 }
omatthews 0:abf096b1334e 149
omatthews 0:abf096b1334e 150