-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataRecorder.ts
196 lines (166 loc) · 6.78 KB
/
dataRecorder.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
namespace microcode {
/** Number of sensor information boxes that can fit onto the screen at once*/
const MAX_SENSORS_ON_SCREEN: number = 5
/** The colours that will be used for the lines & sensor information boxes */
const SENSOR_BOX_COLORS: number[] = [2,3,4,6,7,9]
/**
* Responsible for invoking the logging commands for each sensor,
* Presents information about each sensor's state via colourful collapsing boxes
*
* Sensors are logged via a scheduler
*/
export class DataRecorder extends Scene {
/** */
private scheduler: SensorScheduler;
/** For displaying their status on the screen and passing to the scheduler. */
private sensors: Sensor[]
/** For faster looping, modulo calculation when pressing UP or DOWN */
private numberOfSensors: number;
/** Sensor to be shown */
private currentSensorIndex: number;
/** Last sensor on the screen */
private sensorIndexOffset: number;
/** For the currentSensorIndex */
private sensorBoxColor: number;
constructor(app: App, sensors: Sensor[]) {
super(app, "dataRecorder")
this.scheduler = new SensorScheduler(sensors)
this.sensors = sensors
this.numberOfSensors = sensors.length
this.sensorIndexOffset = 0
this.currentSensorIndex = 0
this.sensorBoxColor = 15
//---------------
// User Controls:
//---------------
// Go Back:
control.onEvent(
ControllerButtonEvent.Pressed,
controller.B.id,
() => {
this.app.popScene()
this.app.pushScene(new Home(this.app))
}
)
// Clear whatever A was previously bound to
control.onEvent(
ControllerButtonEvent.Pressed,
controller.A.id,
() => {}
)
// Scroll Up
control.onEvent(
ControllerButtonEvent.Pressed,
controller.up.id,
() => {
this.currentSensorIndex = Math.max(0, this.currentSensorIndex - 1)
if (this.sensorIndexOffset > 0)
this.sensorIndexOffset = Math.max(0, this.sensorIndexOffset - 1)
this.update()
}
)
// Scroll Down
control.onEvent(
ControllerButtonEvent.Pressed,
controller.down.id,
() => {
this.currentSensorIndex = Math.min(this.currentSensorIndex + 1, this.numberOfSensors - 1)
if (this.currentSensorIndex > 4)
this.sensorIndexOffset = Math.min(this.sensorIndexOffset + 1, this.numberOfSensors - 5)
this.update()
}
)
this.log()
}
log() {this.scheduler.start()}
update(): void {
Screen.fillRect(
Screen.LEFT_EDGE,
Screen.TOP_EDGE,
Screen.WIDTH,
Screen.HEIGHT,
0xc
)
// Check if all sensors have finished their work:
if (this.scheduler.loggingComplete()) {
screen().printCenter("Data Logging Complete.", (screen().height / 2) - 10);
screen().printCenter("Press B to back out.", screen().height / 2);
}
else {
screen().printCenter("Recording data...", 4);
let y = 16
for (let i = this.sensorIndexOffset; i < this.numberOfSensors; i++) {
if (i - this.sensorIndexOffset > MAX_SENSORS_ON_SCREEN)
break
// Get the colour for this box
this.sensorBoxColor = SENSOR_BOX_COLORS[i % SENSOR_BOX_COLORS.length]
const boxWidth: number = 142
// Draw box as collapsed:
if (i != this.currentSensorIndex) {
screen().fillRect(
5,
y,
boxWidth,
16,
16
)
screen().fillRect(
7,
y,
boxWidth + 3,
14,
this.sensorBoxColor
)
screen().print(
this.sensors[i].getName(),
12,
y + 2,
15
)
}
// Box is selected: Draw all information:
else {
screen().fillRect(
5,
y,
boxWidth,
62,
15
)
screen().fillRect(
7,
y,
boxWidth + 3,
60,
this.sensorBoxColor
)
//-------------------------------
// Information inside sensor box:
//-------------------------------
const sensor = this.sensors[i]
screen().print(
sensor.getName(),
12,
y + 2,
15
)
//------------------------------
// Write the sensor information:
//------------------------------
const sensorInfo: string[] = (sensor.isInEventMode) ? sensor.getEventInformation() : sensor.getRecordingInformation();
sensorInfo.forEach((info) => {
y += 12
screen().print(
info,
24,
y,
15
)
});
}
y += 14
}
}
}
}
}