register_rwbit.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_bit import RWBit 5 6 DEVICE_ADDRESS = 0x68 # device address of DS3231 board 7 A_DEVICE_REGISTER = 0x0E # control register on the DS3231 board 8 9 10 class DeviceControl: # pylint: disable-msg=too-few-public-methods 11 def __init__(self, i2c): 12 self.i2c_device = i2c # self.i2c_device required by RWBit class 13 14 flag1 = RWBit(A_DEVICE_REGISTER, 0) # bit 0 of the control register 15 flag2 = RWBit(A_DEVICE_REGISTER, 1) # bit 1 16 flag3 = RWBit(A_DEVICE_REGISTER, 7) # bit 7 17 18 19 # The follow is for I2C communications 20 comm_port = I2C(SCL, SDA) 21 device = I2CDevice(comm_port, DEVICE_ADDRESS) 22 flags = DeviceControl(device) 23 24 # set the bits in the device 25 flags.flag1 = False 26 flags.flag2 = True 27 flags.flag3 = False 28 # display the device values for the bits 29 print("flag1: {}; flag2: {}; flag3: {}".format(flags.flag1, flags.flag2, flags.flag3)) 30 31 # toggle the bits 32 flags.flag1 = not flags.flag1 33 flags.flag2 = not flags.flag2 34 flags.flag3 = not flags.flag3 35 # display the device values for the bits 36 print("flag1: {}; flag2: {}; flag3: {}".format(flags.flag1, flags.flag2, flags.flag3))