Skip to content

Commit

Permalink
UI refactor (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewColvin authored Oct 22, 2023
1 parent 5989736 commit 7c9a062
Show file tree
Hide file tree
Showing 95 changed files with 4,739 additions and 2,320 deletions.
2 changes: 1 addition & 1 deletion Platformio/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@
"variant": "cpp"
},
"cmake.sourceDirectory": "${workspaceFolder}/.pio/libdeps/esp32/Adafruit BusIO",
"editor.formatOnSave": false,
"editor.formatOnSave": true,
"idf.portWin": "COM8"
}
13 changes: 7 additions & 6 deletions Platformio/HAL/HardwareAbstract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once
#include "BatteryInterface.h"
#include "DisplayAbstract.h"
#include "KeyPressAbstract.hpp"
#include "wifiHandlerInterface.h"

#include "Notification.hpp"
Expand All @@ -16,24 +17,24 @@ class HardwareAbstract {
/// @brief Override in order to do setup of hardware devices post construction
virtual void init() = 0;

/// @brief Override to processing in main thread
virtual void loopHandler() = 0;

/// @brief Override to allow printing of a message for debugging
/// @param message - Debug message
virtual void debugPrint(const char* fmt, ...) = 0;
virtual void debugPrint(const char *fmt, ...) = 0;

virtual std::shared_ptr<BatteryInterface> battery() = 0;
virtual std::shared_ptr<BatteryInterface> battery() = 0;
virtual std::shared_ptr<DisplayAbstract> display() = 0;
virtual std::shared_ptr<wifiHandlerInterface> wifi() = 0;
virtual std::shared_ptr<KeyPressAbstract> keys() = 0;

virtual char getCurrentDevice() = 0;
virtual void setCurrentDevice(char currentDevice) = 0;

virtual bool getWakeupByIMUEnabled() = 0;
virtual void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) = 0;

virtual uint16_t getSleepTimeout() = 0;
virtual void setSleepTimeout(uint16_t sleepTimeout) = 0;

protected:

};
20 changes: 20 additions & 0 deletions Platformio/HAL/HardwareFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "HardwareFactory.hpp"

#if OMOTE_SIM
#include "HardwareSimulator.hpp"
#endif

#if OMOTE_ESP32
#include "HardwareRevX.hpp"
#endif

#if OMOTE_SIM
std::unique_ptr<HardwareAbstract> HardwareFactory::mHardware =
std::make_unique<HardwareSimulator>();
#endif
#if OMOTE_ESP32
std::unique_ptr<HardwareAbstract> HardwareFactory::mHardware =
std::make_unique<HardwareRevX>();
#endif

HardwareAbstract &HardwareFactory::getAbstract() { return *mHardware; }
11 changes: 11 additions & 0 deletions Platformio/HAL/HardwareFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "HardwareAbstract.hpp"
#include <memory>
/**
* @brief The HardwareFactory is responsible for making the
*/
class HardwareFactory {
public:
static HardwareAbstract &getAbstract();

static std::unique_ptr<HardwareAbstract> mHardware;
};
8 changes: 8 additions & 0 deletions Platformio/HAL/HardwareModules/KeyPressAbstract.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "KeyPressAbstract.hpp"

KeyPressAbstract::KeyPressAbstract() {}

void KeyPressAbstract::RegisterKeyPressHandler(
std::function<bool(KeyPressAbstract::KeyEvent)> aKeyEventHandler) {
mKeyEventHandler = std::move(aKeyEventHandler);
}
73 changes: 73 additions & 0 deletions Platformio/HAL/HardwareModules/KeyPressAbstract.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once
#include "Notification.hpp"

#include <memory>
class KeyPressAbstract {
public:
// Keys from Top Down left to right.
enum class KeyId {
Power,
// Top 4 Buttons left to right
Stop,
Rewind,
Play,
FastForward,
// Buttons around D Pad
Menu,
Info,
Back,
Source,
// D Pad
Up,
Down,
Left,
Right,
Center,
// Volume Channel and 2 between
VolUp,
VolDown,
Mute,
Record,
ChannelUp,
ChannelDown,
// Bottom 4 buttons left to right
Aux1,
Aux2,
Aux3,
Aux4,
INVALID
};

class KeyEvent {
public:
enum class Type { Press, Release, INVALID };

KeyEvent() = default;
KeyEvent(const KeyId aId, const Type aType) : mId(aId), mType(aType) {}

KeyId mId = KeyId::INVALID;
Type mType = Type::INVALID;
};

KeyPressAbstract();

/// @brief Register a SINGLE handler to be used for proccessing keys
/// @param aKeyEventHandler - Callable the Handles KeyEvent
void RegisterKeyPressHandler(std::function<bool(KeyEvent)> aKeyEventHandler);

protected:
/// @brief Function ment to be called regularly to allow
/// proccesssing of key presses by calling mKeyEventHandler
/// best case this is done on a seprate thread/task
/// since it could take a while to handle a KeyPress
virtual void HandleKeyPresses() = 0;

/// @brief Function to queue up Key events to be handled later on by
/// HandleKeyPresses() hopefully on a seprate thread or task
/// This function should be implemented in a way that makes it ISR
/// safe
/// @param aJustOccuredKeyEvent - A Key even that just occured
virtual void QueueKeyEvent(KeyEvent aJustOccuredKeyEvent) = 0;

std::function<bool(KeyEvent)> mKeyEventHandler;
};
71 changes: 50 additions & 21 deletions Platformio/HAL/HardwareModules/wifiHandlerInterface.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
#pragma once
#include <string>
#include <memory>
#include <functional>
#include <memory>
#include <string>

class wifiHandlerInterface {
public:
wifiHandlerInterface() = default;
struct WifiInfo {
WifiInfo() = default;
WifiInfo(std::string aSsid, int aRssi) : ssid(aSsid), rssi(aRssi) {}

std::string ssid = "";
int rssi = 0;
};

struct wifiStatus {
wifiStatus() = default;
wifiStatus(bool aConnected, std::string aIp, std::string aSsid)
: isConnected(aConnected), IP(aIp), ssid(aSsid){};

bool isConnected = false;
std::string IP = "";
std::string ssid = "";
};

typedef std::vector<WifiInfo> ScanDoneDataTy;
typedef Notification<ScanDoneDataTy> ScanNotificationTy;

/// @brief Initialize the wifi handler
virtual void begin() = 0;
/// @brief Trigger a scan scan for wifi networks
virtual void scan() = 0;
/// @brief Attempt a connection to the wifi using the provided credentials
virtual void connect(std::string ssid, std::string password) = 0;
/// @brief Get the status of the current wifi connection
virtual wifiStatus GetStatus() = 0;

// Register for Scan Notification to handle when scans are completed
std::shared_ptr<ScanNotificationTy> ScanCompleteNotification() {
return mScanNotification;
};

// Register for Status notifications to handle changes in status
std::shared_ptr<Notification<wifiStatus>> WifiStatusNotification() {
return mStatusUpdate;
};

typedef struct {
std::string ssid;
int rssi;
} WifiInfo;

typedef struct {
bool isConnected;
std::string IP;
std::string ssid;
}wifiStatus;

class wifiHandlerInterface{
public:
virtual bool isAvailable() = 0;
virtual void scan() = 0;
virtual void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password) = 0;
virtual void onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function) = 0;
virtual void onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function) = 0;
virtual void begin() = 0;
protected:
std::shared_ptr<ScanNotificationTy> mScanNotification =
std::make_shared<ScanNotificationTy>();
std::shared_ptr<Notification<wifiStatus>> mStatusUpdate =
std::make_shared<Notification<wifiStatus>>();
};
27 changes: 0 additions & 27 deletions Platformio/HAL/Interface/HardwareInterface.h

This file was deleted.

95 changes: 78 additions & 17 deletions Platformio/HAL/Notification.hpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,90 @@
#pragma once
#include <vector>
#include <functional>
#include <map>
#include <memory>

template <class... notifyData>
class Notification{
public:
typedef std::function<void(notifyData...)> HandlerTy;
template <class... notifyData> class Handler;
template <class... notifyData> class Notification {
friend class Handler<notifyData...>;

Notification() = default;
void onNotify(HandlerTy aHandler);
void notify(notifyData... notifySendData);
public:
typedef std::function<void(notifyData...)> HandlerTy;
typedef int HandlerID;

private:
std::vector<HandlerTy> mFunctionHandlers;
};
Notification() { mIdMaker = 0; };
void notify(notifyData... notifySendData);

protected:
HandlerID onNotify(HandlerTy aHandler);
void unregister(HandlerID aHandler);

private:
std::map<HandlerID, HandlerTy> mFunctionHandlers;
HandlerID mIdMaker;
};

template <class... handlerData>
void Notification<handlerData...>::onNotify(HandlerTy aHandler){
mFunctionHandlers.push_back(std::move(aHandler));
int Notification<handlerData...>::onNotify(HandlerTy aHandler) {
if (aHandler) {
mFunctionHandlers[++mIdMaker] = std::move(aHandler);
return mIdMaker;
} else {
return -1;
}
}

template <class... outboundData>
void Notification<outboundData...>::notify(outboundData... notifySendData){
for (auto handler : mFunctionHandlers){
handler(notifySendData...);
void Notification<outboundData...>::notify(outboundData... notifySendData) {
for (auto handler : mFunctionHandlers) {
handler.second(notifySendData...);
}
}

template <class... handlerData>
void Notification<handlerData...>::unregister(HandlerID aHandlerId) {
auto handlerToUnRegister =
std::find_if(mFunctionHandlers.begin(), mFunctionHandlers.end(),
[aHandlerId](auto registeredHandler) {
return aHandlerId == registeredHandler.first;
});
if (handlerToUnRegister != mFunctionHandlers.end()) {
mFunctionHandlers.erase(handlerToUnRegister);
}
}

template <class... notifyData> class Handler {
public:
typedef std::function<void(notifyData...)> callableTy;
void operator=(Handler &other) = delete;

Handler() = default;
Handler(std::shared_ptr<Notification<notifyData...>> aNotification,
callableTy aCallable = nullptr)
: mNotification(aNotification),
mHandlerId(aNotification->onNotify(aCallable)) {}

virtual ~Handler() {
if (mHandlerId >= 0) {
mNotification->unregister(mHandlerId);
}
}

void operator=(callableTy aHandler) {
if (mHandlerId >= 0) {
mNotification->unregister(mHandlerId);
mHandlerId = -1;
}
}
if (aHandler) {
mHandlerId = mNotification->onNotify(aHandler);
}
}

void
SetNotification(std::shared_ptr<Notification<notifyData...>> aNotification) {
mNotification = aNotification;
}

private:
std::shared_ptr<Notification<notifyData...>> mNotification = nullptr;
int mHandlerId = -1;
};
Loading

0 comments on commit 7c9a062

Please sign in to comment.