From 9914b4a9c7b0f71aa5c5f84c9bdd7b7561888d88 Mon Sep 17 00:00:00 2001 From: Chi Huu Huynh <73843190+Chi-EEE@users.noreply.github.com> Date: Sat, 23 Dec 2023 17:05:50 +0000 Subject: [PATCH] Simple code of using libgpiod --- .vscode/c_cpp_properties.json | 2 +- .vscode/settings.json | 12 ++++++- .../repository/packages/t/tb6612/xmake.lua | 2 +- .../tests/tb6612/test_rear_wheels.cpp | 35 +++++++++++++++++++ app/raspberry_pi/xmake.lua | 15 +++++++- 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 app/raspberry_pi/tests/tb6612/test_rear_wheels.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 225d807c..68935dd0 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -10,7 +10,7 @@ "cStandard": "c17", "cppStandard": "gnu++17", "intelliSenseMode": "linux-gcc-x64", - "compileCommands": "legacy/compile_commands.json" + "compileCommands": "app/raspberry_pi/compile_commands.json" } ], "version": 4 diff --git a/.vscode/settings.json b/.vscode/settings.json index 55e6a62d..8defa8bf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -75,6 +75,16 @@ "xutility": "cpp", "unordered_set": "cpp", "set": "cpp", - "variant": "cpp" + "variant": "cpp", + "*.tcc": "cpp", + "codecvt": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "numbers": "cpp", + "semaphore": "cpp" } } \ No newline at end of file diff --git a/app/raspberry_pi/repository/packages/t/tb6612/xmake.lua b/app/raspberry_pi/repository/packages/t/tb6612/xmake.lua index b7f0a0fe..f9c96e49 100644 --- a/app/raspberry_pi/repository/packages/t/tb6612/xmake.lua +++ b/app/raspberry_pi/repository/packages/t/tb6612/xmake.lua @@ -1,5 +1,5 @@ package("tb6612") - add_deps("cppgpio") + add_deps("cppgpiod") set_sourcedir(path.join(os.scriptdir(), "tb6612")) on_install(function (package) local configs = {} diff --git a/app/raspberry_pi/tests/tb6612/test_rear_wheels.cpp b/app/raspberry_pi/tests/tb6612/test_rear_wheels.cpp new file mode 100644 index 00000000..b1be5a53 --- /dev/null +++ b/app/raspberry_pi/tests/tb6612/test_rear_wheels.cpp @@ -0,0 +1,35 @@ +#include + +#include "gpiod.hpp" + +int main() +{ + + return 0; +} + +void setupGPIOMotor() +{ + int directionChannel; + // Setup GPIO for direction channel + gpiod::chip chip("/dev/gpiochip0"); + int direction_channel = 17; + chip.prepare_request() + .add_line_settings( + gpiod::line::offset(direction_channel), gpiod::line_settings() + .set_direction(gpiod::line::direction::OUTPUT) // Setup the GPIO + .set_debounce_period(std::chrono::microseconds(100)) // 100 Speed + .set_output_value(gpiod::line::value::ACTIVE) // Move wheel + ) + .do_request(); + + std::this_thread::sleep_for(std::chrono::seconds(5)); + + chip.prepare_request() + .add_line_settings( + gpiod::line::offset(direction_channel), gpiod::line_settings() + .set_direction(gpiod::line::direction::OUTPUT) // Setup the GPIO + .set_output_value(gpiod::line::value::INACTIVE) // Stop wheel + ) + .do_request(); +} \ No newline at end of file diff --git a/app/raspberry_pi/xmake.lua b/app/raspberry_pi/xmake.lua index 1fe426f6..f5a61f98 100644 --- a/app/raspberry_pi/xmake.lua +++ b/app/raspberry_pi/xmake.lua @@ -19,6 +19,7 @@ add_requires("imath") if is_plat("linux", "macosx") then -- For the SunFounder Car add_requires("pca9685") + add_requires("tb6612") end -- For Functional Programming? @@ -46,6 +47,7 @@ target("raspberry_pi") -- For the SunFounder Car add_packages("rplidar") add_packages("pca9685") + add_packages("tb6612") add_headerfiles("include/**.h") @@ -62,7 +64,6 @@ target("raspberry_pi") add_configfiles("settings/config.jsonc", {onlycopy = true, prefixdir = "settings"}) -- From xmake sample code: - if is_plat("linux") then for _, file in ipairs(os.files("tests/pca9685/test_*.cpp")) do local name = path.basename(file) @@ -76,4 +77,16 @@ if is_plat("linux") then add_files("tests/pca9685/" .. name .. ".cpp") target_end() end + for _, file in ipairs(os.files("tests/tb6612/test_*.cpp")) do + local name = path.basename(file) + target(name) + set_kind("binary") + set_default(false) + set_license("LGPL-2.1") + + add_packages("tb6612") + + add_files("tests/tb6612/" .. name .. ".cpp") + target_end() + end end \ No newline at end of file