pyftdiでI2Cデバイスをつなげてみました。
接続したデバイスは昔秋月で買ったLPS331使用の大気圧センサモジュールです。
回路はこんな感じ。
右側のがLPS331使用の大気圧センサモジュール。真ん中のLEDは使用していません。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from os import environ
import sys
from pyftdi.i2c import I2cController
from time import sleep
class LPS331(object):
"""
"""
def __init__(self):
self._i2c = I2cController()
def open(self):
url = environ.get('FTDI_DEVICE', 'ftdi://ftdi:232h/1')
self._i2c.configure(url,frequency=400000)
self._port = self._i2c.get_port(0x5c)
def close(self):
pass
def init(self):
port = self._port
port.write([0x10,0x6A]) # Pres Average 512 / Temp Average 64
port.write([0x20,0xF4]) # ODR=25Hz、BDU=1
def read(self):
port = self._port
out1 = port.exchange([0x08],1)
out2 = port.exchange([0x09],1)
out3 = port.exchange([0x0a],1)
refp = (out1[0] + out2[0]*256 + out3[0]*65536)/4096.0
out1 = port.exchange([0x28],1)
out2 = port.exchange([0x29],1)
out3 = port.exchange([0x2a],1)
pres = (out1[0] + out2[0]*256 + out3[0]*65536)/4096.0
out1 = port.exchange([0x2b],1)
out2 = port.exchange([0x2c],1)
out = (out1[0] + out2[0]*256)
if out >= 0x8000 : out -= 0x10000
temp = 42.5 + out/480.0
return(refp,pres,temp)
if __name__ == '__main__':
lps331 = LPS331()
lps331.open()
lps331.init()
while True:
(refp,pres,temp) = lps331.read()
print(refp,pres,temp)
sleep(1)
実行すると
$ python ftdi-lps331.py 0.0 1012.830078125 27.485416666666666 0.0 1012.68212890625 27.504166666666666 0.0 1012.84716796875 27.525 0.0 1012.838134765625 27.508333333333333 0.0 1012.654296875 27.541666666666664 0.0 1012.570556640625 27.53125
という感じで気圧と気温を取得できました。
これでデスクトップPCでもI2Cデバイスの評価などができて便利そうです。

