Skip to content

Commit

Permalink
codal_port/microbitfs: Add a hook for when files are opened for writing.
Browse files Browse the repository at this point in the history
And reset the log if main.py is opened for writing.

Addresses issue #95.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Aug 21, 2022
1 parent 88670f6 commit 6ade36b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/codal_port/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "drv_display.h"
#include "modmicrobit.h"

#define MAIN_PY "main.py"

// Use a fixed static buffer for the heap.
static char heap[64 * 1024];

Expand All @@ -65,7 +67,7 @@ void mp_main(void) {
mp_init();

if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
const char *main_py = "main.py";
const char *main_py = MAIN_PY;
if (mp_import_stat(main_py) == MP_IMPORT_STAT_FILE) {
// exec("main.py")
microbit_pyexec_file(main_py);
Expand Down Expand Up @@ -183,6 +185,14 @@ void microbit_pyexec_file(const char *filename) {
}
}

// Callback from microbitfs when a file is opened for writing.
void microbit_file_opened_for_writing(const char *name, size_t name_len) {
if (name_len == strlen(MAIN_PY) && memcmp(name, MAIN_PY, name_len) == 0) {
// The file main.py is changed, so invalidate the data logging storage (fast erase).
microbit_hal_log_delete(false);
}
}

#if MICROPY_MBFS
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
return uos_mbfs_new_reader(filename);
Expand Down
4 changes: 4 additions & 0 deletions src/codal_port/microbitfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* THE SOFTWARE.
*/

// This is a copy of the file micropython:ports/nrf/modules/uos/microbitfs.c with
// a call to `microbit_file_opened_for_writing` added in `microbit_file_open`.

#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -351,6 +354,7 @@ STATIC file_descriptor_obj *microbit_file_open(const char *name, size_t name_len
flash_write_byte((uint32_t)&(file_system_chunks[index].marker), FILE_START);
flash_write_byte((uint32_t)&(file_system_chunks[index].header.name_len), name_len);
flash_write_bytes((uint32_t)&(file_system_chunks[index].header.filename[0]), (uint8_t*)name, name_len);
microbit_file_opened_for_writing(name, name_len);
} else {
if (index == FILE_NOT_FOUND) {
return NULL;
Expand Down
3 changes: 3 additions & 0 deletions src/codal_port/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,7 @@ typedef long mp_off_t;
// Needed for MICROPY_PY_URANDOM_SEED_INIT_FUNC.
extern uint32_t rng_generate_random_word(void);

// Needed for microbitfs.c:microbit_file_open.
void microbit_file_opened_for_writing(const char *name, size_t name_len);

#endif

0 comments on commit 6ade36b

Please sign in to comment.