1
0

More than 1 year has passed since last update.

Eclipse MRAA on Raspberry Pi 4 - Python

Posted at

Eclipse MRAAを、Raspberry Pi 4で動かしてみました。Pythonで。

MRAAとは?

GPIOやI2Cといった、ハードウェア操作するC/C++ライブラリです。
実装はC/C++ですが、JavaやPython、JavaScriptからも呼び出すことができます。

詳しくはこちら。

Raspberry Pi 4

使用したラズパイとOSはこちらです。

  • Raspberry Pi 4 Model B
  • Raspberry Pi OS Lite (32-bit) - 2022-04-04

インストール

ソースを取得してコンパイル、インストールします。

sudo apt update
sudo apt install git build-essential swig python3-dev cmake libjson-c-dev
git clone https://github.com/eclipse/mraa
cd mraa
mkdir build
cd build
cmake -DBUILDSWIGNODE=OFF ..
make
sudo make install
sudo ldconfig

ImportError: libmraa.so.2: cannot open shared object file: No such file or directoryエラーが発生したときは、sudo ldconfigを忘れている可能性大です。→参考?

デジタル入力

digitalread-switch.py
#!/usr/bin/env python3

import mraa
import time

switch = mraa.Gpio(7)  # Pin7 - GPIO4
switch.mode(mraa.MODE_PULLUP)   # IT DON'T WORK :-P
switch.dir(mraa.DIR_IN)

while True:
    if switch.read() == 1:
        print("ON")
    else:
        print("off")
    time.sleep(1)

ハードウェアの結線図は、参考文献1のP181 図5です。

デジタル出力

digitalwrite-led.py
#!/usr/bin/env python3

import mraa
import time

led = mraa.Gpio(7)  # Pin7 - GPIO4
led.dir(mraa.DIR_OUT)

while True:
    led.write(1)
    time.sleep(0.2)

    led.write(0)
    time.sleep(0.8)

ハードウェアの結線図は、参考文献1のP189 図2です。

PWM出力

(TBC)

I2C

i2c-accel.py
#!/usr/bin/env python3

import mraa
import time

sensor = mraa.I2c(0)
sensor.address(0x1d)

# print(sensor.readReg(0x00))

sensor.write(bytearray([0x2d, 0x08]))

while True:
    sensor.write(bytearray([0x32]))
    val = sensor.read(6)

    x = int.from_bytes([val[0], val[1]], "little", signed=True)
    y = int.from_bytes([val[2], val[3]], "little", signed=True)
    z = int.from_bytes([val[4], val[5]], "little", signed=True)
    print(f"{x} {y} {z}")

    time.sleep(0.2)

ハードウェアの結線図は、参考文献1のP218 図6です。

SPI

spi-dac.py
#!/usr/bin/env python3

import mraa
import time

dac = mraa.Spi(0)

while True:
    for i in range(100):
        val = int(i / 100 * 3 / 4.096 * 4096)
        val_bytes = val.to_bytes(2, "big")
        dac.write(bytearray([(0b0001 << 4) | val_bytes[0], val_bytes[1]]))
        time.sleep(0.001)

ハードウェアの結線図は、参考文献1のP247 図5です。

UART

2022/7/14時点、Raspberry Pi 4でUARTが動かない🤔。→issue#1090

参考リンク

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0