thesolarnomad / lora-serialization

LoraWAN serialization/deserialization library for The Things Network
MIT License
165 stars 27 forks source link

Create encoder that takes encoder functions #1

Closed joscha closed 8 years ago

joscha commented 8 years ago

In the decoder, you can currently do something like this:

const bytes = new Bytes(12);
const result = decoder.decode(
    bytes,
    [ decoder.latLng, decoder.unixtime ],
    [ 'coords',       'timestamp']
);
// result is { coords: [/* lat */, /* longitude */], timestamp: /* unixtime */}

It would be great if we had something similar for the Arduino side, because currently it looks like this:

byte buffer[12];
latLngToBytes(buffer   + 0, latitude, longitude);
unixtimeToBytes(buffer + 8, unixtime);
// byte has 12 define bytes

but ideally the user would not need to keep count of what position in the byte array the data is going to be written, e.g. it would be something like:

(pseudo code)

my_encoder = encoder(latLng, unixtime);
byte buffer[my_encoder.targetSize];
my_encoder.encode(buffer, latitude, longitude, timestamp);
joscha commented 8 years ago

Idea from @htdvisser:

loRaBuffer = LoRaBuffer(12)
loRaBuffer.WriteFloat(12.34)
loRaBuffer.Bytes()
joscha commented 8 years ago

fixed via 66eeb59