2023-04-08 00:56:20 +00:00
|
|
|
//
|
|
|
|
// Created by yingluo907 on 2019/4/11.
|
|
|
|
//
|
|
|
|
#include "Gpio.h"
|
|
|
|
#include "DevUtil.h"
|
|
|
|
#include "stringex.hpp"
|
2023-05-02 01:50:55 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <fcntl.h>
|
2023-04-08 00:56:20 +00:00
|
|
|
|
|
|
|
#define IOPATH "%s/gpio%d/%s"
|
|
|
|
|
|
|
|
const std::string Gpio::falling = "falling";
|
|
|
|
const std::string Gpio::rising = "rising";
|
|
|
|
const std::string Gpio::both = "both";
|
|
|
|
const std::string Gpio::none = "none";
|
|
|
|
|
|
|
|
const std::string Gpio::in = "in";
|
|
|
|
const std::string Gpio::out = "out";
|
|
|
|
|
|
|
|
Gpio::Gpio(int port)
|
|
|
|
{
|
|
|
|
this->port = port;
|
|
|
|
path_value = string_format(IOPATH, path_gpiobase.c_str(), port, path_value.c_str());
|
|
|
|
path_edge = string_format(IOPATH, path_gpiobase.c_str(), port, path_edge.c_str());
|
|
|
|
path_direction = string_format(IOPATH, path_gpiobase.c_str(), port, path_direction.c_str());
|
|
|
|
path_active_low = string_format(IOPATH, path_gpiobase.c_str(), port, path_active_low.c_str());
|
2023-05-02 01:50:55 +00:00
|
|
|
char fpath[128]{};
|
|
|
|
sprintf(fpath,"/sys/class/gpio/gpio%d/value",port);
|
|
|
|
printf("Gpio::Gpio(int port = %d) \n",port);
|
|
|
|
gpio_fd = open(fpath,O_RDWR);
|
2023-04-08 00:56:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 01:50:55 +00:00
|
|
|
Gpio::~Gpio()
|
|
|
|
{
|
|
|
|
close(gpio_fd);
|
|
|
|
}
|
2023-04-08 00:56:20 +00:00
|
|
|
int Gpio::getPort()
|
|
|
|
{
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gpio::setValue(GpioLevel level)
|
|
|
|
{
|
2023-05-02 01:50:55 +00:00
|
|
|
//write_dev(path_value, level);
|
|
|
|
if(port == 153 || port == 150)
|
|
|
|
printf("\n Gpio %d setvalue %d ",port,level==Low?0:1);
|
|
|
|
if(level == Low)
|
|
|
|
write(gpio_fd,"0\n",2);
|
|
|
|
else
|
|
|
|
write(gpio_fd,"1\n",2);
|
2023-04-08 00:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Gpio::GpioLevel Gpio::getValue() {
|
|
|
|
return (Gpio::GpioLevel)read_dev_i(path_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Gpio::getDirection()
|
|
|
|
{
|
|
|
|
return read_dev_s(path_direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gpio::setDirection(std::string direction)
|
|
|
|
{
|
2023-05-02 01:50:55 +00:00
|
|
|
if(port == 153 || port == 150)
|
|
|
|
printf("\n Gpio %d setDirection %s ",port,direction.c_str());
|
2023-04-08 00:56:20 +00:00
|
|
|
write_dev(path_direction, direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gpio::setActive(GpioLevel level)
|
|
|
|
{
|
|
|
|
write_dev(path_active_low, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
Gpio::GpioLevel Gpio::getActive()
|
|
|
|
{
|
|
|
|
return (GpioLevel)read_dev_i(path_active_low);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gpio::setEdge(std::string edge)
|
|
|
|
{
|
|
|
|
write_dev(path_edge, edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Gpio::getEdge()
|
|
|
|
{
|
|
|
|
return read_dev_s(path_edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
GpioOut::GpioOut(int port) :
|
|
|
|
Gpio(port)
|
|
|
|
{
|
|
|
|
setDirection(out);
|
|
|
|
}
|