diff --git a/CMakeLists.txt b/CMakeLists.txt index e67028bdf..ba2dfcdc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ libhal_unit_test(SOURCES tests/g_force.test.cpp tests/lengths.test.cpp tests/angular_velocity_sensor.test.cpp + tests/current_sensor.test.cpp tests/main.test.cpp PACKAGES diff --git a/conanfile.py b/conanfile.py index 05f8734ec..fa7f0e511 100644 --- a/conanfile.py +++ b/conanfile.py @@ -26,7 +26,7 @@ class libhal_conan(ConanFile): name = "libhal" - version = "2.1.0" + version = "2.2.0" license = "Apache-2.0" url = "https://github.com/conan-io/conan-center-index" homepage = "https://libhal.github.io/libhal" diff --git a/include/libhal/current_sensor.hpp b/include/libhal/current_sensor.hpp new file mode 100644 index 000000000..e494fd60d --- /dev/null +++ b/include/libhal/current_sensor.hpp @@ -0,0 +1,51 @@ +// Copyright 2023 Google LLC +// +// 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 "units.hpp" + +namespace hal { +/** + * @brief current sensor hardware abstraction interface + * + */ +class current_sensor +{ +public: + /** + * @brief current reading from the sensor + * + * + */ + struct read_t + { + hal::ampere current = 0.0f; + }; + /** + * @brief Reads the most up to date current from the sensor + * + * @return result - current data + */ + [[nodiscard]] result read() + { + return driver_read(); + } + + virtual ~current_sensor() = default; + +private: + virtual hal::result driver_read() = 0; +}; +} // namespace hal diff --git a/tests/current_sensor.test.cpp b/tests/current_sensor.test.cpp new file mode 100644 index 000000000..f5531197c --- /dev/null +++ b/tests/current_sensor.test.cpp @@ -0,0 +1,58 @@ +// Copyright 2023 Google LLC +// +// 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. + +#include + +#include + +namespace hal { +namespace { +class test_current_sensor : public hal::current_sensor +{ +public: + bool m_return_error_status{ false }; + ~test_current_sensor() override = default; + +private: + result driver_read() override + { + if (m_return_error_status) { + return hal::new_error(); + } + return read_t{}; + } +}; +} // namespace + +void current_sensor_test() +{ + using namespace boost::ut; + "current sensor interface test"_test = []() { + test_current_sensor test; + + auto result = test.read(); + + expect(bool{ result }); + }; + "current sensor errors test"_test = []() { + test_current_sensor test; + test.m_return_error_status = true; + + auto result = test.read(); + + expect(!bool{ result }); + }; +} + +} // namespace hal \ No newline at end of file diff --git a/tests/main.test.cpp b/tests/main.test.cpp index a3062b9cc..0ba54bef4 100644 --- a/tests/main.test.cpp +++ b/tests/main.test.cpp @@ -31,6 +31,8 @@ extern void timer_test(); extern void servo_test(); extern void g_force_test(); extern void lengths_test(); +extern void angular_velocity_sensor_test(); +extern void current_sensor_test(); } // namespace hal int main() @@ -53,4 +55,6 @@ int main() hal::timer_test(); hal::g_force_test(); hal::lengths_test(); + hal::angular_velocity_sensor_test(); + hal::current_sensor_test(); }