This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #717 from Coreyboy1820/current_sensor
✨ added current sensor + tests
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters