Skip to content

Commit 6ade36b

Browse files
committed
codal_port/microbitfs: Add a hook for when files are opened for writing.
And reset the log if main.py is opened for writing. Addresses issue #95. Signed-off-by: Damien George <[email protected]>
1 parent 88670f6 commit 6ade36b

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/codal_port/main.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "drv_display.h"
4343
#include "modmicrobit.h"
4444

45+
#define MAIN_PY "main.py"
46+
4547
// Use a fixed static buffer for the heap.
4648
static char heap[64 * 1024];
4749

@@ -65,7 +67,7 @@ void mp_main(void) {
6567
mp_init();
6668

6769
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
68-
const char *main_py = "main.py";
70+
const char *main_py = MAIN_PY;
6971
if (mp_import_stat(main_py) == MP_IMPORT_STAT_FILE) {
7072
// exec("main.py")
7173
microbit_pyexec_file(main_py);
@@ -183,6 +185,14 @@ void microbit_pyexec_file(const char *filename) {
183185
}
184186
}
185187

188+
// Callback from microbitfs when a file is opened for writing.
189+
void microbit_file_opened_for_writing(const char *name, size_t name_len) {
190+
if (name_len == strlen(MAIN_PY) && memcmp(name, MAIN_PY, name_len) == 0) {
191+
// The file main.py is changed, so invalidate the data logging storage (fast erase).
192+
microbit_hal_log_delete(false);
193+
}
194+
}
195+
186196
#if MICROPY_MBFS
187197
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
188198
return uos_mbfs_new_reader(filename);

src/codal_port/microbitfs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
// This is a copy of the file micropython:ports/nrf/modules/uos/microbitfs.c with
29+
// a call to `microbit_file_opened_for_writing` added in `microbit_file_open`.
30+
2831
#include <string.h>
2932
#include <stdio.h>
3033
#include <sys/stat.h>
@@ -351,6 +354,7 @@ STATIC file_descriptor_obj *microbit_file_open(const char *name, size_t name_len
351354
flash_write_byte((uint32_t)&(file_system_chunks[index].marker), FILE_START);
352355
flash_write_byte((uint32_t)&(file_system_chunks[index].header.name_len), name_len);
353356
flash_write_bytes((uint32_t)&(file_system_chunks[index].header.filename[0]), (uint8_t*)name, name_len);
357+
microbit_file_opened_for_writing(name, name_len);
354358
} else {
355359
if (index == FILE_NOT_FOUND) {
356360
return NULL;

src/codal_port/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,7 @@ typedef long mp_off_t;
191191
// Needed for MICROPY_PY_URANDOM_SEED_INIT_FUNC.
192192
extern uint32_t rng_generate_random_word(void);
193193

194+
// Needed for microbitfs.c:microbit_file_open.
195+
void microbit_file_opened_for_writing(const char *name, size_t name_len);
196+
194197
#endif

0 commit comments

Comments
 (0)