RIT VEXU Core API
Loading...
Searching...
No Matches
screen_pages.h
1#pragma once
2
3#include <cassert>
4#include <functional>
5#include <map>
6#include <vector>
7
8#include "core/competition/initializer.h"
9#include "core/subsystems/odometry/odometry_base.h"
10#include "core/math/controls/pid.h"
11#include "core/math/controls/pidff.h"
12#include "core/utils/graph_drawer.h"
13#include "core/math/geometry/point2d.h"
14#include "core/math/geometry/pose2d.h"
15#include "vex/vex.h"
16
18struct Rect {
19 Point2d min;
20 Point2d max;
21 static Rect from_min_and_size(Point2d min, Point2d size) { return {min, min + size}; }
22 Point2d dimensions() const { return max - min; }
23 Point2d center() const { return Point2d{(min + max).x() / 2, (min + max).y() / 2}; }
24 double width() const { return max.x() - min.x(); }
25 double height() const { return max.y() - min.y(); }
26 bool contains(Point2d p) const {
27 bool xin = p.x() > min.x() && p.x() < max.x();
28 bool yin = p.y() > min.y() && p.y() < max.y();
29 return xin && yin;
30 }
31};
32
36 public:
41 ButtonWidget(std::function<void(void)> onpress, Rect rect, std::string name)
42 : onpress(onpress), rect(rect), name(name) {}
43
47 ButtonWidget(void (*onpress)(), Rect rect, std::string name)
48 : onpress(onpress), rect(rect), name(name) {}
49
55 bool update(bool was_pressed, int x, int y);
57 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number);
58
59 private:
60 std::function<void(void)> onpress;
61 Rect rect;
62 std::string name = "";
63 bool was_pressed_last = false;
64};
65
69 public:
76 SliderWidget(double& val, double low, double high, Rect rect, std::string name)
77 : value(val), low(low), high(high), rect(rect), name(name) {}
78
84 bool update(bool was_pressed, int x, int y);
86 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number);
87
88 private:
89 double& value;
90
91 double low;
92 double high;
93
94 Rect rect;
95 std::string name = "";
96};
97
98struct WidgetConfig;
99
100struct SliderConfig {
101 double& val;
102 double low;
103 double high;
104};
105struct ButtonConfig {
106 std::function<void()> onclick;
107};
108struct CheckboxConfig {
109 std::function<void(bool)> onupdate;
110};
111struct LabelConfig {
112 std::string label;
113};
114
115struct TextConfig {
116 std::function<std::string()> text;
117};
118struct SizedWidget {
119 int size;
120 WidgetConfig& widget;
121};
122struct WidgetConfig {
123 enum Type {
124 Col,
125 Row,
126 Slider,
127 Button,
128 Checkbox,
129 Label,
130 Text,
131 Graph,
132 };
133 Type type;
134 union {
135 std::vector<SizedWidget> widgets;
136 SliderConfig slider;
137 ButtonConfig button;
138 CheckboxConfig checkbox;
139 LabelConfig label;
140 TextConfig text;
141 GraphDrawer* graph;
142 } config;
143};
144
145class ScreenPage;
148 public:
149 virtual ~ScreenPage() = default;
158 virtual void update(bool was_pressed, int x, int y);
166 virtual void draw(vex::brain::lcd& screen, bool first_draw, unsigned int frame_number);
167};
168
169struct ScreenRect {
170 uint32_t x1;
171 uint32_t y1;
172 uint32_t x2;
173 uint32_t y2;
174};
175void draw_widget(WidgetConfig& widget, ScreenRect rect);
176
177class WidgetPage : public ScreenPage {
178 public:
179 WidgetPage(WidgetConfig& cfg) : base_widget(cfg) {}
180 void update(bool was_pressed, int x, int y) override;
181
182 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override {
183 draw_widget(base_widget, {.x1 = 20, .y1 = 0, .x2 = 440, .y2 = 240});
184 }
185
186 private:
187 WidgetConfig& base_widget;
188};
189
191using update_func_t = std::function<void(bool, int, int)>;
192
194using draw_func_t = std::function<void(vex::brain::lcd& screen, bool, unsigned int)>;
195
197class StatsPage : public ScreenPage {
198 public:
201 StatsPage(std::map<std::string, vex::motor&> motors);
203 void update(bool was_pressed, int x, int y) override;
205 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
206
207 private:
208 void draw_motor_stats(
209 const std::string& name, vex::motor& mot, unsigned int frame, int x, int y,
210 vex::brain::lcd& scr
211 );
212
213 std::map<std::string, vex::motor&> motors;
214 static const int y_start = 0;
215 static const int per_column = 4;
216 static const int row_height = 20;
217 static const int row_width = 200;
218};
219
224class OdometryPage : public ScreenPage {
225 public:
233 OdometryPage(OdometryBase& odom, double robot_width, double robot_height, bool do_trail);
235 void update(bool was_pressed, int x, int y) override;
237 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
238
239 private:
240 static const int path_len = 40;
241 static constexpr char const* field_filename = "vex_field_240p.png";
242
243 OdometryBase& odom;
244 double robot_width;
245 double robot_height;
246 uint8_t* buf = nullptr;
247 int buf_size = 0;
248 Pose2d path[path_len];
249 int path_index = 0;
250 bool do_trail;
251 GraphDrawer velocity_graph;
252};
253
256class FunctionPage : public ScreenPage {
257 public:
262 FunctionPage(update_func_t update_f, draw_func_t draw_t);
264 void update(bool was_pressed, int x, int y) override;
266 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
267
268 private:
269 update_func_t update_f;
270 draw_func_t draw_f;
271};
272
274class PIDPage : public ScreenPage {
275 public:
281 PIDPage(PID& pid, std::string name, std::function<void(void)> onchange = []() {});
282 PIDPage(PIDFF& pidff, std::string name, std::function<void(void)> onchange = []() {});
283
285 void update(bool was_pressed, int x, int y) override;
287 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
288
289 private:
291 void zero_d_f() { pid.kd = 0; }
293 void zero_i_f() { pid.ki = 0; }
294
295 PID& pid;
296 const std::string name;
297 std::function<void(void)> onchange;
298
299 SliderWidget p_slider;
300 SliderWidget i_slider;
301 SliderWidget d_slider;
302 ButtonWidget zero_i;
303 ButtonWidget zero_d;
304
305 GraphDrawer graph;
306};
307
310 public:
315 InitializerPage(const Initializer& initializer, size_t starting_index = 0);
316
318 void update(bool was_pressed, int x, int y) override;
320 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
321
323 static InitializerPage* Next();
324
327 static size_t selector();
328
334 inline static std::function<Selector::selector_t> timed_selector(
335 unsigned int seconds, size_t fallback = DEFAULT_CANCELATION_INDEX
336 ) {
337 return Selector::timeout(selector, seconds * 1000000, fallback, cancel);
338 }
339
343 static void cancel(size_t selected);
344
347
348 private:
350 inline static size_t selection_buffer = Selector::NO_SELECTION_INDEX;
351
352 const Initializer& initializer;
353 const size_t starting_index;
354 inline static InitializerPage* latest_page = nullptr;
355
356 const static std::array<Rect, 8> buttons;
357};
Widget that does something when you tap it. The function is only called once when you first tap it.
Definition screen_pages.h:35
ButtonWidget(std::function< void(void)> onpress, Rect rect, std::string name)
Create a Button widget.
Definition screen_pages.h:41
bool update(bool was_pressed, int x, int y)
responds to user input
Definition screen_pages.cpp:249
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
draws the button to the screen
Definition screen_pages.cpp:259
ButtonWidget(void(*onpress)(), Rect rect, std::string name)
Create a Button widget.
Definition screen_pages.h:47
FunctionPage(update_func_t update_f, draw_func_t draw_t)
Creates a function page.
Definition screen_pages.cpp:32
void update(bool was_pressed, int x, int y) override
update uses the supplied update function to update this page
Definition screen_pages.cpp:35
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
draw uses the supplied draw function to draw to the screen
Definition screen_pages.cpp:37
void update(bool was_pressed, int x, int y) override
Definition screen_pages.cpp:333
static size_t selector()
When using InitializerPage to select an Initialization, use this as the raw selector function.
Definition screen_pages.cpp:412
InitializerPage(const Initializer &initializer, size_t starting_index=0)
Creates an InitializerPage.
Definition screen_pages.cpp:313
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen_pages.cpp:346
static void cancel(size_t selected)
When using a selector function wrapper that may cancel or otherwise cause the InitializerPage's selec...
Definition screen_pages.cpp:422
static std::function< Selector::selector_t > timed_selector(unsigned int seconds, size_t fallback=DEFAULT_CANCELATION_INDEX)
When using InitializerPage wrapped by a timeout, call this to generate the desired selector function.
Definition screen_pages.h:334
static const size_t DEFAULT_CANCELATION_INDEX
The default selected index if a cancelation occured during selection.
Definition screen_pages.h:346
static InitializerPage * Next()
Creates an InitializerPage that renders the following Initializations from the last.
Definition screen_pages.cpp:318
Definition initializer.h:194
Definition odometry_base.h:27
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen_pages.cpp:123
void update(bool was_pressed, int x, int y) override
Definition screen_pages.cpp:197
OdometryPage(OdometryBase &odom, double robot_width, double robot_height, bool do_trail)
Create an odometry trail. Make sure odometry is initilized before now.
Definition screen_pages.cpp:103
void update(bool was_pressed, int x, int y) override
Definition screen_pages.cpp:281
PIDPage(PID &pid, std::string name, std::function< void(void)> onchange=[]() {})
Create a PIDPage.
Definition screen_pages.cpp:270
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen_pages.cpp:293
Definition pid.h:20
Class representing a lattice point.
Definition point2d.h:7
constexpr int x() const
Gets the X coordinate of the point.
Definition point2d.h:30
constexpr int y() const
Gets the Y coordinate of the point.
Definition point2d.h:36
Definition pose2d.h:21
Page describes one part of the screen slideshow.
Definition screen_pages.h:147
virtual void draw(vex::brain::lcd &screen, bool first_draw, unsigned int frame_number)
draw stored data to the screen (runs at 10 hz and only runs if this page is in front)
virtual void update(bool was_pressed, int x, int y)
collect data, respond to screen input, do fast things (runs at 50hz even if you're not focused on thi...
Widget that updates a double value. Updates by reference so watch out for race conditions cuz the scr...
Definition screen_pages.h:68
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
Page::draws the slide to the screen
Definition screen_pages.cpp:217
bool update(bool was_pressed, int x, int y)
responds to user input
Definition screen_pages.cpp:203
SliderWidget(double &val, double low, double high, Rect rect, std::string name)
Creates a slider widget.
Definition screen_pages.h:76
StatsPage(std::map< std::string, vex::motor & > motors)
Creates a stats page.
Definition screen_pages.cpp:41
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen_pages.cpp:74
void update(bool was_pressed, int x, int y) override
Definition screen_pages.cpp:42
std::function< selector_t > timeout(std::function< selector_t > selector, uint64_t microsec, unsigned int fallback=NO_SELECTION_INDEX, std::function< void(size_t)> cancel=nullptr)
Definition initializer.h:102
constexpr size_t NO_SELECTION_INDEX
A constant index that is used in selector functions to represent when a selection has not yet been ma...
Definition initializer.h:43
Describes a Rectangle with a minimum and maximum point.
Definition screen_pages.h:18