Trying to encode a configuration file and a set of instructions to be passed to a microcontroller
Dependencies: MODSERIAL Nanopb
main.cpp
- Committer:
- omatthews
- Date:
- 2019-08-18
- Revision:
- 0:abf096b1334e
- Child:
- 1:5752892425a3
File content as of revision 0:abf096b1334e:
#include "mbed.h" #include "pb.h" #include "pb_decode.h" #include "pb_encode.h" #include "LEX_Initialisation.pb.h" #include "MODSERIAL.h" 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; bool status; int i = 0; char c = 0; char cn = 0; char cnn = 0; volatile bool sent_flag = false; SetPoint set_point; SetPoint set_point_in; Thread thread; //indicator LEDs DigitalOut signal1(p26); //Green DigitalOut signal2(p25); //Red void incr(int i, int n = 1){ for (int k = 0; k < n; k++){ i++; i = i % sizeof(buffer); } } 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++) { c = pc.getc(); if (c == '#'){ i++; } else{ if (c == 'E'){ if ((cn = pc.getc()) == 'o'){ if ((cnn = pc.getc()) == 'T'){ break; } else { buffer_in[i+2] = cnn; buffer_in[i+1] = cn; buffer_in[i] = c; incr(i,3); } } else{ buffer_in[i+1] = cn; buffer_in[i] = c; incr(i,2); } } else{ buffer_in[i] = c; pc.putc(buffer_in[i]); incr(i); } } } } 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]); } } pc.printf("EoT"); } void decode_setpoint(){ // Create a stream that reads from the buffer. pb_istream_t istream = pb_istream_from_buffer(buffer_in, set_point_in_length); //Now we are ready to decode the message. status = pb_decode(&istream, SetPoint_fields, &set_point_in); // 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); } // main() runs in its own thread in the OS int main() { /* This is the buffer where we will store our message. */ pc.baud(115200); 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; status = pb_encode(&stream, SetPoint_fields, &set_point); set_point_length = stream.bytes_written; 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(); return 0; }