RIT VEXU Core API
Loading...
Searching...
No Matches
legacy.h
1#pragma once
2#include "core/subsystems/odometry/odometry_base.h"
3#include "core/utils/controls/pid.h"
4#include "core/utils/controls/pidff.h"
5#include "core/utils/graph_drawer.h"
6#include "core/utils/math/geometry/pose2d.h"
7#include "core/utils/math/geometry/translation2d.h"
8#include "core/utils/initializer.h"
9#include "vex.h"
10#include <cassert>
11#include <functional>
12#include <map>
13#include <vector>
14
15namespace LegacyScreen {
18 public:
23 ButtonWidget(std::function<void(void)> onpress, Rect rect, std::string name)
24 : onpress(onpress), rect(rect), name(name) {}
25
29 ButtonWidget(void (*onpress)(), Rect rect, std::string name) : onpress(onpress), rect(rect), name(name) {}
30
36 bool update(bool was_pressed, int x, int y);
38 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number);
39
40 private:
41 std::function<void(void)> onpress;
42 Rect rect;
43 std::string name = "";
44 bool was_pressed_last = false;
45};
46
50 public:
57 SliderWidget(double &val, double low, double high, Rect rect, std::string name)
58 : value(val), low(low), high(high), rect(rect), name(name) {}
59
65 bool update(bool was_pressed, int x, int y);
67 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number);
68
69 private:
70 double &value;
71
72 double low;
73 double high;
74
75 Rect rect;
76 std::string name = "";
77};
78
79struct WidgetConfig;
80
81struct SliderConfig {
82 double &val;
83 double low;
84 double high;
85};
86struct ButtonConfig {
87 std::function<void()> onclick;
88};
89struct CheckboxConfig {
90 std::function<void(bool)> onupdate;
91};
92struct LabelConfig {
93 std::string label;
94};
95
96struct TextConfig {
97 std::function<std::string()> text;
98};
99struct SizedWidget {
100 int size;
101 WidgetConfig &widget;
102};
103struct WidgetConfig {
104 enum Type {
105 Col,
106 Row,
107 Slider,
108 Button,
109 Checkbox,
110 Label,
111 Text,
112 Graph,
113 };
114 Type type;
115 union {
116 std::vector<SizedWidget> widgets;
117 SliderConfig slider;
118 ButtonConfig button;
119 CheckboxConfig checkbox;
120 LabelConfig label;
121 TextConfig text;
122 GraphDrawer *graph;
123 } config;
124};
125
126class Page;
128class Page {
129 public:
130 virtual ~Page() = default;
139 virtual void update(bool was_pressed, int x, int y);
147 virtual void draw(vex::brain::lcd &screen, bool first_draw, unsigned int frame_number);
148};
149
150struct ScreenRect {
151 uint32_t x1;
152 uint32_t y1;
153 uint32_t x2;
154 uint32_t y2;
155};
156void draw_widget(WidgetConfig &widget, ScreenRect rect);
157
158class WidgetPage : public Page {
159 public:
160 WidgetPage(WidgetConfig &cfg) : base_widget(cfg) {}
161 void update(bool was_pressed, int x, int y) override;
162
163 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override {
164 draw_widget(base_widget, {.x1 = 20, .y1 = 0, .x2 = 440, .y2 = 240});
165 }
166
167 private:
168 WidgetConfig &base_widget;
169};
170
172using update_func_t = std::function<void(bool, int, int)>;
173
175using draw_func_t = std::function<void(vex::brain::lcd &screen, bool, unsigned int)>;
176
178class StatsPage : public Page {
179 public:
182 StatsPage(std::map<std::string, vex::motor &> motors);
184 void update(bool was_pressed, int x, int y) override;
186 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
187
188 private:
189 void
190 draw_motor_stats(const std::string &name, vex::motor &mot, unsigned int frame, int x, int y, vex::brain::lcd &scr);
191
192 std::map<std::string, vex::motor &> motors;
193 static const int y_start = 0;
194 static const int per_column = 4;
195 static const int row_height = 20;
196 static const int row_width = 200;
197};
198
202class OdometryPage : public Page {
203 public:
210 OdometryPage(OdometryBase &odom, double robot_width, double robot_height, bool do_trail);
212 void update(bool was_pressed, int x, int y) override;
214 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
215
216 private:
217 static const int path_len = 40;
218 static constexpr char const *field_filename = "vex_field_240p.png";
219
220 OdometryBase &odom;
221 double robot_width;
222 double robot_height;
223 uint8_t *buf = nullptr;
224 int buf_size = 0;
225 Pose2d path[path_len];
226 int path_index = 0;
227 bool do_trail;
228 GraphDrawer velocity_graph;
229};
230
233class FunctionPage : public Page {
234 public:
238 FunctionPage(update_func_t update_f, draw_func_t draw_t);
240 void update(bool was_pressed, int x, int y) override;
242 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
243
244 private:
245 update_func_t update_f;
246 draw_func_t draw_f;
247};
248
250class PIDPage : public Page {
251 public:
257 PIDPage(PID &pid, std::string name, std::function<void(void)> onchange = []() {});
258 PIDPage(PIDFF &pidff, std::string name, std::function<void(void)> onchange = []() {});
259
261 void update(bool was_pressed, int x, int y) override;
263 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
264
265 private:
267 void zero_d_f() { cfg.d = 0; }
269 void zero_i_f() { cfg.i = 0; }
270
272 PID &pid;
273 const std::string name;
274 std::function<void(void)> onchange;
275
276 SliderWidget p_slider;
277 SliderWidget i_slider;
278 SliderWidget d_slider;
279 ButtonWidget zero_i;
280 ButtonWidget zero_d;
281
282 GraphDrawer graph;
283};
284
286class InitializerPage : public Page {
287 public:
291 InitializerPage(const Initializer &initializer, size_t starting_index = 0);
292
294 void update(bool was_pressed, int x, int y) override;
296 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
297
299 static InitializerPage* Next();
300
302 static size_t selector();
303
308 inline static std::function<Selector::selector_t> timed_selector(unsigned int seconds, size_t fallback = DEFAULT_CANCELATION_INDEX) {
309 return Selector::timeout(selector, seconds*1000000, fallback, cancel);
310 }
311
314 static void cancel(size_t selected);
315
318
319 private:
321 inline static size_t selection_buffer = Selector::NO_SELECTION_INDEX;
322
323 const Initializer &initializer;
324 const size_t starting_index;
325 inline static InitializerPage* latest_page = nullptr;
326
327 const static std::array<Rect, 8> buttons;
328};
329
330} // namespace screen
Definition initializer.h:195
Widget that does something when you tap it. The function is only called once when you first tap it.
Definition legacy.h:17
ButtonWidget(std::function< void(void)> onpress, Rect rect, std::string name)
Create a Button widget.
Definition legacy.h:23
ButtonWidget(void(*onpress)(), Rect rect, std::string name)
Create a Button widget.
Definition legacy.h:29
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
draws the button to the screen
Definition legacy.cpp:258
bool update(bool was_pressed, int x, int y)
responds to user input
Definition legacy.cpp:248
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:345
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:421
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:308
InitializerPage(const Initializer &initializer, size_t starting_index=0)
Creates an InitializerPage.
Definition legacy.cpp:312
void update(bool was_pressed, int x, int y) override
Definition legacy.cpp:332
static size_t selector()
When using InitializerPage to select an Initialization, use this as the raw selector function.
Definition legacy.cpp:411
static const size_t DEFAULT_CANCELATION_INDEX
The default selected index if a cancelation occured during selection.
Definition legacy.h:317
static InitializerPage * Next()
Creates an InitializerPage that renders the following Initializations from the last.
Definition legacy.cpp:317
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:196
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:280
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition legacy.cpp:292
PIDPage(PID &pid, std::string name, std::function< void(void)> onchange=[]() {})
Create a PIDPage.
Definition legacy.cpp:269
Page describes one part of the screen slideshow.
Definition legacy.h:128
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:49
bool update(bool was_pressed, int x, int y)
responds to user input
Definition legacy.cpp:202
SliderWidget(double &val, double low, double high, Rect rect, std::string name)
Creates a slider widget.
Definition legacy.h:57
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
Page::draws the slide to the screen
Definition legacy.cpp:216
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:31
Definition pid.h:21
Definition pose2d.h:24
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:104
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:45
Definition pid.h:41
Definition geometry.h:9