-
Notifications
You must be signed in to change notification settings - Fork 3
/
liveDataViewer.ts
553 lines (481 loc) · 24.2 KB
/
liveDataViewer.ts
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
namespace microcode {
/** The colours that will be used for the lines & sensor information boxes */
const SENSOR_COLORS: number[] = [2,3,4,6,7,9]
/** Time to wait inbetween updating each frame of the graph - new sensor reading requests are made.
* The rest of the UI is still responsive though - since UP, DOWN, LEFT, RIGHT movements will invoke .update() immediately.
*/
const GRAPH_FRAME_TIME_MS: number = 100
/**
* Used in sensors.draw()
* Neccessary to prevent graph overflowing in the case of extreme readings
*/
export const BUFFERED_SCREEN_HEIGHT = Screen.HEIGHT - (Screen.HEIGHT * 0.078125) // 10
/**
* Is the graph or the sensors being shown? Is the graph zoomed in on?
*/
enum GUI_STATE {
GRAPH,
/** The sensors are being shown */
SENSOR_SELECTION,
/** GUI Buffers are removed, sensor read buffer is increased */
ZOOMED_IN
}
/**
* Indice access alias into this.sensorMinsAndMaxs columns.
*/
enum MIN_MAX_COLUMNS {
MIN = 0,
MAX = 1
}
/**
* One of the 3 main functionalities of MicroData
* Allows for the live feed of a sensor to be plotted,
* Multiple sensors may be plotted at once
* Display modes may be toggled per sensor
*
*
* UI elements have been scaled to allow for Arcade Shields of different dimensions.
* Where this is the case the raw value for an Arcade Shield of Height 128 is commented alongside it.
*/
export class LiveDataViewer extends Scene {
/** Same as the Screen.HEIGHT. But reduced when entering GUI_STATE.ZOOMED_IN. */
private windowWidth: number
/** Same as the Screen.HEIGHT. But reduced when entering GUI_STATE.ZOOMED_IN. */
private windowHeight: number
/** Reduced when entering GUI_STATE.ZOOMED_IN */
private windowLeftBuffer: number
/** Reduced when entering GUI_STATE.ZOOMED_IN */
private windowRightBuffer: number
/** Reduced when entering GUI_STATE.ZOOMED_IN */
private windowTopBuffer: number
/** Reduced when entering GUI_STATE.ZOOMED_IN */
private windowBotBuffer: number
/** How much all UI elements should be offset by, adjusted when pressing up or down. */
private yScrollOffset: number
/** Maximum limit to how far yScrollOffset may travel, this is so the screen cannot go below the sensor information boxes */
private maxYScrollOffset: number
/** By how much should this.yScrollRate be adjusted by? */
private yScrollRate: number
/** Show the graph or show the sensor information below the graph? */
private guiState: GUI_STATE;
//--------------------------------
// Oscilloscope control variables:
//--------------------------------
/** Zoom in on X; adjust offsets to make this point centred, whilst zoomed */
private oscXCoordinate: number
/** Zoom in on Y; adjust offsets to make this point centred, whilst zoomed */
private oscReading: number
/** Which sensor is being zoomed in upon? */
private oscSensorIndex: number
/** That are being drawn */
private sensors: Sensor[]
/** Sensors can be turned on & off: only showSensors[n] == true are shown */
private drawSensorStates: boolean[];
/** Use the sensor minimum and maximum data to wwrite information about them below the plot */
private sensorMinsAndMaxs: number[][];
/** After scrolling past the plot the user can select a sensor to disable/enable */
private informationSensorIndex: number;
/** Lowest of sensor.minimum for all sensors: required to write at the bottom of the y-axis */
private globalSensorMinimum: number;
/** Greatest of sensor.maximum for all sensors: required to write at the top of the y-axis */
private globalSensorMaximum: number;
constructor(app: App, sensors: Sensor[]) {
super(app, "liveDataViewer")
this.backgroundColor = 3
this.windowWidth = Screen.WIDTH
this.windowHeight = Screen.HEIGHT
this.windowLeftBuffer = 38
this.windowRightBuffer = 10
this.windowTopBuffer = (Screen.HEIGHT * 0.0390) // At Screen.HEIGHT=128: 5
this.windowBotBuffer = (Screen.HEIGHT * 0.15625) // 20
this.guiState = GUI_STATE.GRAPH
this.yScrollOffset = 0
this.yScrollRate = (Screen.HEIGHT * 0.15625) // 20
this.maxYScrollOffset = (Screen.HEIGHT * -0.46875) - ((sensors.length - 1) * (Screen.HEIGHT * 0.21875)) // 60, 28
this.oscXCoordinate = 0
this.oscReading = 0
this.oscSensorIndex = 0
this.sensors = sensors
this.drawSensorStates = []
this.sensorMinsAndMaxs = []
sensors.forEach((sensor) => {
this.sensorMinsAndMaxs.push([sensor.getMinimum(), sensor.getMaximum()])
this.drawSensorStates.push(true)
})
this.setGlobalMinAndMax()
}
/* override */ startup() {
super.startup()
//--------------------------------
// Oscilloscope Movement Controls:
//--------------------------------
// Zoom in:
control.onEvent(
ControllerButtonEvent.Pressed,
controller.A.id,
() => {
if (this.guiState == GUI_STATE.SENSOR_SELECTION) {
this.drawSensorStates[this.informationSensorIndex] = !this.drawSensorStates[this.informationSensorIndex];
this.setGlobalMinAndMax(); // Re-calculate
}
else {
this.guiState = GUI_STATE.ZOOMED_IN;
this.sensors.forEach((sensor) => sensor.setBufferSize(140));
const sensor = this.sensors[this.oscSensorIndex];
this.oscXCoordinate = Math.round(sensor.getNormalisedBufferLength() / 2);
this.oscReading = sensor.getNthNormalisedReading(this.oscXCoordinate);
;
this.windowLeftBuffer = 0;
this.windowRightBuffer = 0;
this.windowTopBuffer = 0;
this.windowBotBuffer = 0;
this.update();
}
}
)
// Zoom out, if not ZOOMED_IN then go back to home
control.onEvent(
ControllerButtonEvent.Pressed,
controller.B.id,
() => {
if (this.guiState != GUI_STATE.ZOOMED_IN) {
this.app.popScene()
this.app.pushScene(new Home(this.app))
}
else {
this.guiState = GUI_STATE.GRAPH
this.sensors.forEach((sensor) => sensor.setBufferSize(80))
this.windowHeight = Screen.HEIGHT
this.windowWidth = Screen.WIDTH
this.windowLeftBuffer = 38
this.windowRightBuffer = 10
this.windowTopBuffer = (Screen.HEIGHT * 0.0390) // 5
this.windowBotBuffer = (Screen.HEIGHT * 0.15625) // 20
this.update()
}
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.up.id,
() => {
if (this.guiState != GUI_STATE.ZOOMED_IN) {
this.yScrollOffset = Math.min(this.yScrollOffset + this.yScrollRate, 0)
if (this.yScrollOffset <= (Screen.HEIGHT * -0.46875)) { //-60
this.guiState = GUI_STATE.SENSOR_SELECTION
this.informationSensorIndex = Math.abs(this.yScrollOffset + (Screen.HEIGHT * 0.46875)) / this.yScrollRate // 60
this.yScrollRate = (Screen.HEIGHT * 0.21875)
}
else {
this.guiState = GUI_STATE.GRAPH
this.yScrollRate = (Screen.HEIGHT * 0.15625) // 20
}
}
else if (this.guiState == GUI_STATE.ZOOMED_IN) {
this.oscSensorIndex = Math.max(0, this.oscSensorIndex - 1)
this.oscReading = this.sensors[this.oscSensorIndex].getNthNormalisedReading(this.oscXCoordinate)
}
this.sensors.forEach((sensor) => sensor.normaliseDataBuffer(this.windowBotBuffer - (2 * this.yScrollOffset) + (Screen.HEIGHT * 0.0625))) // 8
this.update() // For fast response to the above change
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.down.id,
() => {
if (this.guiState != GUI_STATE.ZOOMED_IN) {
this.yScrollOffset = Math.max(this.yScrollOffset - this.yScrollRate, this.maxYScrollOffset)
if (this.yScrollOffset <= (Screen.HEIGHT * -0.46875)) { // -60
this.guiState = GUI_STATE.SENSOR_SELECTION
this.informationSensorIndex = Math.abs(this.yScrollOffset + (Screen.HEIGHT * 0.46875)) / this.yScrollRate // 60
this.yScrollRate = (Screen.HEIGHT * 0.21875) // 28
}
else {
this.guiState = GUI_STATE.GRAPH
this.yScrollRate = (Screen.HEIGHT * 0.15625) // 20
}
this.sensors.forEach((sensor) => sensor.normaliseDataBuffer(this.windowBotBuffer - (2 * this.yScrollOffset) + (Screen.HEIGHT * 0.0625))) // 8
this.update() // For fast response to the above change
}
else if (this.guiState == GUI_STATE.ZOOMED_IN) {
this.oscSensorIndex = Math.min(this.oscSensorIndex + 1, this.sensors.length - 1)
this.oscReading = this.sensors[this.oscSensorIndex].getNthNormalisedReading(this.oscXCoordinate)
}
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.left.id,
() => {
if (this.guiState == GUI_STATE.ZOOMED_IN && this.oscXCoordinate > 0) {
this.oscXCoordinate -= 1
this.oscReading = this.sensors[this.oscSensorIndex].getNthNormalisedReading(this.oscXCoordinate)
this.update() // For fast response to the above change
}
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.right.id,
() => {
if (this.guiState == GUI_STATE.ZOOMED_IN && this.oscXCoordinate < this.sensors[this.oscSensorIndex].getNormalisedBufferLength() - 1) {
this.oscXCoordinate += 1
this.oscReading = this.sensors[this.oscSensorIndex].getNthNormalisedReading(this.oscXCoordinate)
this.update() // For fast response to the above change
}
}
)
}
/**
* Looks through the current active sensors and finds the lowest minimum & highest maximum among them.
* Sets: this.globalSensorMinimum & this.globalSensorMaximum.
* Re-invoked upon disabling a sensor.
* These two variables will then be displayed at the bot & top of the y-axis.
*/
private setGlobalMinAndMax(): void {
this.globalSensorMinimum = null
this.globalSensorMaximum = null
// Get the minimum and maximum sensor readings:
for (let i = 0; i < this.sensors.length; i++) {
if (this.drawSensorStates[i]) {
// Minimum and Maximum sensor readings for the y-axis markers
const sensor: Sensor = this.sensors[i]
if (sensor.getMinimum() < this.globalSensorMinimum || this.globalSensorMinimum == null) {
this.globalSensorMinimum = sensor.getMinimum()
}
if (sensor.getMaximum() > this.globalSensorMaximum || this.globalSensorMaximum == null) {
this.globalSensorMaximum = sensor.getMaximum()
}
}
}
}
/**
* Request each sensor updates its buffers,
* Then draw to screen
*/
update() {
screen().fill(this.backgroundColor);
// Make graph region black:
screen().fillRect(
this.windowLeftBuffer,
this.windowTopBuffer + this.yScrollOffset + this.yScrollOffset,
Screen.WIDTH - this.windowLeftBuffer - this.windowRightBuffer,
this.windowHeight - this.windowBotBuffer - ((this.guiState == GUI_STATE.ZOOMED_IN) ? 0 : 4),
0
);
//-------------------------------
// Load the buffer with new data:
//-------------------------------
if (this.guiState != GUI_STATE.SENSOR_SELECTION) {
for (let i = 0; i < this.sensors.length; i++) {
if (this.drawSensorStates[i]) {
const hasSpace = this.sensors[i].getBufferLength() < this.sensors[i].getMaxBufferSize()
if ((this.guiState != GUI_STATE.ZOOMED_IN) || (this.guiState == GUI_STATE.ZOOMED_IN && hasSpace))
this.sensors[i].readIntoBufferOnce(this.windowBotBuffer - (2 * this.yScrollOffset) + (Screen.HEIGHT * 0.0625)) // 8
}
}
}
//----------------------------
// Draw sensor lines & ticker:
//----------------------------
if (this.guiState != GUI_STATE.SENSOR_SELECTION) {
for (let i = 0; i < this.sensors.length; i++) {
if (this.drawSensorStates[i]) {
const sensor = this.sensors[i]
const color: number = SENSOR_COLORS[i % SENSOR_COLORS.length]
// Draw lines:
sensor.draw(
this.windowLeftBuffer + 3,
color
)
// Draw the latest reading on the right-hand side as a Ticker if at no-zoom:
if (this.guiState != GUI_STATE.ZOOMED_IN && sensor.getNormalisedBufferLength() > 0) {
const fromY = this.windowBotBuffer - 2 * this.yScrollOffset + (Screen.HEIGHT * 0.078125) // 10
const reading = sensor.getReading()
const range = Math.abs(sensor.getMinimum()) + sensor.getMaximum()
const y = Math.round(Screen.HEIGHT - ((((reading - sensor.getMinimum()) / range) * (BUFFERED_SCREEN_HEIGHT - fromY)))) - fromY
// Make sure the ticker won't be cut-off by other UI elements
if (y > sensor.getMinimum() + 5) {
screen().print(
sensor.getNthReading(sensor.getBufferLength() - 1).toString().slice(0, 5),
Screen.WIDTH - this.windowRightBuffer - (4 * font.charWidth),
y,
color,
bitmaps.font5,
)
}
}
}
}
}
//--------------------------
// Draw oscilloscope circle:
//--------------------------
if (this.guiState == GUI_STATE.ZOOMED_IN && this.oscReading != undefined) {
screen().drawCircle(
this.windowLeftBuffer + this.oscXCoordinate + 2,
this.oscReading,
5,
1
);
//----------------------------------------------------------
// Write x & y values of the current sensor in the top left:
//----------------------------------------------------------
const xText = "x = " + this.oscXCoordinate
const yText = "y = " + this.oscReading
screen().print(
xText,
this.windowWidth - (1 + xText.length * font.charWidth),
this.windowTopBuffer + (Screen.HEIGHT * 0.0390), // 5
1,
bitmaps.font5,
);
screen().print(
yText,
this.windowWidth - (1 + yText.length * font.charWidth),
this.windowTopBuffer + (Screen.HEIGHT * 0.1172), // 15
1,
bitmaps.font5,
);
}
//---------------------------------
// Draw the axis and their markers:
//---------------------------------
this.draw_axes();
//--------------------------------
// Draw sensor information blocks:
//--------------------------------
if (this.yScrollOffset <= (Screen.HEIGHT * 0.3125) && this.guiState != GUI_STATE.ZOOMED_IN) { // 40
let y = this.windowHeight - 2 + (2 * this.yScrollOffset)
for (let i = 0; i < this.sensors.length; i++) {
// Black edges:
screen().fillRect(
5,
y,
142,
(Screen.HEIGHT * 0.3671), // 47
15
)
// Sensor is disabled:
let blockColor: number = SENSOR_COLORS[i % SENSOR_COLORS.length]
let textColor: number = 15; // black
if (!this.drawSensorStates[i]) {
blockColor = 15; // black
textColor = 1; // white
}
// Coloured block:
screen().fillRect(
7,
y,
145,
(Screen.HEIGHT * 0.3515), // 45
blockColor
)
// Blue outline for selected sensor:
if (this.guiState == GUI_STATE.SENSOR_SELECTION && i == this.informationSensorIndex) {
// Blue edges:
for (let thickness = 0; thickness < 3; thickness++) {
screen().drawRect(
7 - thickness,
y - thickness,
145 + thickness,
(Screen.HEIGHT * 0.3515) + thickness, // 45
6
)
}
}
// Information:
screen().print(
this.sensors[i].getName(),
12,
y + (Screen.HEIGHT * 0.0156),// 2
textColor
)
screen().print(
"Sensor Minimum: " + this.sensorMinsAndMaxs[i][MIN_MAX_COLUMNS.MIN],
12,
y + (Screen.HEIGHT * 0.125),// 16
textColor
)
screen().print(
"Sensor Maximum: " + this.sensorMinsAndMaxs[i][MIN_MAX_COLUMNS.MAX],
12,
y + (Screen.HEIGHT * 0.25),// 32
textColor
)
y += (Screen.HEIGHT * 0.4296) // 55
}
}
basic.pause(GRAPH_FRAME_TIME_MS);
}
/**
* Draw x & y axis double-thickness each, in yellow
* Draw abscissa and ordinate
*/
draw_axes() {
//------
// Axes:
//------
const yAxisOffset = ((this.guiState == GUI_STATE.ZOOMED_IN) ? 2 : 0)
for (let i = 0; i < 2; i++) {
// X-Axis:
screen().drawLine(
this.windowLeftBuffer,
this.windowHeight - this.windowBotBuffer + i + this.yScrollOffset + this.yScrollOffset - yAxisOffset,
this.windowWidth - this.windowRightBuffer,
this.windowHeight - this.windowBotBuffer + i + this.yScrollOffset + this.yScrollOffset - yAxisOffset,
5
);
// Y-Axis:
screen().drawLine(
this.windowLeftBuffer + i,
this.windowTopBuffer + this.yScrollOffset + this.yScrollOffset,
this.windowLeftBuffer + i,
this.windowHeight - this.windowBotBuffer + this.yScrollOffset + this.yScrollOffset,
5
);
}
if (this.guiState != GUI_STATE.ZOOMED_IN) {
//----------
// Ordinate:
//----------
if (this.yScrollOffset > (Screen.HEIGHT * -0.46875)) { // -60
if (this.globalSensorMinimum != null && this.globalSensorMaximum != null) {
// Bot:
screen().print(
this.globalSensorMinimum.toString(),
(6 * font.charWidth) - (this.globalSensorMinimum.toString().length * font.charWidth),
this.windowHeight - this.windowBotBuffer + this.yScrollOffset + this.yScrollOffset - (Screen.HEIGHT * 0.03125), // 4
15
)
// Top:
screen().print(
this.globalSensorMaximum.toString(),
(6 * font.charWidth) - (this.globalSensorMaximum.toString().length * font.charWidth),
Screen.HEIGHT - this.windowHeight + this.windowTopBuffer - Math.floor(0.1 * this.yScrollOffset),
15
)
}
}
//----------
// Abscissa:
//----------
// Start
screen().print(
this.sensors[0].numberOfReadings.toString(),
this.windowLeftBuffer - 2,
this.windowHeight - this.windowBotBuffer + this.yScrollOffset + this.yScrollOffset + (Screen.HEIGHT * 0.03125), // 4
15
)
// End:
const end: string = (this.sensors[0].numberOfReadings + this.sensors[0].getNormalisedBufferLength()).toString()
screen().print(
end,
Screen.WIDTH - this.windowRightBuffer - (end.length * font.charWidth) - 1,
this.windowHeight - this.windowBotBuffer + this.yScrollOffset + this.yScrollOffset + (Screen.HEIGHT * 0.03125), // 4
15
)
}
}
}
}