クラス I2C
¶
例
# Import the I2C class
from robot_hat import I2C
# You can scan for available I2C devices
print([f"0x{addr:02X}" for addr in I2C().scan()])
# You should see at least one device address 0x14, which is the
# on board MCU for PWM and ADC
# Initialize a I2C object with device address, for example
# to communicate with on board MCU 0x14
mcu = I2C(0x14)
# Send ADC channel register to read ADC, 0x10 is Channel 0, 0x11 is Channel 1, etc.
mcu.write([0x10, 0x00, 0x00])
# Read 2 byte for MSB and LSB
msb, lsb = mcu.read(2)
# Convert to integer
value = (msb << 8) + lsb
# Print the value
print(value)
I2Cプロトコルの詳細については、adc.pyとpwm.pyをご覧ください
API
- class robot_hat.I2C(address=None, bus=1, *args, **kwargs)¶
ベースクラス:
_Basic_class
I2Cバスの読み書き機能
- __init__(address=None, bus=1, *args, **kwargs)¶
I2Cバスを初期化する
- パラメータ
address (int) -- I2Cデバイスアドレス
bus (int) -- I2Cバス番号
- scan()¶
I2Cバスをスキャンしてデバイスを検出する
- 戻り値
見つかったデバイスのI2Cアドレスのリスト
- 戻り値の型
list
- write(data)¶
I2Cデバイスにデータを書き込む
- パラメータ
data (int/list/bytearray) -- 書き込むデータ
- 例外
書き込みがint、リスト、またはバイト配列でない場合はValueError
- read(length=1)¶
I2Cデバイスからデータを読み取る
- パラメータ
length (int) -- 受信するバイト数
- 戻り値
受信したデータ
- 戻り値の型
list
- mem_write(data, memaddr)¶
特定のレジスタアドレスにデータを送信する
- パラメータ
data (int/list/bytearray) -- 送信するデータ、int、リスト、またはバイト配列
memaddr (int) -- レジスタアドレス
- 例外
ValueError -- データがint、リスト、またはバイト配列でない場合
- mem_read(length, memaddr)¶
特定のレジスタアドレスからデータを読み取る
- パラメータ
length (int) -- 受信するバイト数
memaddr (int) -- レジスタアドレス
- 戻り値
エラーがない場合は受信したバイト配列データ、エラーがある場合はFalse
- 戻り値の型
list/False
- is_avaliable()¶
I2Cデバイスが利用可能かどうかを確認する
- 戻り値
I2Cデバイスが利用可能な場合はTrue、そうでない場合はFalse
- 戻り値の型
bool