The intention of this module is to interace node.js with an Arduino software I am writing. For this I need to read binary structures that are generated by C++ code running on an Arduino system.
Supported types:
- uint8,16,32
- int8,16,32
- float
- strings
Only one dimensional arrays are supported. It might be possible to extend the code to support this, but I don't see a need for this right now.
Pointers are not supported, but that wouldn't make sense anyway since serialization can't deal with native memory adresses.
struct = require("cpp-struct")
var player = new struct("Player", [
"name", struct.char(12),
"id", struct.uint32_t()
]);
var record = new struct("Record", [
"playerIndex", struct.uint8_t(),
"typeId", struct.uint8_t(),
"record", struct.uint16_t(31)
]);
var EEPROMData = new struct("EEPROMData", [
"gameName", struct.char(14),
"version", struct.uint16_t(),
"players", struct.type(player,32),
"records", struct.type(record,32)
]);
var buffer = new Buffer(EEPROMData.size());
EEPROMData.encode(buffer,0, {
gameName: "SuperTesting!!!",
version: 1,
players:[
{name:"Hello",id:1},
{id:2},
{name:"HUHU"}
]
},{endian:"LE"})
- name: Internal name that's used when exporting and referencing other structs.
- schema: Interleaved array where even elements are variable name identifiers and odd elements are struct instances
- count (optional): If the type is supposed to be an array, this is the number of elements in the array
- bytes (optional): Size of a single element in bytes (total size = count * bytes)
Note: count and bytes arguments are used usually only internally - you don't have to worry about these if you don't intend to add new native types next to the currently implemented ones such as int/uint/float/double - say you'd want a int24, you'll need to care about these arguments.
- func (buffer,pos,data,opt): Function that's called instead of the default encoder -- buffer: Buffer object to write to -- pos: position to write to -- data: the data value to be written (can be undefined) -- opt: optional options object that specifies encoding details (such as endianess)
I want to create schemas in node.js and use those for reading and writing binary buffers. Next to that I also want to output the header files for CPP which I can simply compile and use in my project.
The schema defines exact sizes, not the data - which may be incomplete and is to be filled with default data (zeros).
The other modules I looked at either focused on packing data as binary efficient or packed data directly without schema.
The closest match I could find was buffer-layout.