h文件
/*
    NVIDIA Jetson Xavier NX 控制GPIO
    在linux系统中以文件io的方式控制GPIO示例,开发板为NVIDIA Jetson Xavier NX,其它公司的开发板也可使用。

    此代码功能:GPIO-09接入蜂鸣器,高电平开启,低电平关闭。
*/

#ifndef NVIDIAGPIO_H
#define NVIDIAGPIO_H

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>

using namespace std;

#define ON "1"              //开启
#define OFF "0"             //关闭
#define BUZZER "436"        //控制蜂鸣器引脚
#define DIRECTION_OUT "out"     //方向为输出
#define DIRECTION_IN "in"     //方向为输出

class NvidiaGpio
{
public:
    NvidiaGpio();
    ~NvidiaGpio();

    int openGpio(const char *port); //导入GPIO
    int closeGpio(const char *port); //导出GPIO

    int setGpioDirection(const char *port, const char *direction); //设置GPIO输入/输出模式

    int writeGpioValue(const char *port, const char *value); //设置GPIO值
    int readGpioValue(const char *port); //读取GPIO值
};

#endif // NVIDIAGPIO_H

cpp文件
#include "nvidiagpio.h"

NvidiaGpio::NvidiaGpio()
{

}

NvidiaGpio::~NvidiaGpio()
{

}

int NvidiaGpio::openGpio(const char *port)
{
    int fd = -1;
    const char *path = "/sys/class/gpio/export";

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to open gpio! ");
        return -1;
    }

    write(fd, port, sizeof(port));
    close(fd);

    return 0;
}

int NvidiaGpio::closeGpio(const char *port)
{
    int fd = -1;
    const char *path = "/sys/class/gpio/unexport";

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to close gpio! ");
        return -1;
    }

    write(fd, port, sizeof(port));
    close(fd);

    return 0;
}

int NvidiaGpio::setGpioDirection(const char *port, const char *direction)
{
    int fd = -1;
    char path[40] = {0};
    sprintf(path, "/sys/class/gpio/gpio%s/direction", port);

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to set GPIO dirention! ");
        return -1;
    }

    write(fd, direction, sizeof(direction));
    close(fd);

    return 0;
}

int NvidiaGpio::writeGpioValue(const char *port, const char *value)
{
    int fd = -1;
    char path[40] = {0};
    sprintf(path, "/sys/class/gpio/gpio%s/value", port);

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to set GPIO value! ");
        return -1;
    }

    write(fd, value, sizeof(value));
    close(fd);

    return 0;
}

int NvidiaGpio::readGpioValue(const char *port)
{
    int fd = -1;
    int value = 0;
    char path[40] = {0};
    sprintf(path, "/sys/class/gpio/gpio%s/value", port);

    if((fd = open(path, O_RDONLY)) == -1) {
        perror("Failed to set GPIO value! ");
        return -1;
    }

    char tmp[2] = {0};
    read(fd, tmp, 1);
    close(fd);

    value = atoi(tmp);

    return value;
}

main文件
#include <iostream>
#include "nvidiagpio.h"

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    NvidiaGpio *gpio = new NvidiaGpio();

    if(gpio->openGpio(BUZZER) != 0) { //打开gpio
        exit(1);
    }

    usleep(10*1000);

    if(gpio->setGpioDirection(BUZZER, DIRECTION_OUT) != 0) { // 设置GPIO方向
        gpio->closeGpio(BUZZER);
        exit(1);
    }

    usleep(10*1000);

    if(gpio->writeGpioValue(BUZZER, ON) != 0) { //设置高电平为输出,开启蜂鸣器
        gpio->closeGpio(BUZZER);
        exit(1);
    }

    int ret = 0;
    ret = gpio->readGpioValue(BUZZER);
    cout<<"get gpio value: "<<ret<<endl;

    sleep(1);

    if(gpio->writeGpioValue(BUZZER, OFF) != 0) { //设置低电平为输出,关闭蜂鸣器
        gpio->closeGpio(BUZZER);
        exit(1);
    }

    ret = gpio->readGpioValue(BUZZER);
    cout<<"get gpio value: "<<ret<<endl;

    gpio->closeGpio(BUZZER);

    return 0;
}

注意GPIO文件位置

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐