]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Create and use mcp-i2c devices
authorSimon-Pierre Allaire <spa05@acuitysso.com>
Mon, 5 Oct 2020 16:15:33 +0000 (12:15 -0400)
committerSimon-Pierre Allaire <spallaire@distech-controls.com>
Wed, 7 Oct 2020 18:28:19 +0000 (14:28 -0400)
examples/mcps_busio_i2c.py [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index b90bc4c..6a64c5c
@@ -1,6 +1,4 @@
 import time
-import sys
-import board
 import busio
 import hid
 
@@ -8,8 +6,11 @@ from adafruit_blinka.microcontroller.mcp2221.mcp2221 import mcp2221 as _mcp2221
 from adafruit_blinka.microcontroller.mcp2221.mcp2221 import MCP2221 as _MCP2221
 from adafruit_blinka.microcontroller.mcp2221.i2c import I2C as _MCP2221I2C
 
+MLXADDR = 0x33
+ADDRID1 = 0x2407
 
-class MCP2221(_MCP2221): 
+
+class MCP2221(_MCP2221):
     def __init__(self, address):
         self._hid = hid.device()
         self._hid.open_path(address)
@@ -21,27 +22,36 @@ class MCP2221(_MCP2221):
 
 
 class MCP2221I2C(_MCP2221I2C):
-    def __init__(self, mcp2221, *, frequency=1000000):
+    def __init__(self, mcp2221, *, frequency=100000):
         self._mcp2221 = mcp2221
         self._mcp2221.i2c_configure(frequency)
 
 
 class I2C(busio.I2C):
-    def __init__(self, mcp2221_i2c, *, frequency=1000000):
+    def __init__(self, mcp2221_i2c):
         self._i2c = mcp2221_i2c
 
 
-def temp_c(data):
-    value = data[0] << 8 | data[1]
-    temp = (value & 0xFFF) / 16.0
-    if value & 0x1000:
-        temp -= 256.0
-    return temp
+addresses = [mcp["path"] for mcp in hid.enumerate(0x04D8, 0x00DD)]
+
+i2c_devices = []
+i2c_devices.append(I2C(MCP2221I2C(_mcp2221)))
+
+for addr in addresses:
+    try:
+        i2c_device = I2C(MCP2221I2C(MCP2221(addr)))
+        i2c_devices.append(i2c_device)
+    except OSError:
+        print("Device path: "+ str(addr)+ " is used")
 
 
 while True:
-    i2c.writeto(0x18, bytes([0x05]), stop=False)
-    result = bytearray(2)
-    i2c.readfrom_into(0x18, result)
-    print(temp_c(result))
-    time.sleep(0.5)
+    for i2c in i2c_devices:
+        addrbuf = bytearray(2)
+        addrbuf[0] = ADDRID1 >> 8
+        addrbuf[1] = ADDRID1 & 0xFF
+        inbuf  = bytearray(6)
+        i2c.writeto_then_readfrom(MLXADDR, addrbuf, inbuf)
+        print("Device "+str(i2c_devices.index(i2c))+": ")
+        print(inbuf)
+        time.sleep(0.5)