Flyby SDK v1.0.2
Loading...
Searching...
No Matches
pins.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <atomic>
11#include <functional>
12#include <memory>
13
15
16namespace flyby {
17 class SignalHandler;
18
23 class PinObservable : public Observable {
24 public:
28 [[nodiscard]] virtual std::shared_ptr<PinObservable> on_rising_edge(const std::function<void()>& fn) = 0;
29
33 [[nodiscard]] virtual std::shared_ptr<PinObservable> on_falling_edge(const std::function<void()>& fn) = 0;
34 };
35
43 class Pin final {
44 public:
49 enum PinType : uint8_t { IN, OUT, INOUT };
50
55 enum PinValue : uint8_t { LOW, HIGH };
56
57 ~Pin() = default;
58
62 void set_high() const;
63
67 void set_low() const;
68
74 [[nodiscard]] PinValue get_value() const;
75
81 [[nodiscard]] std::shared_ptr<PinObservable> subscribe();
82
86 [[maybe_unused]] static Pin IN_0;
87
91 [[maybe_unused]] static Pin OUT_0;
92
96 [[maybe_unused]] static Pin OUT_1;
97
103 [[maybe_unused]] static bool initialize_pins();
104
111 [[maybe_unused]] static void release_pins();
112
113 private:
114 explicit Pin(std::string name, unsigned int id, PinType type);
115
116 void subscribe(std::shared_ptr<SignalHandler>& handler);
117
118 [[noreturn]] void update_value();
119
120 std::string m_name;
121 unsigned int m_id;
122 PinType m_type;
123 std::atomic<int> m_last_value;
124 std::vector<std::function<void()>> m_on_rising_edge;
125 std::vector<std::function<void()>> m_on_falling_edge;
126 };
127} // namespace flyby
General observable interface.
Definition observable.h:20
Observable for handling GPIO states.
Definition pins.h:23
virtual std::shared_ptr< PinObservable > on_falling_edge(const std::function< void()> &fn)=0
Handles the falling edge of the pin.
virtual std::shared_ptr< PinObservable > on_rising_edge(const std::function< void()> &fn)=0
Handles the rising edge of the pin.
Interface for the GPIO pins.
Definition pins.h:43
static void release_pins()
Deallocates the pin resources.
PinValue
The value of the pin can be either high or low.
Definition pins.h:55
static Pin IN_0
The GPIO input pin with index 0.
Definition pins.h:86
PinType
The type of pin can be IN, OUT, or INOUT.
Definition pins.h:49
static Pin OUT_1
The GPIO output pin with index 1.
Definition pins.h:96
PinValue get_value() const
Get the current value of the pin.
std::shared_ptr< PinObservable > subscribe()
Subscribe to the pin's state changes.
static Pin OUT_0
The GPIO output pin with index 0.
Definition pins.h:91
void set_low() const
Set the pin to LOW.
void set_high() const
Set the pin to HIGH.
static bool initialize_pins()
Initializes the hardware to support pin operations.