Skyward boardcore
Loading...
Searching...
No Matches
ScreenManager.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <miosix.h>
26#include <utils/KernelTime.h>
27
28#include <deque>
29#include <map>
30
31#include "NavController.h"
32#include "View.h"
33
34namespace Boardcore
35{
36
42{
43public:
44 ScreenManager(mxgui::DisplayManager& display, unsigned int refreshRate)
45 : dc(display.getDisplay()), refreshInterval(1000 / refreshRate)
46 {
47 }
48
49 void showScreen(uint8_t id)
50 {
51 activeScreen = id;
52 controller.updateViewTree(screens[id]);
53 }
54
55 uint8_t getScreen() { return activeScreen; }
56
57 void addScreen(uint8_t id, View* root)
58 {
59 screens[id] = root;
60
61 root->setBounds({{0, 0}, {dc.getWidth(), dc.getHeight()}});
62
63 if (screens.size() == 1)
64 showScreen(id);
65 }
66
67 void onButtonEvent(ButtonEvent press) { controller.onButtonEvent(press); }
68
69 mxgui::DrawingContext& getDrawingContext() { return dc; }
70
71protected:
72 void run() override
73 {
74 uint8_t lastScreen = 0;
75 while (!shouldStop())
76 {
77 if (activeScreen != lastScreen)
78 {
79 lastScreen = activeScreen;
80 dc.clear(mxgui::black);
81 screens[activeScreen]->invalidateTree();
82 }
83
84 long long start = Kernel::getOldTick();
85
86 drawViewTree(screens[activeScreen], dc);
87
88 Kernel::Thread::sleepUntil(start + refreshInterval);
89 }
90 }
91
92private:
99 void drawViewTree(View* root, mxgui::DrawingContext& dc)
100 {
101 // Avoid recursion
102 std::deque<View*> viewsDc;
103 viewsDc.push_back(root);
104
105 while (viewsDc.size() != 0)
106 {
107 View* view = viewsDc.front();
108 viewsDc.pop_front();
109
110 view->draw(dc);
111
112 for (View* c : view->getChilds())
113 viewsDc.push_back(c);
114 }
115 }
116
117 mxgui::DrawingContext dc;
118
119 unsigned int refreshInterval;
120
121 NavController controller;
122 std::map<uint8_t, View*> screens;
123
124 uint8_t activeScreen = 0;
125};
126
127} // namespace Boardcore
bool shouldStop()
Tells whether or not the ActiveObject should stop its execution.
virtual bool start()
Start the thread associated with this active object.
void onButtonEvent(ButtonEvent press)
void updateViewTree(View *root)
UI Thread: Manages multiple view trees ("Screen") and draws the active one at the provided refresh ra...
mxgui::DrawingContext & getDrawingContext()
void addScreen(uint8_t id, View *root)
void showScreen(uint8_t id)
ScreenManager(mxgui::DisplayManager &display, unsigned int refreshRate)
void onButtonEvent(ButtonEvent press)
Base class for anything that can be drawn on the screen and interacted with.
Definition View.h:126
virtual void draw(mxgui::DrawingContext &dc)
Draw the view in its bounds.
Definition View.h:171
void sleepUntil(long long absoluteTimeMs)
Sleep until a given time in milliseconds.
Definition KernelTime.h:69
long long getOldTick()
Get the current time in milliseconds.
Definition KernelTime.h:43
This file includes all the types the logdecoder script will decode.