Skip to content

Commit

Permalink
💥 Upgrade to libhal/3.0.0-alpha.1 exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kammce committed Feb 23, 2024
1 parent 29e8216 commit cf46631
Show file tree
Hide file tree
Showing 23 changed files with 532 additions and 669 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/4.0.0-alpha.1.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# THIS IS HERE FOR TESTING!
name: 🚀 4.0.0-alpha.1

on:
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- run: echo "NOP"
uses: libhal/ci/.github/workflows/[email protected]
with:
version: 4.0.0-alpha.1
secrets: inherit
14 changes: 6 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
name: Checks
name: CI

on:
workflow_dispatch:
pull_request:
release:
types:
- published
- deleted
push:
branches:
- main
# Remove before merging with main
- exceptions
schedule:
- cron: "0 12 * * 0"

jobs:
ci:
uses: libhal/ci/.github/workflows/library.yml@4.x.y
uses: libhal/ci/.github/workflows/library_check.yml@5.x.y
secrets: inherit

devices:
uses: libhal/ci/.github/workflows/deploy.yml@4.x.y
test_deployment_:
uses: libhal/ci/.github/workflows/deploy_all.yml@5.x.y
secrets: inherit
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ libhal_test_and_make_library(
tests/serial.test.cpp
tests/spi.test.cpp
tests/static_callable.test.cpp

tests/static_list.test.cpp
tests/steady_clock.test.cpp
tests/streams.test.cpp
Expand Down
5 changes: 2 additions & 3 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
import os


required_conan_version = ">=2.0.6"
required_conan_version = ">=2.0.14"


class libhal_util_conan(ConanFile):
name = "libhal-util"
version = "3.0.1"
license = "Apache-2.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/libhal/libhal-util"
Expand Down Expand Up @@ -64,7 +63,7 @@ def build_requirements(self):
self.test_requires("boost-ext-ut/1.1.9")

def requirements(self):
self.requires("libhal/[^2.0.3]", transitive_headers=True)
self.requires("libhal/3.0.0-alpha.1", transitive_headers=True)

def layout(self):
cmake_layout(self)
Expand Down
153 changes: 75 additions & 78 deletions include/libhal-util/i2c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
/**
* @defgroup I2CUtils I2C Utils
*/

namespace hal {
/**
* @ingroup I2CUtils
Expand All @@ -52,16 +51,13 @@ namespace hal {
* @param p_address - target address
* @param p_data_out - buffer of bytes to write to the target device
* @param p_timeout - amount of time to execute the transaction
* @return hal::result<hal::i2c::transaction_t> - success or failure
*/
[[nodiscard]] inline hal::result<hal::i2c::transaction_t> write(
i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out,
timeout auto p_timeout)
inline void write(i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out,
timeout auto p_timeout)
{
return p_i2c.transaction(
p_address, p_data_out, std::span<hal::byte>{}, p_timeout);
p_i2c.transaction(p_address, p_data_out, std::span<hal::byte>{}, p_timeout);
}

/**
Expand All @@ -74,12 +70,12 @@ namespace hal {
* @param p_i2c - i2c driver
* @param p_address - target address
* @param p_data_out - buffer of bytes to write to the target device
* @return hal::result<hal::i2c::transaction_t> - success or failure
*/
[[nodiscard]] inline hal::result<hal::i2c::transaction_t>
write(i2c& p_i2c, hal::byte p_address, std::span<const hal::byte> p_data_out)
inline void write(i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out)
{
return write(p_i2c, p_address, p_data_out, hal::never_timeout());
write(p_i2c, p_address, p_data_out, hal::never_timeout());
}

/**
Expand All @@ -92,16 +88,13 @@ write(i2c& p_i2c, hal::byte p_address, std::span<const hal::byte> p_data_out)
* @param p_address - target address
* @param p_data_in - buffer to read bytes into from target device
* @param p_timeout - amount of time to execute the transaction
* @return hal::result<hal::i2c::transaction_t> - success or failure
*/
[[nodiscard]] inline hal::result<hal::i2c::transaction_t> read(
i2c& p_i2c,
hal::byte p_address,
std::span<hal::byte> p_data_in,
timeout auto p_timeout)
inline void read(i2c& p_i2c,
hal::byte p_address,
std::span<hal::byte> p_data_in,
timeout auto p_timeout)
{
return p_i2c.transaction(
p_address, std::span<hal::byte>{}, p_data_in, p_timeout);
p_i2c.transaction(p_address, std::span<hal::byte>{}, p_data_in, p_timeout);
}

/**
Expand All @@ -114,12 +107,12 @@ write(i2c& p_i2c, hal::byte p_address, std::span<const hal::byte> p_data_out)
* @param p_i2c - i2c driver
* @param p_address - target address
* @param p_data_in - buffer to read bytes into from target device
* @return hal::result<hal::i2c::transaction_t> - success or failure
*/
[[nodiscard]] inline hal::result<hal::i2c::transaction_t>
read(i2c& p_i2c, hal::byte p_address, std::span<hal::byte> p_data_in)
inline void read(i2c& p_i2c,
hal::byte p_address,
std::span<hal::byte> p_data_in)
{
return read(p_i2c, p_address, p_data_in, hal::never_timeout());
read(p_i2c, p_address, p_data_in, hal::never_timeout());
}

/**
Expand All @@ -128,19 +121,20 @@ read(i2c& p_i2c, hal::byte p_address, std::span<hal::byte> p_data_in)
*
* Eliminates the need to create a buffer and pass it into the read function.
*
* @tparam BytesToRead - number of bytes to read
* @tparam bytes_to_read - number of bytes to read
* @param p_i2c - i2c driver
* @param p_address - target address
* @param p_timeout - amount of time to execute the transaction
* @return result<std::array<hal::byte, BytesToRead>> - array of
* bytes from target device or an error.
* @return std::array<hal::byte, bytes_to_read> - array of bytes from target
* device
*/
template<size_t BytesToRead>
[[nodiscard]] result<std::array<hal::byte, BytesToRead>>
read(i2c& p_i2c, hal::byte p_address, timeout auto p_timeout)
template<size_t bytes_to_read>
[[nodiscard]] std::array<hal::byte, bytes_to_read> read(i2c& p_i2c,
hal::byte p_address,
timeout auto p_timeout)
{
std::array<hal::byte, BytesToRead> buffer;
HAL_CHECK(read(p_i2c, p_address, buffer, p_timeout));
std::array<hal::byte, bytes_to_read> buffer;
read(p_i2c, p_address, buffer, p_timeout);
return buffer;
}

Expand All @@ -152,18 +146,17 @@ read(i2c& p_i2c, hal::byte p_address, timeout auto p_timeout)
* This operation will never time out and should only be used with devices that
* never perform clock stretching.
*
* @tparam BytesToRead - number of bytes to read
* @tparam bytes_to_read - number of bytes to read
* @param p_i2c - i2c driver
* @param p_address - target address
* @return result<std::array<hal::byte, BytesToRead>> - array of
* bytes from target device or an error.
* @return std::array<hal::byte, bytes_to_read> - array of bytes from target
* device.
*/
template<size_t BytesToRead>
[[nodiscard]] result<std::array<hal::byte, BytesToRead>> read(
i2c& p_i2c,
hal::byte p_address)
template<size_t bytes_to_read>
[[nodiscard]] std::array<hal::byte, bytes_to_read> read(i2c& p_i2c,
hal::byte p_address)
{
return read<BytesToRead>(p_i2c, p_address, hal::never_timeout());
return read<bytes_to_read>(p_i2c, p_address, hal::never_timeout());
}

/**
Expand All @@ -178,17 +171,14 @@ template<size_t BytesToRead>
* @param p_data_out - buffer of bytes to write to the target device
* @param p_data_in - buffer to read bytes into from target device
* @param p_timeout - amount of time to execute the transaction
*
* @return hal::result<hal::i2c::transaction_t> - success or failure
*/
[[nodiscard]] inline hal::result<hal::i2c::transaction_t> write_then_read(
i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out,
std::span<hal::byte> p_data_in,
timeout auto p_timeout = hal::never_timeout())
inline void write_then_read(i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out,
std::span<hal::byte> p_data_in,
timeout auto p_timeout = hal::never_timeout())
{
return p_i2c.transaction(p_address, p_data_out, p_data_in, p_timeout);
p_i2c.transaction(p_address, p_data_out, p_data_in, p_timeout);
}

/**
Expand All @@ -204,14 +194,11 @@ template<size_t BytesToRead>
* @param p_address - target address
* @param p_data_out - buffer of bytes to write to the target device
* @param p_data_in - buffer to read bytes into from target device
*
* @return hal::result<hal::i2c::transaction_t> - success or failure
*/
[[nodiscard]] inline hal::result<hal::i2c::transaction_t> write_then_read(
i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out,
std::span<hal::byte> p_data_in)
inline void write_then_read(i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out,
std::span<hal::byte> p_data_in)
{
return write_then_read(
p_i2c, p_address, p_data_out, p_data_in, hal::never_timeout());
Expand All @@ -224,22 +211,23 @@ template<size_t BytesToRead>
*
* Eliminates the need to create a buffer and pass it into the read function.
*
* @tparam BytesToRead - number of bytes to read after write
* @tparam bytes_to_read - number of bytes to read after write
* @param p_i2c - i2c driver
* @param p_address - target address
* @param p_data_out - buffer of bytes to write to the target device
* @param p_timeout - amount of time to execute the transaction
* @return result<std::array<hal::byte, BytesToRead>>
* @return std::array<hal::byte, bytes_to_read> - array of bytes from target
* device.
*/
template<size_t BytesToRead>
[[nodiscard]] result<std::array<hal::byte, BytesToRead>> write_then_read(
template<size_t bytes_to_read>
[[nodiscard]] std::array<hal::byte, bytes_to_read> write_then_read(
i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out,
timeout auto p_timeout)
{
std::array<hal::byte, BytesToRead> buffer;
HAL_CHECK(write_then_read(p_i2c, p_address, p_data_out, buffer, p_timeout));
std::array<hal::byte, bytes_to_read> buffer;
write_then_read(p_i2c, p_address, p_data_out, buffer, p_timeout);
return buffer;
}

Expand All @@ -250,41 +238,51 @@ template<size_t BytesToRead>
*
* Eliminates the need to create a buffer and pass it into the read function.
*
* @tparam BytesToRead - number of bytes to read after write
* @tparam bytes_to_read - number of bytes to read after write
* @param p_i2c - i2c driver
* @param p_address - target address
* @param p_data_out - buffer of bytes to write to the target device
* @return result<std::array<hal::byte, BytesToRead>>
* @return std::array<hal::byte, bytes_to_read> -
*/
template<size_t BytesToRead>
[[nodiscard]] result<std::array<hal::byte, BytesToRead>> write_then_read(
template<size_t bytes_to_read>
[[nodiscard]] std::array<hal::byte, bytes_to_read> write_then_read(
i2c& p_i2c,
hal::byte p_address,
std::span<const hal::byte> p_data_out)
{
return write_then_read<BytesToRead>(
return write_then_read<bytes_to_read>(
p_i2c, p_address, p_data_out, hal::never_timeout());
}

/**
* @ingroup I2CUtils
* @brief probe the i2c bus to see if a device exists
*
* @param p_i2c - i2c driver
* @param p_address - target address to probe for
* @return hal::result<hal::i2c::transaction_t> - success or failure
* NOTE: that this utilizes the fact that i2c drivers throw
* std::errc::no_such_device_or_address when a transaction is performed and the
* device's address is used on the bus and the device does not respond with an
* acknowledge.
*
* @param p_i2c - i2c driver for the i2c bus that the device may exist on
* @param p_address - device to check
* @return true - if the device appears on the bus
* @return false - if the devices does not appear on the i2c bus
*/
[[nodiscard]] inline hal::result<hal::i2c::transaction_t> probe(
i2c& p_i2c,
hal::byte p_address)
[[nodiscard]] inline bool probe(i2c& p_i2c, hal::byte p_address)
{
// p_data_in: empty placeholder for transcation's data_in
std::array<hal::byte, 1> data_in;

std::array<hal::byte, 1> data_in{};
// p_timeout: no timeout placeholder for transaction's p_timeout
timeout auto timeout = hal::never_timeout();
bool device_acknowledged = true;

return p_i2c.transaction(p_address, std::span<hal::byte>{}, data_in, timeout);
try {
p_i2c.transaction(p_address, std::span<hal::byte>{}, data_in, timeout);
} catch (const hal::no_such_device& p_error) {
device_acknowledged = false;
}

return device_acknowledged;
}

/**
Expand Down Expand Up @@ -316,5 +314,4 @@ enum class i2c_operation
v8bit_address |= hal::value(p_operation);
return v8bit_address;
}

} // namespace hal
Loading

0 comments on commit cf46631

Please sign in to comment.