Skip to content

Commit

Permalink
Merge pull request #15 from MuonPi/feature/i2c
Browse files Browse the repository at this point in the history
feature/i2c
  • Loading branch information
dtreffenstaedt authored Apr 10, 2022
2 parents bd0c950 + f4ceaed commit f7c79a2
Show file tree
Hide file tree
Showing 64 changed files with 2,879 additions and 3,076 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/check-clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ jobs:

- name: clang-format
run: cmake --build ${{github.workspace}}/build --target clangformat

- name: show-changed-files
run: git status

- name: show-changes
run: git diff

- name: check-changes
run: bash -c '[ -z "$(git status --porcelain=v1 2>/dev/null)" ]'
2 changes: 2 additions & 0 deletions cmake/targets/core.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ set(CORE_HEADER_FILES
"${PROJECT_HEADER_DIR}/muonpi/supervision/resource.h"
"${PROJECT_HEADER_DIR}/muonpi/global.h"
"${PROJECT_HEADER_DIR}/muonpi/types.h"
"${PROJECT_HEADER_DIR}/muonpi/concepts.h"
"${PROJECT_HEADER_DIR}/muonpi/range.h"
)


Expand Down
21 changes: 6 additions & 15 deletions cmake/targets/detector.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@ set(DETECTOR_SOURCE_FILES
"${PROJECT_SRC_DIR}/gpio_handler.cpp"
"${PROJECT_SRC_DIR}/serial/i2cdevice.cpp"
"${PROJECT_SRC_DIR}/serial/i2cbus.cpp"
"${PROJECT_SRC_DIR}/serial/i2cdevices/lm75.cpp"
"${PROJECT_SRC_DIR}/serial/i2cdevices/mic184.cpp"
"${PROJECT_SRC_DIR}/serial/i2cdevices/ads1115.cpp"
"${PROJECT_SRC_DIR}/serial/i2cdevices/mcp4728.cpp"
"${PROJECT_SRC_DIR}/serial/i2cdevices/eeprom24aa02.cpp"
"${PROJECT_SRC_DIR}/serial/i2cdevices/generalcall.cpp"
)

set(DETECTOR_HEADER_FILES
"${PROJECT_HEADER_DIR}/muonpi/addressrange.h"
"${PROJECT_HEADER_DIR}/muonpi/multiaddressrange.h"
"${PROJECT_HEADER_DIR}/muonpi/gpio_handler.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevice.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdefinitions.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cbus.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/lm75.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/mic184.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/ads1115.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/mcp4728.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/eeprom24aa02.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/io_extender.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/pca9536.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/pca9554.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2ceeprom.h"
"${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/generalcall.h"
)

if (NOT LIBMUONPI_FORMAT_ONLY)
if (LIBMUONPI_BUILD_DETECTOR) # libraries specific to the Detector library
find_library(LIBGPIOD gpiod REQUIRED)
Expand Down
37 changes: 19 additions & 18 deletions config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,41 @@
#include <string>

#ifdef _MSC_VER
// Microsoft
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
// Microsoft
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
#elif defined(__GNUC__)
// GCC
#define EXPORT __attribute__((visibility("default")))
#define IMPORT
#define HIDDEN __attribute__((visibility("hidden")))
// GCC
#define EXPORT __attribute__((visibility("default")))
#define IMPORT
#define HIDDEN __attribute__((visibility("hidden")))
#else
// do nothing and hope for the best?
#define EXPORT
#define IMPORT
#pragma warning Unknown dynamic link import/export semantics.
// do nothing and hope for the best?
#define EXPORT
#define IMPORT
#pragma warning Unknown dynamic link import / export semantics.
#endif

#cmakedefine LIBMUONPI_COMPILING

#ifdef LIBMUONPI_COMPILING
# define LIBMUONPI_PUBLIC EXPORT
#define LIBMUONPI_PUBLIC EXPORT
#else
# define LIBMUONPI_PUBLIC IMPORT
#define LIBMUONPI_PUBLIC IMPORT
#endif

#define BOOST_ENABLE_ASSERT_DEBUG_HANDLER

namespace muonpi::Version::libmuonpi {
constexpr int major { @PROJECT_VERSION_MAJOR@ };
constexpr int minor { @PROJECT_VERSION_MINOR@ };
constexpr int patch { @PROJECT_VERSION_PATCH@ };
constexpr const char* additional { "@PROJECT_VERSION_ADDITIONAL@" };
// clang-format off
constexpr int major {@PROJECT_VERSION_MAJOR@};
constexpr int minor {@PROJECT_VERSION_MINOR@};
constexpr int patch {@PROJECT_VERSION_PATCH@};
constexpr const char* additional {"@PROJECT_VERSION_ADDITIONAL@"};
// clang-format on

[[nodiscard]] auto string() -> std::string;

} // namespace muonpi::Version::libmuonpi


#endif // MUONPI_GLOBAL_H
42 changes: 26 additions & 16 deletions examples/config/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
#include <iostream>
#include <muonpi/configuration.h>
#include <muonpi/log.h>

#include <iostream>

auto main(int argc, const char* argv[]) -> int
{
muonpi::log::system::setup(muonpi::log::Level::Info, [](int c){exit(c);}, std::cerr);
auto main(int argc, const char* argv[]) -> int {
muonpi::log::system::setup(
muonpi::log::Level::Info,
[](int c) { exit(c); },
std::cerr);

muonpi::config config{};
muonpi::config config {};

auto cmd_options = config.setup("Commandline options");
cmd_options.add_option("test,t", "This is just a test!");
cmd_options.add_option("var,v", boost::program_options::value<std::string>(), "This is also just a test!");
cmd_options.add_option("int,i", boost::program_options::value<int>()->required(), "This is also just a test!");
cmd_options.add_option("var,v",
boost::program_options::value<std::string>(),
"This is also just a test!");
cmd_options.add_option("int,i",
boost::program_options::value<int>()->required(),
"This is also just a test!");
cmd_options.commit(argc, argv);

std::cout<<cmd_options;

std::cout << cmd_options;

auto file_options = config.setup("Commandline options");
file_options.add_option("ftest", boost::program_options::value<std::string>(), "This is just a test!");
file_options.add_option("fvar", boost::program_options::value<std::string>(), "This is also just a test!");
file_options.add_option("fint", boost::program_options::value<int>()->required(), "This is also just a test!");
file_options.add_option("ftest",
boost::program_options::value<std::string>(),
"This is just a test!");
file_options.add_option("fvar",
boost::program_options::value<std::string>(),
"This is also just a test!");
file_options.add_option("fint",
boost::program_options::value<int>()->required(),
"This is also just a test!");
file_options.commit("example.cfg");

std::cout<<file_options;
std::cout << file_options;

if (config.is_set("var")) {
std::cout<<"option var: "<<config.get<std::string>("var")<<'\n';
std::cout << "option var: " << config.get<std::string>("var") << '\n';
}
std::cout<<"option int: "<<config.get<int>("int")<<'\n';
std::cout << "option int: " << config.get<int>("int") << '\n';
}
64 changes: 34 additions & 30 deletions examples/gpiod/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
#include <iostream>
#include <muonpi/gpio_handler.h>
#include <muonpi/log.h>

#include <iostream>

auto main() -> int
{
muonpi::log::system::setup(muonpi::log::Level::Info, [](int c){exit(c);}, std::cerr);

auto main() -> int {
muonpi::log::system::setup(
muonpi::log::Level::Info,
[](int c) { exit(c); },
std::cerr);

muonpi::gpio_handler gpio{"/dev/gpiochip0", "muonpi"};
muonpi::gpio_handler gpio {"/dev/gpiochip0", "muonpi"};

auto chip = gpio.get_chip_info();

std::cout
<<"chip name: "<<chip.name
<<"\nchip label: "<<chip.label
<<"\nchip num lines: "<<chip.num_lines
<<"\nlines:\n";
unsigned int gpio_index { 0 };
for (const auto& line: chip.lines) {
std::cout << "chip name: " << chip.name << "\nchip label: " << chip.label
<< "\nchip num lines: " << chip.num_lines << "\nlines:\n";
unsigned int gpio_index {0};
for (const auto& line : chip.lines) {
std::cout << gpio_index++ << "\t: " << line.name << " " << line.consumer << "\n";
}
std::cout<<'\n';
std::cout << '\n';

auto led_set_fn { gpio.set_pin_output( 19, muonpi::gpio::state_t{muonpi::gpio::state_t::Low}, muonpi::gpio::bias_t::OpenSource ) };
auto gpio_read_fn { gpio.get_pin_input( 23, muonpi::gpio::bias_t::Disabled ) };
auto led_set_fn {gpio.set_pin_output(19,
muonpi::gpio::state_t {muonpi::gpio::state_t::Low},
muonpi::gpio::bias_t::OpenSource)};
auto gpio_read_fn {gpio.get_pin_input(23, muonpi::gpio::bias_t::Disabled)};

muonpi::gpio::pins_t pins {
muonpi::gpio::pin_setting_t{5, muonpi::gpio::edge_t::Falling, muonpi::gpio::bias_t::Disabled},
muonpi::gpio::pin_setting_t{27, muonpi::gpio::edge_t::Rising, muonpi::gpio::bias_t::Disabled},
muonpi::gpio::pin_setting_t {5,
muonpi::gpio::edge_t::Falling,
muonpi::gpio::bias_t::Disabled},
muonpi::gpio::pin_setting_t {27,
muonpi::gpio::edge_t::Rising,
muonpi::gpio::bias_t::Disabled},
};

auto callback { [&](muonpi::gpio::event_t evt){
std::cout
<<evt.pin
<<": "<<((evt.edge==muonpi::gpio::edge_t::Rising)?"Rising":"Falling")
<<": "<<std::chrono::duration_cast<std::chrono::microseconds>(evt.time.time_since_epoch()).count()<<"\n";
auto callback {[&](muonpi::gpio::event_t evt) {
std::cout << evt.pin << ": "
<< ((evt.edge == muonpi::gpio::edge_t::Rising) ? "Rising" : "Falling") << ": "
<< std::chrono::duration_cast<std::chrono::microseconds>(
evt.time.time_since_epoch())
.count()
<< "\n";
// read state of the pin with the provided lambda function
muonpi::gpio::state_t state = gpio_read_fn();
// set state of other pin with this state, but invert
bool ok = led_set_fn(!state);
if (!ok) {
std::cout<<" error setting LED\n";
std::cout << " error setting LED\n";
}
} };
}};

if (gpio.set_pin_interrupt(pins, callback))
{
std::cout<<"success.\n";
if (gpio.set_pin_interrupt(pins, callback)) {
std::cout << "success.\n";
} else {
std::cout<<"fail\n";
std::cout << "fail\n";
}

gpio.join();
Expand Down
11 changes: 4 additions & 7 deletions examples/http-request/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include <iostream>
#include <muonpi/http_request.h>
#include <muonpi/log.h>

#include <iostream>

auto main() -> int
{
auto main() -> int {
muonpi::log::system::setup(muonpi::log::Level::Info);

muonpi::http::destination_t config {};
config.host = "localhost";
config.port = 8000;
config.host = "localhost";
config.port = 8000;
config.target = "/hello";
config.method = muonpi::http::http_verb::get;


std::cout << muonpi::http::http_request(config, "hello.") << '\n';
}
35 changes: 18 additions & 17 deletions examples/http/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
#include <muonpi/http_server.h>
#include <iostream>
#include <muonpi/http_response.h>
#include <muonpi/http_server.h>
#include <muonpi/log.h>

#include <iostream>

auto main() -> int
{
auto main() -> int {
muonpi::log::system::setup(muonpi::log::Level::Info);

muonpi::http::http_server::configuration config {};
config.address = "0.0.0.0";
config.port = 8000;
config.ssl = false;
config.port = 8000;
config.ssl = false;

muonpi::http::http_server service{config};
muonpi::http::http_server service {config};

muonpi::http::path_handler handler1 {};
handler1.matches = [](std::string_view path) {return path == "hello";};
handler1.handle = [](muonpi::http::request_type& req, const std::queue<std::string>& /*unused*/) -> muonpi::http::response_type {
std::cout<<"Got request for /hello\n"<<std::flush;
handler1.matches = [](std::string_view path) { return path == "hello"; };
handler1.handle = [](muonpi::http::request_type& req,
const std::queue<std::string>& /*unused*/) -> muonpi::http::response_type {
std::cout << "Got request for /hello\n" << std::flush;
return muonpi::http::http_response<muonpi::http::http_status::ok>(req)("Hello!");
};

service.add_handler(handler1);

muonpi::http::path_handler handler2 {};
handler2.matches = [](std::string_view path) {return path == "bye";};
handler2.handle = [](muonpi::http::request_type& req, const std::queue<std::string>& /*unused*/) -> muonpi::http::response_type {
std::cout<<"Got request for /bye\n"<<std::flush;
handler2.matches = [](std::string_view path) { return path == "bye"; };
handler2.handle = [](muonpi::http::request_type& req,
const std::queue<std::string>& /*unused*/) -> muonpi::http::response_type {
std::cout << "Got request for /bye\n" << std::flush;
return muonpi::http::http_response<muonpi::http::http_status::ok>(req)("Bye!");
};

muonpi::http::path_handler handler3 {};
handler3.matches = [](std::string_view path) {return path == "bye";};
handler3.handle = [](muonpi::http::request_type& req, const std::queue<std::string>& /*unused*/) -> muonpi::http::response_type {
std::cout<<"Got request for /bye/bye\n"<<std::flush;
handler3.matches = [](std::string_view path) { return path == "bye"; };
handler3.handle = [](muonpi::http::request_type& req,
const std::queue<std::string>& /*unused*/) -> muonpi::http::response_type {
std::cout << "Got request for /bye/bye\n" << std::flush;
return muonpi::http::http_response<muonpi::http::http_status::ok>(req)("Bye-Bye!");
};

Expand Down
Loading

0 comments on commit f7c79a2

Please sign in to comment.