register_rwbits.py
1 from board import SCL, SDA 2 from busio import I2C 3 from adafruit_bus_device.i2c_device import I2CDevice 4 from adafruit_register.i2c_bits import RWBits 5 6 DEVICE_ADDRESS = 0x39 # device address of APDS9960 board 7 A_DEVICE_REGISTER_1 = 0xA2 # a control register on the APDS9960 board 8 A_DEVICE_REGISTER_2 = 0xA3 # another control register on the APDS9960 board 9 10 11 class DeviceControl: # pylint: disable-msg=too-few-public-methods 12 def __init__(self, i2c): 13 self.i2c_device = i2c # self.i2c_device required by RWBit class 14 15 setting1 = RWBits(2, A_DEVICE_REGISTER_1, 6) # 2 bits: bits 6 & 7 16 setting2 = RWBits(2, A_DEVICE_REGISTER_2, 5) # 2 bits: bits 5 & 6 17 18 19 # The follow is for I2C communications 20 comm_port = I2C(SCL, SDA) 21 device = I2CDevice(comm_port, DEVICE_ADDRESS) 22 settings = DeviceControl(device) 23 24 # set the bits in the device 25 settings.setting1 = 0 26 settings.setting2 = 3 27 # display the device values for the bits 28 print("setting1: {}; setting2: {}".format(settings.setting1, settings.setting2)) 29 30 # toggle the bits 31 settings.setting1 = 3 32 settings.setting2 = 0 33 # display the device values for the bits 34 print("setting1: {}; setting2: {}".format(settings.setting1, settings.setting2))