Doxygen with github
Loading...
Searching...
No Matches
ksAppRotator.h
1/*
2 * Copyright (c) 2021-2025, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFramework 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 <stdint.h>
14#include <Arduino.h>
15
16#include "ksApplication.h"
17
22#define KSF_IMPLEMENT_APP_ROTATOR(...) \
23 ksf::ksAppRotator<__VA_ARGS__> appRotator; \
24 void setup() { KSF_FRAMEWORK_INIT() } \
25 void loop() { appRotator.loop(1); }
26
32#define KSF_IMPLEMENT_APP_ROTATOR_INTERVAL(delayBetweenLoops, ...) \
33 ksf::ksAppRotator<__VA_ARGS__> appRotator; \
34 void setup() { KSF_FRAMEWORK_INIT() } \
35 void loop() { appRotator.loop(delayBetweenLoops); }
36
37namespace ksf
38{
43 template <class... AppTypes>
45 {
46 private:
47 uint8_t appIndex{0};
48 std::unique_ptr<ksApplication> currentApplication{nullptr};
49 typedef std::unique_ptr<ksApplication> (*TSpawnerFunc)();
50
56 template <class TApplicationType>
57 static std::unique_ptr<ksApplication> spawnApp()
58 {
59 return std::make_unique<TApplicationType>();
60 }
61
62 static constexpr std::array<TSpawnerFunc, sizeof...(AppTypes)> appSpawners{spawnApp<AppTypes>...};
63
64 public:
69 {
70 /* If current application is valid and we looped, then we're done. */
71 if (currentApplication)
72 {
73 /* If loop fails, then destroy and try to do our business next delay. */
74 if (!currentApplication->loop())
75 currentApplication.reset();
76
77 /* If current application is valid, then we're done. */
78 return;
79 }
80
81 /* Spawn application. */
82 currentApplication = appSpawners[appIndex]();
83
84 /* If not initialized, then destroy and try to do our business next delay. */
85 if (!currentApplication->init())
86 currentApplication.reset();
87
88 /* Set next app index. */
89 if (++appIndex >= appSpawners.size())
90 appIndex = 0;
91 }
92
97 void loop(unsigned long milliseconds)
98 {
100 delay(milliseconds);
101 }
102 };
103}
Application rotator component.
Definition ksAppRotator.h:45
void loopNoDelay()
Runs the application loop.
Definition ksAppRotator.h:68
void loop(unsigned long milliseconds)
Runs application loop with a delay between loops.
Definition ksAppRotator.h:97