106 UniotCore() : mScheduler(), mEventBus(
FOURCC(main)), mpNetworkControllerConfig(nullptr) {}
123 _createNetworkControllerConfig();
124 mpNetworkControllerConfig->pinBtn = pinBtn;
125 mpNetworkControllerConfig->activeLevelBtn = activeLevelBtn;
126 mpNetworkControllerConfig->registerLispBtn = registerLispBtn;
142 _createNetworkControllerConfig();
143 mpNetworkControllerConfig->pinLed = pinLed;
144 mpNetworkControllerConfig->activeLevelLed = activeLevelLed;
183 _createNetworkControllerConfig();
184 mpNetworkControllerConfig->maxRebootCount = maxRebootCount;
185 mpNetworkControllerConfig->rebootWindowMs = rebootWindowMs;
232 void addCustomPage(
const String& path,
const uint8_t* gzData,
size_t gzLen,
const String& label =
"",
const char* contentType =
"text/html") {
258 void addCustomRoute(
const String& path, WebRequestMethodComposite method, ArRequestHandlerFunction handler) {
289 if (periodSeconds > 0) {
291 mScheduler.push(
"store_date", taskStoreDate);
292 taskStoreDate->attach(periodSeconds * 1000L);
396 template <
typename... Pins>
412 template <
typename... Pins>
428 template <
typename... Pins>
441 template <
typename... Pins>
490 constexpr uint8_t longPressTicks = 30;
491 constexpr uint32_t pollMs = 100;
499 mButtons.push(button);
502 if (!mpTaskButtons) {
505 button->execute(times);
508 mScheduler.push(
"lisp_buttons", mpTaskButtons);
509 mpTaskButtons->attach(pollMs);
567 auto id = _generateTimerId();
571 if (!remainingTimes) {
572 mActiveTimers.remove(
id);
576 mActiveTimers.put(
id, task);
577 mScheduler.push(
nullptr, task);
578 task->attach(intervalMs, times);
592 return setInterval(std::move(callback), timeoutMs, 1);
620 auto task = mActiveTimers.get(
id,
nullptr);
623 return mActiveTimers.remove(
id);
640 auto task = mActiveTimers.get(
id,
nullptr);
641 return task && task->isAttached();
652 return mActiveTimers.calcSize();
667 mScheduler.push(name, task);
681 template <
typename... Topics>
687 unsigned int topics[] = {firstTopic,
static_cast<unsigned int>(otherTopics)...};
688 size_t count =
sizeof...(otherTopics) + 1;
691 for (
size_t i = 0; i < count; ++i) {
692 listener->listenToEvent(topics[i]);
695 if (mEventBus.registerEntity(listener.get())) {
696 auto id = _generateListenerId();
697 mActiveListeners.put(
id, listener);
716 auto listener = mActiveListeners.get(
id,
nullptr);
718 mEventBus.unregisterEntity(listener.get());
719 return mActiveListeners.remove(
id);
733 template <
typename... Topics>
735 unsigned int topics[] = {firstTopic,
static_cast<unsigned int>(otherTopics)...};
736 size_t topicCount =
sizeof...(otherTopics) + 1;
737 size_t removedCount = 0;
739 mActiveListeners.begin();
740 while (!mActiveListeners.isEnd()) {
741 auto listener = mActiveListeners.current().second;
742 bool shouldRemove =
false;
745 for (
size_t i = 0; i < topicCount; ++i) {
746 if (listener->isListeningToEvent(topics[i])) {
754 mEventBus.unregisterEntity(listener.get());
755 mActiveListeners.deleteCurrent();
758 mActiveListeners.next();
778 return mActiveListeners.exist(
id);
789 return mActiveListeners.calcSize();
801 mEventBus.emitEvent(topic, message);
824 [callback = std::move(callback)](
unsigned int topic,
int message) {
826 callback(message != 0);
839 void begin(uint32_t eventBusTaskPeriod = 10) {
844 if (mpNetworkControllerConfig) {
845 app.configureNetworkController(*mpNetworkControllerConfig);
846 _deleteNetworkControllerConfig();
849 mEventBus.registerKit(app);
852 mScheduler.push(
"event_bus", taskHandleEventBus);
853 taskHandleEventBus->attach(eventBusTaskPeriod);
862 mScheduler.push(app);
915 template <
typename Fn>
916 void _forEachLispButton(Fn fn) {
919 for (
size_t i = 0; i < count; i++) {
938 void _createNetworkControllerConfig() {
939 if (!mpNetworkControllerConfig) {
941 mpNetworkControllerConfig->pinBtn = UINT8_MAX;
942 mpNetworkControllerConfig->activeLevelBtn = LOW;
943 mpNetworkControllerConfig->pinLed = UINT8_MAX;
944 mpNetworkControllerConfig->activeLevelLed = HIGH;
947 mpNetworkControllerConfig->registerLispBtn =
true;
957 void _deleteNetworkControllerConfig() {
958 if (mpNetworkControllerConfig) {
959 mpNetworkControllerConfig.reset(
nullptr);
987 uniot::TaskScheduler mScheduler;
989 uniot::Map<TimerId, uniot::TaskScheduler::TaskPtr> mActiveTimers;
990 uniot::Map<ListenerId, uniot::SharedPointer<uniot::CoreCallbackEventListener>> mActiveListeners;
992 ClearQueue<uniot::SharedPointer<uniot::Button>> mButtons;
Main API class for the Uniot IoT platform.
Definition Uniot.h:91
TimerId setImmediate(std::function< void()> callback)
Execute a callback on the next scheduler cycle.
Definition Uniot.h:603
bool cancelTimer(TimerId id)
Cancel an active timer.
Definition Uniot.h:615
void loop()
Process scheduled tasks and events.
Definition Uniot.h:873
int getActiveListenersCount() const
Get the number of active event listeners.
Definition Uniot.h:788
void configUser(const String &user)
Configure user identification.
Definition Uniot.h:209
void addCustomPage(const String &path, const uint8_t *gzData, size_t gzLen, const String &label="", const char *contentType="text/html")
Register a custom gzipped page on the configuration portal.
Definition Uniot.h:232
ListenerId addWifiStatusLedListener(std::function< void(bool)> callback)
Add a WiFi status LED listener.
Definition Uniot.h:818
ListenerId addSystemListener(std::function< void(unsigned int, int)> callback, unsigned int firstTopic, Topics... otherTopics)
Add a system event listener.
Definition Uniot.h:682
void setLispEventInterceptor(uniot::LispEventInterceptor interceptor)
Set event interceptor for Lisp interpreter.
Definition Uniot.h:316
uniot::CoreEventBus & getEventBus()
Get access to the event bus.
Definition Uniot.h:895
TimerId setInterval(std::function< void()> callback, uint32_t intervalMs, short times=0)
Create a repeating timer.
Definition Uniot.h:562
static constexpr uint8_t REBOOT_RESET_DISABLED
Sentinel value for configWiFiResetOnReboot(): disables reboot-reset entirely.
Definition Uniot.h:158
UniotCore()
Construct a new UniotCore instance.
Definition Uniot.h:106
bool isTimerActive(TimerId id)
Check if a timer is currently active.
Definition Uniot.h:635
void registerLispAnalogOutput(uint8_t first, Pins... pins)
Register GPIO pins as analog outputs for Lisp access.
Definition Uniot.h:442
static constexpr TimerId INVALID_TIMER_ID
Invalid timer ID constant.
Definition Uniot.h:96
void addCustomRoute(const String &path, WebRequestMethodComposite method, ArRequestHandlerFunction handler)
Register a custom HTTP route on the configuration portal.
Definition Uniot.h:258
void registerLispDigitalOutput(uint8_t first, Pins... pins)
Register GPIO pins as digital outputs for Lisp access.
Definition Uniot.h:397
void setLispStopHook(uniot::LispStopHook hook)
Set the hook called when a Lisp script stops.
Definition Uniot.h:371
static constexpr ListenerId INVALID_LISTENER_ID
Invalid listener ID constant.
Definition Uniot.h:97
uint32_t ListenerId
Type for event listener identifiers.
Definition Uniot.h:94
uniot::TaskScheduler::TaskPtr createTask(const char *name, uniot::SchedulerTask::SchedulerTaskCallback callback)
Create a named task for custom scheduling.
Definition Uniot.h:665
bool removeSystemListener(ListenerId id)
Remove a system event listener.
Definition Uniot.h:711
void addCustomRoute(const String &path, ArRequestHandlerFunction handler)
Register a custom HTTP GET route on the configuration portal.
Definition Uniot.h:245
void addLispPrimitive(Primitive *primitive)
Add a custom primitive to the Lisp interpreter.
Definition Uniot.h:304
void registerLispAnalogInput(uint8_t first, Pins... pins)
Register GPIO pins as analog inputs for Lisp access.
Definition Uniot.h:429
int addLispButton(uint8_t pin, uint8_t activeLevel=LOW, uint32_t id=FOURCC(_btn), uniot::Button::ButtonCallback callback=nullptr)
Create a button on a pin and expose it to Lisp scripts.
Definition Uniot.h:489
void configWiFiCredentials(const String &ssid, const String &password="")
Configure WiFi network credentials.
Definition Uniot.h:196
void begin(uint32_t eventBusTaskPeriod=10)
Initialize and start the Uniot platform.
Definition Uniot.h:839
uniot::AppKit & getAppKit()
Get access to the application kit.
Definition Uniot.h:884
bool registerLispObject(const String &primitiveName, uniot::RecordPtr link, uint32_t id=FOURCC(____))
Register a generic object with the Lisp interpreter.
Definition Uniot.h:548
bool isSystemListenerActive(ListenerId id)
Check if a system listener is active.
Definition Uniot.h:773
void addMQTTDevice(uniot::MQTTDevice &device)
Register a user-defined MQTT device with the platform.
Definition Uniot.h:275
void emitSystemEvent(unsigned int topic, int message)
Emit a system event.
Definition Uniot.h:800
uniot::TaskScheduler & getScheduler()
Get access to the task scheduler.
Definition Uniot.h:906
void resetLispButtons()
Clear pending clicks and long presses on every Lisp button.
Definition Uniot.h:531
void publishLispEvent(const String &eventID, int32_t value)
Publish an event to the Lisp interpreter.
Definition Uniot.h:384
void configWiFiResetOnReboot(uint8_t maxRebootCount=UNIOT_WIFI_REBOOT_RESET_COUNT, uint32_t rebootWindowMs=UNIOT_WIFI_REBOOT_WINDOW_MS)
Configure automatic WiFi reset on repeated reboots.
Definition Uniot.h:182
void configWiFiResetButton(uint8_t pinBtn, uint8_t activeLevelBtn=LOW, bool registerLispBtn=true)
Configure WiFi reset button.
Definition Uniot.h:122
size_t removeSystemListeners(unsigned int firstTopic, Topics... otherTopics)
Remove all listeners for specific topics.
Definition Uniot.h:734
int getActiveTimersCount() const
Get the number of active timers.
Definition Uniot.h:651
uint32_t TimerId
Type for timer identifiers.
Definition Uniot.h:93
void configWiFiStatusLed(uint8_t pinLed, uint8_t activeLevelLed=HIGH)
Configure WiFi status LED.
Definition Uniot.h:141
void registerLispDigitalInput(uint8_t first, Pins... pins)
Register GPIO pins as digital inputs for Lisp access.
Definition Uniot.h:413
void setLispStartHook(uniot::LispStartHook hook)
Set the hook called when a Lisp script starts.
Definition Uniot.h:337
TimerId setTimeout(std::function< void()> callback, uint32_t timeoutMs)
Create a one-shot timer.
Definition Uniot.h:591
bool registerLispButton(uniot::Button *button, uint32_t id=FOURCC(_btn))
Register a button object with the Lisp interpreter.
Definition Uniot.h:455
void enablePeriodicDateSave(uint32_t periodSeconds=5 *60)
Enable periodic saving of date/time information.
Definition Uniot.h:288
bool setUserId(const String &userId)
Set the user identifier for device association.
Definition AppKit.h:151
MQTTKit & getMQTT()
Get the MQTT communication kit instance.
Definition AppKit.h:117
void setLispStopHook(LispStopHook hook)
Set the hook called when a Lisp script stops.
Definition AppKit.h:366
bool addCustomRoute(const String &path, WebRequestMethodComposite method, ArRequestHandlerFunction handler)
Register a custom HTTP route on the configuration portal.
Definition AppKit.h:184
bool setWiFiCredentials(const String &ssid, const String &password)
Set WiFi network credentials.
Definition AppKit.h:138
bool addCustomPage(const String &path, const uint8_t *gzData, size_t gzLen, const String &label="", const char *contentType="text/html")
Register a custom gzipped page on the configuration portal.
Definition AppKit.h:171
void setLispEventInterceptor(LispEventInterceptor interceptor)
Set event interceptor for Lisp interpreter.
Definition AppKit.h:350
void publishLispEvent(const String &eventID, int32_t value)
Publish an event to the Lisp interpreter.
Definition AppKit.h:379
unLisp & getLisp()
Get the Lisp interpreter instance.
Definition AppKit.h:109
void setLispStartHook(LispStartHook hook)
Set the hook called when a Lisp script starts.
Definition AppKit.h:358
Definition MQTTDevice.h:37
virtual void syncSubscriptions()=0
Reconstructs subscriptions after reconnection or credential changes.
void addDevice(MQTTDevice &device)
Adds a device to be managed by this MQTT kit.
Definition MQTTKit.h:150
static RegisterManager & getRegisterManager()
Gets the static register manager instance.
Definition PrimitiveExpeditor.h:71
void setDigitalOutput(uint8_t first, Args... args)
Sets one or more pins as digital outputs.
Definition RegisterManager.h:74
void setAnalogInput(uint8_t first, Args... args)
Sets one or more pins as analog inputs.
Definition RegisterManager.h:86
void setDigitalInput(uint8_t first, Args... args)
Sets one or more pins as digital inputs.
Definition RegisterManager.h:62
void setAnalogOutput(uint8_t first, Args... args)
Sets one or more pins as analog outputs.
Definition RegisterManager.h:98
bool link(const String &name, RecordPtr link, uint32_t id=FOURCC(____))
Links an object to a named register.
Definition RegisterManager.h:111
Definition TaskScheduler.h:67
std::function< void(SchedulerTask &, short)> SchedulerTaskCallback
Callback function signature for scheduled tasks.
Definition TaskScheduler.h:75
static Date & getInstance()
Definition Singleton.h:73
Definition TaskScheduler.h:183
unLisp * pushPrimitive(Primitive *primitive)
Add a new primitive function to the Lisp environment.
Definition unLisp.h:329
std::unique_ptr< T > UniquePointer
Type alias for std::unique_ptr with cleaner syntax.
Definition Common.h:305
std::shared_ptr< T > SharedPointer
Type alias for std::shared_ptr with cleaner syntax.
Definition Common.h:313
#define UNIOT_WIFI_REBOOT_WINDOW_MS
How long after boot a reboot still counts towards the reset, in ms.
Definition Common.h:115
auto MakeUnique(Args &&...args) -> decltype(std::make_unique< T >(std::forward< Args >(args)...))
Creates a unique pointer instance, alias for std::make_unique.
Definition Common.h:332
#define FOURCC(name)
Creates a FourCC constant from a string literal.
Definition Common.h:260
#define UNIOT_WIFI_REBOOT_RESET_COUNT
Consecutive reboots that reset the stored network configuration.
Definition Common.h:104
auto MakeShared(Args &&...args) -> decltype(std::make_shared< T >(std::forward< Args >(args)...))
Creates a shared pointer instance, alias for std::make_shared.
Definition Common.h:327
EventBus< unsigned int, int, Bytes > CoreEventBus
Standard EventBus configuration used throughout the core system.
Definition EventBus.h:211
#define UNIOT_LOG_SET_READY()
Initialize the logging system.
Definition Logger.h:127
#define UNIOT_LOG_ERROR_IF(log_cond, log...)
Conditionally log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:281
ObjectRegisterRecord * RecordPtr
Type alias for ObjectRegisterRecord pointers for better readability.
Definition ObjectRegister.h:33
SharedPointer< SchedulerTask > TaskPtr
Shared pointer type for scheduler tasks.
Definition TaskScheduler.h:190
static TaskPtr make(SchedulerTask::SchedulerTaskCallback callback)
Static factory method to create a task with a callback.
Definition TaskScheduler.h:212
std::function< void(LispStartReason)> LispStartHook
Called after the interpreter is built and before the script is evaluated.
Definition LispHelper.h:186
std::function< void(LispStopReason)> LispStopHook
Called before the interpreter is destroyed.
Definition LispHelper.h:192
constexpr const char * bclicked
Primitive for detecting button click events.
Definition DefaultPrimitives.h:80
UniotCore Uniot
Global Uniot platform instance.
Definition Uniot.cpp:20
@ WIFI_STATUS_LED
LED status indicators for network state.
Definition NetworkEvents.h:75
std::function< bool(const LispEvent &event)> LispEventInterceptor
Definition LispDevice.h:41