Skip to content

Commit

Permalink
🎨 Tidying up the code
Browse files Browse the repository at this point in the history
This code came in without a pull request and before some of the newer
style guide requirements were added.
  • Loading branch information
kammce committed Oct 7, 2024
1 parent 1663336 commit 89f403a
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 166 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ AllowShortFunctionsOnASingleLine: None
FixNamespaceComments: true
SpacesBeforeTrailingComments: 2
ColumnLimit: 80
InsertNewlineAtEOF: true
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@ conanbuildinfo.txt

# CMake
CMakeUserPresets.json

5 changes: 2 additions & 3 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# ${CMAKE_SOURCE_DIR}/compile_commands.json
# DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)


find_package(libhal-linux REQUIRED CONFIG)

set(DEMOS gpio blinker i2c_test uart steady_clock_test)
set(DEMOS gpio blinker i2c uart steady_clock_test)
foreach(DEMO ${DEMOS})
message(STATUS "Generating Demo for \"${PROJECT_NAME}_${DEMO}")
add_executable(${PROJECT_NAME}_${DEMO} main.cpp applications/${DEMO}.cpp)
target_include_directories(${PROJECT_NAME}_${DEMO} PUBLIC .)
target_compile_features(${PROJECT_NAME}_${DEMO} PRIVATE cxx_std_23)
target_link_libraries(${PROJECT_NAME}_${DEMO} PRIVATE libhal::linux -static-libstdc++)

endforeach()
13 changes: 9 additions & 4 deletions demos/applications/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <unistd.h>

#include <iostream>

#include <libhal-linux/input_pin.hpp>
#include <libhal-linux/output_pin.hpp>
#include <libhal/error.hpp>
#include <unistd.h>

void application()
{
auto output_gpio = hal::gnu_linux::output_pin("/dev/gpiochip0", 2);
auto input_gpio = hal::gnu_linux::input_pin("/dev/gpiochip0", 3);

std::cout << "blinking gpio 2 on gpiochip0\n";

bool state = output_gpio.level();
bool saved_state = false;

while (true) {
output_gpio.level(state);
saved_state = output_gpio.level();
std::cout << "current state: " << saved_state << std::endl;
std::cout << "current state: " << saved_state << "\n";
sleep(1);
state ^= 1;
if (!input_gpio.level()) {
std::cout << "quiting, bye bye\n";
if (not input_gpio.level()) {
std::cout << "quitting, bye bye\n";
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void application()
const auto addr = 0x68;
const auto wake_sensor = std::array<hal::byte, 2>{ 0x6B, 0 };
hal::write(bus, addr, wake_sensor);
const auto set_scale = std::array<hal::byte, 2>{ 0xC1, 1 };

while (true) {
auto read_buffer = std::array<hal::byte, 6>{};
auto write_op = std::array<hal::byte, 1>{ 0x3B };
Expand Down
31 changes: 21 additions & 10 deletions demos/applications/uart.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
#include <unistd.h>

#include <iostream>
#include <span>
#include <string_view>

#include <libhal-linux/serial.hpp>
#include <libhal-util/serial.hpp>
#include <span>
#include <unistd.h>

void application()
{
std::cout << "UART test\n";
auto serial_file_path = "/dev/serial0";
constexpr auto serial_file_path = "/dev/serial0";
auto serial_bus = hal::gnu_linux::serial(serial_file_path);
std::string test_str = "Hello from libhal\n";
std::string_view const test_str = "Hello from libhal\n";
std::array<hal::byte, 255> input_buffer = { 0 };

while (true) {
hal::print(serial_bus, test_str);

sleep(1);
auto read_res = serial_bus.read(input_buffer);
if (input_buffer.at(0) == '\0') {

auto const received_data = serial_bus.read(input_buffer);

if (received_data.data.empty()) {
std::cout << "Nothing to read\n";
continue;
}
std::cout << "Len of res buffer: " << read_res.data.size()

std::cout << "Len of res buffer: " << received_data.data.size()
<< " len of input buffer: " << input_buffer.size() << "\n";
auto subspan = read_res.data.subspan(0, read_res.data.size());
auto read_string = std::string(subspan.begin(), subspan.end());
auto const read_string =
std::string_view(reinterpret_cast<char const*>(received_data.data.data()),
received_data.data.size());

sleep(1);
std::cout << "Read from serial:" << read_string;

std::cout << "Read from serial: " << read_string << "\n";
}
}
62 changes: 0 additions & 62 deletions demos/compile_commands 2.json

This file was deleted.

19 changes: 16 additions & 3 deletions include/libhal-linux/errors.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <libhal/error.hpp>
#include <string>
// This is to be internal, will be in the precompiled shared object

namespace hal::gnu_linux {
struct errno_exception : public hal::exception
Expand Down Expand Up @@ -29,4 +42,4 @@ struct invalid_character_device : public errno_exception
std::string invalid_device_path;
};

} // namespace hal::gnu_linux
} // namespace hal::gnu_linux
17 changes: 15 additions & 2 deletions include/libhal-linux/i2c.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <libhal/i2c.hpp>
#include <string>

namespace hal::gnu_linux {
class i2c : public hal::i2c
Expand All @@ -28,4 +41,4 @@ class i2c : public hal::i2c

int m_fd = 0;
};
} // namespace hal::gnu_linux
} // namespace hal::gnu_linux
10 changes: 3 additions & 7 deletions include/libhal-linux/input_pin.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#pragma once

#include <fcntl.h>
#include <linux/gpio.h>

#include <libhal/error.hpp>
#include <libhal/input_pin.hpp>
#include <linux/gpio.h>
#include <string>
#include <sys/ioctl.h>
#include <unistd.h>

namespace {
typedef struct gpio_v2_line_request gpio_line_request;
typedef struct gpio_v2_line_values gpio_values;

} // namespace

namespace hal::gnu_linux {
Expand Down Expand Up @@ -45,4 +41,4 @@ class input_pin : public hal::input_pin
gpio_line_request m_line_request;
gpio_values m_values;
};
} // namespace hal::gnu_linux
} // namespace hal::gnu_linux
44 changes: 24 additions & 20 deletions include/libhal-linux/output_pin.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <fcntl.h>
#include <linux/gpio.h>

#include <libhal/error.hpp>
#include <libhal/output_pin.hpp>
#include <libhal/units.hpp>
#include <linux/gpio.h>
#include <string>
#include <sys/ioctl.h>
#include <unistd.h>

namespace {
typedef struct gpio_v2_line_request gpio_line_request;
typedef struct gpio_v2_line_values gpio_values;
typedef struct gpio_v2_line_config gpio_config;

} // namespace

namespace hal::gnu_linux {

/**
* @brief Output pin for the linux kernel. Wraps libgpiod 2.1 at the earlist.
* Assumes a GPIO driver exists and is properly written for the specific
Expand All @@ -27,10 +30,11 @@ class output_pin : public hal::output_pin
{
public:
/**
* @brief Constructor. Takes a *full path* to the GPIO character device and a
* numeber that is known to said device.
* @param p_chip_name Full path to GPIO character device.
* @param p_pin Pin number for said device
* @brief Takes a *full path* to the GPIO character device and a number that
* is known to said device.
*
* @param p_chip_name - Full path to GPIO character device.
* @param p_pin - Pin number for said device
*
* @throws std::invalid_argument if an invalid chip path was given, an invalid
* pin number was given, or if a request to said line failed.
Expand All @@ -45,7 +49,7 @@ class output_pin : public hal::output_pin
bool driver_level() override;

int m_chip_fd = -1;
gpio_line_request m_line_request;
gpio_values m_values;
gpio_v2_line_request m_line_request;
gpio_v2_line_values m_values;
};
} // namespace hal::gnu_linux
} // namespace hal::gnu_linux
21 changes: 17 additions & 4 deletions include/libhal-linux/serial.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <libhal/serial.hpp>

#include <span>

#include <libhal/serial.hpp>

namespace hal::gnu_linux {
class serial : public hal::serial
{

public:
serial(const std::string& p_file_path, settings p_settings = {});
virtual ~serial();
Expand All @@ -18,5 +32,4 @@ class serial : public hal::serial

int m_fd = 0;
};

} // namespace hal::gnu_linux
} // namespace hal::gnu_linux
Loading

0 comments on commit 89f403a

Please sign in to comment.