Simple tests

Ensure your device works with this simple test.

examples/register_rwbit.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from board import SCL, SDA
from busio import I2C
from adafruit_bus_device.i2c_device import I2CDevice
from adafruit_register.i2c_bit import RWBit

DEVICE_ADDRESS = 0x68 # device address of DS3231 board
A_DEVICE_REGISTER = 0x0E # control register on the DS3231 board

class DeviceControl: #pylint: disable-msg=too-few-public-methods
    def __init__(self, i2c):
        self.i2c_device = i2c # self.i2c_device required by RWBit class

    flag1 = RWBit(A_DEVICE_REGISTER, 0) # bit 0 of the control register
    flag2 = RWBit(A_DEVICE_REGISTER, 1) # bit 1
    flag3 = RWBit(A_DEVICE_REGISTER, 7) # bit 7

# The follow is for I2C communications
comm_port = I2C(SCL, SDA)
device = I2CDevice(comm_port, DEVICE_ADDRESS)
flags = DeviceControl(device)

# set the bits in the device
flags.flag1 = False
flags.flag2 = True
flags.flag3 = False
# display the device values for the bits
print("flag1: {}; flag2: {}; flag3: {}".format(flags.flag1, flags.flag2, flags.flag3))

# toggle the bits
flags.flag1 = not flags.flag1
flags.flag2 = not flags.flag2
flags.flag3 = not flags.flag3
# display the device values for the bits
print("flag1: {}; flag2: {}; flag3: {}".format(flags.flag1, flags.flag2, flags.flag3))
examples/register_rwbits.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from board import SCL, SDA
from busio import I2C
from adafruit_bus_device.i2c_device import I2CDevice
from adafruit_register.i2c_bits import RWBits

DEVICE_ADDRESS = 0x39 # device address of APDS9960 board
A_DEVICE_REGISTER_1 = 0xA2 # a control register on the APDS9960 board
A_DEVICE_REGISTER_2 = 0xA3 # another control register on the APDS9960 board

class DeviceControl: #pylint: disable-msg=too-few-public-methods
    def __init__(self, i2c):
        self.i2c_device = i2c # self.i2c_device required by RWBit class

    setting1 = RWBits(2, A_DEVICE_REGISTER_1, 6) # 2 bits: bits 6 & 7
    setting2 = RWBits(2, A_DEVICE_REGISTER_2, 5) # 2 bits: bits 5 & 6

# The follow is for I2C communications
comm_port = I2C(SCL, SDA)
device = I2CDevice(comm_port, DEVICE_ADDRESS)
settings = DeviceControl(device)

# set the bits in the device
settings.setting1 = 0
settings.setting2 = 3
# display the device values for the bits
print("setting1: {}; setting2: {}".format(settings.setting1, settings.setting2))

# toggle the bits
settings.setting1 = 3
settings.setting2 = 0
# display the device values for the bits
print("setting1: {}; setting2: {}".format(settings.setting1, settings.setting2))
examples/register_struct.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from board import SCL, SDA
from busio import I2C
from adafruit_bus_device.i2c_device import I2CDevice
from adafruit_register.i2c_struct import Struct

DEVICE_ADDRESS = 0x40  # device address of PCA9685 board
A_DEVICE_REGISTER = 0x06 # PWM 0 control register on the PCA9685 board

class DeviceControl: #pylint: disable-msg=too-few-public-methods
    def __init__(self, i2c):
        self.i2c_device = i2c # self.i2c_device required by Struct class

    tuple_of_numbers = Struct(A_DEVICE_REGISTER, "<HH") # 2 16-bit numbers

# The follow is for I2C communications
comm_port = I2C(SCL, SDA)
device = I2CDevice(comm_port, DEVICE_ADDRESS)
registers = DeviceControl(device)

# set the bits in the device
registers.tuple_of_numbers = (0, 0x00FF)
# display the device values for the bits
print("register 1: {}; register 2: {}".format(*registers.tuple_of_numbers))

# toggle the bits
registers.tuple_of_numbers = (0x1000, 0)
# display the device values for the bits
print("register 1: {}; register 2: {}".format(*registers.tuple_of_numbers))
examples/register_unarystruct.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from board import SCL, SDA
from busio import I2C
from adafruit_bus_device.i2c_device import I2CDevice
from adafruit_register.i2c_struct import UnaryStruct

DEVICE_ADDRESS = 0x74  # device address of PCA9685 board
A_DEVICE_REGISTER_1 = 0x00 # Configuration register on the is31fl3731 board
A_DEVICE_REGISTER_2 = 0x03 # Auto Play Control Register 2 on the is31fl3731 board

class DeviceControl: #pylint: disable-msg=too-few-public-methods
    def __init__(self, i2c):
        self.i2c_device = i2c # self.i2c_device required by UnaryStruct class

    register1 = UnaryStruct(A_DEVICE_REGISTER_1, "<B") # 8-bit number
    register2 = UnaryStruct(A_DEVICE_REGISTER_2, "<B") # 8-bit number

# The follow is for I2C communications
comm_port = I2C(SCL, SDA)
device = I2CDevice(comm_port, DEVICE_ADDRESS)
registers = DeviceControl(device)

# set the bits in the device
registers.register1 = 1 << 3 | 2
registers.register2 = 32
# display the device values for the bits
print("register 1: {}; register 2: {}".format(registers.register1, registers.register2))

# toggle the bits
registers.register1 = 2 << 3 | 5
registers.register2 = 60
# display the device values for the bits
print("register 1: {}; register 2: {}".format(registers.register1, registers.register2))