Skip to content

Commit

Permalink
✨ (drivers): Add getLevel()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabroc committed Jun 23, 2021
1 parent a939d62 commit 3179404
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
34 changes: 33 additions & 1 deletion drivers/LKCoreBattery/include/LKCoreBattery.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,46 @@

namespace leka {

namespace battery {

namespace voltage_step {
constexpr float medium_low = 10.00;
constexpr float medium_high = 11.00;
constexpr float high = 12.00;
} // namespace voltage_step

namespace resistor {
constexpr float r1 = 169;
constexpr float r2 = 47;
} // namespace resistor

namespace coefficient {
constexpr float can = 3.33;
// constexpr float _voltage_divider = resistor2 / (resistor2 + resistor1);
constexpr float voltage_divider = 0.125;
} // namespace coefficient

} // namespace battery

class LKCoreBattery
{
public:
enum class BatteryLevel
{
high,
medium_high,
medium_low,
low
};

explicit LKCoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin)} {};

float readVoltage(void);
auto readVoltage(void) -> float;
auto getLevel() -> BatteryLevel;

private:
auto averageVoltage(int number_of_points_for_average) -> float;

mbed::AnalogIn _pin;
};

Expand Down
30 changes: 29 additions & 1 deletion drivers/LKCoreBattery/source/LKCoreBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,35 @@

namespace leka {

float LKCoreBattery::readVoltage(void)
LKCoreBattery::BatteryLevel LKCoreBattery ::getLevel()
{
auto batteryVoltage = averageVoltage(20);

if (batteryVoltage >
(battery::voltage_step::high / battery::coefficient::can) * battery::coefficient::voltage_divider) {
return BatteryLevel::high;
}
if (batteryVoltage >
(battery::voltage_step::medium_high / battery::coefficient::can) * battery::coefficient::voltage_divider) {
return BatteryLevel::medium_high;
}
if (batteryVoltage >
(battery::voltage_step::medium_low / battery::coefficient::can) * battery::coefficient::voltage_divider) {
return BatteryLevel::medium_low;
}
return BatteryLevel::low;
}

auto LKCoreBattery::averageVoltage(int number_of_points_for_average) -> float
{
float average = 0;
for (int i = 0; i < number_of_points_for_average; ++i) {
average += readVoltage();
}

return average / number_of_points_for_average;
}
auto LKCoreBattery::readVoltage(void) -> float
{
return _pin.read();
}
Expand Down
31 changes: 21 additions & 10 deletions drivers/LKCoreBattery/tests/LKCoreBattery_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

using namespace leka;

LKCoreBattery battery(PinName::BATTERY_VOLTAGE);
LKCoreBattery corebattery(PinName::BATTERY_VOLTAGE);

float test_set_Voltage(float value)
{
Expand All @@ -19,26 +19,37 @@ float test_set_Voltage(float value)

TEST(LKCoreBatteryTest, initialization)
{
ASSERT_NE(&battery, nullptr);
ASSERT_NE(&corebattery, nullptr);
}

TEST(LKCoreBatteryTest, readMinVoltage)
TEST(LKCoreBatteryTest, readLowVoltage)
{
auto expected = test_set_Voltage(0.0f);
auto expected = test_set_Voltage(0.25f);

ASSERT_EQ(expected, battery.readVoltage());
ASSERT_EQ(expected, corebattery.readVoltage());
ASSERT_EQ(LKCoreBattery::BatteryLevel::low, corebattery.getLevel());
}

TEST(LKCoreBatteryTest, readMiddleVoltage)
TEST(LKCoreBatteryTest, readMiddleLowVoltage)
{
auto expected = test_set_Voltage(0.25f);
auto expected = test_set_Voltage(0.4f);

ASSERT_EQ(expected, corebattery.readVoltage());
ASSERT_EQ(LKCoreBattery::BatteryLevel::medium_low, corebattery.getLevel());
}

TEST(LKCoreBatteryTest, readMiddleHighVoltage)
{
auto expected = test_set_Voltage(0.45f);

ASSERT_EQ(expected, battery.readVoltage());
ASSERT_EQ(expected, corebattery.readVoltage());
ASSERT_EQ(LKCoreBattery::BatteryLevel::medium_high, corebattery.getLevel());
}

TEST(LKCoreBatteryTest, readMaxVoltage)
TEST(LKCoreBatteryTest, readHighVoltage)
{
auto expected = test_set_Voltage(1.0f);

ASSERT_EQ(expected, battery.readVoltage());
ASSERT_EQ(expected, corebattery.readVoltage());
ASSERT_EQ(LKCoreBattery::BatteryLevel::high, corebattery.getLevel());
}

0 comments on commit 3179404

Please sign in to comment.