refactored GPIO to support callbacks

This commit is contained in:
2021-02-23 13:24:05 +01:00
parent c042bbea63
commit 434d0586b3
9 changed files with 268 additions and 125 deletions

View File

@ -12,6 +12,17 @@ enum Direction {
OUT = 1
};
enum GpioOperation {
READ,
WRITE
};
enum GpioTypes {
NONE,
GPIOD_REGULAR,
CALLBACK
};
static constexpr gpioId_t NO_GPIO = -1;
}
@ -29,23 +40,52 @@ static constexpr gpioId_t NO_GPIO = -1;
* @param lineHandle The handle returned by gpiod_chip_get_line will be later written to this
* pointer.
*/
typedef struct GpioConfig {
GpioConfig(): chipname(), lineNum(0), consumer(), direction(gpio::Direction::OUT),
initValue(0) {};
class GpioBase {
public:
GpioConfig(std::string chipname_, int lineNum_, std::string consumer_,
gpio::Direction direction_, int initValue_):
chipname(chipname_), lineNum(lineNum_), consumer(consumer_),
direction(direction_), initValue(initValue_) {}
std::string chipname;
int lineNum;
GpioBase() = default;
GpioBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction,
int initValue):
gpioType(gpioType), consumer(consumer),direction(direction), initValue(initValue) {}
virtual~ GpioBase() {};
/* Can be used to cast GpioBase to a concrete child implementation */
gpio::GpioTypes gpioType = gpio::GpioTypes::NONE;
std::string consumer;
gpio::Direction direction;
int initValue;
struct gpiod_line* lineHandle = nullptr;
} GpioConfig_t;
gpio::Direction direction = gpio::Direction::IN;
int initValue = 0;
};
using GpioMap = std::unordered_map<gpioId_t, GpioConfig_t>;
class GpiodRegular: public GpioBase {
public:
GpiodRegular(): GpioBase() {};
GpiodRegular(std::string chipname_, int lineNum_, std::string consumer_,
gpio::Direction direction_, int initValue_):
GpioBase(gpio::GpioTypes::GPIOD_REGULAR, consumer_, direction_, initValue_),
chipname(chipname_), lineNum(lineNum_) {}
std::string chipname;
int lineNum = 0;
struct gpiod_line* lineHandle = nullptr;
};
class GpioCallback: public GpioBase {
public:
GpioCallback(std::string consumer, gpio::Direction direction_, int initValue_,
void (* callback) (gpioId_t gpioId, gpio::GpioOperation gpioOp, int value, void* args),
void* callbackArgs):
GpioBase(gpio::GpioTypes::CALLBACK, consumer, direction_, initValue_),
callback(callback), callbackArgs(callbackArgs) {}
void (* callback) (gpioId_t gpioId, gpio::GpioOperation gpioOp,
int value, void* args) = nullptr;
void* callbackArgs = nullptr;
};
using GpioMap = std::unordered_map<gpioId_t, GpioBase*>;
using GpioMapIter = GpioMap::iterator;
#endif /* LINUX_GPIO_GPIODEFINITIONS_H_ */