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 21, 2021
1 parent 8d12317 commit f8de43f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
20 changes: 19 additions & 1 deletion drivers/LKCoreBattery/include/LKCoreBattery.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,30 @@ namespace leka {
class LKCoreBattery
{
public:
enum class BatteryLevel
{
high,
medium,
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;

float high_battery_level_step = 12.00;
float low_battery_level_step = 10.00;

float battery_level_sensor_coefficient = 3.33;
float resistor1 = 169;
float resistor2 = 47;
float _voltage_divider_coefficient = resistor2 / (resistor2 + resistor1);
};

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

namespace leka {

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

if (batteryVoltage < (low_battery_level_step / battery_level_sensor_coefficient) * _voltage_divider_coefficient) {
return BatteryLevel::low;
}
if (batteryVoltage > (high_battery_level_step / battery_level_sensor_coefficient) * _voltage_divider_coefficient) {
return BatteryLevel::high;
}
return BatteryLevel::medium;
}

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 / points_number_for_average;
}
auto LKCoreBattery::readVoltage(void) -> float
{
return _pin.read();
}
Expand Down
9 changes: 6 additions & 3 deletions drivers/LKCoreBattery/tests/LKCoreBattery_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ TEST(LKCoreBatteryTest, initialization)
ASSERT_NE(&battery, nullptr);
}

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

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

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

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

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

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

0 comments on commit f8de43f

Please sign in to comment.