Mraa 使用
MRAA 简介
Eclipse Mraa(Libmraa)是一个包含 Java、Python 和 JavaScript 绑定的 C/C++ 库,用于连接各种物联网和边缘平台上的 I/O 引脚和总线,其 API 结构合理,端口名称/编号与所在的电路板相匹配。
安装 MRAA
sudo apt-get update -y
sudo apt-get install libmraa2 libmraa-dev libmraa-java python3-mraa mraa-tools -y
MRAA 命令行工具
GPIO
- mraa-gpio list: 列出所有可用引脚
- mraa-gpio get pin: 获取引脚状态
- mraa-gpio set pin level: 设置引脚状态
- mraa-gpio version: 获取版本信息
I2C
- mraa-i2c list: 列出所有可用 I2C 总线
- mraa-i2c version: 获取 mraa 版本和板卡名称
- mraa-i2c detect bus: 列出指定总线上检测到的设备
- mraa-i2c get bus device reg: 从指定设备寄存器中获取值
- mraa-i2c set bus device reg value: 将指定设备寄存器设置为值
UART
- mraa-uart list: 列出所有可用 UART 端口
- mraa-uart dev dev_num baud customized_baud send str: 将字符串 str 发送到指定端口
- mraa-uart dev dev_num baud customized_baud recv 1000: 读取端口 dev_num 上的数据,最多读取 1000 字节,并将它们显示在 stdout 上
mraa-uart
使用例子示例代码
GPIO
- C
- Python
blink.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa/gpio.h"
/* gpio declaration */
#define GPIO_PIN 3
volatile sig_atomic_t flag = 1;
void sig_handler(int signum) {
if(signum == SIGINT) {
fprintf(stdout, "Exiting...\n");
flag = 0;
}
}
int main(void) {
mraa_result_t status = MRAA_SUCCESS;
mraa_gpio_context gpio;
signal(SIGINT, sig_handler);
mraa_init();
/* initialize GPIO */
gpio = mraa_gpio_init(GPIO_PIN);
if(gpio == NULL) {
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN);
mraa_deinit();
return EXIT_FAILURE;
}
/* set output */
status = mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
while(flag) {
status = mraa_gpio_write(gpio, 1);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
sleep(1);
status = mraa_gpio_write(gpio, 0);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
sleep(1);
}
/* release gpio's */
status = mraa_gpio_close(gpio);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
mraa_deinit();
return EXIT_SUCCESS;
err_exit:
mraa_result_print(status);
mraa_deinit();
return EXIT_FAILURE;
}
使用方法:
-
将 led 信号脚连接到 PIN3,VCC 接板子 VCC,GND 接板子 GND
-
测试
gcc blink.c -lmraa -o blink && sudo ./blink
若测试成功,led 会有一个闪烁的效果。
blink.py
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import mraa
import time
pin = 3 # set pin num
try:
gpio = mraa.Gpio(pin)
gpio.dir(mraa.DIR_OUT) # set mode as input
while True:
gpio.write(1)
time.sleep(1)
gpio.write(0)
time.sleep(1)
except KeyboardInterrupt:
print("\nstop\n")
使用方法:
-
将 led 信号脚连接到 PIN3,VCC 接板子 VCC,GND 接板子 GND
-
测试
sudo python3 Blink.py
若测试成功,led 会有一个闪烁的效果。
I2C
- C
- Python
led_i2c_blink.c
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa/i2c.h"
#define I2C_BUS 2
#define LED_ADDRESS 0x20
volatile sig_atomic_t flag = 1;
void sig_handler(int signum) {
if (signum == SIGINT) {
fprintf(stdout, "Exiting...\n");
flag = 0;
}
}
int main() {
mraa_result_t status = MRAA_SUCCESS;
mraa_i2c_context i2c;
signal(SIGINT, sig_handler);
mraa_init();
i2c = mraa_i2c_init(I2C_BUS);
if(i2c == NULL) {
fprintf(stderr, "Failed to initialize I2C\n");
mraa_deinit();
return EXIT_FAILURE;
}
/* set slave address */
status = mraa_i2c_address(i2c, LED_ADDRESS);
if(status != MRAA_SUCCESS) {
return -1;
}
while(1) {
/**
* Write a single word to an i2c context
*
* @param dev The i2c context
* @param data The word to write
* @param command The register
* @return Result of operation
*/
mraa_i2c_write_byte_data(i2c, 0x23, 0x08);
sleep(1);
mraa_i2c_write_byte_data(i2c, 0x21, 0x01);
sleep(1);
mraa_i2c_write_byte_data(i2c, 0x33, 0x12);
sleep(1);
mraa_i2c_write_byte_data(i2c, 0x1f, 0x42);
sleep(1);
}
return 0;
}
使用方法:
-
打开 I2C2 的 Overlay,并重启
-
接线: SDA <--> SDA, SCL <--> SCL, GDN <--> GND, VCC <--> VCC
-
打开终端,输入以下命令进行测试
gcc led_i2c_blink.c -lmraa -o led_i2c_blink && sudo ./led_i2c_blink