Skip to content

Commit

Permalink
⚡ (battery): Remove Ticker and use only EventQueue call_every
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Mar 3, 2022
1 parent 724eb85 commit 9cfef5d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
5 changes: 2 additions & 3 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
using namespace leka;
using namespace std::chrono;

auto sleep_timeout = CoreTimeout {};
auto battery_ticker = CoreTicker {};
auto sleep_timeout = CoreTimeout {};

auto charge_input = mbed::InterruptIn {PinName::BATTERY_CHARGE_STATUS};
auto corebattery = leka::CoreBattery {PinName::BATTERY_VOLTAGE, charge_input};
auto batterykit = leka::BatteryKit {corebattery, battery_ticker};
auto batterykit = leka::BatteryKit {corebattery};

auto service_battery = BLEServiceBattery {};
auto services = std::to_array<interface::BLEService *>({&service_battery});
Expand Down
5 changes: 5 additions & 0 deletions drivers/CoreEventQueue/include/CoreEventQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class CoreEventQueue : public interface::EventQueue

void call(auto f, auto... params) { _event_queue.call(f, params...); }

void call_every(std::chrono::duration<int, std::milli> duration, auto f, auto... params)
{
_event_queue.call_every(duration, f, params...);
}

// ? Overload needed for mbed::BLE compatibility
void callMbedCallback(mbed::Callback<void()> const &f);

Expand Down
6 changes: 1 addition & 5 deletions libs/BatteryKit/include/BatteryKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@

#include "CoreEventQueue.h"
#include "interface/drivers/Battery.h"
#include "interface/drivers/Ticker.h"

namespace leka {

class BatteryKit
{
public:
explicit BatteryKit(interface::Battery &battery, interface::Ticker &ticker) : _battery(battery), _ticker(ticker) {};
explicit BatteryKit(interface::Battery &battery) : _battery(battery) {};

void startRoutine();
void stopRoutine();

auto level() -> uint8_t;
auto isCharging() -> bool;
Expand All @@ -29,8 +27,6 @@ class BatteryKit
private:
interface::Battery &_battery;

interface::Ticker &_ticker;

CoreEventQueue _event_queue {};

std::function<void(uint8_t)> on_data_updated {};
Expand Down
19 changes: 5 additions & 14 deletions libs/BatteryKit/source/BatteryKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,17 @@ using namespace std::chrono_literals;
void BatteryKit::startRoutine()
{
auto on_tick = [this] {
_event_queue.call([this] {
if (level() == 0) {
on_low_battery();
}
on_data_updated(level());
});
if (level() == 0) {
on_low_battery();
}
on_data_updated(level());
};

_ticker.onTick(on_tick);

_ticker.start(1s);
_event_queue.call_every(1s, on_tick);

_event_queue.dispatch_forever();
}

void BatteryKit::stopRoutine()
{
_ticker.stop();
}

auto BatteryKit::level() -> uint8_t
{
return _battery.level();
Expand Down
4 changes: 1 addition & 3 deletions spikes/lk_sensors_battery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
using namespace leka;
using namespace std::chrono;

auto battery_ticker = CoreTicker {};

auto charge_input = mbed::InterruptIn {PinName::BATTERY_CHARGE_STATUS};
auto corebattery = CoreBattery {PinName::BATTERY_VOLTAGE, charge_input};
auto batterykit = BatteryKit {corebattery, battery_ticker};
auto batterykit = BatteryKit {corebattery};
auto mainboard_led = mbed::DigitalOut {LED1};

auto main() -> int
Expand Down

0 comments on commit 9cfef5d

Please sign in to comment.