FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
Gpio Class Reference

#include <bsp_gpio.hpp>

Public Types

using Callback = std::function< void()>
 

Public Member Functions

 Gpio (const std::string &chip_name, unsigned int line_num, bool is_output, int default_value=1)
 
 ~Gpio ()
 
void Write (int value)
 
int Read () const
 
void EnableInterruptRisingEdgeWithCallback (Callback cb)
 

Private Member Functions

void InterruptWaitLoop ()
 

Private Attributes

gpiod_chip * chip_
 
gpiod_line * line_ {}
 
unsigned int line_num_
 
bool is_output_
 
std::atomic< bool > running_ {false}
 
std::thread interrupt_thread_
 
Callback callback_
 

Detailed Description

Gpio class

Handles GPIO initialization, configuration (input/output), read/write operations, and interrupt callback support.

Definition at line 19 of file bsp_gpio.hpp.

Member Typedef Documentation

◆ Callback

using Gpio::Callback = std::function<void()>

Definition at line 21 of file bsp_gpio.hpp.

Constructor & Destructor Documentation

◆ Gpio()

Gpio::Gpio ( const std::string &  chip_name,
unsigned int  line_num,
bool  is_output,
int  default_value = 1 
)
inline

Constructor

Parameters
chip_nameGPIO chip name, e.g., "gpiochip0"
line_numGPIO line number
is_outputtrue for output mode, false for input mode
default_valueInitial value for output mode (default: 1)

Definition at line 31 of file bsp_gpio.hpp.

33 : chip_(gpiod_chip_open_by_name(chip_name.c_str())),
34 is_output_(is_output),
35 line_num_(line_num) {
36 assert(!chip_name.empty()); /* Ensure chip name is valid */
37
38 if (!chip_) {
39 std::perror("Failed to open GPIO chip");
40 }
41
42 line_ = gpiod_chip_get_line(chip_, line_num_);
43 if (!line_) {
44 gpiod_chip_close(chip_);
45 std::perror("Failed to get GPIO line");
46 }
47
48 int ret = is_output_
49 ? gpiod_line_request_output(line_, nullptr, default_value)
50 : gpiod_line_request_input(line_, nullptr);
51 if (ret < 0) {
52 std::perror("Failed to configure GPIO line");
53 }
54 }

◆ ~Gpio()

Gpio::~Gpio ( )
inline

Destructor

Releases allocated resources and stops interrupt thread.

Definition at line 61 of file bsp_gpio.hpp.

61 {
62 running_ = false;
63
64 if (interrupt_thread_.joinable()) {
65 interrupt_thread_.join();
66 }
67
68 if (line_) {
69 gpiod_line_release(line_);
70 }
71
72 if (chip_) {
73 gpiod_chip_close(chip_);
74 }
75 }

Member Function Documentation

◆ EnableInterruptRisingEdgeWithCallback()

void Gpio::EnableInterruptRisingEdgeWithCallback ( Callback  cb)
inline

Enable rising edge interrupt and register callback.

This starts a background thread to monitor GPIO events.

Parameters
cbCallback function to be invoked on rising edge.

Definition at line 109 of file bsp_gpio.hpp.

109 {
110 if (is_output_) {
111 std::perror("Cannot register interrupt on output GPIO");
112 }
113
114 /* If line is already in use, release it */
115 if (line_) {
116 gpiod_line_release(line_);
117 }
118
119 /* Request GPIO line for interrupt events */
120 if (gpiod_line_request_rising_edge_events(line_, nullptr) < 0) {
121 std::perror("Failed to enable rising edge interrupt");
122 }
123
124 callback_ = std::move(cb);
125 running_ = true;
126
127 interrupt_thread_ = std::thread(&Gpio::InterruptWaitLoop, this);
128 }
void InterruptWaitLoop()
Definition bsp_gpio.hpp:134

◆ InterruptWaitLoop()

void Gpio::InterruptWaitLoop ( )
inlineprivate

Background thread loop that waits for GPIO rising edge events.

Definition at line 134 of file bsp_gpio.hpp.

134 {
135 while (running_) {
136 int ret = gpiod_line_event_wait(line_, nullptr); // block until event
137 if (ret == 1 && callback_) {
138 struct gpiod_line_event event;
139 if (gpiod_line_event_read(line_, &event) == 0 &&
140 event.event_type == GPIOD_LINE_EVENT_RISING_EDGE) {
141 callback_();
142 }
143 }
144 }
145 }

◆ Read()

int Gpio::Read ( ) const
inline

Read the current GPIO value.

Returns
Current GPIO value (0 or 1)

Definition at line 94 of file bsp_gpio.hpp.

94 {
95 int value = gpiod_line_get_value(line_);
96 if (value < 0) {
97 std::perror("GPIO read failed");
98 }
99 return value;
100 }

◆ Write()

void Gpio::Write ( int  value)
inline

Write a value to the GPIO line (only valid in output mode).

Parameters
valueValue to write (0 or 1)

Definition at line 82 of file bsp_gpio.hpp.

82 {
83 assert(is_output_);
84 if (gpiod_line_set_value(line_, value) < 0) {
85 std::perror("GPIO write failed");
86 }
87 }

Field Documentation

◆ callback_

Callback Gpio::callback_
private

Definition at line 154 of file bsp_gpio.hpp.

◆ chip_

gpiod_chip* Gpio::chip_
private

Definition at line 147 of file bsp_gpio.hpp.

◆ interrupt_thread_

std::thread Gpio::interrupt_thread_
private

Definition at line 153 of file bsp_gpio.hpp.

◆ is_output_

bool Gpio::is_output_
private

Definition at line 150 of file bsp_gpio.hpp.

◆ line_

gpiod_line* Gpio::line_ {}
private

Definition at line 148 of file bsp_gpio.hpp.

148{}; // GPIO line handle

◆ line_num_

unsigned int Gpio::line_num_
private

Definition at line 149 of file bsp_gpio.hpp.

◆ running_

std::atomic<bool> Gpio::running_ {false}
private

Definition at line 152 of file bsp_gpio.hpp.

152{false}; // Interrupt thread flag

The documentation for this class was generated from the following file: