Doxygen with github
Loading...
Searching...
No Matches
ksAppRotator.h
1/*
2 * Copyright (c) 2021-2023, 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 && currentApplication->loop())
72 return;
73
74 /* Spawn application. */
75 currentApplication = appSpawners[appIndex]();
76
77 /* If not initialized, then destroy and try to do our business next delay. */
78 if (!currentApplication->init())
79 currentApplication.reset(nullptr);
80
81 /* Set next app index. */
82 if (++appIndex >= appSpawners.size())
83 appIndex = 0;
84 }
85
90 void loop(unsigned long milliseconds)
91 {
93 delay(milliseconds);
94 }
95 };
96}
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:90