18#include <linux/input.h>
22#define GPIO_TOUT_USEC 100000
26 m_fd(-1), m_gpio(gpioNo), m_isRead(false)
34std::string GPIO::GPIO_EDGES_STR(
GPIO_EDGES edge)
59 return SetDirection(isRead);
67 return SetGPIOEdgeEvent(edge);
79 char buff[64] = { 0 };
80 snprintf(buff,
sizeof(buff),
SYS_GPIO_PATH "/gpio%d/value", m_gpio);
82 m_fd = open(buff, (m_isRead ? (O_RDONLY | O_NONBLOCK) : O_WRONLY));
85 printf(
"Error opening gpio");
101 char buff[64] = { 0 };
106 printf(
"unable to open export for gpio %d\n", m_gpio);
110 length = snprintf(buff,
sizeof(buff),
"%d", m_gpio);
111 if (write(fd, buff, length) != length)
114 printf(
"unable to init gpio %d\n", m_gpio);
125 char buff[64] = { 0 };
130 printf(
"unable to open gpio %d for unexport\n", m_gpio);
134 length = snprintf(buff,
sizeof(buff),
"%d", m_gpio);
135 if (write(fd, buff, length) != length)
138 printf(
"unable to unexport gpio %d\n", m_gpio);
145bool GPIO::SetDirection(
bool isRead)
148 char buff[64] = { 0 };
150 snprintf(buff,
sizeof(buff),
153 fd = open(buff, O_WRONLY);
156 printf(
"SetDirection unable to open gpio%d", m_gpio);
162 if (write(fd,
"out", 4) != 4)
165 printf(
"unable to make gpio%d as output\n", m_gpio);
171 if (write(fd,
"in", 3) != 3)
174 printf(
"unable to make gpio%d as input\n", m_gpio);
184 char buff[64] = { 0 };
186 snprintf(buff,
sizeof(buff),
SYS_GPIO_PATH "/gpio%d/edge", m_gpio);
188 fd = open(buff, O_WRONLY);
191 printf(
"Unable to open gpio%d/edge\n", m_gpio);
194 std::string edgeStr = GPIO_EDGES_STR(edge);
195 if (write(fd, edgeStr.c_str(), edgeStr.length() + 1)
196 != ((
int) (edgeStr.length() + 1)))
198 printf(
"Error setting edge to \"%s\"\n", edgeStr.c_str());
212 lseek(m_fd, 0, SEEK_SET);
213 if (read(m_fd, &ch, 1) != 1)
215 printf(
"Error fetching GPIO value\n");
232 std::string state = makeHigh ?
"1" :
"0";
233 if (write(m_fd, state.c_str(), 2) != 2)
235 printf(
"Error setting GPIO state %s\n", state.c_str());
244 if (!gpio.
Init(
true))
249 int data = gpio.
Read();
257 if (!gpio.
Init(
true))
262 bool response = gpio.
Write(makeHigh);
276 fdset.events = POLLPRI;
278 auto rc = poll(&fdset, 1, uSecTout);
281 printf(
"\npoll() failed!\n");
290 if (fdset.revents & POLLPRI)
static int ReadOnce(uint gpioNo)
int & GetGPIODescriptor()
static bool WriteOnce(uint gpioNo, bool makeHigh)
int ReadWithInterrupt(unsigned long uSecTout)
bool Write(bool makeHigh)
bool Init4EdgeInterrupt(bool isRead, GPIO_EDGES edge)