tx-gxx-linux/device/gxx-linux/small_lcd/app_spi_lcd/Gpio.cpp

91 lines
2.0 KiB
C++

//
// Created by yingluo907 on 2019/4/11.
//
#include "Gpio.h"
#include "DevUtil.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#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());
sprintf (fname, "/sys/class/gpio/gpio%d/value", port);
gpio_fd = open (fname, O_RDWR);
printf("Gpio::gpio%d.fd = %d.\n",port,gpio_fd);
}
int Gpio::getPort()
{
return port;
}
void Gpio::setValue(GpioLevel level)
{
//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);
}
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)
{
if(port == 153 || port == 150)
printf("\n Gpio %d setDirection %s ",port,direction.c_str());
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);
}