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;
|
||||
}
|
||||
|
||||
void HardwareAbstract::notifyBatteryChange(HardwareAbstract::batteryStatus aStatus){
|
||||
for (auto handler : mBatteryUpdateHandlers){
|
||||
handler(aStatus);
|
||||
}
|
||||
}
|
||||
|
||||
void HardwareAbstract::onBatteryChange(std::function<void(HardwareAbstract::batteryStatus)> onBatteryStatusChangeHandler){
|
||||
mBatteryUpdateHandlers.push_back(std::move(onBatteryStatusChangeHandler));
|
||||
}
|
||||
void HardwareAbstract::onBatteryChange(std::function<void(HardwareAbstract::batteryStatus)> onBatteryStatusChangeHandler){
|
||||
mBatteryNotification.onNotify(std::move(onBatteryStatusChangeHandler));
|
||||
}
|
|
@ -11,10 +11,15 @@
|
|||
#include "BatteryInterface.h"
|
||||
#include "DisplayInterface.h"
|
||||
#include "wifiHandlerInterface.h"
|
||||
#include "Notification.hpp"
|
||||
|
||||
class HardwareAbstract {
|
||||
public:
|
||||
|
||||
HardwareAbstract(std::shared_ptr<BatteryInterface> aBattery = nullptr,
|
||||
std::shared_ptr<wifiHandlerInterface> aWifiHandler = nullptr,
|
||||
std::shared_ptr<DisplayInterface> aDisplay = nullptr
|
||||
);
|
||||
|
||||
struct batteryStatus {
|
||||
/// @brief Percent of battery remaining (0-100]
|
||||
int percentage;
|
||||
|
@ -24,11 +29,11 @@ public:
|
|||
};
|
||||
virtual std::optional<batteryStatus> getBatteryStatus();
|
||||
|
||||
HardwareAbstract(std::shared_ptr<BatteryInterface> aBattery = nullptr,
|
||||
std::shared_ptr<wifiHandlerInterface> aWifiHandler = nullptr,
|
||||
std::shared_ptr<DisplayInterface> aDisplay = nullptr
|
||||
);
|
||||
|
||||
/// @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);
|
||||
|
||||
/// @brief Override in order to do setup of hardware devices
|
||||
virtual void init() = 0;
|
||||
|
||||
|
@ -36,17 +41,9 @@ public:
|
|||
/// @param message - Debug message
|
||||
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:
|
||||
/// @brief Call in child class implementation to alert users
|
||||
/// the battery status has changed
|
||||
/// @param aStatus - Current Battery Status
|
||||
void notifyBatteryChange(batteryStatus aStatus);
|
||||
|
||||
Notification<batteryStatus> mBatteryNotification;
|
||||
|
||||
private:
|
||||
std::shared_ptr<BatteryInterface> mBattery;
|
||||
std::shared_ptr<wifiHandlerInterface> mWifiHandler;
|
||||
|
|
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