forked from KierPalin/MicroData
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataRecorder.ts
More file actions
322 lines (273 loc) · 11.5 KB
/
dataRecorder.ts
File metadata and controls
322 lines (273 loc) · 11.5 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
namespace microdata {
import AppInterface = user_interface_base.AppInterface
import Screen = user_interface_base.Screen
import Scene = user_interface_base.Scene
import Sprite = user_interface_base.Sprite
import Affine = user_interface_base.Affine
import font = user_interface_base.font
/** 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]
/** The colours that will be used for writing the information about the sensor. */
const SENSOR_BOX_TEXT_COLORS: number[] = [1, 1, 1, 1, 15, 15]
/**
* 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;
private showCancelRecordingScreen: boolean;
private currentlyCancelling: boolean
private yesBtn: Sprite // currentBtn = 0
private noBtn: Sprite // currentBtn = 1
constructor(app: AppInterface, 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
this.showCancelRecordingScreen = false;
this.currentlyCancelling = false;
//---------------
// User Controls:
//---------------
// Go Back:
context.onEvent(
ControllerButtonEvent.Pressed,
controller.B.id,
() => {
if (this.scheduler.loggingComplete()) {
this.app.popScene()
this.app.pushScene(new Home(this.app))
}
else {
this.showCancelRecordingScreen = !this.showCancelRecordingScreen
}
}
)
// Clear whatever A was previously bound to
context.onEvent(
ControllerButtonEvent.Pressed,
controller.A.id,
() => {
if (this.showCancelRecordingScreen) {
this.currentlyCancelling = true
this.scheduler.stop()
basic.pause(1000)
this.app.popScene()
this.app.pushScene(new Home(this.app))
}
}
)
// Scroll Up
context.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
context.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()
}
)
// For cancelling the current recording:
this.yesBtn = new Sprite({ img: Icons.get("tile_button_a") })
this.yesBtn.bindXfrm(new Affine())
this.yesBtn.xfrm.parent = new Affine()
this.yesBtn.xfrm.worldPos.x = Screen.HALF_WIDTH
this.yesBtn.xfrm.worldPos.y = Screen.HALF_HEIGHT
this.yesBtn.xfrm.localPos.x = -40
this.yesBtn.xfrm.localPos.y = 12
this.noBtn = new Sprite({ img: Icons.get("tile_button_b") })
this.noBtn.bindXfrm(new Affine())
this.noBtn.xfrm.parent = new Affine()
this.noBtn.xfrm.worldPos.x = Screen.HALF_WIDTH
this.noBtn.xfrm.worldPos.y = Screen.HALF_HEIGHT
this.noBtn.xfrm.localPos.x = 40
this.noBtn.xfrm.localPos.y = 12
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 >> 1) - 10);
screen().printCenter("Press B to back out.", screen().height >> 1);
}
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, idx) => {
y += 12
screen().print(
info,
24,
y,
SENSOR_BOX_TEXT_COLORS[idx]
)
});
}
y += 14
}
if (this.showCancelRecordingScreen) {
const headerX = Screen.HALF_WIDTH; // Log has data in it
// Outline:
screen().fillRect(
Screen.HALF_WIDTH - 65,
Screen.HALF_HEIGHT - 30,
130 + 2,
60 + 2,
15 // Black
)
screen().fillRect(
Screen.HALF_WIDTH - 65,
Screen.HALF_HEIGHT - 30,
130,
60,
4 // Orange
)
const tutorialTextLength = ("Cancel recording?".length * font.charWidth)
screen().print(
"Cancel recording?",
headerX - (tutorialTextLength >> 1),
Screen.HALF_HEIGHT - 30 + 7,
15 // Black
)
// Underline the title:
screen().fillRect(
headerX - (tutorialTextLength >> 1) - 1,
Screen.HALF_HEIGHT - 30 + 16,
tutorialTextLength,
2,
15 // Black
)
if (this.currentlyCancelling)
screen().printCenter("Cancelling...", Screen.HALF_HEIGHT - 9, 15)
// Draw button prompts:
screen().print(
"Yes",
Screen.HALF_WIDTH - 48,
Screen.HALF_HEIGHT + 20,
15
)
screen().print(
"No",
Screen.HALF_WIDTH + 33,
Screen.HALF_HEIGHT + 20,
15
)
// White boxes behind yes & no btns:
screen().fillRect(
Screen.HALF_WIDTH - 47,
Screen.HALF_HEIGHT + 6,
12,
12,
1
)
screen().fillRect(
Screen.HALF_WIDTH + 34,
Screen.HALF_HEIGHT + 6,
12,
12,
1
)
this.yesBtn.draw()
this.noBtn.draw()
}
}
}
}
}