Skip to content

Commit

Permalink
suppoort multiple boards at once, via serial number of the chip
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaz.strus committed Nov 19, 2024
1 parent b91d3fc commit 8c10d94
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
50 changes: 50 additions & 0 deletions luxonis_example/multipleBoards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
from adafruit_blinka.microcontroller.rp2040_u2if.pin import Pin
from adafruit_blinka.microcontroller.rp2040_u2if.rp2040_u2if import RP2040_u2if
from specific_board import init_specific_board, display_connected_rp2040, RP2040_u2if_init

os.environ["BLINKA_U2IF"]="1"


display_connected_rp2040()


import time
import adafruit_apds9960.apds9960
# Initialize I2C using u2if's I2C interface
#board2=init_specific_board(0xE6636825930C7C23)

#This class is more low level, used mostly for GPIO manipulation.
rp = RP2040_u2if_init("0xE6636825930C7C23")


# This is high level adafruit class used for I2C.
# Do not import the board class if you are using multiple rp2040 without this method as you cant control the serial of the board
board1, busio1=init_specific_board("0xE462B0659F1B2036")
i2c = busio1.I2C(board1.GP5, board1.GP4) # SCL, SDA for default I2C1
sensor = adafruit_apds9960.apds9960.APDS9960(i2c)


# Enable color sensing
sensor.enable_color = True

print("INIT GPIO")
rp.gpio_init_pin(0, Pin.OUT,Pin.PULL_NONE)
rp.gpio_init_pin(1, Pin.IN,Pin.PULL_NONE)
# Main loop to read RGB data
print("Inside main loop")
r, g, b, c = 1,1,1,1
while True:
r, g, b, c = sensor.color_data # Red, Green, Blue, and Clear channel data
print(f"Red: {r}, Green: {g}, Blue: {b}, Clear: {c}")
if r > g and r > b:
print("RED")
if g > r and g > b:
print("GREEN")
if b > g and b > r:
print("BLUE")
print(rp.gpio_get_pin(1))
(rp.gpio_set_pin(0, 0))
time.sleep(2)
(rp.gpio_set_pin(0, 1))
time.sleep(1)
47 changes: 47 additions & 0 deletions luxonis_example/muxerExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
os.environ["BLINKA_U2IF"]="1"
from time import sleep
from specific_board import init_specific_board, display_connected_rp2040, RP2040_u2if_init
import digitalio
display_connected_rp2040()



# led 0 means the USB is on the LED soldiered side

# Set up a GPIO pin as an output
board, busio = init_specific_board("0xE6636825930C7C23")

button = digitalio.DigitalInOut(board.GP0)
button.direction = digitalio.Direction.OUTPUT
button.value=True

usbLed = digitalio.DigitalInOut(board.GP1)
usbLed.direction = digitalio.Direction.INPUT

def press_button(button):
print("Pressing button")
button.value = False
# If delay is too small button will not siwtch. With delay of 3 seconds, this worked pretty much 100% od the time
sleep(3)
button.value = True
#If you retry multiple times, you need this delay.
sleep(2)


def toogle_to_correct(led, button, wanted_state):
for i in range(5):
if bool(int(wanted_state)) == led.value:
return True
else:
press_button(button)

raise Exception("Toogle unsuccessful, didnt change states")


while True:
toogle_to_correct(usbLed, button, True)
print(usbLed.value)
sleep(1)
toogle_to_correct(usbLed, button, False)
print(usbLed.value)
1 change: 1 addition & 0 deletions src/adafruit_blinka/microcontroller/rp2040_u2if/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, pin_id=None):
self.id = pin_id
self._mode = None
self._pull = None
self.rp2040_u2if=rp2040_u2if

# pylint:disable = no-self-use
def _u2if_open_hid(self, vid, pid):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ def _reset(self):
# ----------------------------------------------------------------
# MISC
# ----------------------------------------------------------------
def open(self, vid, pid):
def open(self, vid, pid, serial=None):
"""Open HID interface for given USB VID and PID."""

if self._opened:
return
self._vid = vid
self._pid = pid
self._hid = hid.device()
self._hid.open(self._vid, self._pid)
if not serial:
serial =os.environ["CURRENT_BOARD_SERIAL"]
self._serial =serial
self._hid.open(self._vid, self._pid, self._serial)
if RP2040_U2IF_RESET_DELAY >= 0:
self._reset()
self._opened = True
Expand Down
22 changes: 22 additions & 0 deletions src/specific_board.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
from adafruit_blinka.microcontroller.rp2040_u2if.rp2040_u2if import RP2040_u2if

def init_specific_board(serial):
os.environ["CURRENT_BOARD_SERIAL"] = serial
import board
import busio
return board, busio

def RP2040_u2if_init(serial):
os.environ["CURRENT_BOARD_SERIAL"] = serial
rp= RP2040_u2if()
rp.open(51966, 16389, serial=serial)
return rp

def display_connected_rp2040():
import hid
for dev in hid.enumerate():
vendor = dev["vendor_id"]
product = dev["product_id"]
if vendor == 0xCAFE and product == 0x4005:
print(dev)

0 comments on commit 8c10d94

Please sign in to comment.