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 18:53:32 2019 +0000
Revision:
0:abf096b1334e
Child:
1:5752892425a3
With EoT working

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 0:abf096b1334e 10 uint8_t buffer_in[128];
omatthews 0:abf096b1334e 11 uint8_t buffer[128];
omatthews 0:abf096b1334e 12 size_t set_point_length;
omatthews 0:abf096b1334e 13 size_t set_point_in_length;
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 0:abf096b1334e 20 SetPoint set_point;
omatthews 0:abf096b1334e 21 SetPoint set_point_in;
omatthews 0:abf096b1334e 22
omatthews 0:abf096b1334e 23 Thread thread;
omatthews 0:abf096b1334e 24
omatthews 0:abf096b1334e 25 //indicator LEDs
omatthews 0:abf096b1334e 26 DigitalOut signal1(p26); //Green
omatthews 0:abf096b1334e 27 DigitalOut signal2(p25); //Red
omatthews 0:abf096b1334e 28
omatthews 0:abf096b1334e 29
omatthews 0:abf096b1334e 30 void incr(int i, int n = 1){
omatthews 0:abf096b1334e 31 for (int k = 0; k < n; k++){
omatthews 0:abf096b1334e 32 i++;
omatthews 0:abf096b1334e 33 i = i % sizeof(buffer);
omatthews 0:abf096b1334e 34 }
omatthews 0:abf096b1334e 35 }
omatthews 0:abf096b1334e 36
omatthews 0:abf096b1334e 37 void read_setpoint(){
omatthews 0:abf096b1334e 38 if (pc.scanf("%d",&set_point_in_length) < 0){pc.printf("Error in reading message length");}
omatthews 0:abf096b1334e 39 for (int i = 0; i < set_point_length; i++) {
omatthews 0:abf096b1334e 40 c = pc.getc();
omatthews 0:abf096b1334e 41 if (c == '#'){
omatthews 0:abf096b1334e 42 i++;
omatthews 0:abf096b1334e 43 }
omatthews 0:abf096b1334e 44 else{
omatthews 0:abf096b1334e 45 if (c == 'E'){
omatthews 0:abf096b1334e 46 if ((cn = pc.getc()) == 'o'){
omatthews 0:abf096b1334e 47 if ((cnn = pc.getc()) == 'T'){
omatthews 0:abf096b1334e 48 break;
omatthews 0:abf096b1334e 49 }
omatthews 0:abf096b1334e 50 else {
omatthews 0:abf096b1334e 51 buffer_in[i+2] = cnn;
omatthews 0:abf096b1334e 52 buffer_in[i+1] = cn;
omatthews 0:abf096b1334e 53 buffer_in[i] = c;
omatthews 0:abf096b1334e 54 incr(i,3);
omatthews 0:abf096b1334e 55 }
omatthews 0:abf096b1334e 56 }
omatthews 0:abf096b1334e 57 else{
omatthews 0:abf096b1334e 58 buffer_in[i+1] = cn;
omatthews 0:abf096b1334e 59 buffer_in[i] = c;
omatthews 0:abf096b1334e 60 incr(i,2);
omatthews 0:abf096b1334e 61 }
omatthews 0:abf096b1334e 62 }
omatthews 0:abf096b1334e 63 else{
omatthews 0:abf096b1334e 64 buffer_in[i] = c;
omatthews 0:abf096b1334e 65 pc.putc(buffer_in[i]);
omatthews 0:abf096b1334e 66 incr(i);
omatthews 0:abf096b1334e 67 }
omatthews 0:abf096b1334e 68 }
omatthews 0:abf096b1334e 69 }
omatthews 0:abf096b1334e 70 }
omatthews 0:abf096b1334e 71
omatthews 0:abf096b1334e 72 void write_setpoint(){
omatthews 0:abf096b1334e 73 pc.printf("%d ",set_point_length);
omatthews 0:abf096b1334e 74 for (int i = 0; i < set_point_length; i++) {
omatthews 0:abf096b1334e 75 if (buffer[i] == NULL){
omatthews 0:abf096b1334e 76 pc.putc('#');
omatthews 0:abf096b1334e 77 }
omatthews 0:abf096b1334e 78 else{
omatthews 0:abf096b1334e 79 pc.putc(buffer[i]);
omatthews 0:abf096b1334e 80 }
omatthews 0:abf096b1334e 81 }
omatthews 0:abf096b1334e 82 pc.printf("EoT");
omatthews 0:abf096b1334e 83 }
omatthews 0:abf096b1334e 84
omatthews 0:abf096b1334e 85
omatthews 0:abf096b1334e 86
omatthews 0:abf096b1334e 87 void decode_setpoint(){
omatthews 0:abf096b1334e 88 // Create a stream that reads from the buffer.
omatthews 0:abf096b1334e 89
omatthews 0:abf096b1334e 90 pb_istream_t istream = pb_istream_from_buffer(buffer_in, set_point_in_length);
omatthews 0:abf096b1334e 91
omatthews 0:abf096b1334e 92 //Now we are ready to decode the message.
omatthews 0:abf096b1334e 93 status = pb_decode(&istream, SetPoint_fields, &set_point_in);
omatthews 0:abf096b1334e 94
omatthews 0:abf096b1334e 95 // Check for errors...
omatthews 0:abf096b1334e 96 if (!status)
omatthews 0:abf096b1334e 97 {
omatthews 0:abf096b1334e 98 pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
omatthews 0:abf096b1334e 99 }
omatthews 0:abf096b1334e 100
omatthews 0:abf096b1334e 101 // Print the data contained in the message.
omatthews 0:abf096b1334e 102 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);
omatthews 0:abf096b1334e 103 }
omatthews 0:abf096b1334e 104
omatthews 0:abf096b1334e 105
omatthews 0:abf096b1334e 106
omatthews 0:abf096b1334e 107
omatthews 0:abf096b1334e 108 // main() runs in its own thread in the OS
omatthews 0:abf096b1334e 109 int main()
omatthews 0:abf096b1334e 110 {
omatthews 0:abf096b1334e 111 /* This is the buffer where we will store our message. */
omatthews 0:abf096b1334e 112 pc.baud(115200);
omatthews 0:abf096b1334e 113
omatthews 0:abf096b1334e 114
omatthews 0:abf096b1334e 115
omatthews 0:abf096b1334e 116
omatthews 0:abf096b1334e 117
omatthews 0:abf096b1334e 118
omatthews 0:abf096b1334e 119 pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
omatthews 0:abf096b1334e 120
omatthews 0:abf096b1334e 121 set_point.r_set_point = 0.5;
omatthews 0:abf096b1334e 122 set_point.time_point = 100;
omatthews 0:abf096b1334e 123 set_point.trig_time = 101;
omatthews 0:abf096b1334e 124
omatthews 0:abf096b1334e 125 status = pb_encode(&stream, SetPoint_fields, &set_point);
omatthews 0:abf096b1334e 126 set_point_length = stream.bytes_written;
omatthews 0:abf096b1334e 127
omatthews 0:abf096b1334e 128
omatthews 0:abf096b1334e 129 if (!status)
omatthews 0:abf096b1334e 130 {
omatthews 0:abf096b1334e 131 pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
omatthews 0:abf096b1334e 132 return 1;
omatthews 0:abf096b1334e 133 }
omatthews 0:abf096b1334e 134
omatthews 0:abf096b1334e 135 write_setpoint();
omatthews 0:abf096b1334e 136
omatthews 0:abf096b1334e 137
omatthews 0:abf096b1334e 138 pc.getc();
omatthews 0:abf096b1334e 139 pc.printf("\n Input your set point:\n");
omatthews 0:abf096b1334e 140 read_setpoint();
omatthews 0:abf096b1334e 141 decode_setpoint();
omatthews 0:abf096b1334e 142
omatthews 0:abf096b1334e 143
omatthews 0:abf096b1334e 144 return 0;
omatthews 0:abf096b1334e 145 }
omatthews 0:abf096b1334e 146
omatthews 0:abf096b1334e 147