From 452cbe3aa468b461ede4705e6624874d85c269a6 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Fri, 9 Sep 2022 15:26:49 +0100 Subject: [PATCH] Tidy data logging state. Closes https://github.com/microbit-foundation/micropython-microbit-v2-simulator/issues/52 --- src/board/data-logging.ts | 28 ++++++++++++++-------------- src/board/index.ts | 6 +----- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/board/data-logging.ts b/src/board/data-logging.ts index f28c4f9c..deafdbb6 100644 --- a/src/board/data-logging.ts +++ b/src/board/data-logging.ts @@ -9,7 +9,7 @@ import { MICROBIT_HAL_LOG_TIMESTAMP_NONE, MICROBIT_HAL_LOG_TIMESTAMP_SECONDS, } from "./constants"; -import { State } from "./state"; +import { DataLoggingState, State } from "./state"; // Determined via a CODAL program dumping logEnd - dataStart in MicroBitLog.cpp. // This is only approximate as we don't serialize our state in the same way but @@ -24,7 +24,7 @@ export class DataLogging { private headingsChanged: boolean = false; private headings: string[] = []; private row: string[] | undefined; - private logFull: boolean = false; + state: DataLoggingState = { type: "dataLogging", logFull: false }; constructor( private currentTimeMillis: () => number, @@ -84,13 +84,13 @@ export class DataLogging { if (entry.data || entry.headings) { const entrySize = calculateEntrySize(entry); if (this.size + entrySize > maxSizeBytes) { - if (!this.logFull) { - this.logFull = true; + if (!this.state.logFull) { + this.state = { + ...this.state, + logFull: true, + }; this.onChange({ - dataLogging: { - type: "dataLogging", - logFull: true, - }, + dataLogging: this.state, }); } return MICROBIT_HAL_DEVICE_NO_RESOURCES; @@ -148,13 +148,13 @@ export class DataLogging { this.timestampOnLastEndRow = undefined; this.size = 0; - if (this.logFull) { - this.logFull = false; + if (this.state.logFull) { + this.state = { + ...this.state, + logFull: false, + }; this.onChange({ - dataLogging: { - type: "dataLogging", - logFull: false, - }, + dataLogging: this.state, }); } diff --git a/src/board/index.ts b/src/board/index.ts index a6934b3c..a5d6d3ec 100644 --- a/src/board/index.ts +++ b/src/board/index.ts @@ -181,11 +181,7 @@ export class Board { compassHeading: this.compass.state.compassHeading, lightLevel: this.display.lightLevel, - dataLogging: { - // Placeholder. - type: "dataLogging", - logFull: false, - }, + dataLogging: this.dataLogging.state, soundLevel: this.microphone.soundLevel, temperature: this.temperature, };