
Nanopb (lightweight version of googles protobuf) test. It is not working as it should yet.
main.cpp@3:fd0e1bc80f78, 2014-04-09 (annotated)
- Committer:
- Tomas
- Date:
- Wed Apr 09 11:44:14 2014 +0000
- Revision:
- 3:fd0e1bc80f78
- Parent:
- 2:487359a1a439
removed unnecessary libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tomas | 0:bada2c7bd577 | 1 | #include "mbed.h" |
Tomas | 0:bada2c7bd577 | 2 | #include "pb.h" |
Tomas | 0:bada2c7bd577 | 3 | #include "pb_encode.h" |
Tomas | 0:bada2c7bd577 | 4 | #include "pb_decode.h" |
Tomas | 0:bada2c7bd577 | 5 | #include "threeaxis.pb.h" |
Tomas | 0:bada2c7bd577 | 6 | #include "MODSERIAL.h" |
Tomas | 0:bada2c7bd577 | 7 | |
Tomas | 0:bada2c7bd577 | 8 | MODSERIAL pc(USBTX, USBRX, "modser"); //modified modser lib to be able to input custom stream |
Tomas | 0:bada2c7bd577 | 9 | |
Tomas | 0:bada2c7bd577 | 10 | int main() { |
Tomas | 0:bada2c7bd577 | 11 | pc.baud(115200); |
Tomas | 1:f02249a6e8bb | 12 | gyro_message GyroOut, GyroIn; |
Tomas | 0:bada2c7bd577 | 13 | |
Tomas | 0:bada2c7bd577 | 14 | pc.claim(); |
Tomas | 1:f02249a6e8bb | 15 | uint8_t bufferout[150]; |
Tomas | 1:f02249a6e8bb | 16 | uint8_t bufferin[150]; |
Tomas | 0:bada2c7bd577 | 17 | |
Tomas | 1:f02249a6e8bb | 18 | pb_ostream_t streamout = pb_ostream_from_buffer(bufferout, sizeof(bufferout)); |
Tomas | 2:487359a1a439 | 19 | |
Tomas | 1:f02249a6e8bb | 20 | GyroOut.X=1.1; |
Tomas | 1:f02249a6e8bb | 21 | GyroOut.Y=2.1; |
Tomas | 1:f02249a6e8bb | 22 | GyroOut.Z=3.1; |
Tomas | 1:f02249a6e8bb | 23 | pc.printf("starting..\r\n"); |
Tomas | 0:bada2c7bd577 | 24 | while(1){ |
Tomas | 1:f02249a6e8bb | 25 | GyroOut.X+=0.1; |
Tomas | 1:f02249a6e8bb | 26 | GyroOut.Y+=0.2; |
Tomas | 1:f02249a6e8bb | 27 | GyroOut.Z+=0.3; |
Tomas | 0:bada2c7bd577 | 28 | |
Tomas | 2:487359a1a439 | 29 | pc.printf("Raw values: x: %4.2f, y: %4.2f, z: %4.2f\r\n", GyroOut.X, GyroOut.Y, GyroOut.Z); //print values before encoding |
Tomas | 2:487359a1a439 | 30 | |
Tomas | 2:487359a1a439 | 31 | if (pb_encode(&streamout, gyro_message_fields, &GyroOut)) { //encode message |
Tomas | 1:f02249a6e8bb | 32 | /*pc.putc('$'); |
Tomas | 0:bada2c7bd577 | 33 | //fwrite(buffer, 1, stream.bytes_written, stdout); |
Tomas | 0:bada2c7bd577 | 34 | pc.printf("%s", buffer); |
Tomas | 0:bada2c7bd577 | 35 | pc.putc('e'); |
Tomas | 0:bada2c7bd577 | 36 | pc.putc('n'); |
Tomas | 0:bada2c7bd577 | 37 | pc.putc('d'); |
Tomas | 1:f02249a6e8bb | 38 | //pc.printf("x: %4.2f, y: %4.2f, z: %4.2f\r\n", Gyro.X, Gyro.Y, Gyro.Z); |
Tomas | 0:bada2c7bd577 | 39 | //stream*=0; //empty stream, not working as of now but creating error on mbed |
Tomas | 1:f02249a6e8bb | 40 | //memset(&buffer[0], 0, sizeof(buffer)); //empty buffer*/ |
Tomas | 1:f02249a6e8bb | 41 | |
Tomas | 1:f02249a6e8bb | 42 | //new test code: |
Tomas | 2:487359a1a439 | 43 | |
Tomas | 1:f02249a6e8bb | 44 | pc.printf("%s\r\n", bufferout); |
Tomas | 0:bada2c7bd577 | 45 | } |
Tomas | 0:bada2c7bd577 | 46 | |
Tomas | 2:487359a1a439 | 47 | else { //print error message if encoding fails |
Tomas | 1:f02249a6e8bb | 48 | pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&streamout)); |
Tomas | 1:f02249a6e8bb | 49 | return 0; |
Tomas | 1:f02249a6e8bb | 50 | } |
Tomas | 2:487359a1a439 | 51 | |
Tomas | 1:f02249a6e8bb | 52 | pc.printf("decoding...\r\n"); |
Tomas | 2:487359a1a439 | 53 | pb_istream_t streamin = pb_istream_from_buffer(bufferin, sizeof(bufferin)); //create input stream |
Tomas | 1:f02249a6e8bb | 54 | for(int i=0;i<=streamout.bytes_written;i++) //copy output buffer to input buffer |
Tomas | 1:f02249a6e8bb | 55 | bufferin[i]=bufferout[i]; |
Tomas | 1:f02249a6e8bb | 56 | if (pb_decode(&streamin, gyro_message_fields, &GyroIn)) { //decode message |
Tomas | 2:487359a1a439 | 57 | pc.printf("Decoded values: x: %4.2f, y: %4.2f, z: %4.2f\r\n", GyroIn.X, GyroIn.Y, GyroIn.Z); //print decoded values |
Tomas | 1:f02249a6e8bb | 58 | } |
Tomas | 1:f02249a6e8bb | 59 | |
Tomas | 2:487359a1a439 | 60 | else { //print error message if decoding fails |
Tomas | 1:f02249a6e8bb | 61 | pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&streamin)); |
Tomas | 1:f02249a6e8bb | 62 | return 0; |
Tomas | 1:f02249a6e8bb | 63 | } |
Tomas | 0:bada2c7bd577 | 64 | wait(1); |
Tomas | 0:bada2c7bd577 | 65 | } |
Tomas | 0:bada2c7bd577 | 66 | } |