-
Notifications
You must be signed in to change notification settings - Fork 15
Experimental new API endpoints #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Pull Request Test Coverage Report for Build 23
💛 - Coveralls |
|
A possible flaw in this approach may be the instability of There's nothing particularly special about |
This changeset introduces a new
setandgetmethod for i2cdevice. The purpose of these methods is to provide a less-bonkers approach to writing and reading values than the current (MicroPython incompatible) system of overridinggetattributeand redirecting attribute lookups to our internal register dictionary.Get
The
getmethod accepts a register name, reads the register and returns a namedtuple of all the available fields. These changes turn accessing multiple registers from:To:
Note: It is important to understand that
my_registeris, at this point, a statelessnamedtuplethat's disconnected from the hardware. If it's a non-volatile register (IE: it never changes) then you can keep this object for the lifetime of your program. If you want to read it again (in the case of a volatile register) then you must calldevice.get('REGISTER_NAME')again.Set
Set cleans up the current conflation of
readandwritewhereupon using this pattern:Could result in errors where the user (me, usually) forgets to call
write()and their expected register changes are thus never written.setis explicit about the fact it's for writing to a register, and thus does not allow for this mistake, the above code would become:This still allows for multi-register writes, but it is disambiguated from the
getmethod and ensures your changes are always written to device. Under the hood this function reads the current register state, locks it, updates all the fields you have supplied, writes the changed register value(s) back to the device, and unlocks the register.This change should come with a deadline for deprication of the existing, confusing and fragile API methods- removing the pattern
device.REGISTER_NAME.get_field_name()in favour ofdevice.get('REGISTER_NAME').field_name. This is a subtle aesthetic change on the face of it, but under the hood the latter method does not rely on any fragile and overzealousgetattributetrickery.@rabid-inventor and @Septolum - since you've both worked with i2cdevice I'd appreciate your feedback here. All of our libraries can, and should be migrated over to this new style pretty easily and this will - hopefully (@Septolum correct me if I'm wrong) facilitate MicroPython compatibility without having to use the ugly (and non read/write combining)
device.set_field('REGISTER_NAME', 'FIELD_NAME', value)and `device.get_field('REGISTER_NAME', 'FIELD_NAME') approach.