ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/SmartVariable.hpp

50 lines
1.5 KiB
C++
Raw Normal View History

2024-03-22 19:09:50 +00:00
#pragma once
#include <FRAM.h>
#include <ESPMegaIoT.hpp>
#include <map>
2024-03-23 03:25:16 +00:00
2024-03-22 19:09:50 +00:00
/**
* @brief SmartVariable is a local variable that can be accessed remotely and have FRAM support
*/
class SmartVariable {
public:
SmartVariable();
~SmartVariable();
void begin(size_t size);
void enableIoT(ESPMegaIoT* iot, const char* topic);
void enableValueRequest(const char* valueRequestTopic);
void setValue(const char* value);
char* getValue();
void enableSetValue(const char* setValueTopic);
void publishValue();
2024-03-23 03:25:16 +00:00
void bindFRAM(FRAM *fram, uint32_t framAddress);
void bindFRAM(FRAM *fram, uint32_t framAddress, bool loadValue);
2024-03-22 19:09:50 +00:00
void loadValue();
void saveValue();
void setValueAutoSave(bool autoSave);
uint16_t registerCallback(std::function<void(char*)> callback);
2024-03-22 19:09:50 +00:00
void unregisterCallback(uint16_t handlerId);
2024-03-23 03:25:16 +00:00
int32_t getIntValue();
void setIntValue(int32_t value);
double getDoubleValue();
void setDoubleValue(double value);
2024-03-22 19:09:50 +00:00
protected:
ESPMegaIoT* iot;
2024-03-23 03:25:16 +00:00
bool iotEnabled;
2024-03-22 19:09:50 +00:00
const char* topic;
char* value;
size_t size;
bool useValueRequest;
const char* valueRequestTopic;
bool setValueEnabled;
const char* setValueTopic;
bool autoSave;
2024-03-23 03:25:16 +00:00
FRAM *fram;
2024-03-22 19:09:50 +00:00
uint32_t framAddress;
void handleMqttCallback(char* topic, char* payload);
2024-03-23 03:25:16 +00:00
void subscribeMqtt();
2024-03-22 19:09:50 +00:00
// Value Change Callback
uint16_t currentHandlerId;
std::map<uint16_t, std::function<void(char*)>> valueChangeCallbacks;
2024-03-22 19:09:50 +00:00
};