-
Notifications
You must be signed in to change notification settings - Fork 14
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
Experimental new API endpoints #3
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
set
andget
method 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 overridinggetattribute
and redirecting attribute lookups to our internal register dictionary.Get
The
get
method 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_register
is, at this point, a statelessnamedtuple
that'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
read
andwrite
whereupon 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.set
is 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
get
method 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 overzealousgetattribute
trickery.@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.