系统库操作GPIO口。
class gpioOperateClass
{
#undef SYS_GPIO_CLASS_DIREC
#define SYS_GPIO_CLASS_DIREC "/sys/class/gpio/"
public:
gpioOperateClass()
{
freeGpioInform();
valueFileOperate = NULL;
// firstflag = 0;
}
virtual ~gpioOperateClass()
{
freeGpioOperate();
}
void freeGpioInform()
{
gpioPin = -1;
gpioDirection = "";
// directionFile = "";
// valueFile = "";
}
void closeGpioFile()
{
if(valueFileOperate != NULL)
{
logwdbg("close file:%d", gpioPin);
fclose(valueFileOperate);
valueFileOperate = NULL;
}
}
void freeGpioOperate()
{
freeGpioInform();
closeGpioFile();
}
int initPin(int setpin, std::string setdir = "")
{
gpioPin = setpin;
gpioDirection = setdir;
std::string tempPinStr = miscOperateClass::printStr("%d", gpioPin);
std::string directionFile = miscOperateClass::printStr(SYS_GPIO_CLASS_DIREC "gpio%d/direction", setpin);
std::string valueFile = miscOperateClass::printStr(SYS_GPIO_CLASS_DIREC "gpio%d/value", setpin);
closeGpioFile();
if(gpioDirection == "in")
{
miscOperateClass::writefileStr(SYS_GPIO_CLASS_DIREC "export", tempPinStr);
miscOperateClass::writefileStr(directionFile.c_str(), gpioDirection);
valueFileOperate = ::fopen(valueFile.c_str(), "r");
}
else if(gpioDirection == "out")
{
miscOperateClass::writefileStr(SYS_GPIO_CLASS_DIREC "export", tempPinStr);
miscOperateClass::writefileStr(directionFile.c_str(), gpioDirection);
valueFileOperate = ::fopen(valueFile.c_str(), "r+");
}
else
{
logwerr("err not init?: %d:%s", gpioPin, gpioDirection.c_str());
return -1;
}
return 0;
}
int readPin()
{
char tempBuffer[5] = {};
if(!valueFileOperate)
{
logwerr("dir err?: %d:%s", gpioPin, gpioDirection.c_str());
return -1;
}
fseek(valueFileOperate, 0, SEEK_SET);
fread(tempBuffer, sizeof(tempBuffer), 1, valueFileOperate);
return atoi(tempBuffer);
}
int writePin(int value)
{
char tempBuffer[1] = {};
if(gpioDirection != "out")
{
logwerr("dir err?: %d:%s", gpioPin, gpioDirection.c_str());
return -1;
}
if(!valueFileOperate)
{
logwerr("file err?: %d:%s", gpioPin, gpioDirection.c_str());
return -1;
}
if(value)
{
tempBuffer[0] = '1';
}
else
{
tempBuffer[0] = '0';
}
fseek(valueFileOperate, 0, SEEK_SET);
fwrite(tempBuffer, sizeof(tempBuffer), 1, valueFileOperate);
fflush(valueFileOperate);
return 0;
}
int getpin()
{
return gpioPin;
}
private:
int gpioPin;
std::string gpioDirection;//"in" or "out"
// std::string directionFile;// generate by pin and dir
// std::string valueFile;// generate by pin and dir
FILE *valueFileOperate;
};
class ioInputFilterBaseClass
{
public:
ioInputFilterBaseClass(uint32_t setKeepMs)
{
initIoInput(setKeepMs);
}
virtual ~ioInputFilterBaseClass()
{
}
virtual int initIoInput(uint32_t setKeepMs)
{
invalue = -1;
inputKeepMs = LIMIT_VAL(setKeepMs, 50, 30000);
inputKeepTimer.begin();
return 0;
}
virtual int readIoPin() = 0;
int updateIoInput()
{
int tmpvalue = readIoPin();
if(tmpvalue == invalue)
{
inputKeepTimer.begin();
return -1;
}
if(inputKeepTimer.runMs() < inputKeepMs)
{
return -1;
}
invalue = tmpvalue;
return invalue;
}
private:
int invalue;
uint32_t inputKeepMs;
sysTimerClass inputKeepTimer;
};
class gpioInputOperateClass : public ioInputFilterBaseClass
{
public:
gpioInputOperateClass(int setpin, uint32_t setKeepMs = 160)
:ioInputFilterBaseClass(setKeepMs)
{
gpioInput.initPin(setpin, "in");
}
~gpioInputOperateClass()
{
}
int readIoPin()
{
return gpioInput.readPin();
}
private:
gpioOperateClass gpioInput;
};
class ioInputManageClass
{
public:
ioInputManageClass(std::string ioInTaskName)
{
}
~ioInputManageClass()
{
}
void setValueChangeCallback(std::function<void(int,int)> cbkfunc)
{
ioInputCallback = cbkfunc;
}
void taskInit()
{
}
void taskRun()
{
for(ulong i = 0; i < ioInputVect.size(); i++)
{
int tmpvalue = ioInputVect[i]->updateIoInput();
if(tmpvalue < 0)
{
continue;
}
if(ioInputCallback)
{
ioInputCallback(i, tmpvalue);
}
else
{
logwdbg("input value:%lu,%d", i, tmpvalue);
}
}
}
void taskQuit()
{
}
int registerOneIoInput(std::shared_ptr<ioInputFilterBaseClass> inputInstance)
{
ioInputVect.push_back(inputInstance);
return 0;
}
static std::shared_ptr<ioInputManageClass> instance()
{
static std::shared_ptr<ioInputManageClass> gpioInputManageInstance = std::make_shared<ioInputManageClass>("ioInputMngDef");
return gpioInputManageInstance;
}
private:
std::vector<std::shared_ptr<ioInputFilterBaseClass>> ioInputVect;
std::function<void(int,int)> ioInputCallback;
};
class ioOutputBaseClass
{
public:
ioOutputBaseClass()
{
}
virtual ~ioOutputBaseClass()
{
}
virtual void writeIoPin(int value) = 0;
virtual int readIoPin()
{
return -1;
}
private:
};
class gpioOutputOperateClass : public ioOutputBaseClass
{
public:
gpioOutputOperateClass(int setpin)
{
gpioOutput.initPin(setpin, "out");
}
~gpioOutputOperateClass()
{
}
void writeIoPin(int value)
{
gpioOutput.writePin(value);
}
int readIoPin()
{
return gpioOutput.readPin();
}
private:
gpioOperateClass gpioOutput;
};
class ioOutputManageClass
{
public:
ioOutputManageClass()
{
}
~ioOutputManageClass()
{
}
int registerOneIoOutput(std::shared_ptr<ioOutputBaseClass> inputInstance)
{
ioOutVect.push_back(inputInstance);
return 0;
}
void writeOutIoValue(uint32_t outid, int value)
{
if(!(outid < ioOutVect.size()))
{
logwerr("id err:%d", outid);
return;
}
if(ioOutVect[outid] == nullptr)
{
logwerr("id null:%d", outid);
return;
}
ioOutVect[outid]->writeIoPin(value);
}
int readOutIoValue(uint32_t outid)
{
if(!(outid < ioOutVect.size()))
{
logwerr("id err:%d", outid);
return -1;
}
if(ioOutVect[outid] == nullptr)
{
logwerr("id null:%d", outid);
return -1;
}
return ioOutVect[outid]->readIoPin();
}
static std::shared_ptr<ioOutputManageClass> instance()
{
static std::shared_ptr<ioOutputManageClass> gpioInputManageInstance = std::make_shared<ioOutputManageClass>();
return gpioInputManageInstance;
}
private:
std::vector<std::shared_ptr<ioOutputBaseClass>> ioOutVect;
};
74HC165读取OK
void ioInputOpt165Class::updateFrom165()
{
size_t i;
hc165LockPin.writePin(1);
for (i = 0; i < hc165DataStr.size(); i++)
{
hc165ClockPin.writePin(0);
hc165DataStr[hc165DataStr.size() - 1 - i] = hc165DataPin.readPin();
hc165ClockPin.writePin(1);
}
hc165LockPin.writePin(0);
}
74HC595写OK
void ioOutOpt595Class::updateTo595()
{
size_t i;
hc595LockPin.writePin(0);
for (i = 0; i < hc595DataStr.size(); i++)
{
hc595ClockPin.writePin(0);
if (hc595DataStr[hc595DataStr.size() - 1 - i])
{
hc595DataPin.writePin(1);
}
else
{
hc595DataPin.writePin(0);
}
hc595ClockPin.writePin(1);
}
hc595LockPin.writePin(1);
}