Doxygen with github
Loading...
Searching...
No Matches
ksAppRotator.h
1/*
2 * Copyright (c) 2020-2026, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFrameworkLib IoT library.
5 * All licensing information can be found inside LICENSE.md file.
6 *
7 * https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
8 */
9
10#pragma once
11
12#include <memory>
13#include <array>
14#include <cstdint>
15#include <Arduino.h>
16
17#include "ksApplication.h"
18
23#define KSF_IMPLEMENT_APP_ROTATOR(...) \
24 ksf::ksAppRotator<__VA_ARGS__> appRotator; \
25 void setup() { KSF_FRAMEWORK_INIT() } \
26 void loop() { appRotator.loop(1); }
27
33#define KSF_IMPLEMENT_APP_ROTATOR_INTERVAL(delayBetweenLoops, ...) \
34 ksf::ksAppRotator<__VA_ARGS__> appRotator; \
35 void setup() { KSF_FRAMEWORK_INIT() } \
36 void loop() { appRotator.loop(delayBetweenLoops); }
37
38namespace ksf
39{
44 template <class... AppTypes>
46 {
47 private:
48 uint8_t appIndex{0};
49 std::unique_ptr<ksApplication> currentApplication{nullptr};
50 typedef std::unique_ptr<ksApplication> (*TSpawnerFunc)();
51
57 template <class TApplicationType>
58 static std::unique_ptr<ksApplication> spawnApp()
59 {
60 return std::make_unique<TApplicationType>();
61 }
62
63 static constexpr std::array<TSpawnerFunc, sizeof...(AppTypes)> appSpawners{spawnApp<AppTypes>...};
64
65 public:
70 {
71 /* If current application is valid and we looped, then we're done. */
72 if (currentApplication)
73 {
74 /* If loop fails, then destroy and try to do our business next delay. */
75 if (!currentApplication->loop())
76 currentApplication.reset();
77
78 /* If current application is valid, then we're done. */
79 return;
80 }
81
82 /* Spawn application. */
83 currentApplication = appSpawners[appIndex]();
84
85 /* If not initialized, then destroy and try to do our business next delay. */
86 if (!currentApplication->init())
87 currentApplication.reset();
88
89 /* Set next app index. */
90 if (++appIndex >= appSpawners.size())
91 appIndex = 0;
92 }
93
98 void loop(unsigned long milliseconds)
99 {
100 loopNoDelay();
101 delay(milliseconds);
102 }
103 };
104}
Application rotator component.
Definition ksAppRotator.h:46
void loopNoDelay()
Runs the application loop.
Definition ksAppRotator.h:69
void loop(unsigned long milliseconds)
Runs application loop with a delay between loops.
Definition ksAppRotator.h:98