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

Commit 9c017a0

Browse files
author
Khalil Estell
authored
Merge pull request #717 from Coreyboy1820/current_sensor
✨ added current sensor + tests
2 parents 2d9b473 + 96fc842 commit 9c017a0

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ libhal_unit_test(SOURCES
4545
tests/g_force.test.cpp
4646
tests/lengths.test.cpp
4747
tests/angular_velocity_sensor.test.cpp
48+
tests/current_sensor.test.cpp
4849
tests/main.test.cpp
4950

5051
PACKAGES

conanfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
class libhal_conan(ConanFile):
2828
name = "libhal"
29-
version = "2.1.0"
29+
version = "2.2.0"
3030
license = "Apache-2.0"
3131
url = "https://github.com/conan-io/conan-center-index"
3232
homepage = "https://libhal.github.io/libhal"

include/libhal/current_sensor.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "units.hpp"
18+
19+
namespace hal {
20+
/**
21+
* @brief current sensor hardware abstraction interface
22+
*
23+
*/
24+
class current_sensor
25+
{
26+
public:
27+
/**
28+
* @brief current reading from the sensor
29+
*
30+
*
31+
*/
32+
struct read_t
33+
{
34+
hal::ampere current = 0.0f;
35+
};
36+
/**
37+
* @brief Reads the most up to date current from the sensor
38+
*
39+
* @return result<read_t> - current data
40+
*/
41+
[[nodiscard]] result<read_t> read()
42+
{
43+
return driver_read();
44+
}
45+
46+
virtual ~current_sensor() = default;
47+
48+
private:
49+
virtual hal::result<read_t> driver_read() = 0;
50+
};
51+
} // namespace hal

tests/current_sensor.test.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <libhal/current_sensor.hpp>
16+
17+
#include <boost/ut.hpp>
18+
19+
namespace hal {
20+
namespace {
21+
class test_current_sensor : public hal::current_sensor
22+
{
23+
public:
24+
bool m_return_error_status{ false };
25+
~test_current_sensor() override = default;
26+
27+
private:
28+
result<read_t> driver_read() override
29+
{
30+
if (m_return_error_status) {
31+
return hal::new_error();
32+
}
33+
return read_t{};
34+
}
35+
};
36+
} // namespace
37+
38+
void current_sensor_test()
39+
{
40+
using namespace boost::ut;
41+
"current sensor interface test"_test = []() {
42+
test_current_sensor test;
43+
44+
auto result = test.read();
45+
46+
expect(bool{ result });
47+
};
48+
"current sensor errors test"_test = []() {
49+
test_current_sensor test;
50+
test.m_return_error_status = true;
51+
52+
auto result = test.read();
53+
54+
expect(!bool{ result });
55+
};
56+
}
57+
58+
} // namespace hal

tests/main.test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ extern void timer_test();
3131
extern void servo_test();
3232
extern void g_force_test();
3333
extern void lengths_test();
34+
extern void angular_velocity_sensor_test();
35+
extern void current_sensor_test();
3436
} // namespace hal
3537

3638
int main()
@@ -53,4 +55,6 @@ int main()
5355
hal::timer_test();
5456
hal::g_force_test();
5557
hal::lengths_test();
58+
hal::angular_velocity_sensor_test();
59+
hal::current_sensor_test();
5660
}

0 commit comments

Comments
 (0)