Skip to content

Enable user to pass platform's native I2C object to get_i2c_driver() #31

@sfe-SparkFro

Description

@sfe-SparkFro

If a user wants to use a non-default I2C bus, the current implementation requires the user to provide arguments for the platform's native I2C implementation:

################################################################################
# Qwiic I2C Driver
################################################################################

import qwiic_i2c

# MicroPython and CircuitPython
i2c_driver = qwiic_i2c.get_i2c_driver(sda = 0, scl = 1)

# Linux
i2c_driver = qwiic_i2c.get_i2c_driver(iBus = 0)

################################################################################
# Qwiic Device
################################################################################

# Pass driver to the Qwiic device constructor
import qwiic_device
my_device = qwiic_device.QwiicDevice(i2c_driver = i2c_driver)

However, it would be beneficial to allow the user to provide a platform's native I2C object that's already pre-configured:

################################################################################
# Platform I2C Object
################################################################################

# MicroPython
import machine
native_i2c = machine.I2C(sda = 0, scl = 1)
native_i2c = machine.SoftI2C(sda = 0, scl = 1)

# CircuitPython
import busio
native_i2c = busio.I2C(sda = 0, scl = 1)

# Linux
import smbus2
native_i2c = smbus2.SMBus(iBus = 0)

################################################################################
# Qwiic I2C Driver
################################################################################

# All platforms
import qwiic_i2c
i2c_driver = qwiic_i2c.get_i2c_driver(native_i2c)

################################################################################
# Qwiic Device
################################################################################

# Pass driver to the Qwiic device constructor
import qwiic_device
my_device = qwiic_device.QwiicDevice(i2c_driver = i2c_driver)

One major benefit is the ability to use a SoftI2C object in MicroPython, which is current impossible.


This is bigger scope, but it would be nice if all the device drivers could also accept the native_i2c object:

################################################################################
# Platform I2C Object
################################################################################

# MicroPython
import machine
native_i2c = machine.I2C(sda = 0, scl = 1)
native_i2c = machine.SoftI2C(sda = 0, scl = 1)

# CircuitPython
import busio
native_i2c = busio.I2C(sda = 0, scl = 1)

# Linux
import smbus2
native_i2c = smbus2.SMBus(iBus = 0)

################################################################################
# Qwiic Device
################################################################################

# Pass native I2C object to the Qwiic device constructor
import qwiic_device
my_device = qwiic_device.QwiicDevice(i2c_driver = native_i2c)

The device driver constructors would have to check isinstance(i2c_driver, qwiic_i2c.I2CDriver):

 class QwiicDevice():
     def __init__(self, i2c_driver, ...):
         # Load the I2C driver if one isn't provided
         if i2c_driver is None:
             self._i2c = qwiic_i2c.getI2CDriver()
             if self._i2c is None:
                 print("Unable to load I2C driver for this platform.")
                 return
+        # Load the I2C driver if native I2C object is provided
+        elif not isinstance(i2c_driver, qwiic_i2c.I2CDriver):
+            self._i2c = qwiic_i2c.getI2CDriver(i2c_driver)
         else:
             self._i2c = i2c_driver      

This would eliminate the need for users to call i2c_driver = qwiic_i2c.get_i2c_driver(...) (which they are likely unfamiliar with) in favor of only having to use the platform's native I2C object (which they are likely already familiar with).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions