49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
#/usr/bin/python3
|
|
|
|
"""
|
|
is the port available:
|
|
ls /dev/*i2c*
|
|
|
|
scan connected I2C slaves:
|
|
sudo i2cdetect -y 1
|
|
"""
|
|
|
|
from enum import IntEnum
|
|
import time
|
|
|
|
|
|
# Prefer system-installed `smbus2`, otherwise fall back to local `smbus2.py`.
|
|
try:
|
|
import smbus2
|
|
except ImportError:
|
|
import RPi.fake_smbus as smbus2 # type: ignore
|
|
print('smbus2 not available; using fake smbus2.py')
|
|
|
|
_bus = smbus2.SMBus(1)
|
|
|
|
def poll_i2c_data():
|
|
""" poll I2C data from Arduino """
|
|
try:
|
|
data = _bus.read_i2c_block_data(0x08, 0, 4)
|
|
|
|
x_position_update = int.from_bytes(bytes(data[0:2]), 'little', signed=True)
|
|
z_position_update = int.from_bytes(bytes(data[2:4]), 'little', signed=True)
|
|
print(f"X Position: {x_position_update}, Z Position: {z_position_update}")
|
|
|
|
except IOError as e:
|
|
print(f"I2C read error: {e}")
|
|
except OSError as e:
|
|
print(f"I2C OS error: {e}")
|
|
|
|
def main():
|
|
try:
|
|
while 1:
|
|
poll_i2c_data()
|
|
time.sleep(0.2) # Poll every 200ms
|
|
|
|
except KeyboardInterrupt:
|
|
raise SystemExit
|
|
|
|
if __name__ == '__main__':
|
|
main()
|