Trying to encode a configuration file and a set of instructions to be passed to a microcontroller
Dependencies: MODSERIAL Nanopb
main.cpp@2:89156c267f7a, 2019-08-18 (annotated)
- Committer:
- omatthews
- Date:
- Sun Aug 18 19:14:59 2019 +0000
- Revision:
- 2:89156c267f7a
- Parent:
- 1:5752892425a3
- Child:
- 3:49faa91b696e
New proto;
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:89156c267f7a | 20 | Configuration config_o; |
omatthews | 2:89156c267f7a | 21 | Configuration config_i; |
omatthews | 0:abf096b1334e | 22 | SetPoint set_point; |
omatthews | 0:abf096b1334e | 23 | SetPoint set_point_in; |
omatthews | 0:abf096b1334e | 24 | |
omatthews | 0:abf096b1334e | 25 | Thread thread; |
omatthews | 0:abf096b1334e | 26 | |
omatthews | 0:abf096b1334e | 27 | //indicator LEDs |
omatthews | 0:abf096b1334e | 28 | DigitalOut signal1(p26); //Green |
omatthews | 0:abf096b1334e | 29 | DigitalOut signal2(p25); //Red |
omatthews | 0:abf096b1334e | 30 | |
omatthews | 2:89156c267f7a | 31 | //Parameters |
omatthews | 2:89156c267f7a | 32 | float r_set_array[5] = {0.5,0.53,0.55,0.56,0.5}; |
omatthews | 2:89156c267f7a | 33 | int time_array[5] = {500,1000,1500,2500,3000}; |
omatthews | 2:89156c267f7a | 34 | int trig_array[5] = {50,250,0,100,100}; |
omatthews | 0:abf096b1334e | 35 | |
omatthews | 0:abf096b1334e | 36 | void read_setpoint(){ |
omatthews | 0:abf096b1334e | 37 | if (pc.scanf("%d",&set_point_in_length) < 0){pc.printf("Error in reading message length");} |
omatthews | 0:abf096b1334e | 38 | for (int i = 0; i < set_point_length; i++) { |
omatthews | 1:5752892425a3 | 39 | i = i % sizeof(buffer_in); |
omatthews | 1:5752892425a3 | 40 | c = pc.getc(); |
omatthews | 1:5752892425a3 | 41 | if (c == '#'){ |
omatthews | 0:abf096b1334e | 42 | } |
omatthews | 1:5752892425a3 | 43 | else{ |
omatthews | 1:5752892425a3 | 44 | buffer_in[i] = c; |
omatthews | 1:5752892425a3 | 45 | } |
omatthews | 1:5752892425a3 | 46 | pc.putc(buffer_in[i]); |
omatthews | 1:5752892425a3 | 47 | } |
omatthews | 1:5752892425a3 | 48 | } |
omatthews | 0:abf096b1334e | 49 | |
omatthews | 0:abf096b1334e | 50 | void write_setpoint(){ |
omatthews | 0:abf096b1334e | 51 | pc.printf("%d ",set_point_length); |
omatthews | 0:abf096b1334e | 52 | for (int i = 0; i < set_point_length; i++) { |
omatthews | 0:abf096b1334e | 53 | if (buffer[i] == NULL){ |
omatthews | 0:abf096b1334e | 54 | pc.putc('#'); |
omatthews | 0:abf096b1334e | 55 | } |
omatthews | 0:abf096b1334e | 56 | else{ |
omatthews | 0:abf096b1334e | 57 | pc.putc(buffer[i]); |
omatthews | 0:abf096b1334e | 58 | } |
omatthews | 0:abf096b1334e | 59 | } |
omatthews | 0:abf096b1334e | 60 | } |
omatthews | 0:abf096b1334e | 61 | |
omatthews | 0:abf096b1334e | 62 | |
omatthews | 0:abf096b1334e | 63 | |
omatthews | 0:abf096b1334e | 64 | void decode_setpoint(){ |
omatthews | 0:abf096b1334e | 65 | // Create a stream that reads from the buffer. |
omatthews | 0:abf096b1334e | 66 | |
omatthews | 0:abf096b1334e | 67 | pb_istream_t istream = pb_istream_from_buffer(buffer_in, set_point_in_length); |
omatthews | 0:abf096b1334e | 68 | |
omatthews | 0:abf096b1334e | 69 | //Now we are ready to decode the message. |
omatthews | 0:abf096b1334e | 70 | status = pb_decode(&istream, SetPoint_fields, &set_point_in); |
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 | // Print the data contained in the message. |
omatthews | 0:abf096b1334e | 79 | 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 | 80 | } |
omatthews | 0:abf096b1334e | 81 | |
omatthews | 0:abf096b1334e | 82 | |
omatthews | 0:abf096b1334e | 83 | |
omatthews | 0:abf096b1334e | 84 | |
omatthews | 0:abf096b1334e | 85 | // main() runs in its own thread in the OS |
omatthews | 0:abf096b1334e | 86 | int main() |
omatthews | 0:abf096b1334e | 87 | { |
omatthews | 0:abf096b1334e | 88 | /* This is the buffer where we will store our message. */ |
omatthews | 0:abf096b1334e | 89 | pc.baud(115200); |
omatthews | 2:89156c267f7a | 90 | |
omatthews | 0:abf096b1334e | 91 | |
omatthews | 0:abf096b1334e | 92 | |
omatthews | 0:abf096b1334e | 93 | |
omatthews | 0:abf096b1334e | 94 | |
omatthews | 0:abf096b1334e | 95 | |
omatthews | 0:abf096b1334e | 96 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
omatthews | 2:89156c267f7a | 97 | |
omatthews | 0:abf096b1334e | 98 | set_point.r_set_point = 0.5; |
omatthews | 0:abf096b1334e | 99 | set_point.time_point = 100; |
omatthews | 0:abf096b1334e | 100 | set_point.trig_time = 101; |
omatthews | 0:abf096b1334e | 101 | |
omatthews | 0:abf096b1334e | 102 | status = pb_encode(&stream, SetPoint_fields, &set_point); |
omatthews | 0:abf096b1334e | 103 | set_point_length = stream.bytes_written; |
omatthews | 0:abf096b1334e | 104 | |
omatthews | 0:abf096b1334e | 105 | |
omatthews | 0:abf096b1334e | 106 | if (!status) |
omatthews | 0:abf096b1334e | 107 | { |
omatthews | 0:abf096b1334e | 108 | pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); |
omatthews | 0:abf096b1334e | 109 | return 1; |
omatthews | 0:abf096b1334e | 110 | } |
omatthews | 0:abf096b1334e | 111 | |
omatthews | 0:abf096b1334e | 112 | write_setpoint(); |
omatthews | 0:abf096b1334e | 113 | |
omatthews | 0:abf096b1334e | 114 | |
omatthews | 0:abf096b1334e | 115 | pc.getc(); |
omatthews | 0:abf096b1334e | 116 | pc.printf("\n Input your set point:\n"); |
omatthews | 0:abf096b1334e | 117 | read_setpoint(); |
omatthews | 0:abf096b1334e | 118 | decode_setpoint(); |
omatthews | 0:abf096b1334e | 119 | return 0; |
omatthews | 0:abf096b1334e | 120 | } |
omatthews | 0:abf096b1334e | 121 | |
omatthews | 0:abf096b1334e | 122 |