Skip to content

Commit 8434cc6

Browse files
OOB: Full erase of data logging storage on first run.
1 parent 42b6c39 commit 8434cc6

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

source/samples/OOB_v3.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2023 Micro:bit Educational Foundation
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
DEALINGS IN THE SOFTWARE.
23+
*/
124
#include "MicroBit.h"
225
#include "Synthesizer.h"
326
#include "StreamRecording.h"
@@ -214,8 +237,8 @@ static void onButtonLogo(MicroBitEvent) {
214237
splitterChannel->requestSampleRate( sampleRate );
215238

216239
// Uncomment these two lines and comment out the *recording declaration after them to insert a low-pass-filter.
217-
// static LowPassFilter *lowPassFilter = new LowPassFilter(*splitterChannel, 0.812313f, false);
218-
// static StreamRecording *recording = new StreamRecording(*lowPassFilter);
240+
//static LowPassFilter *lowPassFilter = new LowPassFilter(*splitterChannel, 0.812313f, false);
241+
//static StreamRecording *recording = new StreamRecording(*lowPassFilter);
219242
static StreamRecording *recording = new StreamRecording(*splitterChannel);
220243

221244
static MixerChannel *channel = uBit.audio.mixer.addChannel(*recording, sampleRate);
@@ -302,6 +325,25 @@ static void onStart() {
302325
uBit.display.print(HEART);
303326
}
304327

328+
static void clearDataLoggingStorage() {
329+
// This 32-bit word will be initially stored in flash as 0xffffffff
330+
// On first run, it will be cleared to 0x00000000 in flash, to be able to
331+
// use it as a flag to only erase the data logging storage once
332+
static const uint32_t flash_original_value __ALIGNED(4) = 0xffffffff;
333+
uint32_t zero = 0x00000000;
334+
335+
NRF52FlashManager flashManager(0, 128, 4096);
336+
337+
// Read the value currently in flash
338+
uint32_t flash_current_value;
339+
flashManager.read(&flash_current_value, (uint32_t)&flash_original_value, 1);
340+
if (flash_current_value) {
341+
// Value is not cleared yet, so full erase data storage and clear the flag
342+
uBit.log.clear(true);
343+
flashManager.write((uint32_t)&flash_original_value, &zero, 1);
344+
}
345+
}
346+
305347
void out_of_box_experience() {
306348
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
307349
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
@@ -312,8 +354,11 @@ void out_of_box_experience() {
312354

313355
onStart();
314356

315-
//uBit.audio.rawSplitter->status |= DEVICE_COMPONENT_STATUS_SYSTEM_TICK;
316-
//uBit.audio.splitter->status |= DEVICE_COMPONENT_STATUS_SYSTEM_TICK;
357+
// The Out of Box Experience is the default programme loaded to micro:bit from factory.
358+
// It can be flashed to a micro:bit to resemble a "factory reset"
359+
// For that, the data logging storage should be fully erased (by default it will do a quick erase)
360+
// This process can take several seconds, so it is done in a separate fiber
361+
create_fiber(clearDataLoggingStorage);
317362

318363
while (true) {
319364
uBit.sleep(1000);

0 commit comments

Comments
 (0)