Simple tests

Ensure your device works with this simple test.

examples/register_rwbit.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4from adafruit_bus_device.i2c_device import I2CDevice
 5from board import SCL, SDA
 6from busio import I2C
 7
 8from adafruit_register.i2c_bit import RWBit
 9
10DEVICE_ADDRESS = 0x68  # device address of DS3231 board
11A_DEVICE_REGISTER = 0x0E  # control register on the DS3231 board
12
13
14class DeviceControl:
15    def __init__(self, i2c):
16        self.i2c_device = i2c  # self.i2c_device required by RWBit class
17
18    flag1 = RWBit(A_DEVICE_REGISTER, 0)  # bit 0 of the control register
19    flag2 = RWBit(A_DEVICE_REGISTER, 1)  # bit 1
20    flag3 = RWBit(A_DEVICE_REGISTER, 7)  # bit 7
21
22
23# The follow is for I2C communications
24comm_port = I2C(SCL, SDA)
25device = I2CDevice(comm_port, DEVICE_ADDRESS)
26flags = DeviceControl(device)
27
28# set the bits in the device
29flags.flag1 = False
30flags.flag2 = True
31flags.flag3 = False
32# display the device values for the bits
33print(f"flag1: {flags.flag1}; flag2: {flags.flag2}; flag3: {flags.flag3}")
34
35# toggle the bits
36flags.flag1 = not flags.flag1
37flags.flag2 = not flags.flag2
38flags.flag3 = not flags.flag3
39# display the device values for the bits
40print(f"flag1: {flags.flag1}; flag2: {flags.flag2}; flag3: {flags.flag3}")
examples/register_rwbits.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4from adafruit_bus_device.i2c_device import I2CDevice
 5from board import SCL, SDA
 6from busio import I2C
 7
 8from adafruit_register.i2c_bits import RWBits
 9
10DEVICE_ADDRESS = 0x39  # device address of APDS9960 board
11A_DEVICE_REGISTER_1 = 0xA2  # a control register on the APDS9960 board
12A_DEVICE_REGISTER_2 = 0xA3  # another control register on the APDS9960 board
13
14
15class DeviceControl:
16    def __init__(self, i2c):
17        self.i2c_device = i2c  # self.i2c_device required by RWBit class
18
19    setting1 = RWBits(2, A_DEVICE_REGISTER_1, 6)  # 2 bits: bits 6 & 7
20    setting2 = RWBits(2, A_DEVICE_REGISTER_2, 5)  # 2 bits: bits 5 & 6
21
22
23# The follow is for I2C communications
24comm_port = I2C(SCL, SDA)
25device = I2CDevice(comm_port, DEVICE_ADDRESS)
26settings = DeviceControl(device)
27
28# set the bits in the device
29settings.setting1 = 0
30settings.setting2 = 3
31# display the device values for the bits
32print(f"setting1: {settings.setting1}; setting2: {settings.setting2}")
33
34# toggle the bits
35settings.setting1 = 3
36settings.setting2 = 0
37# display the device values for the bits
38print(f"setting1: {settings.setting1}; setting2: {settings.setting2}")
examples/register_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4from adafruit_bus_device.i2c_device import I2CDevice
 5from board import SCL, SDA
 6from busio import I2C
 7
 8from adafruit_register.i2c_struct import Struct
 9
10DEVICE_ADDRESS = 0x40  # device address of PCA9685 board
11A_DEVICE_REGISTER = 0x06  # PWM 0 control register on the PCA9685 board
12
13
14class DeviceControl:
15    def __init__(self, i2c):
16        self.i2c_device = i2c  # self.i2c_device required by Struct class
17
18    tuple_of_numbers = Struct(A_DEVICE_REGISTER, "<HH")  # 2 16-bit numbers
19
20
21# The follow is for I2C communications
22comm_port = I2C(SCL, SDA)
23device = I2CDevice(comm_port, DEVICE_ADDRESS)
24registers = DeviceControl(device)
25
26# set the bits in the device
27registers.tuple_of_numbers = (0, 0x00FF)
28# display the device values for the bits
29print("register 1: {}; register 2: {}".format(*registers.tuple_of_numbers))
30
31# toggle the bits
32registers.tuple_of_numbers = (0x1000, 0)
33# display the device values for the bits
34print("register 1: {}; register 2: {}".format(*registers.tuple_of_numbers))
examples/register_unarystruct.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4from adafruit_bus_device.i2c_device import I2CDevice
 5from board import SCL, SDA
 6from busio import I2C
 7
 8from adafruit_register.i2c_struct import UnaryStruct
 9
10DEVICE_ADDRESS = 0x74  # device address of PCA9685 board
11A_DEVICE_REGISTER_1 = 0x00  # Configuration register on the is31fl3731 board
12A_DEVICE_REGISTER_2 = 0x03  # Auto Play Control Register 2 on the is31fl3731 board
13
14
15class DeviceControl:
16    def __init__(self, i2c):
17        self.i2c_device = i2c  # self.i2c_device required by UnaryStruct class
18
19    register1 = UnaryStruct(A_DEVICE_REGISTER_1, "<B")  # 8-bit number
20    register2 = UnaryStruct(A_DEVICE_REGISTER_2, "<B")  # 8-bit number
21
22
23# The follow is for I2C communications
24comm_port = I2C(SCL, SDA)
25device = I2CDevice(comm_port, DEVICE_ADDRESS)
26registers = DeviceControl(device)
27
28# set the bits in the device
29registers.register1 = 1 << 3 | 2
30registers.register2 = 32
31# display the device values for the bits
32print(f"register 1: {registers.register1}; register 2: {registers.register2}")
33
34# toggle the bits
35registers.register1 = 2 << 3 | 5
36registers.register2 = 60
37# display the device values for the bits
38print(f"register 1: {registers.register1}; register 2: {registers.register2}")