Skip to content

Commit

Permalink
⚡ (rc): Use EventQueue to update Battery in BLE
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Mar 1, 2022
1 parent d6e50ba commit d4b8347
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions drivers/CoreEventQueue/include/CoreEventQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class CoreEventQueue : public interface::EventQueue
void dispatch_forever() final;

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
2 changes: 2 additions & 0 deletions include/interface/drivers/EventQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <chrono>
#include <functional>

namespace leka::interface {
Expand All @@ -16,6 +17,7 @@ class EventQueue
virtual void dispatch_forever() = 0;

void call(auto f, auto... params);
void call_every(std::chrono::microseconds duration, auto f, auto... params);
};

} // namespace leka::interface
11 changes: 11 additions & 0 deletions libs/RobotKit/include/RobotController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "BLEKit.h"
#include "BLEServiceBattery.h"

#include "CoreEventQueue.h"
#include "StateMachine.h"
#include "interface/RobotController.h"
#include "interface/drivers/Battery.h"
Expand Down Expand Up @@ -39,6 +40,7 @@ class RobotController : public interface::RobotController
void registerEvents()
{
using namespace system::robot::sm;
using namespace std::chrono_literals;

// Setup callbacks for each events

Expand All @@ -51,13 +53,22 @@ class RobotController : public interface::RobotController
auto on_charge_did_stop = [this]() { raise(event::charge_did_stop {}); };
_battery.onChargeDidStop(on_charge_did_stop);

// Setup recurrent checking

auto update_battery_level = [this]() { _service_battery.setBatteryLevel(_battery.level()); };
_recurrent_checking.call_every(1s, update_battery_level);

_recurrent_checking.dispatch_forever();

raise(event::setup_complete {});
};

private:
std::chrono::seconds _sleep_timeout_duration {10};
interface::Timeout &_sleep_timeout;

CoreEventQueue _recurrent_checking {};

interface::Battery &_battery;

BLEServiceBattery _service_battery {};
Expand Down

0 comments on commit d4b8347

Please sign in to comment.