🎨 Moving project to use event bus
This commit is contained in:
@@ -14,21 +14,8 @@ class BluetoothService : public CommBase<> {
|
||||
BLECharacteristic* rxCharacteristic {nullptr};
|
||||
bool connected {false};
|
||||
|
||||
protected:
|
||||
template <typename Msg>
|
||||
using EventBusHandle = typename EventBus<Msg>::Handle;
|
||||
|
||||
template <typename Msg>
|
||||
EventBusHandle<Msg>& getHandle(Topic topic) {
|
||||
return *static_cast<EventBusHandle<Msg>*>(subscriptionHandle[static_cast<size_t>(topic)]);
|
||||
}
|
||||
|
||||
template <typename Msg>
|
||||
void setHandle(Topic topic, EventBusHandle<Msg>&& handle) {
|
||||
subscriptionHandle[static_cast<size_t>(topic)] = new EventBusHandle<Msg>(std::move(handle));
|
||||
}
|
||||
|
||||
std::array<void*, static_cast<size_t>(Topic::COUNT)> subscriptionHandle {};
|
||||
public:
|
||||
void begin(const char* name);
|
||||
|
||||
private:
|
||||
void handleReceive(const std::string& data);
|
||||
@@ -53,8 +40,4 @@ class BluetoothService : public CommBase<> {
|
||||
if (!v.empty()) svc->handleReceive(v);
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
void begin(const char* name);
|
||||
void loop() {}
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "event_bus.hpp"
|
||||
#include <ArduinoJson.h>
|
||||
#include <array>
|
||||
#include <bitset>
|
||||
@@ -17,6 +18,8 @@ class CommBase {
|
||||
std::array<Bits, NTopics> subs_;
|
||||
portMUX_TYPE mux_ portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
std::array<void*, static_cast<size_t>(Topic::COUNT)> subscriptionHandle {};
|
||||
|
||||
static constexpr size_t idx(Topic t) { return static_cast<size_t>(t); }
|
||||
|
||||
template <Topic T>
|
||||
@@ -30,6 +33,18 @@ class CommBase {
|
||||
protected:
|
||||
virtual void send(size_t cid, const char* data, size_t len) = 0;
|
||||
|
||||
template <class Msg>
|
||||
auto& getHandle(Topic topic) {
|
||||
using H = typename EventBus<Msg>::Handle;
|
||||
return *static_cast<H*>(subscriptionHandle[static_cast<size_t>(topic)]);
|
||||
}
|
||||
|
||||
template <class Msg>
|
||||
void setHandle(Topic topic, typename EventBus<Msg>::Handle&& h) {
|
||||
using H = typename EventBus<Msg>::Handle;
|
||||
subscriptionHandle[static_cast<size_t>(topic)] = new H(std::move(h));
|
||||
}
|
||||
|
||||
public:
|
||||
void subscribe(Topic t, size_t cid) {
|
||||
portENTER_CRITICAL(&mux_);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef Socket_h
|
||||
#define Socket_h
|
||||
|
||||
#include <PsychicHttp.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <functional>
|
||||
|
||||
#include "event_bus.hpp"
|
||||
#include "adapters/comm_base.hpp"
|
||||
#include "topic.hpp"
|
||||
// #include "msgs/motion_input_msg.hpp"
|
||||
// #include "msgs/motion_angles_msg.hpp"
|
||||
// #include "msgs/motion_position_msg.hpp"
|
||||
// #include "msgs/motion_mode_msg.hpp"
|
||||
|
||||
// typedef std::function<void(JsonObject &root, int originId)> EventCallback;
|
||||
|
||||
class EventSocket : public CommBase<> {
|
||||
PsychicWebSocketHandler _socket;
|
||||
|
||||
public:
|
||||
EventSocket();
|
||||
PsychicWebSocketHandler *getHandler() { return &_socket; }
|
||||
|
||||
private:
|
||||
void send(size_t clientId, const char *data, size_t len) override;
|
||||
|
||||
void handleReceive(const std::string &data);
|
||||
// void handleTypedMessage(const std::string &data);
|
||||
// void handleLegacyMessage(const std::string &data, int originId);
|
||||
// void handleEventCallbacks(String event, JsonObject &jsonObject, int originId);
|
||||
|
||||
void onWSOpen(PsychicWebSocketClient *client);
|
||||
void onWSClose(PsychicWebSocketClient *client);
|
||||
esp_err_t onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame);
|
||||
};
|
||||
|
||||
extern EventSocket socket;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user