RIT VEXU Core API
Loading...
Searching...
No Matches
screen.h
1#pragma once
2#include "../core/include/subsystems/odometry/odometry_base.h"
3#include "../core/include/utils/controls/pid.h"
4#include "../core/include/utils/controls/pidff.h"
5#include "../core/include/utils/graph_drawer.h"
6#include "vex.h"
7#include <cassert>
8#include <functional>
9#include <map>
10#include <vector>
11
12namespace screen {
15public:
20 ButtonWidget(std::function<void(void)> onpress, Rect rect, std::string name)
21 : onpress(onpress), rect(rect), name(name) {}
26 ButtonWidget(void (*onpress)(), Rect rect, std::string name) : onpress(onpress), rect(rect), name(name) {}
27
33 bool update(bool was_pressed, int x, int y);
35 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number);
36
37private:
38 std::function<void(void)> onpress;
39 Rect rect;
40 std::string name = "";
41 bool was_pressed_last = false;
42};
43
47public:
54 SliderWidget(double &val, double low, double high, Rect rect, std::string name)
55 : value(val), low(low), high(high), rect(rect), name(name) {}
56
62 bool update(bool was_pressed, int x, int y);
64 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number);
65
66private:
67 double &value;
68
69 double low;
70 double high;
71
72 Rect rect;
73 std::string name = "";
74};
75
76struct WidgetConfig;
77
78struct SliderConfig {
79 double &val;
80 double low;
81 double high;
82};
83struct ButtonConfig {
84 std::function<void()> onclick;
85};
86struct CheckboxConfig {
87 std::function<void(bool)> onupdate;
88};
89struct LabelConfig {
90 std::string label;
91};
92
93struct TextConfig {
94 std::function<std::string()> text;
95};
96struct SizedWidget {
97 int size;
98 WidgetConfig &widget;
99};
100struct WidgetConfig {
101 enum Type {
102 Col,
103 Row,
104 Slider,
105 Button,
106 Checkbox,
107 Label,
108 Text,
109 Graph,
110 };
111 Type type;
112 union {
113 std::vector<SizedWidget> widgets;
114 SliderConfig slider;
115 ButtonConfig button;
116 CheckboxConfig checkbox;
117 LabelConfig label;
118 TextConfig text;
119 GraphDrawer *graph;
120 } config;
121};
122
123class Page;
125class Page {
126public:
135 virtual void update(bool was_pressed, int x, int y);
143 virtual void draw(vex::brain::lcd &screen, bool first_draw, unsigned int frame_number);
144};
145
146struct ScreenRect {
147 uint32_t x1;
148 uint32_t y1;
149 uint32_t x2;
150 uint32_t y2;
151};
152void draw_widget(WidgetConfig &widget, ScreenRect rect);
153
154class WidgetPage : public Page {
155public:
156 WidgetPage(WidgetConfig &cfg) : base_widget(cfg) {}
157 void update(bool was_pressed, int x, int y) override;
158
159 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override {
160 draw_widget(base_widget, {.x1 = 20, .y1 = 0, .x2 = 440, .y2 = 240});
161 }
162
163private:
164 WidgetConfig &base_widget;
165};
166
173void start_screen(vex::brain::lcd &screen, std::vector<Page *> pages, int first_page = 0);
174
175void next_page();
176void prev_page();
177void goto_page(size_t page);
178
180void stop_screen();
181
183using update_func_t = std::function<void(bool, int, int)>;
184
186using draw_func_t = std::function<void(vex::brain::lcd &screen, bool, unsigned int)>;
187
189class StatsPage : public Page {
190public:
193 StatsPage(std::map<std::string, vex::motor &> motors);
195 void update(bool was_pressed, int x, int y) override;
197 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
198
199private:
200 void draw_motor_stats(const std::string &name, vex::motor &mot, unsigned int frame, int x, int y,
201 vex::brain::lcd &scr);
202
203 std::map<std::string, vex::motor &> motors;
204 static const int y_start = 0;
205 static const int per_column = 4;
206 static const int row_height = 20;
207 static const int row_width = 200;
208};
209
213class OdometryPage : public Page {
214public:
221 OdometryPage(OdometryBase &odom, double robot_width, double robot_height, bool do_trail);
223 void update(bool was_pressed, int x, int y) override;
225 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
226
227private:
228 static const int path_len = 40;
229 static constexpr char const *field_filename = "vex_field_240p.png";
230
231 OdometryBase &odom;
232 double robot_width;
233 double robot_height;
234 uint8_t *buf = nullptr;
235 int buf_size = 0;
236 pose_t path[path_len];
237 int path_index = 0;
238 bool do_trail;
239 GraphDrawer velocity_graph;
240};
241
244class FunctionPage : public Page {
245public:
249 FunctionPage(update_func_t update_f, draw_func_t draw_t);
251 void update(bool was_pressed, int x, int y) override;
253 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
254
255private:
256 update_func_t update_f;
257 draw_func_t draw_f;
258};
259
261class PIDPage : public Page {
262public:
268 PIDPage(
269 PID &pid, std::string name, std::function<void(void)> onchange = []() {});
270 PIDPage(
271 PIDFF &pidff, std::string name, std::function<void(void)> onchange = []() {});
272
274 void update(bool was_pressed, int x, int y) override;
276 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
277
278private:
280 void zero_d_f() { cfg.d = 0; }
282 void zero_i_f() { cfg.i = 0; }
283
285 PID &pid;
286 const std::string name;
287 std::function<void(void)> onchange;
288
289 SliderWidget p_slider;
290 SliderWidget i_slider;
291 SliderWidget d_slider;
292 ButtonWidget zero_i;
293 ButtonWidget zero_d;
294
295 GraphDrawer graph;
296};
297
298} // namespace screen
Definition odometry_base.h:24
Definition pid.h:23
Widget that does something when you tap it. The function is only called once when you first tap it.
Definition screen.h:14
ButtonWidget(std::function< void(void)> onpress, Rect rect, std::string name)
Create a Button widget.
Definition screen.h:20
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
draws the button to the screen
Definition screen.cpp:383
bool update(bool was_pressed, int x, int y)
responds to user input
Definition screen.cpp:373
ButtonWidget(void(*onpress)(), Rect rect, std::string name)
Create a Button widget.
Definition screen.h:26
Simple page that stores no internal data. the draw and update functions use only global data rather t...
Definition screen.h:244
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.cpp:171
FunctionPage(update_func_t update_f, draw_func_t draw_t)
Creates a function page.
Definition screen.cpp:166
void update(bool was_pressed, int x, int y) override
update uses the supplied update function to update this page
Definition screen.cpp:169
a page that shows odometry position and rotation and a map (if an sd card with the file is on)
Definition screen.h:213
void update(bool was_pressed, int x, int y) override
Definition screen.cpp:321
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen.cpp:255
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.cpp:235
PIDPage provides a way to tune a pid controller on the screen.
Definition screen.h:261
void update(bool was_pressed, int x, int y) override
Definition screen.cpp:405
PIDPage(PID &pid, std::string name, std::function< void(void)> onchange=[]() {})
Create a PIDPage.
Definition screen.cpp:394
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen.cpp:417
Page describes one part of the screen slideshow.
Definition screen.h:125
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.h:46
bool update(bool was_pressed, int x, int y)
responds to user input
Definition screen.cpp:327
SliderWidget(double &val, double low, double high, Rect rect, std::string name)
Creates a slider widget.
Definition screen.h:54
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
Page::draws the slide to the screen
Definition screen.cpp:341
Draws motor stats and battery stats to the screen.
Definition screen.h:189
StatsPage(std::map< std::string, vex::motor & > motors)
Creates a stats page.
Definition screen.cpp:175
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen.cpp:207
void update(bool was_pressed, int x, int y) override
Definition screen.cpp:176
Definition pid.h:43
double d
derivitave coeffecient d * derivative(error)
Definition pid.h:46
double i
integral coeffecient i * integral(error)
Definition pid.h:45
Definition geometry.h:52