pimoroni / i2cdevice-python

Domain-specific language for describing smbus/i2c register maps
MIT License
26 stars 14 forks source link

TODO: Allow registers to be passed as an argument to Device #1

Closed Gadgetoid closed 6 years ago

Gadgetoid commented 6 years ago

The current pattern is slightly topsy-turvey in that a Device is defined, and then passed to each register- some wonky underlying logic then handles relating these two things together reasonably.

Ideally the Register should be passed as an argument to the Device, in much the same way as the BitField is passed to the register:

my_device = Device(
    0x44,
    PS_CONTROL = Register('PS_CONTROL', 0x81, fields=(
        BitField('saturation_indicator_enable', 0b00100000),
        BitField('active', 0b00000011, values_map={False: 0b00, True: 0b11}),
    ))
)

And then accessed with:

my_device.PS_CONTROL.set_saturation_indicator_enable(True)

Registers could then be defined once and re-used in multiple devices. Granted this is unlikely, but I'm trying to achieve the expressiveness of Construct.

Similarly values_in, values_out and values_map should be just "adaptor=" and a factory class should be capable of taking a dictionary of values and producing an "adaptor" that translates them accordingly:

class Adaptor():
    def in(self, value):
        return value
    def out(self, value):
        return value