Switch to gpiod to avoid issues with pi 5

This commit is contained in:
Lorenzooone
2024-06-03 22:27:08 +02:00
parent 56e64df9f2
commit 13eb923286
4 changed files with 53 additions and 38 deletions

View File

@@ -13,7 +13,7 @@ set(SFML_BUILD_NETWORK FALSE)
set(EXTRA_LIBRARIES "")
if (RASPBERRY_PI_COMPILATION)
set(EXTRA_LIBRARIES "pigpiod_if2")
set(EXTRA_LIBRARIES "gpiod")
set(EXTRA_CXX_FLAGS "${EXTRA_CXX_FLAGS}-DRASPI 1 ")
endif()
@@ -62,15 +62,15 @@ else()
set(FTD3XX_URL_TIME 2023/03)
set(FTD3XX_VER 1.0.5)
if((${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch") OR (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm"))
if((${CMAKE_SYSTEM_PROCESSOR} MATCHES "64") OR (${CMAKE_SYSTEM_PROCESSOR} MATCHES "v8"))
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
set(FTD3XX_VOL libftd3xx-linux-arm-v8-${FTD3XX_VER})
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "v7")
elseif("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
set(FTD3XX_VOL libftd3xx-linux-arm-v7_32-${FTD3XX_VER})
endif()
else()
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "64")
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
set(FTD3XX_VOL libftd3xx-linux-x86_64-${FTD3XX_VER})
elseif((${CMAKE_SYSTEM_PROCESSOR} MATCHES "32") OR (${CMAKE_SYSTEM_PROCESSOR} MATCHES "86"))
elseif("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
set(FTD3XX_VOL libftd3xx-linux-x86_32-${FTD3XX_VER})
endif()
endif()

View File

@@ -46,6 +46,8 @@ sudo apt install \
libfreetype-dev
```
Additionally, when compiling for a Raspberry Pi, install gpiod and libgpiod-dev.
## Compilation
To compile the program, assuming CMake, git and g++ are installed on the system, this is the command which should be launched:

View File

@@ -3,24 +3,29 @@
#include <chrono>
#include "sfml_gfx_structs.hpp"
#ifdef RASPI
#include <gpiod.h>
#endif
class ExtraButton {
public:
void initialize(int pi_value, int id, sf::Keyboard::Key corresponding_key, bool is_power, float first_re_press_time, float later_re_press_time, bool use_pud_up);
int get_pi_value();
void initialize(int id, sf::Keyboard::Key corresponding_key, bool is_power, float first_re_press_time, float later_re_press_time, bool use_pud_up);
void poll(std::queue<SFEvent> &events_queue);
void end();
private:
bool initialized = false;
int id;
sf::Keyboard::Key corresponding_key;
bool is_power;
int pi_value;
bool started;
bool after_first;
float first_re_press_time;
float later_re_press_time;
std::chrono::time_point<std::chrono::high_resolution_clock> last_press_time;
bool is_time_valid;
#ifdef RASPI
gpiod_line *gpioline_ptr;
#endif
bool is_pressed();
bool is_valid();

View File

@@ -1,12 +1,8 @@
#include "ExtraButtons.hpp"
#ifdef RASPI
#include <pigpiod_if2.h>
#endif
static ExtraButton pi_page_up, pi_page_down, pi_enter, pi_power;
void ExtraButton::initialize(int pi_value, int id, sf::Keyboard::Key corresponding_key, bool is_power, float first_re_press_time, float later_re_press_time, bool use_pud_up) {
this->pi_value = pi_value;
void ExtraButton::initialize(int id, sf::Keyboard::Key corresponding_key, bool is_power, float first_re_press_time, float later_re_press_time, bool use_pud_up) {
this->id = id;
this->is_power = is_power;
this->corresponding_key = corresponding_key;
@@ -17,30 +13,47 @@ void ExtraButton::initialize(int pi_value, int id, sf::Keyboard::Key correspondi
this->first_re_press_time = first_re_press_time;
this->later_re_press_time = later_re_press_time;
#ifdef RASPI
set_mode(this->pi_value, this->id, PI_INPUT);
if(use_pud_up)
set_pull_up_down(this->pi_value, this->id, PI_PUD_UP);
if(this->id >= 0) {
std::string gpio_str = "GPIO" + std::to_string(this->id);
this->gpioline_ptr = gpiod_line_find(gpio_str.c_str());
}
else
set_pull_up_down(this->pi_value, this->id, PI_PUD_DOWN);
this->gpioline_ptr = NULL;
if(this->gpioline_ptr) {
if(use_pud_up)
gpiod_line_request_input_flags(this->gpioline_ptr, "cc3dsfs", GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP);
else
gpiod_line_request_input_flags(this->gpioline_ptr, "cc3dsfs", GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_DOWN);
}
#endif
}
int ExtraButton::get_pi_value() {
if(!this->initialized)
return -1;
return this->pi_value;
void ExtraButton::end() {
if(!initialized)
return;
#ifdef RASPI
if(!this->gpioline_ptr)
return;
gpiod_line_close_chip(this->gpioline_ptr);
gpiod_line_release(this->gpioline_ptr);
this->gpioline_ptr = NULL;
#endif
}
bool ExtraButton::is_pressed() {
#ifdef RASPI
return gpio_read(this->pi_value, this->id) == 0;
#else
return false;
if(this->gpioline_ptr)
return gpiod_line_get_value(this->gpioline_ptr) == 0;
#endif
return false;
}
bool ExtraButton::is_valid() {
return this->initialized && (this->id >= 0) && (this->pi_value >= 0);
#ifdef RASPI
return this->initialized && (this->id >= 0) && this->gpioline_ptr;
#else
return false;
#endif
}
void ExtraButton::poll(std::queue<SFEvent> &events_queue) {
@@ -74,22 +87,17 @@ void ExtraButton::poll(std::queue<SFEvent> &events_queue) {
}
void init_extra_buttons_poll(int page_up_id, int page_down_id, int enter_id, int power_id, bool use_pud_up) {
int pi_value = -1;
#ifdef RASPI
pi_value = pigpio_start(NULL, NULL);
#endif
pi_page_up.initialize(pi_value, page_up_id, sf::Keyboard::PageUp, false, 0.5, 0.03, use_pud_up);
pi_page_down.initialize(pi_value, page_down_id, sf::Keyboard::PageDown, false, 0.5, 0.03, use_pud_up);
pi_enter.initialize(pi_value, enter_id, sf::Keyboard::Enter, false, 0.5, 0.075, use_pud_up);
pi_power.initialize(pi_value, power_id, sf::Keyboard::Escape, true, 30.0, 30.0, use_pud_up);
pi_page_up.initialize(page_up_id, sf::Keyboard::PageUp, false, 0.5, 0.03, use_pud_up);
pi_page_down.initialize(page_down_id, sf::Keyboard::PageDown, false, 0.5, 0.03, use_pud_up);
pi_enter.initialize(enter_id, sf::Keyboard::Enter, false, 0.5, 0.075, use_pud_up);
pi_power.initialize(power_id, sf::Keyboard::Escape, true, 30.0, 30.0, use_pud_up);
}
void end_extra_buttons_poll() {
#ifdef RASPI
int pi_value = pi_page_up.get_pi_value();
if(pi_value >= 0)
pigpio_stop(pi_value);
#endif
pi_page_up.end();
pi_page_down.end();
pi_enter.end();
pi_power.end();
}
void extra_buttons_poll(std::queue<SFEvent> &events_queue) {