Trying to encode a configuration file and a set of instructions to be passed to a microcontroller
Dependencies: MODSERIAL Nanopb
main.cpp@1:5752892425a3, 2019-08-18 (annotated)
- Committer:
- omatthews
- Date:
- Sun Aug 18 19:03:28 2019 +0000
- Revision:
- 1:5752892425a3
- Parent:
- 0:abf096b1334e
- Child:
- 2:89156c267f7a
Working well without EoT
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 | 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 | |
omatthews | 0:abf096b1334e | 31 | void read_setpoint(){ |
omatthews | 0:abf096b1334e | 32 | if (pc.scanf("%d",&set_point_in_length) < 0){pc.printf("Error in reading message length");} |
omatthews | 0:abf096b1334e | 33 | for (int i = 0; i < set_point_length; i++) { |
omatthews | 1:5752892425a3 | 34 | i = i % sizeof(buffer_in); |
omatthews | 1:5752892425a3 | 35 | c = pc.getc(); |
omatthews | 1:5752892425a3 | 36 | if (c == '#'){ |
omatthews | 0:abf096b1334e | 37 | } |
omatthews | 1:5752892425a3 | 38 | else{ |
omatthews | 1:5752892425a3 | 39 | buffer_in[i] = c; |
omatthews | 1:5752892425a3 | 40 | } |
omatthews | 1:5752892425a3 | 41 | pc.putc(buffer_in[i]); |
omatthews | 1:5752892425a3 | 42 | } |
omatthews | 1:5752892425a3 | 43 | } |
omatthews | 0:abf096b1334e | 44 | |
omatthews | 0:abf096b1334e | 45 | void write_setpoint(){ |
omatthews | 0:abf096b1334e | 46 | pc.printf("%d ",set_point_length); |
omatthews | 0:abf096b1334e | 47 | for (int i = 0; i < set_point_length; i++) { |
omatthews | 0:abf096b1334e | 48 | if (buffer[i] == NULL){ |
omatthews | 0:abf096b1334e | 49 | pc.putc('#'); |
omatthews | 0:abf096b1334e | 50 | } |
omatthews | 0:abf096b1334e | 51 | else{ |
omatthews | 0:abf096b1334e | 52 | pc.putc(buffer[i]); |
omatthews | 0:abf096b1334e | 53 | } |
omatthews | 0:abf096b1334e | 54 | } |
omatthews | 0:abf096b1334e | 55 | } |
omatthews | 0:abf096b1334e | 56 | |
omatthews | 0:abf096b1334e | 57 | |
omatthews | 0:abf096b1334e | 58 | |
omatthews | 0:abf096b1334e | 59 | void decode_setpoint(){ |
omatthews | 0:abf096b1334e | 60 | // Create a stream that reads from the buffer. |
omatthews | 0:abf096b1334e | 61 | |
omatthews | 0:abf096b1334e | 62 | pb_istream_t istream = pb_istream_from_buffer(buffer_in, set_point_in_length); |
omatthews | 0:abf096b1334e | 63 | |
omatthews | 0:abf096b1334e | 64 | //Now we are ready to decode the message. |
omatthews | 0:abf096b1334e | 65 | status = pb_decode(&istream, SetPoint_fields, &set_point_in); |
omatthews | 0:abf096b1334e | 66 | |
omatthews | 0:abf096b1334e | 67 | // Check for errors... |
omatthews | 0:abf096b1334e | 68 | if (!status) |
omatthews | 0:abf096b1334e | 69 | { |
omatthews | 0:abf096b1334e | 70 | pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&istream)); |
omatthews | 0:abf096b1334e | 71 | } |
omatthews | 0:abf096b1334e | 72 | |
omatthews | 0:abf096b1334e | 73 | // Print the data contained in the message. |
omatthews | 0:abf096b1334e | 74 | 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 | 75 | } |
omatthews | 0:abf096b1334e | 76 | |
omatthews | 0:abf096b1334e | 77 | |
omatthews | 0:abf096b1334e | 78 | |
omatthews | 0:abf096b1334e | 79 | |
omatthews | 0:abf096b1334e | 80 | // main() runs in its own thread in the OS |
omatthews | 0:abf096b1334e | 81 | int main() |
omatthews | 0:abf096b1334e | 82 | { |
omatthews | 0:abf096b1334e | 83 | /* This is the buffer where we will store our message. */ |
omatthews | 0:abf096b1334e | 84 | pc.baud(115200); |
omatthews | 0:abf096b1334e | 85 | |
omatthews | 0:abf096b1334e | 86 | |
omatthews | 0:abf096b1334e | 87 | |
omatthews | 0:abf096b1334e | 88 | |
omatthews | 0:abf096b1334e | 89 | |
omatthews | 0:abf096b1334e | 90 | |
omatthews | 0:abf096b1334e | 91 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
omatthews | 0:abf096b1334e | 92 | |
omatthews | 0:abf096b1334e | 93 | set_point.r_set_point = 0.5; |
omatthews | 0:abf096b1334e | 94 | set_point.time_point = 100; |
omatthews | 0:abf096b1334e | 95 | set_point.trig_time = 101; |
omatthews | 0:abf096b1334e | 96 | |
omatthews | 0:abf096b1334e | 97 | status = pb_encode(&stream, SetPoint_fields, &set_point); |
omatthews | 0:abf096b1334e | 98 | set_point_length = stream.bytes_written; |
omatthews | 0:abf096b1334e | 99 | |
omatthews | 0:abf096b1334e | 100 | |
omatthews | 0:abf096b1334e | 101 | if (!status) |
omatthews | 0:abf096b1334e | 102 | { |
omatthews | 0:abf096b1334e | 103 | pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); |
omatthews | 0:abf096b1334e | 104 | return 1; |
omatthews | 0:abf096b1334e | 105 | } |
omatthews | 0:abf096b1334e | 106 | |
omatthews | 0:abf096b1334e | 107 | write_setpoint(); |
omatthews | 0:abf096b1334e | 108 | |
omatthews | 0:abf096b1334e | 109 | |
omatthews | 0:abf096b1334e | 110 | pc.getc(); |
omatthews | 0:abf096b1334e | 111 | pc.printf("\n Input your set point:\n"); |
omatthews | 0:abf096b1334e | 112 | read_setpoint(); |
omatthews | 1:5752892425a3 | 113 | for (i=0;i<20;i++){ |
omatthews | 1:5752892425a3 | 114 | pc.putc(buffer[i]); |
omatthews | 1:5752892425a3 | 115 | pc.putc(buffer_in[i]); |
omatthews | 1:5752892425a3 | 116 | if(buffer_in[i] == buffer[i]) {pc.putc('y');} |
omatthews | 1:5752892425a3 | 117 | else {pc.putc('n');} |
omatthews | 1:5752892425a3 | 118 | pc.printf("\n"); |
omatthews | 1:5752892425a3 | 119 | } |
omatthews | 0:abf096b1334e | 120 | decode_setpoint(); |
omatthews | 0:abf096b1334e | 121 | |
omatthews | 0:abf096b1334e | 122 | |
omatthews | 0:abf096b1334e | 123 | return 0; |
omatthews | 0:abf096b1334e | 124 | } |
omatthews | 0:abf096b1334e | 125 | |
omatthews | 0:abf096b1334e | 126 |