harp-tech / protocol

Description of the Harp protocol.
https://harp-tech.org/protocol/BinaryProtocol-8bit.html
MIT License
3 stars 5 forks source link

Sequential Register Writing/Reading #30

Open bruno-f-cruz opened 9 months ago

bruno-f-cruz commented 9 months ago

Discussed in https://github.com/harp-tech/protocol/discussions/27

Originally posted by **Poofjunior** May 18, 2023 Hi everyone, I'd like to propose a minor feature tweak to the existing (and future) harp protocol. For register reads and writes, I propose that *if we read/write **more** than the specified number of bytes, we simply continue reading/writing sequentially from the next register.* The main benefit is that we can read/write groups of sequential registers. For future harp devices, we can then group related registers together, which enables us to read/write structs. In C++, this is straightforward to implement if the register data is stored in a packed struct. ````c++ // Example register values stored contiguously in memory with a packed struct (i.e no padding). #pragma pack(push, 1) struct RegValues { const int16_t R_POSITION_X; const int16_t R_POSITION_Y; const int16_t R_POSITION_Z; const uint8_t R_RESET_POSITION; }; #pragma pack(pop) ```` Suppose we want to read out the x,y, and z position as a single harp read request, instead of 3 register read requests. Then, on the client side, we simply ask for 6 bytes. Since they're stored contiguously in memory with no padding, the embedded device can just memcpy it into the outgoing packet. In fact, if the data on the harp device is already stored contiguously in memory, then little-to-no changes on the embedded device need to be adjusted. Here's some "client-side" pseudocode. ````python dev = harp_device("3D_JOYSTICK") x = dev.read_request(reg=R_POSITION_X, bytes=2).payload.unpack('