Leosac  0.8.0
Open Source Access Control
LibgpiodPin.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2022 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "LibgpiodModule.hpp"
23 #include "hardware/GPIO.hpp"
24 #include <zmqpp/zmqpp.hpp>
25 #include <gpiod.h>
26 
27 namespace Leosac
28 {
29 namespace Module
30 {
31 namespace Libgpiod
32 {
33 class LibgpiodModule;
34 class LibgpiodConfig;
35 
43 {
44  public:
46 
47  enum class InterruptMode
48  {
49  None,
50  Rising,
51  Falling,
52  Both,
53  };
54 
55  LibgpiodPin(zmqpp::context &ctx, const std::string &name, const std::string &gpio_device,
56  int gpio_offset, Direction direction, InterruptMode interrupt_mode,
57  bool initial_value, LibgpiodModule &module);
58 
59  ~LibgpiodPin();
60 
61  LibgpiodPin(const LibgpiodPin &) = delete;
62 
63  LibgpiodPin &operator=(const LibgpiodPin &) = delete;
64 
65  LibgpiodPin &operator=(LibgpiodPin &&) = delete;
66 
67  LibgpiodPin(LibgpiodPin &&o) = delete;
68 
72  void release();
73 
78  void register_sockets(zmqpp::reactor *reactor);
79 
83  std::chrono::system_clock::time_point next_update() const;
84 
92  void update();
93 
94  private:
98  void handle_interrupt();
99 
103  bool read_value();
104 
108  bool turn_on(zmqpp::message *msg = nullptr);
109 
113  bool turn_off();
114 
118  bool toggle();
119 
124  void handle_message();
125 
129  void set_direction(Direction dir);
130 
134  void set_interrupt(InterruptMode mode);
135 
139  zmqpp::socket sock_;
140 
141  std::string name_;
142 
146  std::string gpio_device_;
147 
152 
157 
161  const bool initial_value_;
162 
167 
171  std::chrono::system_clock::time_point next_update_time_;
172 
173  gpiod_chip *gpiod_chip_;
174 
175  gpiod_line *gpiod_line_;
176 
178 };
179 }
180 }
181 }
Leosac::Module::Libgpiod::LibgpiodPin::set_direction
void set_direction(Direction dir)
Write direction to the direction file.
Definition: LibgpiodPin.cpp:71
Leosac::Module::Libgpiod::LibgpiodPin::handle_message
void handle_message()
The SysFsGpioModule will register this method so its called when a message is ready on the pin socket...
Definition: LibgpiodPin.cpp:100
Leosac::Module::Libgpiod::LibgpiodPin::module_
LibgpiodModule & module_
Reference to the module.
Definition: LibgpiodPin.hpp:166
Leosac::Module::Libgpiod::LibgpiodPin::gpiod_fd_
int gpiod_fd_
Definition: LibgpiodPin.hpp:177
Leosac::Module::Libgpiod::LibgpiodPin::update
void update()
Update the PIN.
Definition: LibgpiodPin.cpp:188
Leosac::Module::Libgpiod::LibgpiodPin::next_update
std::chrono::system_clock::time_point next_update() const
This method shall returns the time point at which we want to be updated.
Definition: LibgpiodPin.cpp:183
Leosac::Module::Libgpiod::LibgpiodPin::gpiod_line_
gpiod_line * gpiod_line_
Definition: LibgpiodPin.hpp:175
Leosac::Module::Libgpiod::LibgpiodPin::InterruptMode::None
@ None
Leosac::Module::Libgpiod::LibgpiodPin::release
void release()
Release Libgpiod resources.
Definition: LibgpiodPin.cpp:59
Leosac::Module::Libgpiod::LibgpiodPin::Direction
Hardware::GPIO::Direction Direction
Definition: LibgpiodPin.hpp:45
Leosac::Module::Libgpiod::LibgpiodPin::turn_on
bool turn_on(zmqpp::message *msg=nullptr)
Write to sysfs to turn the gpio on.
Definition: LibgpiodPin.cpp:121
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::Libgpiod::LibgpiodModule
Handle GPIO management over libgpiod.
Definition: LibgpiodModule.hpp:49
Leosac::Module::Libgpiod::LibgpiodPin::toggle
bool toggle()
Read to sysfs and then write the opposite value.
Definition: LibgpiodPin.cpp:148
Leosac::Module::Libgpiod::LibgpiodPin::InterruptMode::Both
@ Both
Leosac::Hardware::GPIO::Direction
Direction
Definition: GPIO.hpp:44
Leosac::Module::Libgpiod::LibgpiodPin::next_update_time_
std::chrono::system_clock::time_point next_update_time_
Time point of next wished update.
Definition: LibgpiodPin.hpp:171
Leosac::Module::Libgpiod::LibgpiodPin::set_interrupt
void set_interrupt(InterruptMode mode)
Write interrupt mode to the edge file.
Definition: LibgpiodPin.cpp:83
Leosac::Module::Libgpiod::LibgpiodPin::LibgpiodPin
LibgpiodPin(zmqpp::context &ctx, const std::string &name, const std::string &gpio_device, int gpio_offset, Direction direction, InterruptMode interrupt_mode, bool initial_value, LibgpiodModule &module)
Definition: LibgpiodPin.cpp:27
Leosac::Module::Libgpiod::LibgpiodPin::InterruptMode::Falling
@ Falling
Leosac::Module::Libgpiod::LibgpiodPin::handle_interrupt
void handle_interrupt()
Interrupt happened for this GPIO ping.
Definition: LibgpiodPin.cpp:162
GPIO.hpp
Leosac::Module::Libgpiod::LibgpiodPin
This is a implementation class.
Definition: LibgpiodPin.hpp:42
Leosac::Module::Libgpiod::LibgpiodPin::InterruptMode
InterruptMode
Definition: LibgpiodPin.hpp:47
LibgpiodModule.hpp
Leosac::Module::Libgpiod::LibgpiodPin::~LibgpiodPin
~LibgpiodPin()
Definition: LibgpiodPin.cpp:54
Leosac::Module::Libgpiod::LibgpiodPin::gpiod_chip_
gpiod_chip * gpiod_chip_
Definition: LibgpiodPin.hpp:173
Leosac::Module::Libgpiod::LibgpiodPin::InterruptMode::Rising
@ Rising
Leosac::Module::Libgpiod::LibgpiodPin::operator=
LibgpiodPin & operator=(const LibgpiodPin &)=delete
Leosac::Module::Libgpiod::LibgpiodPin::register_sockets
void register_sockets(zmqpp::reactor *reactor)
Register own socket to the module's reactor.
Definition: LibgpiodPin.cpp:171
Leosac::Module::Libgpiod::LibgpiodPin::direction_
const Direction direction_
Direction of the PIN.
Definition: LibgpiodPin.hpp:156
Leosac::Module::Libgpiod::LibgpiodPin::turn_off
bool turn_off()
Write to sysfs to turn the gpio on.
Definition: LibgpiodPin.cpp:142
Leosac::Module::Libgpiod::LibgpiodPin::gpio_offset_
int gpio_offset_
Offset of the GPIO.
Definition: LibgpiodPin.hpp:151
Leosac::Module::Libgpiod::LibgpiodPin::gpio_device_
std::string gpio_device_
File descriptor of the GPIO in sysfs.
Definition: LibgpiodPin.hpp:146
Leosac::Module::Libgpiod::LibgpiodPin::read_value
bool read_value()
Read value from filesystem.
Definition: LibgpiodPin.cpp:155
Leosac::Module::Libgpiod::LibgpiodPin::name_
std::string name_
Definition: LibgpiodPin.hpp:141
Leosac::Module::Libgpiod::LibgpiodPin::initial_value_
const bool initial_value_
Initial value of the PIN.
Definition: LibgpiodPin.hpp:161
Leosac::Module::Libgpiod::LibgpiodPin::sock_
zmqpp::socket sock_
listen to command from other component.
Definition: LibgpiodPin.hpp:139