From 200cc7e714b5c08c0a05368acc5b4c9b5e0a5bd7 Mon Sep 17 00:00:00 2001 From: Benjamin Roussilhe Date: Mon, 28 Jun 2021 10:28:53 +0200 Subject: [PATCH 1/2] :truck: (drivers): Rename LKCoreBattery to CoreBattery - Remove LK prefix from LKCoreBattery --- drivers/CMakeLists.txt | 2 +- .../CMakeLists.txt | 12 ++--- .../include/CoreBattery.h} | 4 +- .../source/CoreBattery.cpp} | 4 +- .../CoreBattery/tests/CoreBattery_test.cpp | 44 +++++++++++++++++++ .../tests/LKCoreBattery_test.cpp | 44 ------------------- spikes/lk_sensors_battery/CMakeLists.txt | 2 +- spikes/lk_sensors_battery/main.cpp | 4 +- tests/unit/CMakeLists.txt | 2 +- 9 files changed, 59 insertions(+), 59 deletions(-) rename drivers/{LKCoreBattery => CoreBattery}/CMakeLists.txt (51%) rename drivers/{LKCoreBattery/include/LKCoreBattery.h => CoreBattery/include/CoreBattery.h} (80%) rename drivers/{LKCoreBattery/source/LKCoreBattery.cpp => CoreBattery/source/CoreBattery.cpp} (70%) create mode 100644 drivers/CoreBattery/tests/CoreBattery_test.cpp delete mode 100644 drivers/LKCoreBattery/tests/LKCoreBattery_test.cpp diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index da3e5cca75..ceb11d8b3b 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -15,7 +15,7 @@ add_subdirectory(${DRIVERS_DIR}/LKCoreVideo) # Sensors drivers add_subdirectory(${DRIVERS_DIR}/CoreHTS) -add_subdirectory(${DRIVERS_DIR}/LKCoreBattery) +add_subdirectory(${DRIVERS_DIR}/CoreBattery) add_subdirectory(${DRIVERS_DIR}/LKCoreMicrophone) add_subdirectory(${DRIVERS_DIR}/LKCoreLightSensor) diff --git a/drivers/LKCoreBattery/CMakeLists.txt b/drivers/CoreBattery/CMakeLists.txt similarity index 51% rename from drivers/LKCoreBattery/CMakeLists.txt rename to drivers/CoreBattery/CMakeLists.txt index b36a33a5e4..f9e8efd512 100644 --- a/drivers/LKCoreBattery/CMakeLists.txt +++ b/drivers/CoreBattery/CMakeLists.txt @@ -2,24 +2,24 @@ # Copyright 2021 APF France handicap # SPDX-License-Identifier: Apache-2.0 -add_library(LKCoreBattery STATIC) +add_library(CoreBattery STATIC) -target_include_directories(LKCoreBattery +target_include_directories(CoreBattery PUBLIC include ) -target_sources(LKCoreBattery +target_sources(CoreBattery PRIVATE - source/LKCoreBattery.cpp + source/CoreBattery.cpp ) -target_link_libraries(LKCoreBattery mbed-os) +target_link_libraries(CoreBattery mbed-os) if (${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests") leka_unit_tests_sources( - tests/LKCoreBattery_test.cpp + tests/CoreBattery_test.cpp ) endif() diff --git a/drivers/LKCoreBattery/include/LKCoreBattery.h b/drivers/CoreBattery/include/CoreBattery.h similarity index 80% rename from drivers/LKCoreBattery/include/LKCoreBattery.h rename to drivers/CoreBattery/include/CoreBattery.h index 05036c586a..eba75bd231 100644 --- a/drivers/LKCoreBattery/include/LKCoreBattery.h +++ b/drivers/CoreBattery/include/CoreBattery.h @@ -9,10 +9,10 @@ namespace leka { -class LKCoreBattery +class CoreBattery { public: - explicit LKCoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin)} {}; + explicit CoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin)} {}; float readVoltage(void); diff --git a/drivers/LKCoreBattery/source/LKCoreBattery.cpp b/drivers/CoreBattery/source/CoreBattery.cpp similarity index 70% rename from drivers/LKCoreBattery/source/LKCoreBattery.cpp rename to drivers/CoreBattery/source/CoreBattery.cpp index 5dd0918c02..7f14ae366f 100644 --- a/drivers/LKCoreBattery/source/LKCoreBattery.cpp +++ b/drivers/CoreBattery/source/CoreBattery.cpp @@ -2,11 +2,11 @@ // Copyright 2021 APF France handicap // SPDX-License-Identifier: Apache-2.0 -#include "LKCoreBattery.h" +#include "CoreBattery.h" namespace leka { -float LKCoreBattery::readVoltage(void) +float CoreBattery::readVoltage(void) { return _pin.read(); } diff --git a/drivers/CoreBattery/tests/CoreBattery_test.cpp b/drivers/CoreBattery/tests/CoreBattery_test.cpp new file mode 100644 index 0000000000..060a414ac4 --- /dev/null +++ b/drivers/CoreBattery/tests/CoreBattery_test.cpp @@ -0,0 +1,44 @@ +// Leka - LekaOS +// Copyright 2021 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "CoreBattery.h" + +#include "gtest/gtest.h" +#include "stubs/mbed/AnalogIn.h" + +using namespace leka; + +CoreBattery corebattery(PinName::BATTERY_VOLTAGE); + +float test_set_Voltage(float value) +{ + spy_AnalogIn_setValue(value); + return value; +} + +TEST(CoreBatteryTest, initialization) +{ + ASSERT_NE(&corebattery, nullptr); +} + +TEST(CoreBatteryTest, readMinVoltage) +{ + auto expected = test_set_Voltage(0.0f); + + ASSERT_EQ(expected, corebattery.readVoltage()); +} + +TEST(CoreBatteryTest, readMiddleVoltage) +{ + auto expected = test_set_Voltage(0.25f); + + ASSERT_EQ(expected, corebattery.readVoltage()); +} + +TEST(CoreBatteryTest, readMaxVoltage) +{ + auto expected = test_set_Voltage(1.0f); + + ASSERT_EQ(expected, corebattery.readVoltage()); +} diff --git a/drivers/LKCoreBattery/tests/LKCoreBattery_test.cpp b/drivers/LKCoreBattery/tests/LKCoreBattery_test.cpp deleted file mode 100644 index aa4ec381d8..0000000000 --- a/drivers/LKCoreBattery/tests/LKCoreBattery_test.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Leka - LekaOS -// Copyright 2021 APF France handicap -// SPDX-License-Identifier: Apache-2.0 - -#include "LKCoreBattery.h" - -#include "gtest/gtest.h" -#include "stubs/mbed/AnalogIn.h" - -using namespace leka; - -LKCoreBattery battery(PinName::BATTERY_VOLTAGE); - -float test_set_Voltage(float value) -{ - spy_AnalogIn_setValue(value); - return value; -} - -TEST(LKCoreBatteryTest, initialization) -{ - ASSERT_NE(&battery, nullptr); -} - -TEST(LKCoreBatteryTest, readMinVoltage) -{ - auto expected = test_set_Voltage(0.0f); - - ASSERT_EQ(expected, battery.readVoltage()); -} - -TEST(LKCoreBatteryTest, readMiddleVoltage) -{ - auto expected = test_set_Voltage(0.25f); - - ASSERT_EQ(expected, battery.readVoltage()); -} - -TEST(LKCoreBatteryTest, readMaxVoltage) -{ - auto expected = test_set_Voltage(1.0f); - - ASSERT_EQ(expected, battery.readVoltage()); -} diff --git a/spikes/lk_sensors_battery/CMakeLists.txt b/spikes/lk_sensors_battery/CMakeLists.txt index 7cd292f98b..68767ea7d5 100644 --- a/spikes/lk_sensors_battery/CMakeLists.txt +++ b/spikes/lk_sensors_battery/CMakeLists.txt @@ -16,7 +16,7 @@ target_sources(spike_lk_sensors_battery target_link_libraries(spike_lk_sensors_battery FileManager - LKCoreBattery + CoreBattery CoreMotor ) diff --git a/spikes/lk_sensors_battery/main.cpp b/spikes/lk_sensors_battery/main.cpp index f813ed222e..22db8771b8 100644 --- a/spikes/lk_sensors_battery/main.cpp +++ b/spikes/lk_sensors_battery/main.cpp @@ -11,10 +11,10 @@ #include "rtos/ThisThread.h" #include "rtos/Thread.h" +#include "CoreBattery.h" #include "CoreMotor.h" #include "FATFileSystem.h" #include "HelloWorld.h" -#include "LKCoreBattery.h" #include "LogKit.h" #include "SDBlockDevice.h" #include "Utils.h" @@ -38,7 +38,7 @@ auto main() -> int HelloWorld hello; hello.start(); - auto battery = LKCoreBattery {PinName::BATTERY_VOLTAGE}; + auto battery = CoreBattery {PinName::BATTERY_VOLTAGE}; auto battery_thread = rtos::Thread {}; auto battery_lambda = [&battery] { diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index c2e1d5b47e..5f0f838f40 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -184,7 +184,7 @@ leka_register_unit_tests_for_driver(LKCoreSTM32Hal) leka_register_unit_tests_for_driver(LKCoreVideo) leka_register_unit_tests_for_driver(LKCoreLightSensor) leka_register_unit_tests_for_driver(LKCoreMicrophone) -leka_register_unit_tests_for_driver(LKCoreBattery) +leka_register_unit_tests_for_driver(CoreBattery) leka_register_unit_tests_for_driver(CoreHTS) leka_register_unit_tests_for_driver(CoreI2C) From 62fd075e60acb6bd6599a53d3bf5ae4f9868ffb9 Mon Sep 17 00:00:00 2001 From: Benjamin Roussilhe Date: Tue, 6 Jul 2021 16:54:09 +0200 Subject: [PATCH 2/2] :sparkles: (drivers): Refactor getVoltage method - move namespance constexpr to class - Add voltage reference - Add max/min capacity values --- drivers/CoreBattery/include/CoreBattery.h | 23 ++++++++++-- drivers/CoreBattery/source/CoreBattery.cpp | 11 ++++-- .../CoreBattery/tests/CoreBattery_test.cpp | 35 ++++++++++--------- spikes/lk_sensors_battery/main.cpp | 2 +- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/drivers/CoreBattery/include/CoreBattery.h b/drivers/CoreBattery/include/CoreBattery.h index eba75bd231..3e062d3ac8 100644 --- a/drivers/CoreBattery/include/CoreBattery.h +++ b/drivers/CoreBattery/include/CoreBattery.h @@ -12,11 +12,30 @@ namespace leka { class CoreBattery { public: - explicit CoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin)} {}; + explicit CoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin, voltage::reference)} {}; - float readVoltage(void); + auto getVoltage() -> float; + + struct capacity { + static constexpr auto max = float {12.60}; + static constexpr auto min = float {7.50}; + }; + + struct resistor { + // TODO (@Benjamin) - find the resistor values, call Mikael + static constexpr auto r1 = float {47}; + static constexpr auto r2 = float {169}; + }; + + struct voltage { + // TODO (@Benjamin) - should be float {resistor::r1 / (resistor::r1 + resistor::r2)}; + static constexpr auto divider = float {0.129}; + static constexpr auto reference = float {3.33}; + }; private: + auto readRawVoltage() -> float; + mbed::AnalogIn _pin; }; diff --git a/drivers/CoreBattery/source/CoreBattery.cpp b/drivers/CoreBattery/source/CoreBattery.cpp index 7f14ae366f..2a43ca46b8 100644 --- a/drivers/CoreBattery/source/CoreBattery.cpp +++ b/drivers/CoreBattery/source/CoreBattery.cpp @@ -6,9 +6,16 @@ namespace leka { -float CoreBattery::readVoltage(void) +auto CoreBattery::getVoltage() -> float { - return _pin.read(); + auto raw = readRawVoltage(); + auto voltage = raw / voltage::divider; + return voltage; +} + +auto CoreBattery::readRawVoltage() -> float +{ + return _pin.read_voltage(); } } // namespace leka diff --git a/drivers/CoreBattery/tests/CoreBattery_test.cpp b/drivers/CoreBattery/tests/CoreBattery_test.cpp index 060a414ac4..bc06f0d979 100644 --- a/drivers/CoreBattery/tests/CoreBattery_test.cpp +++ b/drivers/CoreBattery/tests/CoreBattery_test.cpp @@ -1,5 +1,5 @@ // Leka - LekaOS -// Copyright 2021 APF France handicap +// Copyright 2021 APF rance handicap // SPDX-License-Identifier: Apache-2.0 #include "CoreBattery.h" @@ -11,34 +11,37 @@ using namespace leka; CoreBattery corebattery(PinName::BATTERY_VOLTAGE); -float test_set_Voltage(float value) -{ - spy_AnalogIn_setValue(value); - return value; -} - TEST(CoreBatteryTest, initialization) { ASSERT_NE(&corebattery, nullptr); } -TEST(CoreBatteryTest, readMinVoltage) +TEST(CoreBatteryTest, getVoltage) { - auto expected = test_set_Voltage(0.0f); + auto AnalogIn_read_voltage_value = float {1.2}; + spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value); - ASSERT_EQ(expected, corebattery.readVoltage()); + auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider; + + ASSERT_FLOAT_EQ(expected, corebattery.getVoltage()); } -TEST(CoreBatteryTest, readMiddleVoltage) +TEST(CoreBatteryTest, getVoltageLowest) { - auto expected = test_set_Voltage(0.25f); + auto AnalogIn_read_voltage_value = float {0}; + spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value); + + auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider; - ASSERT_EQ(expected, corebattery.readVoltage()); + ASSERT_FLOAT_EQ(expected, corebattery.getVoltage()); } -TEST(CoreBatteryTest, readMaxVoltage) +TEST(CoreBatteryTest, getVoltageHighest) { - auto expected = test_set_Voltage(1.0f); + auto AnalogIn_read_voltage_value = float {1.65}; + spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value); + + auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider; - ASSERT_EQ(expected, corebattery.readVoltage()); + ASSERT_FLOAT_EQ(expected, corebattery.getVoltage()); } diff --git a/spikes/lk_sensors_battery/main.cpp b/spikes/lk_sensors_battery/main.cpp index 22db8771b8..130e5599e0 100644 --- a/spikes/lk_sensors_battery/main.cpp +++ b/spikes/lk_sensors_battery/main.cpp @@ -43,7 +43,7 @@ auto main() -> int auto battery_thread = rtos::Thread {}; auto battery_lambda = [&battery] { auto now = [] { return static_cast(rtos::Kernel::Clock::now().time_since_epoch().count()); }; - auto voltage = [&] { return battery.readVoltage(); }; + auto voltage = [&] { return battery.getVoltage(); }; auto buffer = std::array {};