RIT VEXU Core API
Loading...
Searching...
No Matches
screen.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 "vex.h"
9#include <cassert>
10#include <functional>
11#include <map>
12#include <vector>
13
14namespace screen {
17 public:
22 ButtonWidget(std::function<void(void)> onpress, Rect rect, std::string name)
23 : onpress(onpress), rect(rect), name(name) {}
24
28 ButtonWidget(void (*onpress)(), Rect rect, std::string name) : onpress(onpress), rect(rect), name(name) {}
29
35 bool update(bool was_pressed, int x, int y);
37 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number);
38
39 private:
40 std::function<void(void)> onpress;
41 Rect rect;
42 std::string name = "";
43 bool was_pressed_last = false;
44};
45
49 public:
56 SliderWidget(double &val, double low, double high, Rect rect, std::string name)
57 : value(val), low(low), high(high), rect(rect), name(name) {}
58
64 bool update(bool was_pressed, int x, int y);
66 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number);
67
68 private:
69 double &value;
70
71 double low;
72 double high;
73
74 Rect rect;
75 std::string name = "";
76};
77
78struct WidgetConfig;
79
80struct SliderConfig {
81 double &val;
82 double low;
83 double high;
84};
85struct ButtonConfig {
86 std::function<void()> onclick;
87};
88struct CheckboxConfig {
89 std::function<void(bool)> onupdate;
90};
91struct LabelConfig {
92 std::string label;
93};
94
95struct TextConfig {
96 std::function<std::string()> text;
97};
98struct SizedWidget {
99 int size;
100 WidgetConfig &widget;
101};
102struct WidgetConfig {
103 enum Type {
104 Col,
105 Row,
106 Slider,
107 Button,
108 Checkbox,
109 Label,
110 Text,
111 Graph,
112 };
113 Type type;
114 union {
115 std::vector<SizedWidget> widgets;
116 SliderConfig slider;
117 ButtonConfig button;
118 CheckboxConfig checkbox;
119 LabelConfig label;
120 TextConfig text;
121 GraphDrawer *graph;
122 } config;
123};
124
125class Page;
127class Page {
128 public:
137 virtual void update(bool was_pressed, int x, int y);
145 virtual void draw(vex::brain::lcd &screen, bool first_draw, unsigned int frame_number);
146};
147
148struct ScreenRect {
149 uint32_t x1;
150 uint32_t y1;
151 uint32_t x2;
152 uint32_t y2;
153};
154void draw_widget(WidgetConfig &widget, ScreenRect rect);
155
156class WidgetPage : public Page {
157 public:
158 WidgetPage(WidgetConfig &cfg) : base_widget(cfg) {}
159 void update(bool was_pressed, int x, int y) override;
160
161 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override {
162 draw_widget(base_widget, {.x1 = 20, .y1 = 0, .x2 = 440, .y2 = 240});
163 }
164
165 private:
166 WidgetConfig &base_widget;
167};
168
175void start_screen(vex::brain::lcd &screen, std::vector<Page *> pages, int first_page = 0);
176
177void next_page();
178void prev_page();
179void goto_page(size_t page);
180
182void stop_screen();
183
185using update_func_t = std::function<void(bool, int, int)>;
186
188using draw_func_t = std::function<void(vex::brain::lcd &screen, bool, unsigned int)>;
189
191class StatsPage : public Page {
192 public:
195 StatsPage(std::map<std::string, vex::motor &> motors);
197 void update(bool was_pressed, int x, int y) override;
199 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
200
201 private:
202 void
203 draw_motor_stats(const std::string &name, vex::motor &mot, unsigned int frame, int x, int y, vex::brain::lcd &scr);
204
205 std::map<std::string, vex::motor &> motors;
206 static const int y_start = 0;
207 static const int per_column = 4;
208 static const int row_height = 20;
209 static const int row_width = 200;
210};
211
215class OdometryPage : public Page {
216 public:
223 OdometryPage(OdometryBase &odom, double robot_width, double robot_height, bool do_trail);
225 void update(bool was_pressed, int x, int y) override;
227 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
228
229 private:
230 static const int path_len = 40;
231 static constexpr char const *field_filename = "vex_field_240p.png";
232
233 OdometryBase &odom;
234 double robot_width;
235 double robot_height;
236 uint8_t *buf = nullptr;
237 int buf_size = 0;
238 Pose2d path[path_len];
239 int path_index = 0;
240 bool do_trail;
241 GraphDrawer velocity_graph;
242};
243
246class FunctionPage : public Page {
247 public:
251 FunctionPage(update_func_t update_f, draw_func_t draw_t);
253 void update(bool was_pressed, int x, int y) override;
255 void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override;
256
257 private:
258 update_func_t update_f;
259 draw_func_t draw_f;
260};
261
263class PIDPage : public Page {
264 public:
270 PIDPage(PID &pid, std::string name, std::function<void(void)> onchange = []() {});
271 PIDPage(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
278 private:
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:31
Definition core/utils/controls/pid.h:23
Definition pose2d.h:23
Widget that does something when you tap it. The function is only called once when you first tap it.
Definition screen.h:16
ButtonWidget(std::function< void(void)> onpress, Rect rect, std::string name)
Create a Button widget.
Definition screen.h:22
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
draws the button to the screen
Definition screen.cpp:388
bool update(bool was_pressed, int x, int y)
responds to user input
Definition screen.cpp:378
ButtonWidget(void(*onpress)(), Rect rect, std::string name)
Create a Button widget.
Definition screen.h:28
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
void update(bool was_pressed, int x, int y) override
Definition screen.cpp:326
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen.cpp:254
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:234
void update(bool was_pressed, int x, int y) override
Definition screen.cpp:410
PIDPage(PID &pid, std::string name, std::function< void(void)> onchange=[]() {})
Create a PIDPage.
Definition screen.cpp:399
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number) override
Definition screen.cpp:422
Page describes one part of the screen slideshow.
Definition screen.h:127
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:48
bool update(bool was_pressed, int x, int y)
responds to user input
Definition screen.cpp:332
SliderWidget(double &val, double low, double high, Rect rect, std::string name)
Creates a slider widget.
Definition screen.h:56
void draw(vex::brain::lcd &, bool first_draw, unsigned int frame_number)
Page::draws the slide to the screen
Definition screen.cpp:346
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 core/utils/controls/pid.h:43
Definition geometry.h:11