GPIO通过计数进行控制

TempBranch
Matthew 7 months ago
parent 3bdd0ce344
commit 6dceb6f70f

@ -31,20 +31,73 @@ typedef struct
char str[MAX_STRING_LEN];
}IOT_PARAM;
std::mutex GpioControl::m_locker;
std::vector<std::pair<int, uint32_t>> GpioControl::m_references;
void GpioControl::setInt(int cmd, int value)
{
int fd = open(GPIO_NODE_MP, O_RDONLY);
int fd = -1;
IOT_PARAM param;
param.cmd = cmd;
param.value = value;
int res = 0;
// LOGE("set_int fd=%d,cmd=%d,value=%d\r\n",fd, cmd, value);
if( fd > 0 )
uint32_t references = (value != 0) ? 1 : 0;
std::vector<std::pair<int, uint32_t> >::iterator it;
if (value)
{
int res = ioctl(fd, IOT_PARAM_WRITE, &param);
// LOGE("set_int22 cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result);
close(fd);
m_locker.lock();
fd = open(GPIO_NODE_MP, O_RDONLY);
if( fd > 0 )
{
res = ioctl(fd, IOT_PARAM_WRITE, &param);
close(fd);
// check res???
for (it = m_references.begin(); it != m_references.end(); ++it)
{
if (it->first == cmd)
{
it->second++;
references = it->second;
break;
}
}
if (it == m_references.end())
{
m_references.push_back(std::pair<int, uint32_t >(cmd, references));
}
}
m_locker.unlock();
}
else
{
m_locker.lock();
for (it = m_references.begin(); it != m_references.end(); ++it)
{
if (it->first == cmd)
{
if (it->second > 0)
{
it->second--;
}
references = it->second;
break;
}
}
if (references == 0)
{
fd = open(GPIO_NODE_MP, O_RDONLY);
if (fd > 0) {
res = ioctl(fd, IOT_PARAM_WRITE, &param);
close(fd);
}
}
m_locker.unlock();
}
return;
}
int GpioControl::getInt(int cmd)

@ -8,6 +8,9 @@
#include <string>
#include <chrono>
#include <thread>
#include <mutex>
#include <vector>
#include <utils>
#define CMD_GET_LIGHT_ADC 101
#define CMD_SET_LIGHT_ADC 102
@ -176,6 +179,10 @@
class GpioControl
{
private:
static std::mutex m_locker;
static std::vector<std::pair<int, uint32_t>> m_references;
public:
static void setInt(int cmd, int value);

Loading…
Cancel
Save