]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
completed busio i2c demo
authorladyada <limor@ladyada.net>
Wed, 20 Jun 2018 21:37:43 +0000 (17:37 -0400)
committerScott Shawcroft <scott@tannewt.org>
Wed, 27 Jun 2018 20:00:30 +0000 (13:00 -0700)
examples/pi_busio_i2c.py [new file with mode: 0644]
examples/pi_i2c.py
src/adafruit_blinka/microcontroller/raspi_23/i2c.py

diff --git a/examples/pi_busio_i2c.py b/examples/pi_busio_i2c.py
new file mode 100644 (file)
index 0000000..6e8e0ac
--- /dev/null
@@ -0,0 +1,33 @@
+import sys
+import time
+sys.path.append('/home/pi/Adafruit_Micropython_Blinka/src')
+sys.path.append('/home/pi/Adafruit_Python_GPIO')
+sys.path.append('/home/pi/Adafruit_Python_PureIO')
+
+import board
+import digitalio
+import busio
+
+print("hello blinka!")
+
+i2c = busio.I2C(board.SCL, board.SDA)
+
+print("I2C devices found: ", [hex(i) for i in i2c.scan()])
+
+if not 0x18 in i2c.scan():
+    print("Didn't find MCP9808")
+    exit()
+
+def temp_c(data):
+    value = data[0] << 8 | data[1]
+    temp = (value & 0xFFF) / 16.0
+    if value & 0x1000:
+        temp -= 256.0
+    return temp
+
+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)
index cdafa953e90f417064cb189f45fa39fcbc64fef2..6e8e0acf683aef0c535ccff03ea054f039930252 100644 (file)
@@ -2,6 +2,7 @@ import sys
 import time
 sys.path.append('/home/pi/Adafruit_Micropython_Blinka/src')
 sys.path.append('/home/pi/Adafruit_Python_GPIO')
 import time
 sys.path.append('/home/pi/Adafruit_Micropython_Blinka/src')
 sys.path.append('/home/pi/Adafruit_Python_GPIO')
+sys.path.append('/home/pi/Adafruit_Python_PureIO')
 
 import board
 import digitalio
 
 import board
 import digitalio
@@ -9,12 +10,24 @@ import busio
 
 print("hello blinka!")
 
 
 print("hello blinka!")
 
-
 i2c = busio.I2C(board.SCL, board.SDA)
 
 i2c = busio.I2C(board.SCL, board.SDA)
 
-print([hex(i) for i in i2c.scan()])
+print("I2C devices found: ", [hex(i) for i in i2c.scan()])
+
+if not 0x18 in i2c.scan():
+    print("Didn't find MCP9808")
+    exit()
 
 
-led = digitalio.DigitalInOut(board.D4)
-led.direction = digitalio.Direction.OUTPUT
-led.value = True
+def temp_c(data):
+    value = data[0] << 8 | data[1]
+    temp = (value & 0xFFF) / 16.0
+    if value & 0x1000:
+        temp -= 256.0
+    return temp
 
 
+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)
index a4647b69978003af533f17820d9f498fdff083c5..6e9f31cfae71ff8f4b4fa7558aad88b5f2ac3632 100644 (file)
@@ -1,4 +1,4 @@
-import smbus
+import Adafruit_PureIO.smbus as smbus
 import time
 
 class I2C:
 import time
 
 class I2C:
@@ -22,11 +22,20 @@ class I2C:
             raise RuntimeError("I2C Bus #%d not found, check if enabled in config!" % bus_num)
 
     def scan(self):
             raise RuntimeError("I2C Bus #%d not found, check if enabled in config!" % bus_num)
 
     def scan(self):
+        """Try to read a byte from each address, if you get an OSError it means the device isnt there"""
         found = []
         found = []
-        for addr in range(0,0x7F):
+        for addr in range(0,0x80):
             try:
                 self._i2c_bus.read_byte(addr)
             except OSError:
                 continue
             found.append(addr)
         return found
             try:
                 self._i2c_bus.read_byte(addr)
             except OSError:
                 continue
             found.append(addr)
         return found
+
+    def writeto(self, address, buffer, stop=True):
+        self._i2c_bus.write_i2c_block_data(address, buffer[0], buffer[1:])
+
+    def readfrom_into(self, address, buffer, stop=True):
+        readin = self._i2c_bus.read_bytes(address, len(buffer))
+        for i in range(len(buffer)):
+            buffer[i] = readin[i]