Nanopb (lightweight version of googles protobuf) test. It is not working as it should yet.

Dependencies:   MODSERIAL mbed

Committer:
Tomas
Date:
Wed Apr 09 11:31:53 2014 +0000
Revision:
1:f02249a6e8bb
Parent:
0:bada2c7bd577
Child:
2:487359a1a439
Rewrote program to encode and decode inside mbed, no need for any external programs

Who changed what in which revision?

UserRevisionLine numberNew 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 1:f02249a6e8bb 13
Tomas 0:bada2c7bd577 14 DigitalOut myled(LED1);
Tomas 0:bada2c7bd577 15 MODSERIAL pc(USBTX, USBRX, "modser"); //modified modser lib to be able to input custom stream
Tomas 0:bada2c7bd577 16
Tomas 0:bada2c7bd577 17
Tomas 0:bada2c7bd577 18 using namespace std;
Tomas 0:bada2c7bd577 19
Tomas 0:bada2c7bd577 20 int main() {
Tomas 0:bada2c7bd577 21 pc.baud(115200);
Tomas 0:bada2c7bd577 22 status_message Status;
Tomas 1:f02249a6e8bb 23 gyro_message GyroOut, GyroIn;
Tomas 0:bada2c7bd577 24
Tomas 0:bada2c7bd577 25 pc.claim();
Tomas 1:f02249a6e8bb 26 uint8_t bufferout[150];
Tomas 1:f02249a6e8bb 27 uint8_t bufferin[150];
Tomas 0:bada2c7bd577 28
Tomas 1:f02249a6e8bb 29 pb_ostream_t streamout = pb_ostream_from_buffer(bufferout, sizeof(bufferout));
Tomas 1:f02249a6e8bb 30
Tomas 0:bada2c7bd577 31 Status.Mode=1;
Tomas 1:f02249a6e8bb 32 GyroOut.X=1.1;
Tomas 1:f02249a6e8bb 33 GyroOut.Y=2.1;
Tomas 1:f02249a6e8bb 34 GyroOut.Z=3.1;
Tomas 1:f02249a6e8bb 35 pc.printf("starting..\r\n");
Tomas 0:bada2c7bd577 36 while(1){
Tomas 1:f02249a6e8bb 37 GyroOut.X+=0.1;
Tomas 1:f02249a6e8bb 38 GyroOut.Y+=0.2;
Tomas 1:f02249a6e8bb 39 GyroOut.Z+=0.3;
Tomas 0:bada2c7bd577 40
Tomas 1:f02249a6e8bb 41 if (pb_encode(&streamout, gyro_message_fields, &GyroOut)) {
Tomas 1:f02249a6e8bb 42 /*pc.putc('$');
Tomas 0:bada2c7bd577 43 //fwrite(buffer, 1, stream.bytes_written, stdout);
Tomas 0:bada2c7bd577 44 pc.printf("%s", buffer);
Tomas 0:bada2c7bd577 45 pc.putc('e');
Tomas 0:bada2c7bd577 46 pc.putc('n');
Tomas 0:bada2c7bd577 47 pc.putc('d');
Tomas 1:f02249a6e8bb 48 //pc.printf("x: %4.2f, y: %4.2f, z: %4.2f\r\n", Gyro.X, Gyro.Y, Gyro.Z);
Tomas 0:bada2c7bd577 49 //stream*=0; //empty stream, not working as of now but creating error on mbed
Tomas 1:f02249a6e8bb 50 //memset(&buffer[0], 0, sizeof(buffer)); //empty buffer*/
Tomas 1:f02249a6e8bb 51
Tomas 1:f02249a6e8bb 52 //new test code:
Tomas 1:f02249a6e8bb 53 pc.printf("Raw values: x: %4.2f, y: %4.2f, z: %4.2f\r\n", GyroOut.X, GyroOut.Y, GyroOut.Z);
Tomas 1:f02249a6e8bb 54 pc.printf("%s\r\n", bufferout);
Tomas 0:bada2c7bd577 55 }
Tomas 0:bada2c7bd577 56
Tomas 0:bada2c7bd577 57 else {
Tomas 1:f02249a6e8bb 58 pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&streamout));
Tomas 1:f02249a6e8bb 59 return 0;
Tomas 1:f02249a6e8bb 60 }
Tomas 1:f02249a6e8bb 61 pc.printf("decoding...\r\n");
Tomas 1:f02249a6e8bb 62 pb_istream_t streamin = pb_istream_from_buffer(bufferin, sizeof(bufferin));
Tomas 1:f02249a6e8bb 63 for(int i=0;i<=streamout.bytes_written;i++) //copy output buffer to input buffer
Tomas 1:f02249a6e8bb 64 bufferin[i]=bufferout[i];
Tomas 1:f02249a6e8bb 65 if (pb_decode(&streamin, gyro_message_fields, &GyroIn)) { //decode message
Tomas 1:f02249a6e8bb 66 pc.printf("Decoded values: x: %4.2f, y: %4.2f, z: %4.2f\r\n", GyroIn.X, GyroIn.Y, GyroIn.Z);
Tomas 1:f02249a6e8bb 67 }
Tomas 1:f02249a6e8bb 68
Tomas 1:f02249a6e8bb 69 else {
Tomas 1:f02249a6e8bb 70 pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&streamin));
Tomas 1:f02249a6e8bb 71 return 0;
Tomas 1:f02249a6e8bb 72 }
Tomas 0:bada2c7bd577 73 wait(1);
Tomas 0:bada2c7bd577 74 }
Tomas 0:bada2c7bd577 75 }