Skip to content

Commit eaeb100

Browse files
committed
noArcadeShieldMode.ts -> headlessMode.ts. Brought up to date with the sensor refactor, tweaked thresholds.
1 parent 74ed940 commit eaeb100

5 files changed

Lines changed: 78 additions & 79 deletions

File tree

app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace microdata {
3535
if (arcadeShieldConnected)
3636
this.pushScene(new microdata.Home(this));
3737
else
38-
new DistributedLoggingProtocol(this, false);
38+
new HeadlessMode(this);
3939
}
4040

4141
public pushScene(scene: Scene) {

distributedLogging.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace microdata {
101101
//------------------------------------------------------
102102
// Variables used by both the Commander and the Targets:
103103
//------------------------------------------------------
104-
104+
105105
public id: number;
106106

107107
/** Target (many) or Commander (only can be 1)? This is set inside .initialiseCommunication() and does not change thereon. */
@@ -135,15 +135,15 @@ namespace microdata {
135135

136136
/** The Target needs to build a list of sensors from sensor names, configure them according to the Commander's specification, then pass that to the recorder */
137137
private sensors: Sensor[]
138-
138+
139139
//--------------------------
140140
// Commander only Variables:
141141
//--------------------------
142-
142+
143143
private static nextMicrobitIDToIssue: number;
144144
public numberOfTargetsConnected: number;
145145
private callbackObj: ITargetDataLoggedCallback;
146-
146+
147147
/** Should the target send each row of data it logs back to the Commander? See DistributedLoggingProtocol.log() */
148148
private streamDataBack: boolean;
149149

@@ -153,12 +153,12 @@ namespace microdata {
153153
constructor(app: AppInterface, arcadeShieldIsConnected: boolean, callbackObj?: ITargetDataLoggedCallback) {
154154
control.singleSimulator()
155155

156-
//--------------
157-
// Unbind A & B:
158-
//--------------
156+
//-----------------------------------------------------------------------
157+
// Unbind A & B: Since we may've come from headless mode which uses them.
158+
//-----------------------------------------------------------------------
159159

160-
input.onButtonPressed(1, () => {});
161-
input.onButtonPressed(2, () => {});
160+
input.onButtonPressed(1, () => { });
161+
input.onButtonPressed(2, () => { });
162162

163163
//------------------------------------------------------
164164
// Variables used by both the Commander and the Targets:
@@ -176,11 +176,11 @@ namespace microdata {
176176
this.numberOfMessagesReceived = 0;
177177

178178
this.sensors = []
179-
179+
180180
//--------------------------
181181
// Commander only Variables:
182182
//--------------------------
183-
183+
184184
DistributedLoggingProtocol.nextMicrobitIDToIssue = 0;
185185
this.numberOfTargetsConnected = 0;
186186
this.callbackObj = callbackObj;
@@ -226,7 +226,7 @@ namespace microdata {
226226
return message
227227
}
228228

229-
private sendMessage(message: string): void {radio.sendString(message)}
229+
private sendMessage(message: string): void { radio.sendString(message) }
230230

231231

232232
private initialiseCommunication() {
@@ -300,7 +300,7 @@ namespace microdata {
300300
* Message with 'NETWORK_COMMAND.START_LOGGING' -> 'radio.onReceivedString(getSensorConfigData)'
301301
* @param receivedString The first message in a command; SENDER_ID + NETWORK_COMMAND + ?ADDITIONAL_INFO (number of future messages for START_LOGGING as an example)
302302
*/
303-
radio.onReceivedString(function (receivedString: string): void {
303+
radio.onReceivedString(function(receivedString: string): void {
304304
const message = receivedString.split(",")
305305

306306
if (message[MESSAGE_COMPONENT.NETWORK_COMMAND] == NETWORK_COMMAND_STRING[NETWORK_COMMAND.START_LOGGING]) {
@@ -345,15 +345,15 @@ namespace microdata {
345345
const configType = dataStream[1]
346346
if (configType == "P") {
347347
const measurements: number = +dataStream[2]
348-
const period: number = +dataStream[3]
349-
sensor.setConfig({measurements, period})
348+
const period: number = +dataStream[3]
349+
sensor.setConfig({ measurements, period })
350350
}
351351

352352
else if (configType == "E") {
353353
const measurements: number = +dataStream[2]
354-
const inequality: string = dataStream[3]
355-
const comparator: number = +dataStream[4]
356-
sensor.setConfig({measurements, period: SENSOR_EVENT_POLLING_PERIOD_MS, inequality, comparator})
354+
const inequality: string = dataStream[3]
355+
const comparator: number = +dataStream[4]
356+
sensor.setConfig({ measurements, period: SENSOR_EVENT_POLLING_PERIOD_MS, inequality, comparator })
357357
}
358358

359359
this.addSensor(sensor)
@@ -398,8 +398,8 @@ namespace microdata {
398398
this.targetIDs.push(id)
399399
this.targetIDs = this.targetIDs.sort();
400400
}
401-
protected isDuplicateTarget(id: number) {return this.targetIDs.filter((value) => value == id).length}
402-
401+
protected isDuplicateTarget(id: number) { return this.targetIDs.filter((value) => value == id).length }
402+
403403

404404
private becomeCommander() {
405405
this.radioMode = RADIO_LOGGING_MODE.COMMANDER
@@ -409,7 +409,7 @@ namespace microdata {
409409

410410
radio.onReceivedString(function commanderControlFlowFn(receivedString: string): void {
411411
const message = receivedString.split(",")
412-
412+
413413
/**
414414
* INCOMING JOIN REQUEST
415415
*/
@@ -457,7 +457,7 @@ namespace microdata {
457457
);
458458
}
459459
})
460-
}
460+
}
461461

462462
//--------------------------------------------------------------------------------
463463
// Methods invoked on the Commander Screen that tell command the Target Microbits:
@@ -565,15 +565,15 @@ namespace microdata {
565565

566566
if (DistributedLoggingScreen.showTabularData) {
567567
this.app.popScene()
568-
this.app.pushScene(new TabularDataViewer(this.app, function () {this.app.popScene(); this.app.pushScene(new DistributedLoggingScreen(this.app))}))
568+
this.app.pushScene(new TabularDataViewer(this.app, function() { this.app.popScene(); this.app.pushScene(new DistributedLoggingScreen(this.app)) }))
569569
}
570570
}
571571
}
572572

573573
callback(msg: string) {
574574
DistributedLoggingScreen.streamingDone = true
575575
}
576-
576+
577577
/* override */ startup() {
578578
super.startup()
579579

@@ -646,7 +646,7 @@ namespace microdata {
646646
style: ButtonStyles.Transparent,
647647
icon: "radio_set_group",
648648
ariaId: "Start streaming",
649-
x: 20,
649+
x: 20,
650650
y,
651651
onClick: () => {
652652
if (DistributedLoggingScreen.streamingDone) {
@@ -669,7 +669,7 @@ namespace microdata {
669669
onClick: () => {
670670
if (DistributedLoggingScreen.showTabularData) {
671671
this.app.popScene();
672-
this.app.pushScene(new TabularDataViewer(this.app, function () {this.app.popScene(); this.app.pushScene(new DistributedLoggingScreen(this.app))}));
672+
this.app.pushScene(new TabularDataViewer(this.app, function() { this.app.popScene(); this.app.pushScene(new DistributedLoggingScreen(this.app)) }));
673673
}
674674
},
675675
})
@@ -695,36 +695,36 @@ namespace microdata {
695695
)
696696
break;
697697
}
698-
698+
699699
case RADIO_LOGGING_MODE.COMMANDER: {
700700
screen().printCenter(
701701
"Commander Mode",
702702
2
703703
)
704-
704+
705705
this.navigator.drawComponents();
706706
break;
707707
}
708-
708+
709709
case RADIO_LOGGING_MODE.TARGET: {
710710
const connectedText = "Connected to Commander,"
711-
const asMicrobit = "as Microbit " + this.distributedLogger.id + "."
712-
711+
const asMicrobit = "as Microbit " + this.distributedLogger.id + "."
712+
713713
screen().print(
714714
connectedText,
715-
Screen.HALF_WIDTH - ((connectedText.length * font.charWidth)>> 1),
715+
Screen.HALF_WIDTH - ((connectedText.length * font.charWidth) >> 1),
716716
2
717717
)
718-
718+
719719
// Left-aligned with above text
720720
screen().print(
721721
asMicrobit,
722-
Screen.HALF_WIDTH - ((connectedText.length * font.charWidth)>> 1),
722+
Screen.HALF_WIDTH - ((connectedText.length * font.charWidth) >> 1),
723723
12
724724
)
725725
break;
726726
}
727-
727+
728728
default:
729729
break;
730730
}
@@ -751,4 +751,4 @@ namespace microdata {
751751
super.draw()
752752
}
753753
}
754-
}
754+
}
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace microdata {
3838
};
3939

4040
/** For module inside of B button. */
41-
const SENSOR_SELECTION_SIZE = 4;
41+
const UI_SENSOR_SELECT_STATE_LEN = 5;
4242
/** How long should each LED picture be shown for? Series of pictures divide this by how many there are. */
4343
const SHOW_EACH_SENSOR_FOR_MS: number = 1000;
4444

@@ -57,7 +57,7 @@ namespace microdata {
5757
*
5858
* Fibers and special waiting functions .waitUntilSensorSelectStateChange & .waitUntilUIModeChanges are required to maintain low-latency and the dynamic behaviour described above.
5959
*/
60-
export class NoArcadeShieldMode {
60+
export class HeadlessMode {
6161
private app: App;
6262
/** Mutated by the A & B button */
6363
private uiMode: UI_MODE;
@@ -80,8 +80,7 @@ namespace microdata {
8080
// B Button
8181
input.onButtonPressed(2, () => {
8282
if (this.uiMode == UI_MODE.SENSOR_SELECTION)
83-
this.uiSensorSelectState = (this.uiSensorSelectState + 1) % SENSOR_SELECTION_SIZE
84-
83+
this.uiSensorSelectState = (this.uiSensorSelectState + 1) % UI_SENSOR_SELECT_STATE_LEN
8584
else if (this.uiMode == UI_MODE.LOGGING) {
8685
this.uiMode = UI_MODE.SENSOR_SELECTION;
8786
this.dynamicSensorSelectionLoop();
@@ -105,11 +104,11 @@ namespace microdata {
105104
*/
106105
private dynamicSensorSelectionLoop() {
107106
const dynamicInfo = [
108-
{sensor: new AccelerometerXSensor(), uiState: UI_SENSOR_SELECT_STATE.ACCELERATION, threshold: 0.25},
109-
{sensor: new AccelerometerYSensor(), uiState: UI_SENSOR_SELECT_STATE.ACCELERATION, threshold: 0.25},
110-
{sensor: new AccelerometerZSensor(), uiState: UI_SENSOR_SELECT_STATE.ACCELERATION, threshold: 0.25},
111-
{sensor: new LightSensor(), uiState: UI_SENSOR_SELECT_STATE.LIGHT, threshold: 0.75},
112-
{sensor: new MagnetSensor(), uiState: UI_SENSOR_SELECT_STATE.MAGNET, threshold: 0.80},
107+
{ sensor: Sensor.getFromName("Accel. X"), uiState: UI_SENSOR_SELECT_STATE.ACCELERATION, threshold: 0.25 },
108+
{ sensor: Sensor.getFromName("Accel. Y"), uiState: UI_SENSOR_SELECT_STATE.ACCELERATION, threshold: 0.25 },
109+
{ sensor: Sensor.getFromName("Accel. Z"), uiState: UI_SENSOR_SELECT_STATE.ACCELERATION, threshold: 0.25 },
110+
{ sensor: Sensor.getFromName("Light"), uiState: UI_SENSOR_SELECT_STATE.LIGHT, threshold: 0.85 },
111+
{ sensor: Sensor.getFromName("Magnet"), uiState: UI_SENSOR_SELECT_STATE.MAGNET, threshold: 0.80 },
113112
];
114113

115114
// Don't trigger the same sensor selection twice in a row:
@@ -228,7 +227,7 @@ namespace microdata {
228227

229228
break;
230229
}
231-
230+
232231
case UI_SENSOR_SELECT_STATE.TEMPERATURE: {
233232
basic.showLeds(`
234233
# . . . .
@@ -250,16 +249,16 @@ namespace microdata {
250249
. . . . .
251250
. . # . .
252251
`);
253-
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS>> 1), 50, UI_SENSOR_SELECT_STATE.LIGHT)) break;
254-
252+
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS >> 1), 50, UI_SENSOR_SELECT_STATE.LIGHT)) break;
253+
255254
basic.showLeds(`
256255
. # # # .
257256
. # # # .
258257
. # # # .
259258
. . . . .
260259
. . # . .
261260
`);
262-
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS>> 1), 50, UI_SENSOR_SELECT_STATE.LIGHT)) break;
261+
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS >> 1), 50, UI_SENSOR_SELECT_STATE.LIGHT)) break;
263262

264263
break;
265264
}
@@ -272,7 +271,7 @@ namespace microdata {
272271
. . . . .
273272
. . . . .
274273
`)
275-
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS>> 1), 50, UI_SENSOR_SELECT_STATE.MAGNET)) break;
274+
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS >> 1), 50, UI_SENSOR_SELECT_STATE.MAGNET)) break;
276275

277276
basic.showLeds(`
278277
. # # # .
@@ -281,7 +280,7 @@ namespace microdata {
281280
. . . . .
282281
# # . # #
283282
`)
284-
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS>> 1), 50, UI_SENSOR_SELECT_STATE.MAGNET)) break;
283+
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS >> 1), 50, UI_SENSOR_SELECT_STATE.MAGNET)) break;
285284

286285
break;
287286
}
@@ -294,16 +293,16 @@ namespace microdata {
294293
# . . . #
295294
. . # . .
296295
`);
297-
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS>> 1), 50, UI_SENSOR_SELECT_STATE.RADIO)) break;
298-
296+
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS >> 1), 50, UI_SENSOR_SELECT_STATE.RADIO)) break;
297+
299298
basic.showLeds(`
300299
. # # # .
301300
# . . . #
302301
. # # # .
303302
# . . . #
304303
. . # . .
305304
`);
306-
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS>> 1), 50, UI_SENSOR_SELECT_STATE.RADIO)) break;
305+
if (!this.waitUntilSensorSelectStateChange((SHOW_EACH_SENSOR_FOR_MS >> 1), 50, UI_SENSOR_SELECT_STATE.RADIO)) break;
307306

308307
break;
309308
}
@@ -315,7 +314,7 @@ namespace microdata {
315314
});
316315
}
317316

318-
317+
319318
/**
320319
* Get the sensor(s) that the user selected and start logging them.
321320
* Exit if the UI_MODE changes back to SENSOR_SELECTION (upon the user pressing B)
@@ -357,24 +356,24 @@ namespace microdata {
357356
private uiSelectionToSensors(): Sensor[] {
358357
switch (this.uiSensorSelectState) {
359358
case UI_SENSOR_SELECT_STATE.ACCELERATION:
360-
return [new AccelerometerXSensor(), new AccelerometerYSensor(), new AccelerometerZSensor()]
359+
return [Sensor.getFromName("Accel. X"), Sensor.getFromName("Accel. Y"), Sensor.getFromName("Accel. Z")]
361360

362361
case UI_SENSOR_SELECT_STATE.TEMPERATURE:
363-
return [new TemperatureSensor()]
362+
return [Sensor.getFromName("Temp.")]
364363

365364
case UI_SENSOR_SELECT_STATE.LIGHT:
366-
return [new LightSensor()]
365+
return [Sensor.getFromName("Light")]
367366

368367
case UI_SENSOR_SELECT_STATE.MAGNET:
369-
return [new MagnetSensor()]
368+
return [Sensor.getFromName("Magnet")]
370369

371370
case UI_SENSOR_SELECT_STATE.RADIO:
372-
// new DistributedLoggingProtocol(this.app, false);
371+
new DistributedLoggingProtocol(this.app, false);
373372
return []
374-
373+
375374
default:
376375
return []
377376
}
378377
}
379378
}
380-
}
379+
}

0 commit comments

Comments
 (0)