Files
tactility/Tactility/Private/Tactility/service/espnow/EspNowService.h
T
Ken Van Hoeylandt 3f1bfee3f5 Various improvements (#264)
- Replace C function pointers with C++ `std::function` in `Thread`, `Timer` and `DispatcherThread`
- Rename `SystemEvent`-related functions
- WiFi: fix auto-connect when WiFi disconnects from bad signal
- WiFi: fix auto-connect when WiFi fails to auto-connect
- WiFi: implement disconnect() when tapping connected WiFi ap in WiFi management app
2025-03-30 21:59:31 +02:00

74 lines
1.8 KiB
C++

#pragma once
#ifdef ESP_PLATFORM
#include "Tactility/MessageQueue.h"
#include "Tactility/service/Service.h"
#include "Tactility/service/espnow/EspNow.h"
#include <Tactility/Mutex.h>
#include <functional>
namespace tt::service::espnow {
class EspNowService final : public Service {
private:
struct ReceiverSubscriptionData {
ReceiverSubscription id;
std::function<void(const esp_now_recv_info_t* receiveInfo, const uint8_t* data, int length)> onReceive;
};
struct SendCallback {
uint8_t macAddress[ESP_NOW_ETH_ALEN];
bool success;
};
Mutex mutex = Mutex(Mutex::Type::Recursive);
std::vector<ReceiverSubscriptionData> subscriptions;
ReceiverSubscription lastSubscriptionId = 0;
bool enabled = false;
// Dispatcher calls this and forwards to non-static function
void enableFromDispatcher(const EspNowConfig& config);
void disableFromDispatcher();
static void receiveCallback(const esp_now_recv_info_t* receiveInfo, const uint8_t* data, int length);
void onReceive(const esp_now_recv_info_t* receiveInfo, const uint8_t* data, int length);
public:
// region Overrides
void onStart(ServiceContext& service) override;
void onStop(ServiceContext& service) override;
// endregion Overrides
// region Internal API
void enable(const EspNowConfig& config);
void disable();
bool isEnabled() const;
bool addPeer(const esp_now_peer_info_t& peer);
bool send(const uint8_t* address, const uint8_t* buffer, size_t bufferLength);
ReceiverSubscription subscribeReceiver(std::function<void(const esp_now_recv_info_t* receiveInfo, const uint8_t* data, int length)> onReceive);
void unsubscribeReceiver(ReceiverSubscription subscription);
// region Internal API
};
std::shared_ptr<EspNowService> findService();
}
#endif // ESP_PLATFORM