RIT VEXU Core API
Loading...
Searching...
No Matches
legacy.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/utils/controls/pid.h"
11#include "core/utils/controls/pidff.h"
12#include "core/utils/graph_drawer.h"
13#include "core/utils/math/geometry/point2d.h"
14#include "core/utils/math/geometry/pose2d.h"
15#include "vex/vex.h"
16
17namespace LegacyScreen {
18
20struct Rect {
21 Point2d min;
22 Point2d max;
23 static Rect from_min_and_size(Point2d min, Point2d size) { return {min, min + size}; }
24 Point2d dimensions() const { return max - min; }
25 Point2d center() const { return Point2d{(min + max).x() / 2, (min + max).y() / 2}; }
26 double width() const { return max.x() - min.x(); }
27 double height() const { return max.y() - min.y(); }
28 bool contains(Point2d p) const {
29 bool xin = p.x() > min.x() && p.x() < max.x();
30 bool yin = p.y() > min.y() && p.y() < max.y();
31 return xin && yin;
32 }
33};
34
38 public:
43 ButtonWidget(std::function<void(void)> onpress, Rect rect, std::string name)
44 : onpress(onpress), rect(rect), name(name) {}
45
49 ButtonWidget(void (*onpress)(), Rect rect, std::string name)
50 : onpress(onpress), rect(rect), name(name) {}
51
57 bool update(bool was_pressed, int x, int y);
59 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number);
60
61 private:
62 std::function<void(void)> onpress;
63 Rect rect;
64 std::string name = "";
65 bool was_pressed_last = false;
66};
67
71 public:
78 SliderWidget(double& val, double low, double high, Rect rect, std::string name)
79 : value(val), low(low), high(high), rect(rect), name(name) {}
80
86 bool update(bool was_pressed, int x, int y);
88 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number);
89
90 private:
91 double& value;
92
93 double low;
94 double high;
95
96 Rect rect;
97 std::string name = "";
98};
99
100struct WidgetConfig;
101
102struct SliderConfig {
103 double& val;
104 double low;
105 double high;
106};
107struct ButtonConfig {
108 std::function<void()> onclick;
109};
110struct CheckboxConfig {
111 std::function<void(bool)> onupdate;
112};
113struct LabelConfig {
114 std::string label;
115};
116
117struct TextConfig {
118 std::function<std::string()> text;
119};
120struct SizedWidget {
121 int size;
122 WidgetConfig& widget;
123};
124struct WidgetConfig {
125 enum Type {
126 Col,
127 Row,
128 Slider,
129 Button,
130 Checkbox,
131 Label,
132 Text,
133 Graph,
134 };
135 Type type;
136 union {
137 std::vector<SizedWidget> widgets;
138 SliderConfig slider;
139 ButtonConfig button;
140 CheckboxConfig checkbox;
141 LabelConfig label;
142 TextConfig text;
143 GraphDrawer* graph;
144 } config;
145};
146
147class Page;
149class Page {
150 public:
151 virtual ~Page() = default;
160 virtual void update(bool was_pressed, int x, int y);
168 virtual void draw(vex::brain::lcd& screen, bool first_draw, unsigned int frame_number);
169};
170
171struct ScreenRect {
172 uint32_t x1;
173 uint32_t y1;
174 uint32_t x2;
175 uint32_t y2;
176};
177void draw_widget(WidgetConfig& widget, ScreenRect rect);
178
179class WidgetPage : public Page {
180 public:
181 WidgetPage(WidgetConfig& cfg) : base_widget(cfg) {}
182 void update(bool was_pressed, int x, int y) override;
183
184 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override {
185 draw_widget(base_widget, {.x1 = 20, .y1 = 0, .x2 = 440, .y2 = 240});
186 }
187
188 private:
189 WidgetConfig& base_widget;
190};
191
193using update_func_t = std::function<void(bool, int, int)>;
194
196using draw_func_t = std::function<void(vex::brain::lcd& screen, bool, unsigned int)>;
197
199class StatsPage : public Page {
200 public:
203 StatsPage(std::map<std::string, vex::motor&> motors);
205 void update(bool was_pressed, int x, int y) override;
207 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
208
209 private:
210 void draw_motor_stats(
211 const std::string& name, vex::motor& mot, unsigned int frame, int x, int y,
212 vex::brain::lcd& scr
213 );
214
215 std::map<std::string, vex::motor&> motors;
216 static const int y_start = 0;
217 static const int per_column = 4;
218 static const int row_height = 20;
219 static const int row_width = 200;
220};
221
226class OdometryPage : public Page {
227 public:
235 OdometryPage(OdometryBase& odom, double robot_width, double robot_height, bool do_trail);
237 void update(bool was_pressed, int x, int y) override;
239 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
240
241 private:
242 static const int path_len = 40;
243 static constexpr char const* field_filename = "vex_field_240p.png";
244
245 OdometryBase& odom;
246 double robot_width;
247 double robot_height;
248 uint8_t* buf = nullptr;
249 int buf_size = 0;
250 Pose2d path[path_len];
251 int path_index = 0;
252 bool do_trail;
253 GraphDrawer velocity_graph;
254};
255
258class FunctionPage : public Page {
259 public:
264 FunctionPage(update_func_t update_f, draw_func_t draw_t);
266 void update(bool was_pressed, int x, int y) override;
268 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
269
270 private:
271 update_func_t update_f;
272 draw_func_t draw_f;
273};
274
276class PIDPage : public Page {
277 public:
283 PIDPage(PID& pid, std::string name, std::function<void(void)> onchange = []() {});
284 PIDPage(PIDFF& pidff, std::string name, std::function<void(void)> onchange = []() {});
285
287 void update(bool was_pressed, int x, int y) override;
289 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
290
291 private:
293 void zero_d_f() { pid.kd = 0; }
295 void zero_i_f() { pid.ki = 0; }
296
297 PID& pid;
298 const std::string name;
299 std::function<void(void)> onchange;
300
301 SliderWidget p_slider;
302 SliderWidget i_slider;
303 SliderWidget d_slider;
304 ButtonWidget zero_i;
305 ButtonWidget zero_d;
306
307 GraphDrawer graph;
308};
309
311class InitializerPage : public Page {
312 public:
317 InitializerPage(const Initializer& initializer, size_t starting_index = 0);
318
320 void update(bool was_pressed, int x, int y) override;
322 void draw(vex::brain::lcd&, bool first_draw, unsigned int frame_number) override;
323
325 static InitializerPage* Next();
326
329 static size_t selector();
330
336 inline static std::function<Selector::selector_t> timed_selector(
337 unsigned int seconds, size_t fallback = DEFAULT_CANCELATION_INDEX
338 ) {
339 return Selector::timeout(selector, seconds * 1000000, fallback, cancel);
340 }
341
345 static void cancel(size_t selected);
346
349
350 private:
352 inline static size_t selection_buffer = Selector::NO_SELECTION_INDEX;
353
354 const Initializer& initializer;
355 const size_t starting_index;
356 inline static InitializerPage* latest_page = nullptr;
357
358 const static std::array<Rect, 8> buttons;
359};
360
361} // namespace LegacyScreen
Definition initializer.h:194
Widget that does something when you tap it. The function is only called once when you first tap it.
Definition legacy.h:37
ButtonWidget(std::function< void(void)> onpress, Rect rect, std::string name)
Create a Button widget.
Definition legacy.h:43
ButtonWidget(void(*onpress)(), Rect rect, std::string name)
Create a Button widget.
Definition legacy.h:49
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
draws the button to the screen
Definition legacy.cpp:259
bool update(bool was_pressed, int x, int y)
responds to user input
Definition legacy.cpp:249
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 legacy.cpp:37
void update(bool was_pressed, int x, int y) override
update uses the supplied update function to update this page
Definition legacy.cpp:35
FunctionPage(update_func_t update_f, draw_func_t draw_t)
Creates a function page.
Definition legacy.cpp:32
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition legacy.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 legacy.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 legacy.h:336
InitializerPage(const Initializer &initializer, size_t starting_index=0)
Creates an InitializerPage.
Definition legacy.cpp:313
void update(bool was_pressed, int x, int y) override
Definition legacy.cpp:333
static size_t selector()
When using InitializerPage to select an Initialization, use this as the raw selector function.
Definition legacy.cpp:412
static const size_t DEFAULT_CANCELATION_INDEX
The default selected index if a cancelation occured during selection.
Definition legacy.h:348
static InitializerPage * Next()
Creates an InitializerPage that renders the following Initializations from the last.
Definition legacy.cpp:318
OdometryPage(OdometryBase &odom, double robot_width, double robot_height, bool do_trail)
Create an odometry trail. Make sure odometry is initilized before now.
Definition legacy.cpp:103
void update(bool was_pressed, int x, int y) override
Definition legacy.cpp:197
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition legacy.cpp:123
void update(bool was_pressed, int x, int y) override
Definition legacy.cpp:281
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition legacy.cpp:293
PIDPage(PID &pid, std::string name, std::function< void(void)> onchange=[]() {})
Create a PIDPage.
Definition legacy.cpp:270
Page describes one part of the screen slideshow.
Definition legacy.h:149
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...
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)
Widget that updates a double value. Updates by reference so watch out for race conditions cuz the scr...
Definition legacy.h:70
bool update(bool was_pressed, int x, int y)
responds to user input
Definition legacy.cpp:203
SliderWidget(double &val, double low, double high, Rect rect, std::string name)
Creates a slider widget.
Definition legacy.h:78
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
Page::draws the slide to the screen
Definition legacy.cpp:217
void update(bool was_pressed, int x, int y) override
Definition legacy.cpp:42
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition legacy.cpp:74
StatsPage(std::map< std::string, vex::motor & > motors)
Creates a stats page.
Definition legacy.cpp:41
Definition odometry_base.h:30
Definition pid.h:21
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
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 legacy.h:20