Skip to content

Commit

Permalink
Merge pull request #8 from mrmcwethy/examples
Browse files Browse the repository at this point in the history
added examples folder and example .py files
  • Loading branch information
ladyada committed Jan 12, 2018
2 parents 6977532 + 72152b3 commit 2576221
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/rwbit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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))
32 changes: 32 additions & 0 deletions examples/rwbits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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))
28 changes: 28 additions & 0 deletions examples/struct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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))
32 changes: 32 additions & 0 deletions examples/unarystruct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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))

0 comments on commit 2576221

Please sign in to comment.