Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if os.getenv("VIRTUAL_ENV") == None:
print("Please activate your virtual environment then re-run script")
exit(0)
# Ensure platform info is sourced
if os.getenv("PLATFORM") == None:
print("Please source your platform info then re-run script")
exit(0)
# Ensure platform is usb-to-i2c enabled
if os.getenv("IS_USB_I2C_ENABLED") != "true":
print("Platform is not usb-to-i2c enabled")
exit(0)
# Initialize i2c instance with the USB-I2C cable
i2c_controller = I2cController()
i2c_controller.configure("ftdi://ftdi:232h/1")
# Get our I2C bus multiplexer (MUX).
# It's really a DAC that lets us have 8 different I2C busses.
mux_address = int(os.getenv("DEFAULT_MUX_ADDRESS"), 16)
i2c = i2c_controller.get_port(mux_address)
# Set MUX channel byte to 3, for the PFC_EDU board and 6 color LED DAC.
channel = int(3)
channel_byte = 0x01 << channel
# Write to the MUX, to set the channel number
i2c.write([channel_byte])
# Set up to talk to the LED DAC at I2C address 0x47
led_dac = i2c_controller.get_port(0x47)
print("Please activate your virtual environment then re-run script")
exit(0)
# Ensure platform info is sourced
if os.getenv("PLATFORM") == None:
print("Please source your platform info then re-run script")
exit(0)
# Ensure platform is usb-to-i2c enabled
if os.getenv("IS_USB_I2C_ENABLED") != "true":
print("Platform is not usb-to-i2c enabled")
exit(0)
# Initialize i2c instance
print("I2C cable init...")
i2c_controller = I2cController()
i2c_controller.configure("ftdi://ftdi:232h/1")
address = int(sys.argv[1], 16)
print("I2C address 0x{:2X}".format(address))
i2c = i2c_controller.get_port(address)
# Get the port from the command line
port = int(sys.argv[2])
byte = 0x30 + port
# Get the value in hex from the command line
value = int(sys.argv[3], 16)
# Set the port high
print("Port={} value={}/0x{:2X}".format(port, value, value))
i2c.write([byte, value, 0x00])
if os.getenv("VIRTUAL_ENV") == None:
print("Please activate your virtual environment then re-run script")
exit(0)
# Ensure platform info is sourced
if os.getenv("PLATFORM") == None:
print("Please source your platform info then re-run script")
exit(0)
# Ensure platform is usb-to-i2c enabled
if os.getenv("IS_USB_I2C_ENABLED") != "true":
print("Platform is not usb-to-i2c enabled")
exit(0)
# Initialize i2c instance
i2c_controller = I2cController()
i2c_controller.configure("ftdi://ftdi:232h/1")
i2c = i2c_controller.get_port(0x47)
# Set all channels low
for i in range(8):
byte = 0x30 + i
i2c.write([byte, 0x00, 0x00])
print("Please activate your virtual environment then re-run script")
exit(0)
# Ensure platform info is sourced
if os.getenv("PLATFORM") == None:
print("Please source your platform info then re-run script")
exit(0)
# Ensure platform is usb-to-i2c enabled
if os.getenv("IS_USB_I2C_ENABLED") != "true":
print("Platform is not usb-to-i2c enabled")
exit(0)
# Initialize i2c instance
print("I2C cable init...")
i2c_controller = I2cController()
i2c_controller.configure("ftdi://ftdi:232h/1")
address = int(sys.argv[1], 16)
print("I2C address 0x{:2X}".format(address))
i2c = i2c_controller.get_port(address)
# Get the port from the command line
port = int(sys.argv[2])
byte = 0x30 + port
# Toggle the port high, wait a sec, then low.
print("Port {} high...".format(port))
i2c.write([byte, 0xFF, 0x00]) # to high
time.sleep(2)
print("Port {} low...".format(port))
i2c.write([byte, 0x00, 0x00]) # to low
if os.getenv("VIRTUAL_ENV") == None:
print("Please activate your virtual environment then re-run script")
exit(0)
# Ensure platform info is sourced
if os.getenv("PLATFORM") == None:
print("Please source your platform info then re-run script")
exit(0)
# Ensure platform is usb-to-i2c enabled
if os.getenv("IS_USB_I2C_ENABLED") != "true":
print("Platform is not usb-to-i2c enabled")
exit(0)
# Initialize i2c instance
i2c_controller = I2cController()
i2c_controller.configure("ftdi://ftdi:232h/1")
i2c = i2c_controller.get_port(0x15)
# Get co2 data bytes
i2c.write([0x04, 0x13, 0x8A, 0x00, 0x01]) # status
bytes_ = i2c.read(4)
# Parse co2 data bytes
_, _, msb, lsb = bytes_
co2 = float(msb * 256 + lsb)
co2 = round(co2, 0)
print("CO2: {} ppm".format(co2))
if os.getenv("VIRTUAL_ENV") == None:
print("Please activate your virtual environment then re-run script")
exit(0)
# Ensure platform info is sourced
if os.getenv("PLATFORM") == None:
print("Please source your platform info then re-run script")
exit(0)
# Ensure platform is usb-to-i2c enabled
if os.getenv("IS_USB_I2C_ENABLED") != "true":
print("Platform is not usb-to-i2c enabled")
exit(0)
# Initialize i2c instance
i2c_controller = I2cController()
i2c_controller.configure("ftdi://ftdi:232h/1")
i2c = i2c_controller.get_port(0x15)
# Read data bytes
i2c.write([0x04, 0x13, 0x8B, 0x00, 0x01])
bytes_ = i2c.read(4)
# Parse data bytes
_, _, status_msb, status_lsb = bytes_
error_condition = (bool(bitwise.get_bit_from_byte(0, status_lsb)),)
flash_error = (bool(bitwise.get_bit_from_byte(1, status_lsb)),)
calibration_error = (bool(bitwise.get_bit_from_byte(2, status_lsb)),)
rs232 = (bool(bitwise.get_bit_from_byte(0, status_msb)),)
rs485 = (bool(bitwise.get_bit_from_byte(1, status_msb)),)
i2c_ = (bool(bitwise.get_bit_from_byte(2, status_msb)),)
warm_up_mode = (bool(bitwise.get_bit_from_byte(3, status_msb)),)
def __init__(self, pin_id=None):
# setup GPIO controller if not done yet
# use one provided by I2C as default
if not Pin.ft232h_gpio:
from pyftdi.i2c import I2cController
i2c = I2cController()
i2c.configure("ftdi:///1")
Pin.ft232h_gpio = i2c.get_gpio()
# check if pin is valid
if pin_id:
if Pin.ft232h_gpio.all_pins & 1 << pin_id == 0:
raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
# ID is just bit position
self.id = pin_id
def open(self) -> None:
"""Opens io stream."""
try:
if os.getenv("IS_I2C_ENABLED") == "true":
device_name = "/dev/i2c-{}".format(self.bus)
self.io = io.open(device_name, "r+b", buffering=0)
elif os.getenv("IS_USB_I2C_ENABLED") == "true":
self.io = I2cController()
self.io.configure("ftdi://ftdi:232h/1") # type: ignore
else:
message = "Platform does not support i2c communication"
raise InitError(message)
except (PermissionError, I2cIOError, I2cNackError) as e:
message = "Unable to open device io: {}".format(device_name)
raise InitError(message, logger=self.logger) from e
def scan(url):
"""Open an I2c connection to a slave."""
i2c = I2cController()
slaves = []
getLogger('pyftdi.i2c').setLevel(ERROR)
try:
i2c.set_retry_count(1)
i2c.configure(url)
for addr in range(i2c.HIGHEST_I2C_ADDRESS+1):
port = i2c.get_port(addr)
try:
port.read(0)
slaves.append('X')
except I2cNackError:
slaves.append('.')
finally:
i2c.terminate()
columns = 16
row = 0