![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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:
- 2:89156c267f7a
- Parent:
- 1:5752892425a3
- Child:
- 3:49faa91b696e
File content as of revision 2:89156c267f7a:
#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; Configuration config_o; Configuration config_i; SetPoint set_point; SetPoint set_point_in; Thread thread; //indicator LEDs DigitalOut signal1(p26); //Green DigitalOut signal2(p25); //Red //Parameters float r_set_array[5] = {0.5,0.53,0.55,0.56,0.5}; 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 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 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; }