66 for (
auto &line : _lines) {
67 auto off = line.offset();
68 auto value = line.get_value();
69 if (value != _values[to_string(
off)]) {
71 _values[to_string(
off)] = value;
81 for (
auto &[
off, line] : _lines) {
82 gpiod_line_release(line);
84 gpiod_chip_close(_chip);
89 for (
auto &[
off, line] : _lines) {
90 int val = gpiod_line_get_value(line);
91 if (val != _values[to_string(
off)]) {
94 _values[to_string(
off)] = val;
109 nlohmann::json
values() {
return _values; }
111 void info(ostream &out = cout)
override {
113 out <<
" GPIO chip: " << style::bold << _chip_path << style::reset
115 out <<
" GPIO lines: " << style::bold;
116 for (
auto &line : _offsets) {
119 out << style::reset << endl;
120 out <<
" GPIOD version: " << style::bold << gpiod_version_string()
121 << style::reset << endl;
131 void load_settings()
override {
133 _chip_number = cfg[
"gpio_chip"].value_or(0);
134 _chip_path = cfg[
"gpio_chip_path"].value_or(
"/dev/gpiochip0");
135 if (cfg[
"gpio_lines"].type() == toml::node_type::integer) {
136 _offsets.push_back(cfg[
"gpio_lines"].value_or(0));
137 }
else if (cfg[
"gpio_lines"].type() == toml::node_type::array) {
138 toml::array *a = cfg[
"gpio_lines"].as_array();
139 a->for_each([&](
auto &&el) { _offsets.push_back(el.value_or(0)); });
147 _chip = gpiod::chip(_chip_path);
148 _lines = _chip.get_lines(_offsets);
149 _lines.request({
_name.c_str(), ::gpiod::line_request::DIRECTION_INPUT, 0});
156 _chip = gpiod_chip_open_by_number(_chip_number);
157 for (
auto &
off : _offsets) {
158 struct gpiod_line *line = gpiod_chip_get_line(_chip,
off);
159 ret = gpiod_line_request_input(line,
_name.c_str());
161 cerr <<
"Error reserving GPIO line " <<
off <<
": " << strerror(errno)
171 int _chip_number = 0;
172 string _chip_path =
"/dev/gpiochip0";
173 vector<unsigned int> _offsets;
174 nlohmann::json _values;
177 gpiod::line_bulk _lines;
179 struct gpiod_chip *_chip = NULL;
180 map<int, struct gpiod_line *> _lines;