整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
81
hardware/GPIO.cpp
Normal file
81
hardware/GPIO.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* INCLUDE
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "GPIO.h"
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/poll.h>
|
||||
#include <Poco/Exception.h>
|
||||
#include <Poco/Format.h>
|
||||
|
||||
namespace Hardware
|
||||
{
|
||||
/*
|
||||
*******************************************************************************
|
||||
* CLASS DEFINE
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
GPIO::GPIO(int pinno): _pinno(pinno)
|
||||
{
|
||||
std::string s = std::to_string(_pinno);
|
||||
writeFile("/sys/class/gpio/export", s.c_str(), s.length());
|
||||
_fd = ::open(Poco::format("/sys/class/gpio/gpio%d/value", _pinno).c_str(), O_RDWR);
|
||||
if (_fd < 0)
|
||||
throw Poco::InvalidAccessException("Open FAIL");
|
||||
}
|
||||
|
||||
GPIO::~GPIO()
|
||||
{
|
||||
::close(_fd);
|
||||
std::string s = std::to_string(_pinno);
|
||||
writeFile("/sys/class/gpio/unexport", s.c_str(), s.length());
|
||||
}
|
||||
|
||||
void GPIO::direction(Direction dir)
|
||||
{
|
||||
switch (dir)
|
||||
{
|
||||
case Direction::OUT:
|
||||
default:
|
||||
writeDirection("out");
|
||||
break;
|
||||
|
||||
case Direction::IN:
|
||||
writeDirection("in");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void GPIO::write(unsigned level)
|
||||
{
|
||||
if (level)
|
||||
::write(_fd, "1", 1);
|
||||
else
|
||||
::write(_fd, "0", 1);
|
||||
}
|
||||
|
||||
unsigned GPIO::read()
|
||||
{
|
||||
char value;
|
||||
::read(_fd, &value, 1);
|
||||
return value != '0';
|
||||
}
|
||||
|
||||
void GPIO::writeFile(const std::string& path, const void* pData, int len)
|
||||
{
|
||||
int fd = ::open(path.c_str(), O_WRONLY);
|
||||
if (fd < 0)
|
||||
throw Poco::InvalidAccessException("Open FAIL");
|
||||
|
||||
::write(fd, pData, len);
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
void GPIO::writeDirection(const std::string& value)
|
||||
{
|
||||
writeFile(Poco::format("/sys/class/gpio/gpio%d/direction", _pinno), value.c_str(), value.length());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue