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