This repository was archived by the owner on Nov 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgraph.rs
More file actions
382 lines (337 loc) · 10.9 KB
/
graph.rs
File metadata and controls
382 lines (337 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// MakAir Control UI
//
// Copyright: 2020, Makers For Life
// License: Public Domain License
use std::ops::Range;
use chrono::{DateTime, Utc};
use conrod_core::{
color,
widget::{self, Id as WidgetId},
Positionable, Sizeable, Widget,
};
use makair_telemetry::structures::MachineStateSnapshot;
use plotters::prelude::*;
use plotters::style::{Color, ShapeStyle, TextStyle};
use plotters_conrod::{ConrodBackend, ConrodBackendReusableGraph};
use crate::chip::{ChipData, ChipState};
use crate::config::environment::*;
use crate::display::widget::ControlWidget;
use crate::APP_I18N;
const GRAPH_PRESSURE_LINE_COLOR: RGBColor = plotters::style::RGBColor(0, 196, 255);
const GRAPH_FLOW_LINE_COLOR: RGBColor = plotters::style::RGBColor(196, 37, 20);
const GRAPH_MESH_AXIS_COLOR_RGB: RGBColor = plotters::style::RGBColor(255, 255, 255);
const GRAPH_MESH_AXIS_COLOR_ALPHA: f64 = 0.5;
const GRAPH_MESH_BOLD_COLOR_RGB: RGBColor = plotters::style::RGBColor(255, 255, 255);
const GRAPH_MESH_BOLD_COLOR_ALPHA: f64 = 0.22;
const GRAPH_MESH_LIGHT_COLOR_RGB: RGBColor = plotters::style::RGBColor(0, 0, 0);
const GRAPH_AXIS_Y_FONT_COLOR_RGB: RGBColor = plotters::style::RGBColor(255, 255, 255);
const GRAPH_AXIS_Y_FONT_COLOR_ALPHA: f64 = 0.75;
const GRAPH_SATURATE_COLOR: color::Color =
color::Color::Rgba(184.0 / 255.0, 1.0 / 255.0, 24.0 / 255.0, 1.0);
pub struct Config<'a> {
pub width: f64,
pub height: f64,
pub parent: WidgetId,
pub wrapper_id: WidgetId,
pub pressure_id: WidgetId,
pub flow_id: WidgetId,
pub pressure_label_box_id: WidgetId,
pub pressure_label_text_id: WidgetId,
pub flow_label_box_id: WidgetId,
pub flow_label_text_id: WidgetId,
pub pressure_saturate_ids: (WidgetId, WidgetId),
pub flow_saturate_ids: (WidgetId, WidgetId),
pub boot_time: Option<DateTime<Utc>>,
pub last_tick: Option<u64>,
pub data_pressure: &'a ChipData,
pub data_flow: &'a ChipData,
pub chip_state: &'a ChipState,
pub machine_snapshot: &'a MachineStateSnapshot,
pub plot_graphs: &'a mut (ConrodBackendReusableGraph, ConrodBackendReusableGraph),
}
struct PlotContext<'a, 'b> {
value_range: Range<i32>,
plot_id: WidgetId,
precision_divide: i32,
line_color: &'a RGBColor,
data_values: &'b ChipData,
}
lazy_static! {
static ref GRAPH_AXIS_Y_FONT: TextStyle<'static> =
TextStyle::from(("sans-serif", GRAPH_DRAW_AXIS_FONT_SIZE).into_font());
}
pub fn render<'a>(master: &mut ControlWidget<'a>, mut config: Config<'a>) -> f64 {
// Create wrapper
gen_widget_container!(
master,
container_id: config.wrapper_id,
color: color::TRANSPARENT,
width: config.width as _,
height: config.height as _,
positions: top_left_of[
config.parent,
]
);
// Acquire common graph size
let size = (config.width, (config.height - GRAPH_SPACING) / 2.0);
// Acquire graph reference time
// Notice: to prevent the graph from progressing in time periodically when stopped, use the \
// latest pressure data as a reference (will also be used for the flow graph in that case). \
// Using the last tick otherwise gives out much smoother graph results at high FPS, with \
// less visual jitter.
let reference_time = if config.chip_state == &ChipState::Running {
config.boot_time.map(|boot_time| {
boot_time + chrono::Duration::microseconds(config.last_tick.unwrap_or(0) as i64)
})
} else {
config
.data_pressure
.points
.front()
.map(|pressure| pressure.0)
};
// Acquire common graph time range
let newest_time = reference_time.unwrap_or_else(Utc::now);
let oldest_time = newest_time - chrono::Duration::seconds(GRAPH_DRAW_SECONDS);
// Draw plots
pressure(master, &mut config, size, oldest_time..newest_time);
flow(master, &mut config, size, oldest_time..newest_time);
config.width
}
fn pressure<'a>(
master: &mut ControlWidget<'a>,
config: &mut Config<'a>,
size: (f64, f64),
time_range: Range<DateTime<Utc>>,
) {
// Create pressure container
gen_widget_container!(
master,
container_id: config.pressure_id,
color: color::TRANSPARENT,
width: size.0,
height: size.1,
positions: top_left_of[
config.wrapper_id,
]
);
// Draw pressure plot
plot(
master,
size,
time_range.clone(),
&mut config.plot_graphs.0,
PlotContext {
value_range: GRAPH_DRAW_PRESSURE_RANGE_LOW_PRECISION_DIVIDED
..GRAPH_DRAW_PRESSURE_RANGE_HIGH_PRECISION_DIVIDED,
plot_id: config.pressure_id,
precision_divide: TELEMETRY_POINTS_PRESSURE_PRECISION_DIVIDE,
line_color: &GRAPH_PRESSURE_LINE_COLOR,
data_values: &config.data_pressure,
},
);
// Create label box
label(
master,
config.pressure_id,
config.pressure_label_box_id,
config.pressure_label_text_id,
&APP_I18N.t("telemetry-unit-cmh2o"),
);
// Create saturate lines? (as needed)
if config.chip_state == &ChipState::Running {
saturate(
master,
size,
time_range,
config.pressure_id,
config.pressure_saturate_ids,
&config.data_pressure,
);
}
}
fn flow<'a>(
master: &mut ControlWidget<'a>,
config: &mut Config<'a>,
size: (f64, f64),
time_range: Range<DateTime<Utc>>,
) {
// Create flow container
gen_widget_container!(
master,
container_id: config.flow_id,
color: color::TRANSPARENT,
width: size.0,
height: size.1,
positions: bottom_left_of[
config.wrapper_id,
]
);
// Draw flow plot
plot(
master,
size,
time_range.clone(),
&mut config.plot_graphs.1,
PlotContext {
value_range: GRAPH_DRAW_FLOW_RANGE_LOW_PRECISION_DIVIDED
..GRAPH_DRAW_FLOW_RANGE_HIGH_PRECISION_DIVIDED,
plot_id: config.flow_id,
precision_divide: TELEMETRY_POINTS_FLOW_PRECISION_DIVIDE,
line_color: &GRAPH_FLOW_LINE_COLOR,
data_values: &config.data_flow,
},
);
// Create label box
label(
master,
config.flow_id,
config.flow_label_box_id,
config.flow_label_text_id,
&APP_I18N.t("telemetry-unit-lpm"),
);
// Create saturate lines? (as needed)
if config.chip_state == &ChipState::Running {
saturate(
master,
size,
time_range,
config.flow_id,
config.flow_saturate_ids,
&config.data_flow,
);
}
}
fn plot<'a>(
master: &mut ControlWidget<'a>,
size: (f64, f64),
time_range: Range<DateTime<Utc>>,
plot_graph: &mut ConrodBackendReusableGraph,
context: PlotContext,
) {
// Create drawing backend
let drawing = ConrodBackend::new(
&mut master.ui,
(size.0 as u32, size.1 as u32),
context.plot_id,
master.fonts.regular,
plot_graph,
)
.into_drawing_area();
// Configure chart
let mut chart = ChartBuilder::on(&drawing)
.margin_top(GRAPH_DRAW_MARGIN_TOP)
.margin_bottom(GRAPH_DRAW_MARGIN_BOTTOM)
.margin_left(GRAPH_DRAW_MARGIN_LEFT)
.margin_right(GRAPH_DRAW_MARGIN_RIGHT)
.x_label_area_size(0)
.y_label_area_size(GRAPH_DRAW_LABEL_WIDTH)
.build_cartesian_2d(time_range, context.value_range)
.expect("failed to build chart");
// Configure mesh
let precision_divide = context.precision_divide;
chart
.configure_mesh()
.bold_line_style(&GRAPH_MESH_BOLD_COLOR_RGB.mix(GRAPH_MESH_BOLD_COLOR_ALPHA))
.light_line_style(&GRAPH_MESH_LIGHT_COLOR_RGB)
.axis_style(ShapeStyle {
color: GRAPH_MESH_AXIS_COLOR_RGB.mix(GRAPH_MESH_AXIS_COLOR_ALPHA),
filled: true,
stroke_width: GRAPH_DRAW_AXIS_SIZE,
})
.y_labels(GRAPH_DRAW_LABEL_NUMBER_MAX)
.y_label_style(
GRAPH_AXIS_Y_FONT
.color(&GRAPH_AXIS_Y_FONT_COLOR_RGB.mix(GRAPH_AXIS_Y_FONT_COLOR_ALPHA)),
)
.y_label_formatter(&|y| {
// Convert high-precision point to low-precision point (which measurements & targets \
// both use), eg. cL to L or mmH2O to cmH2O.
(y / precision_divide).to_string()
})
.draw()
.expect("failed to draw chart mesh");
// Draw plot
chart
.draw_series(
AreaSeries::new(
context.data_values.points.iter().map(|x| (x.0, x.1 as i32)),
0,
&context.line_color.mix(0.3),
)
.border_style(ShapeStyle::from(context.line_color).stroke_width(GRAPH_DRAW_LINE_SIZE)),
)
.expect("failed to draw chart data");
}
fn label<'a>(
master: &mut ControlWidget<'a>,
parent_id: WidgetId,
box_id: WidgetId,
text_id: WidgetId,
text: &str,
) {
// Draw label box
gen_widget_container!(
master,
container_id: box_id,
color: color::BLACK,
width: GRAPH_LABEL_BOX_WIDTH,
height: GRAPH_LABEL_BOX_HEIGHT,
positions: top_left_of[
parent_id,
]
);
// Draw label text
let mut text_style = conrod_core::widget::primitive::text::Style::default();
text_style.font_id = Some(Some(master.fonts.bold));
text_style.color = Some(color::WHITE);
text_style.font_size = Some(GRAPH_LABEL_BOX_FONT_SIZE);
widget::Text::new(text)
.mid_top_with_margin_on(box_id, 5.0)
.with_style(text_style)
.set(text_id, &mut master.ui);
}
fn saturate<'a>(
master: &mut ControlWidget<'a>,
size: (f64, f64),
time_range: Range<DateTime<Utc>>,
parent_id: WidgetId,
saturate_ids: (WidgetId, WidgetId),
data_values: &ChipData,
) {
// Check if should draw saturation indicators
if data_values
.bounds_low
.map(|bounds| bounds.0 >= time_range.start)
== Some(true)
{
widget::Rectangle::fill_with(
[
size.0 - GRAPH_DRAW_LABEL_WIDTH as f64,
GRAPH_SATURATE_LINE_THICKNESS,
],
GRAPH_SATURATE_COLOR,
)
.bottom_left_with_margins_on(
parent_id,
GRAPH_SPACING + GRAPH_SATURATE_LINE_THICKNESS,
GRAPH_DRAW_LABEL_WIDTH as _,
)
.set(saturate_ids.0, &mut master.ui);
}
if data_values
.bounds_high
.map(|bounds| bounds.0 >= time_range.start)
== Some(true)
{
widget::Rectangle::fill_with(
[
size.0 - GRAPH_DRAW_LABEL_WIDTH as f64,
GRAPH_SATURATE_LINE_THICKNESS,
],
GRAPH_SATURATE_COLOR,
)
.top_left_with_margins_on(parent_id, -1.0, GRAPH_DRAW_LABEL_WIDTH as _)
.set(saturate_ids.1, &mut master.ui);
}
}