An exceptionally lightweight Protocol-Buffers support lib which consumes no RAM and less than 1% flash
#include <PicoPB.h>
PicoPB PicoPB(0);
byte buf[10];
void setup() {
unsigned int int_example=42;
unsigned int num_bytes_to_encode= PicoPB.encode_varint(buf,int_example));
unsigned int decoded_example=PicoPB.decode_varint(buf);
}
// Most calls (except the ones returning numbers) will return the number of storage buffer bytes of the value
encode_varint(buffer,value); // Convert unsigned into varints and signed into svarints - returns #bytes taken to store it
decode_varint(buffer); // Gets it back
decode_svarint(buffer); // Gets back a signed result ** Important - use when required, and don't mix signed and unsigned **
decode_varint(buffer,&value); // Puts the decoded varint into value, and returns the #bytes that were taken to store it
encode_string(char *buffer,char *input, unsigned int length); // Store text or data
encode_string(char *buffer,char *input); // calls encode_string with strlen(input)
decode_string(char *buffer,char *output, unsigned int maxlen);// Get back stored text or data - NB: function returns the buffer space it took, not the length you got back
encode_fixed32(char *buffer,float input); // Store a float or something - note that on Arduino, float, double and long-double are all the same thing (4 bytes; 32bits)
decode_fixed32(char *buffer); // returns the number
encode_fixed64(char *buffer,long double input);// Coded, but don't work on arduino (which has no 64bit datatypes)
decode_fixed64(char *buffer);
(Method 1)
- Open a terminal (commandline, bash shell, whatever)
- Change into your Arduino folder
cd /Applications/Arduino.app/Contents/Java/libraries/
(or)
cd ~/Arduino/libraries/
- grab and install this code
git clone https://github.com/gitcnd/PicoPB.git
- restart your arduino IDE
- Choose File => Examples => PicoPB => PicoPB_example
- Hit the "build" button and enjoy!
(Method 2) - see https://www.arduino.cc/en/Guide/Libraries
- Download the ZIP of this repo: https://github.com/gitcnd/PicoPB/archive/master.zip
- In your IDE, select Sketch -> Include Library -> Add .ZIP Library
- Choose File => Examples => PicoPB => hello
- Hit the "build" button and enjoy!