Skip to content

Commit

Permalink
Bring back libgpiod v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kotochleb committed May 10, 2023
1 parent febc8b0 commit 4ca10ea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 46 deletions.
22 changes: 2 additions & 20 deletions panther_lights/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,6 @@ project(panther_lights)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_library(LIBGPIOD libgpiod)

if(NOT LIBGPIOD)
message(WARNING "libgpiod not found. Building from source")
include(ExternalProject)

ExternalProject_Add(libgpiod
GIT_REPOSITORY git://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git
GIT_TAG v2.0.1
PREFIX ${CATKIN_DEVEL_PREFIX}
CONFIGURE_COMMAND <SOURCE_DIR>/autogen.sh --prefix=<INSTALL_DIR> --enable-tools=no --enable-bindings-cxx
BUILD_COMMAND make -j ${N}
INSTALL_COMMAND make install INSTALL_PREFIX=${CATKIN_DEVEL_PREFIX}/lib/libgpiod
BUILD_IN_SOURCE 1
)
endif()

find_package(catkin REQUIRED COMPONENTS
image_transport
panther_msgs
Expand All @@ -33,7 +16,6 @@ find_package(catkin REQUIRED COMPONENTS

catkin_package(
INCLUDE_DIRS include
LIBRARIES libgpiod
CATKIN_DEPENDS panther_msgs roscpp
)

Expand All @@ -48,9 +30,9 @@ add_executable(driver_node
src/apa102.cpp
)

add_dependencies(driver_node libgpiod ${catkin_EXPORTED_TARGETS})
add_dependencies(driver_node ${catkin_EXPORTED_TARGETS})
target_link_libraries(driver_node
${CATKIN_DEVEL_PREFIX}/lib/libgpiodcxx.so
gpiodcxx
${catkin_LIBRARIES}
)

Expand Down
2 changes: 1 addition & 1 deletion panther_lights/include/panther_lights/driver_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DriverNode
int num_led_;
double frame_timeout_;
bool panels_initialised_ = false;
gpiod::line power_pin_;
std::string node_name_;

APA102 front_panel_;
Expand All @@ -46,7 +47,6 @@ class DriverNode
void frame_cb(
const sensor_msgs::Image::ConstPtr & msg, const APA102 & panel, const ros::Time & last_time,
const std::string & panel_name);
void set_pin_value(const gpiod::line::value value) const;
bool set_brightness_cb(
panther_msgs::SetLEDBrightness::Request & req, panther_msgs::SetLEDBrightness::Response & res);
};
Expand Down
7 changes: 1 addition & 6 deletions panther_lights/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@

<buildtool_depend>catkin</buildtool_depend>

<build_depend>autoconf</build_depend>
<build_depend>autoconf-archive</build_depend>
<build_depend>libtool</build_depend>
<build_depend>m4</build_depend>
<build_depend>pkg-config</build_depend>

<depend>image_transport</depend>
<depend>libgpiod-dev</depend>
<depend>panther_msgs</depend>
<depend>roscpp</depend>
<depend>rospy</depend>
Expand Down
28 changes: 9 additions & 19 deletions panther_lights/src/driver_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ DriverNode::DriverNode(
frame_timeout_ = ph_->param<double>("frame_timeout", 0.1);
num_led_ = ph_->param<int>("num_led", 46);

set_pin_value(gpiod::line::value::INACTIVE);
const gpiod::chip chip("gpiochip0");
power_pin_ = chip.find_line("LED_SBC_SEL");

const gpiod::line_request lr = {
node_name_, gpiod::line_request::DIRECTION_OUTPUT, gpiod::line_request::FLAG_ACTIVE_LOW};
power_pin_.request(lr, 0);

front_panel_ts_ = ros::Time::now();
rear_panel_ts_ = ros::Time::now();
Expand Down Expand Up @@ -71,7 +76,8 @@ DriverNode::~DriverNode()
rear_panel_.set_panel(std::vector<std::uint8_t>(num_led_ * 4, 0));

// give back control over LEDs
set_pin_value(gpiod::line::value::INACTIVE);
power_pin_.set_value(0);
power_pin_.release();
}

bool DriverNode::set_brightness_cb(
Expand Down Expand Up @@ -124,26 +130,10 @@ void DriverNode::frame_cb(
panels_initialised_ = true;

// take control over LEDs
set_pin_value(gpiod::line::value::ACTIVE);
power_pin_.set_value(1);
}
panel.set_panel(msg->data);
}
}

void DriverNode::set_pin_value(const gpiod::line::value value) const
{
auto chip = gpiod::chip(std::filesystem::path{"/dev/gpiochip0"});
auto power_pin_offset = gpiod::line::offset(chip.get_line_offset_from_name("LED_SBC_SEL"));

auto settings = gpiod::line_settings();
settings.set_direction(gpiod::line::direction::OUTPUT);
settings.set_active_low(true);
settings.set_output_value(value);

auto rb = chip.prepare_request();
rb.set_consumer(node_name_);
rb.add_line_settings(power_pin_offset, settings);
rb.do_request();
}

} // namespace panther_lights

0 comments on commit 4ca10ea

Please sign in to comment.