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 45f8946
Show file tree
Hide file tree
Showing 23 changed files with 190 additions and 200 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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ jobs:
with:
compiler_profile_url: https://github.com/libhal/arm-gnu-toolchain.git
compiler_profile: v1/arm-gcc-12.3
# TODO: Update this with the correct information
# platform_profile_url: https://github.com/libhal/libhal-<insert repo name>.git
# platform_profile: profile1
secrets: inherit
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

49 changes: 30 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
# libhal-linux
> [!IMPORTANT]
> There is not yet a published package on JFrog and you will need to build the package manually using `conan create . --version=latest`. This will be fixed soon.
> No cross compiler profiles have been created yet, to use develop using this you currently need develop with the same archtecture and operating system as your build target. A Dockerfile has been provided for arm64 linux development. (Raspberry Pis and alike)

> [!IMPORTANT]
> There is not yet a published package on JFrog and you will need to build the
> package manually using `conan create . --version=latest`. This will be fixed
> soon.
> No cross compiler profiles have been created yet, to use develop using this
> you currently need develop with the same architecture and operating system as
> your build target. A Dockerfile has been provided for arm64 linux
> development. (Raspberry Pis and alike)
[![✅ Checks](https://github.com/libhal/libhal-linux/actions/workflows/ci.yml/badge.svg)](https://github.com/libhal/libhal-linux/actions/workflows/ci.yml)
[![Coverage](https://libhal.github.io/libhal-linux/coverage/coverage.svg)](https://libhal.github.io/libhal-linux/coverage/)
[![GitHub stars](https://img.shields.io/github/stars/libhal/libhal-linux.svg)](https://github.com/libhal/libhal-linux/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/libhal/libhal-linux.svg)](https://github.com/libhal/libhal-linux/network)
[![GitHub issues](https://img.shields.io/github/issues/libhal/libhal-linux.svg)](https://github.com/libhal/libhal-linux/issues)

Userspace library for embedded linux

Git History for implementations of output_pin, input_pin, i2c, and serial can be seen [here](https://github.com/PhazonicRidley/libhal-linux-alpha)

## Contributing

See [`CONTRIBUTING.md`](CONTRIBUTING.md) for details.

## License

Apache 2.0; see [`LICENSE`](LICENSE) for details.

---
User space library for embedded linux

## 📚 Software APIs & Usage

To learn about available kernel APIs, documentation will be be made available soon.

To see some example code on how various APIs are used, see the programs inside the `demos` folder.
Expand All @@ -40,18 +34,35 @@ Coming soon, see important notice.

## 🏗️ Building Demos

**ON the device that is to run this code, a computer that is the same OS and architecture, or inside the provided docker container:**
> [!NOTE]
> ON the device that is to run this code, a computer that is the same OS and
> architecture, or inside the provided docker container.
To build demos, start at the root of the repo and execute the following command:

```bash
conan build demos
```

## 💾 Flashing/Programming

The easiest way to get your compiled program onto hardware like a Raspberry Pi is via ssh. (Assuming your hardware's linux installation has sshd).
The easiest way to get your compiled program onto hardware like a Raspberry Pi
is via ssh. (Assuming your hardware's linux installation has sshd).

Run the following command:

```bash
scp path/to/your.bin username@hardware_device:new_path/to/app
```

## Contributing

See [`CONTRIBUTING.md`](CONTRIBUTING.md) for details.

> [!NOTE]
> For git History for implementations of output_pin, input_pin, i2c, and serial
> can be seen [here](https://github.com/PhazonicRidley/libhal-linux-alpha)
## License

Apache 2.0; see [`LICENSE`](LICENSE) for details.
8 changes: 0 additions & 8 deletions conan/profiles/linux

This file was deleted.

3 changes: 0 additions & 3 deletions conan/profiles/profile1

This file was deleted.

3 changes: 0 additions & 3 deletions conan/profiles/profile2

This file was deleted.

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
Loading

0 comments on commit 45f8946

Please sign in to comment.