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/CoreBattery/include/CoreBattery.h b/drivers/CoreBattery/include/CoreBattery.h new file mode 100644 index 0000000000..3e062d3ac8 --- /dev/null +++ b/drivers/CoreBattery/include/CoreBattery.h @@ -0,0 +1,44 @@ +// Leka - LekaOS +// Copyright 2021 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#ifndef _LEKA_OS_DRIVER_LK_CORE_BATTERY_H_ +#define _LEKA_OS_DRIVER_LK_CORE_BATTERY_H_ + +#include "drivers/AnalogIn.h" + +namespace leka { + +class CoreBattery +{ + public: + explicit CoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin, voltage::reference)} {}; + + 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; +}; + +} // namespace leka + +#endif //_LEKA_OS_DRIVER_LK_CORE_BATTERY_H_ diff --git a/drivers/CoreBattery/source/CoreBattery.cpp b/drivers/CoreBattery/source/CoreBattery.cpp new file mode 100644 index 0000000000..2a43ca46b8 --- /dev/null +++ b/drivers/CoreBattery/source/CoreBattery.cpp @@ -0,0 +1,21 @@ +// Leka - LekaOS +// Copyright 2021 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "CoreBattery.h" + +namespace leka { + +auto CoreBattery::getVoltage() -> float +{ + 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 new file mode 100644 index 0000000000..bc06f0d979 --- /dev/null +++ b/drivers/CoreBattery/tests/CoreBattery_test.cpp @@ -0,0 +1,47 @@ +// Leka - LekaOS +// Copyright 2021 APF rance 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); + +TEST(CoreBatteryTest, initialization) +{ + ASSERT_NE(&corebattery, nullptr); +} + +TEST(CoreBatteryTest, getVoltage) +{ + auto AnalogIn_read_voltage_value = float {1.2}; + spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value); + + auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider; + + ASSERT_FLOAT_EQ(expected, corebattery.getVoltage()); +} + +TEST(CoreBatteryTest, getVoltageLowest) +{ + auto AnalogIn_read_voltage_value = float {0}; + spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value); + + auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider; + + ASSERT_FLOAT_EQ(expected, corebattery.getVoltage()); +} + +TEST(CoreBatteryTest, getVoltageHighest) +{ + 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_FLOAT_EQ(expected, corebattery.getVoltage()); +} diff --git a/drivers/LKCoreBattery/include/LKCoreBattery.h b/drivers/LKCoreBattery/include/LKCoreBattery.h deleted file mode 100644 index 05036c586a..0000000000 --- a/drivers/LKCoreBattery/include/LKCoreBattery.h +++ /dev/null @@ -1,25 +0,0 @@ -// Leka - LekaOS -// Copyright 2021 APF France handicap -// SPDX-License-Identifier: Apache-2.0 - -#ifndef _LEKA_OS_DRIVER_LK_CORE_BATTERY_H_ -#define _LEKA_OS_DRIVER_LK_CORE_BATTERY_H_ - -#include "drivers/AnalogIn.h" - -namespace leka { - -class LKCoreBattery -{ - public: - explicit LKCoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin)} {}; - - float readVoltage(void); - - private: - mbed::AnalogIn _pin; -}; - -} // namespace leka - -#endif //_LEKA_OS_DRIVER_LK_CORE_BATTERY_H_ diff --git a/drivers/LKCoreBattery/source/LKCoreBattery.cpp b/drivers/LKCoreBattery/source/LKCoreBattery.cpp deleted file mode 100644 index 5dd0918c02..0000000000 --- a/drivers/LKCoreBattery/source/LKCoreBattery.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// Leka - LekaOS -// Copyright 2021 APF France handicap -// SPDX-License-Identifier: Apache-2.0 - -#include "LKCoreBattery.h" - -namespace leka { - -float LKCoreBattery::readVoltage(void) -{ - return _pin.read(); -} - -} // namespace leka 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..130e5599e0 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,12 +38,12 @@ 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] { 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 {}; 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)