adafruit / Adafruit_CircuitPython_FeatherWing

CircuitPython classes for Adafruit FeatherWings
MIT License
11 stars 13 forks source link

I2C pin conflicts #35

Closed jerryneedell closed 5 years ago

jerryneedell commented 5 years ago

This is just FYI regardsing some experimentation I have been doing

I was trying to use the an alphanumeric display vi the featherwing library along with some other I2C sesnors. I wanted to get the sensor data and display it on the alphanumeric featherwing. A common use case, I think.

When I tried to create the sensor objects, i got errors indicating that the I2C pins were "in use".

I experimented a bit using the board.I2C() function instead of the busio.I2C() with the sensors and got it working, but then found that if I make the following change, I think shared.py becomes unnecessary.

uit_featherwing$ git diff
diff --git a/adafruit_featherwing/alphanum_featherwing.py b/adafruit_featherwing/alphanum_featherwing.py
index 06b0409..cf445a6 100755
--- a/adafruit_featherwing/alphanum_featherwing.py
+++ b/adafruit_featherwing/alphanum_featherwing.py
@@ -31,8 +31,8 @@ Helper for using the `14-Segment AlphaNumeric FeatherWing <https://www.adafruit.
 __version__ = "0.0.0-auto.0"
 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"

+import board
 import adafruit_ht16k33.segments as segments
-from adafruit_featherwing import shared
 from adafruit_featherwing.led_segments import Segments

 class AlphaNumFeatherWing(Segments):
@@ -42,5 +42,5 @@ class AlphaNumFeatherWing(Segments):
        Automatically uses the feather's I2C bus."""
     def __init__(self, address=0x70):
         super().__init__()
-        self._segments = segments.Seg14x4(shared.I2C_BUS, address)
+        self._segments = segments.Seg14x4(board.I2C(), address)
         self._segments.auto_write = False

here is the test code I am running

import time
import board
import busio
import adafruit_sht31d
import adafruit_veml6070

from adafruit_featherwing import alphanum_featherwing

display = alphanum_featherwing.AlphaNumFeatherWing()
sensor = adafruit_sht31d.SHT31D(board.I2C())
uv = adafruit_veml6070.VEML6070(board.I2C())

loopcount = 0
while True:
    uv_raw = uv.read
    risk_level = uv.get_index(uv_raw)
    print('Reading: {0} | Risk Level: {1}'.format(uv_raw, risk_level))
    display.print(uv_raw)
    time.sleep(2)
    temperature = sensor.temperature
    print("\nTemperature: %0.1f C" % temperature)
    display.print(temperature)
    print("Humidity: %0.1f %%" % sensor.relative_humidity)
    loopcount += 1
    time.sleep(2)
    # every 10 passes turn on the heater for 1 second
    if loopcount == 10:
        loopcount = 0
        sensor.heater = True
        print("Sensor Heater status =", sensor.heater)
        time.sleep(1)
        sensor.heater = False
        print("Sensor Heater status =", sensor.heater)

One issue I an into was that in the current master, board.I2C() is not implemented for the particle boards and I was testing on a particle_argon. I implmented it in a local build and it now is working. It was already implemented in the feather_nrf52840.

makermelissa commented 5 years ago

The shared library was in place from @tannewt and @kattni's previous work. The library isn't very big and iI don't know if it's set up this way for more for backwards compatibility or convenience. Maybe they can chime in on this one.

jerryneedell commented 5 years ago

There may well be better ways to do this -- I just wanted to capture the issue.

jerryneedell commented 5 years ago

Why don't we just pass the I2C device into the alphanum_featherwing (and others) library like we do for the sensor libraries or have a n optional argument for it - if it is omitted or None then the lib can create it.

makermelissa commented 5 years ago

I'd like to keep it simple to initialize, however giving the user the option to pass it in but initializing inside the class like it does now if it's not passed in may be a good compromise.

jerryneedell commented 5 years ago

I think that is better than the user having to import shared.py from adafruit_featherwing and use shared.I2C_BUS. That works, but seems awfully kludgey to me

makermelissa commented 5 years ago

As Kattni said, it was intended to reduce duplicate code, but if it is broken we can change it.

jerryneedell commented 5 years ago

NP -- your call -- as long there is a documented way to use multiple sensors with the wings, I'll be happy.

makermelissa commented 5 years ago

Yeah, I was eventually planning on adding some more examples that used multiple wings, so I may attempt that sooner than I originally planned.

tannewt commented 5 years ago

I think the shared bit was added before board.I2C(). I'd suggest using board.I2C() from now on and replacing the shared stuff. By using board.I2C() it's easier to use for external stuff.

makermelissa commented 5 years ago

Fixed by #36.