summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/drivers/include/drivers/adc.hpp4
-rw-r--r--src/drivers/test/CMakeLists.txt2
-rw-r--r--src/drivers/test/test_adc.cpp2
-rw-r--r--src/drivers/test/test_samd.cpp32
-rw-r--r--src/tangara/test/CMakeLists.txt7
-rw-r--r--src/tangara/test/battery/test_battery.cpp67
6 files changed, 110 insertions, 4 deletions
diff --git a/src/drivers/include/drivers/adc.hpp b/src/drivers/include/drivers/adc.hpp
index 3e94a9ee..2fd8a626 100644
--- a/src/drivers/include/drivers/adc.hpp
+++ b/src/drivers/include/drivers/adc.hpp
@@ -22,12 +22,12 @@ class AdcBattery {
public:
static auto Create() -> AdcBattery* { return new AdcBattery(); }
AdcBattery();
- ~AdcBattery();
+ virtual ~AdcBattery();
/**
* Returns the current battery level in millivolts.
*/
- auto Millivolts() -> uint32_t;
+ virtual auto Millivolts() -> uint32_t;
private:
adc_oneshot_unit_handle_t adc_handle_;
diff --git a/src/drivers/test/CMakeLists.txt b/src/drivers/test/CMakeLists.txt
index ff1dab0b..f18dc357 100644
--- a/src/drivers/test/CMakeLists.txt
+++ b/src/drivers/test/CMakeLists.txt
@@ -3,5 +3,5 @@
# SPDX-License-Identifier: GPL-3.0-only
idf_component_register(
- SRCS "test_adc.cpp" "test_storage.cpp" "test_dac.cpp"
+ SRCS "test_adc.cpp" "test_storage.cpp" "test_dac.cpp" "test_samd.cpp"
INCLUDE_DIRS "." REQUIRES catch2 cmock drivers fixtures)
diff --git a/src/drivers/test/test_adc.cpp b/src/drivers/test/test_adc.cpp
index df103af3..a8dad4bd 100644
--- a/src/drivers/test/test_adc.cpp
+++ b/src/drivers/test/test_adc.cpp
@@ -12,7 +12,7 @@
namespace drivers {
-TEST_CASE("battery measurement", "[integration]") {
+TEST_CASE("battery adc", "[integration]") {
AdcBattery battery;
SECTION("voltage is within range") {
diff --git a/src/drivers/test/test_samd.cpp b/src/drivers/test/test_samd.cpp
new file mode 100644
index 00000000..c466d88e
--- /dev/null
+++ b/src/drivers/test/test_samd.cpp
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "drivers/samd.hpp"
+
+#include <cstdint>
+
+#include "catch2/catch.hpp"
+
+#include "i2c_fixture.hpp"
+
+namespace drivers {
+
+TEST_CASE("samd21 interface", "[integration]") {
+ I2CFixture i2c;
+ auto samd = std::make_unique<Samd>();
+
+ REQUIRE(samd);
+
+ SECTION("usb reports connection") {
+ samd->UpdateUsbStatus();
+
+ auto status = samd->GetUsbStatus();
+
+ REQUIRE(status == Samd::UsbStatus::kAttachedIdle);
+ }
+}
+
+} // namespace drivers
diff --git a/src/tangara/test/CMakeLists.txt b/src/tangara/test/CMakeLists.txt
new file mode 100644
index 00000000..728c06b0
--- /dev/null
+++ b/src/tangara/test/CMakeLists.txt
@@ -0,0 +1,7 @@
+# Copyright 2023 jacqueline <me@jacqueline.id.au>
+#
+# SPDX-License-Identifier: GPL-3.0-only
+
+idf_component_register(
+ SRC_DIRS "battery"
+ INCLUDE_DIRS "." REQUIRES catch2 cmock tangara fixtures)
diff --git a/src/tangara/test/battery/test_battery.cpp b/src/tangara/test/battery/test_battery.cpp
new file mode 100644
index 00000000..7b55bd59
--- /dev/null
+++ b/src/tangara/test/battery/test_battery.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2024 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "battery/battery.hpp"
+
+#include <cstdint>
+#include <memory>
+
+#include "catch2/catch.hpp"
+#include "drivers/adc.hpp"
+#include "i2c_fixture.hpp"
+
+namespace battery {
+
+class FakeAdc : public drivers::AdcBattery {
+ private:
+ uint32_t mv_;
+
+ public:
+ virtual auto Millivolts() -> uint32_t override { return mv_; }
+ auto Millivolts(uint32_t mv) -> void { mv_ = mv; }
+};
+
+TEST_CASE("battery charge state", "[unit]") {
+ I2CFixture i2c;
+
+ // FIXME: mock the SAMD21 as well.
+ std::unique_ptr<drivers::Samd> samd{drivers::Samd::Create()};
+ FakeAdc* adc = new FakeAdc{}; // Freed by Battery.
+ Battery battery{*samd, std::unique_ptr<drivers::AdcBattery>{adc}};
+
+ SECTION("full charge is 100%") {
+ // NOTE: in practice, our curve-fitting slightly undershoots
+ adc->Millivolts(4210);
+
+ battery.Update();
+
+ auto state = battery.State();
+ REQUIRE(state.has_value());
+ REQUIRE(state->percent == 100);
+ }
+
+ SECTION("empty charge is 0%") {
+ adc->Millivolts(3000);
+
+ battery.Update();
+
+ auto state = battery.State();
+ REQUIRE(state.has_value());
+ REQUIRE(state->percent == 0);
+ }
+
+ SECTION("overcharge is clamped to 100%") {
+ adc->Millivolts(5000);
+
+ battery.Update();
+
+ auto state = battery.State();
+ REQUIRE(state.has_value());
+ REQUIRE(state->percent == 100);
+ }
+}
+
+} // namespace battery