Skip to content

The 3.x change #16

@BenUniqcode

Description

@BenUniqcode

Hi
Thanks so much for your library, which I've been using for quite a while.

I've just got around to updating from 2.x to 3.x and I see your reason for the major version bump is given as:

In versions < 3.0.0, there were several functions that returned a structure of type "xyzFloat". To be exact not the structure was returned but a pointer to this structure. Since the structures were created in the functions, the memory space where they were stored could have been overwritten. I have now changed this, but unfortunately, former sketches are not compatible anymore.

It's not a major problem to adapt my code, but I don't understand the reasoning.
Functions that returned a struct, returned a struct, by value. They didn't return a pointer to a struct inside the function.
Returning a struct by value is quite safe.
It's considered bad practice to do it for a large struct, because it's inefficient to copy a load of data. But xyzFloat is small.

So, the change wasn't necessary for that reason.

However, now that it's been done, there is a potential benefit. These functions could now use a bool return value to indicate success or failure. Currently the library doesn't really do any error checking of comms, and it would be great if it could be a bit more active in that regard. The underlying TwoWire::requestFrom() and SPIClass::transfer() will return 0, or less than the expected number of bytes, in the event of a comms error. You could check for that in functions like readRegister8() and readMultipleRegisters(), return false if there was an error, and pass that up the chain of functions, allowing the user to retry, or at least ignore the values in the xyzFloat if they should not be trusted.

Now, before you shrink from the amount of work this entails, here's the best news - I'll do it!

I'm intending to fork your library to adapt it for the ADXL366 (remarkable new device - power consumption in measure mode is of the order of 1uA, compared to >100uA for the ADXL345, and yet it's cheaper and has more features!). Because quite a lot has changed from the 345, I think is best done as a separate library, but I intend to keep the same API that this library uses wherever possible, so that it can be a drop-in replacement for users of the newer accelerometer. Anyway, as part of that work I thought it would be good to add the above error checking, and I will be happy to feed that part of it back to you as a Pull Request if you are interested.

All the best
Ben

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions