Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #717 from Coreyboy1820/current_sensor
Browse files Browse the repository at this point in the history
✨ added current sensor + tests
  • Loading branch information
Khalil Estell authored Jan 8, 2024
2 parents 2d9b473 + 96fc842 commit 9c017a0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
51 changes: 51 additions & 0 deletions include/libhal/current_sensor.hpp
Original file line number Diff line number Diff line change
@@ -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<read_t> - current data
*/
[[nodiscard]] result<read_t> read()
{
return driver_read();
}

virtual ~current_sensor() = default;

private:
virtual hal::result<read_t> driver_read() = 0;
};
} // namespace hal
58 changes: 58 additions & 0 deletions tests/current_sensor.test.cpp
Original file line number Diff line number Diff line change
@@ -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 <libhal/current_sensor.hpp>

#include <boost/ut.hpp>

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<read_t> 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
4 changes: 4 additions & 0 deletions tests/main.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}

0 comments on commit 9c017a0

Please sign in to comment.