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