105 UniotCore() : mScheduler(), mEventBus(
FOURCC(main)), mpNetworkControllerConfig(nullptr) {}
118 _createNetworkControllerConfig();
119 mpNetworkControllerConfig->pinBtn = pinBtn;
120 mpNetworkControllerConfig->activeLevelBtn = activeLevelBtn;
121 mpNetworkControllerConfig->registerLispBtn = registerLispBtn;
137 _createNetworkControllerConfig();
138 mpNetworkControllerConfig->pinLed = pinLed;
139 mpNetworkControllerConfig->activeLevelLed = activeLevelLed;
152 _createNetworkControllerConfig();
153 mpNetworkControllerConfig->maxRebootCount = maxRebootCount;
154 mpNetworkControllerConfig->rebootWindowMs = rebootWindowMs;
192 if (periodSeconds > 0) {
194 mScheduler.push(
"store_date", taskStoreDate);
195 taskStoreDate->attach(periodSeconds * 1000L);
244 template <
typename... Pins>
257 template <
typename... Pins>
270 template <
typename... Pins>
283 template <
typename... Pins>
330 auto id = _generateTimerId();
334 if (!remainingTimes) {
335 mActiveTimers.remove(
id);
339 mActiveTimers.put(
id, task);
340 mScheduler.push(
nullptr, task);
341 task->attach(intervalMs, times);
355 return setInterval(std::move(callback), timeoutMs, 1);
383 auto task = mActiveTimers.get(
id,
nullptr);
386 return mActiveTimers.remove(
id);
403 auto task = mActiveTimers.get(
id,
nullptr);
404 return task && task->isAttached();
415 return mActiveTimers.calcSize();
430 mScheduler.push(name, task);
444 template <
typename... Topics>
450 unsigned int topics[] = {firstTopic,
static_cast<unsigned int>(otherTopics)...};
451 size_t count =
sizeof...(otherTopics) + 1;
454 for (
size_t i = 0; i < count; ++i) {
455 listener->listenToEvent(topics[i]);
458 if (mEventBus.registerEntity(listener.get())) {
459 auto id = _generateListenerId();
460 mActiveListeners.put(
id, listener);
479 auto listener = mActiveListeners.get(
id,
nullptr);
481 mEventBus.unregisterEntity(listener.get());
482 return mActiveListeners.remove(
id);
496 template <
typename... Topics>
498 unsigned int topics[] = {firstTopic,
static_cast<unsigned int>(otherTopics)...};
499 size_t topicCount =
sizeof...(otherTopics) + 1;
500 size_t removedCount = 0;
502 mActiveListeners.begin();
503 while (!mActiveListeners.isEnd()) {
504 auto listener = mActiveListeners.current().second;
505 bool shouldRemove =
false;
508 for (
size_t i = 0; i < topicCount; ++i) {
509 if (listener->isListeningToEvent(topics[i])) {
517 mEventBus.unregisterEntity(listener.get());
518 mActiveListeners.deleteCurrent();
521 mActiveListeners.next();
541 return mActiveListeners.exist(
id);
552 return mActiveListeners.calcSize();
564 mEventBus.emitEvent(topic, message);
581 [callback = std::move(callback)](
unsigned int topic,
int message) {
583 callback(message != 0);
596 void begin(uint32_t eventBusTaskPeriod = 10) {
601 if (mpNetworkControllerConfig) {
602 app.configureNetworkController(*mpNetworkControllerConfig);
603 _deleteNetworkControllerConfig();
606 mEventBus.registerKit(app);
609 mScheduler.push(
"event_bus", taskHandleEventBus);
610 taskHandleEventBus->attach(eventBusTaskPeriod);
612 mScheduler.push(app);
667 void _createNetworkControllerConfig() {
668 if (!mpNetworkControllerConfig) {
670 mpNetworkControllerConfig->pinBtn = UINT8_MAX;
671 mpNetworkControllerConfig->activeLevelBtn = LOW;
672 mpNetworkControllerConfig->pinLed = UINT8_MAX;
673 mpNetworkControllerConfig->activeLevelLed = HIGH;
674 mpNetworkControllerConfig->maxRebootCount = 5;
675 mpNetworkControllerConfig->rebootWindowMs = 10000;
676 mpNetworkControllerConfig->registerLispBtn =
true;
686 void _deleteNetworkControllerConfig() {
687 if (mpNetworkControllerConfig) {
688 mpNetworkControllerConfig.reset(
nullptr);
716 uniot::TaskScheduler mScheduler;
718 uniot::Map<TimerId, uniot::TaskScheduler::TaskPtr> mActiveTimers;
719 uniot::Map<ListenerId, uniot::SharedPointer<uniot::CoreCallbackEventListener>> mActiveListeners;
Main API class for the Uniot IoT platform.
Definition Uniot.h:90
TimerId setImmediate(std::function< void()> callback)
Execute a callback on the next scheduler cycle.
Definition Uniot.h:366
bool cancelTimer(TimerId id)
Cancel an active timer.
Definition Uniot.h:378
void loop()
Process scheduled tasks and events.
Definition Uniot.h:623
int getActiveListenersCount() const
Get the number of active event listeners.
Definition Uniot.h:551
void configUser(const String &user)
Configure user identification.
Definition Uniot.h:178
ListenerId addWifiStatusLedListener(std::function< void(bool)> callback)
Add a WiFi status LED listener.
Definition Uniot.h:575
ListenerId addSystemListener(std::function< void(unsigned int, int)> callback, unsigned int firstTopic, Topics... otherTopics)
Add a system event listener.
Definition Uniot.h:445
void setLispEventInterceptor(uniot::LispEventInterceptor interceptor)
Set event interceptor for Lisp interpreter.
Definition Uniot.h:219
uniot::CoreEventBus & getEventBus()
Get access to the event bus.
Definition Uniot.h:645
TimerId setInterval(std::function< void()> callback, uint32_t intervalMs, short times=0)
Create a repeating timer.
Definition Uniot.h:325
UniotCore()
Construct a new UniotCore instance.
Definition Uniot.h:105
bool isTimerActive(TimerId id)
Check if a timer is currently active.
Definition Uniot.h:398
void registerLispAnalogOutput(uint8_t first, Pins... pins)
Register GPIO pins as analog outputs for Lisp access.
Definition Uniot.h:284
static constexpr TimerId INVALID_TIMER_ID
Invalid timer ID constant.
Definition Uniot.h:95
void registerLispDigitalOutput(uint8_t first, Pins... pins)
Register GPIO pins as digital outputs for Lisp access.
Definition Uniot.h:245
static constexpr ListenerId INVALID_LISTENER_ID
Invalid listener ID constant.
Definition Uniot.h:96
uint32_t ListenerId
Type for event listener identifiers.
Definition Uniot.h:93
uniot::TaskScheduler::TaskPtr createTask(const char *name, uniot::SchedulerTask::SchedulerTaskCallback callback)
Create a named task for custom scheduling.
Definition Uniot.h:428
bool removeSystemListener(ListenerId id)
Remove a system event listener.
Definition Uniot.h:474
void addLispPrimitive(Primitive *primitive)
Add a custom primitive to the Lisp interpreter.
Definition Uniot.h:207
void registerLispAnalogInput(uint8_t first, Pins... pins)
Register GPIO pins as analog inputs for Lisp access.
Definition Uniot.h:271
void configWiFiCredentials(const String &ssid, const String &password="")
Configure WiFi network credentials.
Definition Uniot.h:165
void begin(uint32_t eventBusTaskPeriod=10)
Initialize and start the Uniot platform.
Definition Uniot.h:596
uniot::AppKit & getAppKit()
Get access to the application kit.
Definition Uniot.h:634
bool registerLispObject(const String &primitiveName, uniot::RecordPtr link, uint32_t id=FOURCC(____))
Register a generic object with the Lisp interpreter.
Definition Uniot.h:311
bool isSystemListenerActive(ListenerId id)
Check if a system listener is active.
Definition Uniot.h:536
void emitSystemEvent(unsigned int topic, int message)
Emit a system event.
Definition Uniot.h:563
uniot::TaskScheduler & getScheduler()
Get access to the task scheduler.
Definition Uniot.h:656
void publishLispEvent(const String &eventID, int32_t value)
Publish an event to the Lisp interpreter.
Definition Uniot.h:232
void configWiFiResetOnReboot(uint8_t maxRebootCount, uint32_t rebootWindowMs=10000)
Configure automatic WiFi reset on repeated reboots.
Definition Uniot.h:151
void configWiFiResetButton(uint8_t pinBtn, uint8_t activeLevelBtn=LOW, bool registerLispBtn=true)
Configure WiFi reset button.
Definition Uniot.h:117
size_t removeSystemListeners(unsigned int firstTopic, Topics... otherTopics)
Remove all listeners for specific topics.
Definition Uniot.h:497
int getActiveTimersCount() const
Get the number of active timers.
Definition Uniot.h:414
uint32_t TimerId
Type for timer identifiers.
Definition Uniot.h:92
void configWiFiStatusLed(uint8_t pinLed, uint8_t activeLevelLed=HIGH)
Configure WiFi status LED.
Definition Uniot.h:136
void registerLispDigitalInput(uint8_t first, Pins... pins)
Register GPIO pins as digital inputs for Lisp access.
Definition Uniot.h:258
TimerId setTimeout(std::function< void()> callback, uint32_t timeoutMs)
Create a one-shot timer.
Definition Uniot.h:354
bool registerLispButton(uniot::Button *button, uint32_t id=FOURCC(_btn))
Register a button object with the Lisp interpreter.
Definition Uniot.h:297
void enablePeriodicDateSave(uint32_t periodSeconds=5 *60)
Enable periodic saving of date/time information.
Definition Uniot.h:191
bool setUserId(const String &userId)
Set the user identifier for device association.
Definition AppKit.h:147
bool setWiFiCredentials(const String &ssid, const String &password)
Set WiFi network credentials.
Definition AppKit.h:134
void setLispEventInterceptor(LispEventInterceptor interceptor)
Set event interceptor for Lisp interpreter.
Definition AppKit.h:322
void publishLispEvent(const String &eventID, int32_t value)
Publish an event to the Lisp interpreter.
Definition AppKit.h:335
unLisp & getLisp()
Get the Lisp interpreter instance.
Definition AppKit.h:105
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:164
unLisp * pushPrimitive(Primitive *primitive)
Add a new primitive function to the Lisp environment.
Definition unLisp.h:295
std::unique_ptr< T > UniquePointer
Type alias for std::unique_ptr with cleaner syntax.
Definition Common.h:152
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:179
#define FOURCC(name)
Creates a FourCC constant from a string literal.
Definition Common.h:107
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:174
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:122
#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:234
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:171
static TaskPtr make(SchedulerTask::SchedulerTaskCallback callback)
Static factory method to create a task with a callback.
Definition TaskScheduler.h:193
constexpr const char * bclicked
Primitive for detecting button click events.
Definition DefaultPrimitives.h:80
UniotCore Uniot
Global Uniot platform instance.
Definition Uniot.cpp:23
@ WIFI_STATUS_LED
LED status indicators for network state.
Definition NetworkEvents.h:75
std::function< bool(const LispEvent &event)> LispEventInterceptor
Definition LispDevice.h:41