Add Template Class for Notification to allow for easier creation of notifications
This commit is contained in:
parent
d73b816578
commit
7967300508
3 changed files with 42 additions and 25 deletions
|
@ -20,12 +20,6 @@ std::optional<HardwareAbstract::batteryStatus> HardwareAbstract::getBatteryStatu
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HardwareAbstract::notifyBatteryChange(HardwareAbstract::batteryStatus aStatus){
|
void HardwareAbstract::onBatteryChange(std::function<void(HardwareAbstract::batteryStatus)> onBatteryStatusChangeHandler){
|
||||||
for (auto handler : mBatteryUpdateHandlers){
|
mBatteryNotification.onNotify(std::move(onBatteryStatusChangeHandler));
|
||||||
handler(aStatus);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void HardwareAbstract::onBatteryChange(std::function<void(HardwareAbstract::batteryStatus)> onBatteryStatusChangeHandler){
|
|
||||||
mBatteryUpdateHandlers.push_back(std::move(onBatteryStatusChangeHandler));
|
|
||||||
}
|
|
|
@ -11,9 +11,14 @@
|
||||||
#include "BatteryInterface.h"
|
#include "BatteryInterface.h"
|
||||||
#include "DisplayInterface.h"
|
#include "DisplayInterface.h"
|
||||||
#include "wifiHandlerInterface.h"
|
#include "wifiHandlerInterface.h"
|
||||||
|
#include "Notification.hpp"
|
||||||
|
|
||||||
class HardwareAbstract {
|
class HardwareAbstract {
|
||||||
public:
|
public:
|
||||||
|
HardwareAbstract(std::shared_ptr<BatteryInterface> aBattery = nullptr,
|
||||||
|
std::shared_ptr<wifiHandlerInterface> aWifiHandler = nullptr,
|
||||||
|
std::shared_ptr<DisplayInterface> aDisplay = nullptr
|
||||||
|
);
|
||||||
|
|
||||||
struct batteryStatus {
|
struct batteryStatus {
|
||||||
/// @brief Percent of battery remaining (0-100]
|
/// @brief Percent of battery remaining (0-100]
|
||||||
|
@ -24,10 +29,10 @@ public:
|
||||||
};
|
};
|
||||||
virtual std::optional<batteryStatus> getBatteryStatus();
|
virtual std::optional<batteryStatus> getBatteryStatus();
|
||||||
|
|
||||||
HardwareAbstract(std::shared_ptr<BatteryInterface> aBattery = nullptr,
|
/// @brief Register function to be ran when hardware notifies battery
|
||||||
std::shared_ptr<wifiHandlerInterface> aWifiHandler = nullptr,
|
/// status has changed.
|
||||||
std::shared_ptr<DisplayInterface> aDisplay = nullptr
|
/// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes
|
||||||
);
|
void onBatteryChange(std::function<void(batteryStatus)> onBatteryStatusChangeHandler);
|
||||||
|
|
||||||
/// @brief Override in order to do setup of hardware devices
|
/// @brief Override in order to do setup of hardware devices
|
||||||
virtual void init() = 0;
|
virtual void init() = 0;
|
||||||
|
@ -36,16 +41,8 @@ public:
|
||||||
/// @param message - Debug message
|
/// @param message - Debug message
|
||||||
virtual void debugPrint(std::string message) = 0;
|
virtual void debugPrint(std::string message) = 0;
|
||||||
|
|
||||||
/// @brief Register function to be ran when hardware notifies battery
|
|
||||||
/// status has changed.
|
|
||||||
/// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes
|
|
||||||
void onBatteryChange(std::function<void(batteryStatus)> onBatteryStatusChangeHandler);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// @brief Call in child class implementation to alert users
|
Notification<batteryStatus> mBatteryNotification;
|
||||||
/// the battery status has changed
|
|
||||||
/// @param aStatus - Current Battery Status
|
|
||||||
void notifyBatteryChange(batteryStatus aStatus);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<BatteryInterface> mBattery;
|
std::shared_ptr<BatteryInterface> mBattery;
|
||||||
|
|
26
Platformio/HAL/Notification.hpp
Normal file
26
Platformio/HAL/Notification.hpp
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
template <class... notifyData>
|
||||||
|
class Notification{
|
||||||
|
public:
|
||||||
|
Notification() = default;
|
||||||
|
void onNotify(std::function<void(notifyData...)> aHandler);
|
||||||
|
void notify(notifyData... notifySendData);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::function<void(notifyData...)>> mFunctionHandlers;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class... handlerData>
|
||||||
|
void Notification<handlerData...>::onNotify(std::function<void(handlerData...)> aHandler){
|
||||||
|
mFunctionHandlers.push_back(std::move(aHandler));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... outboundData>
|
||||||
|
void Notification<outboundData...>::notify(outboundData... notifySendData){
|
||||||
|
for (auto handler : mFunctionHandlers){
|
||||||
|
handler(notifySendData...);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue