Skip to content

Commit a062942

Browse files
committed
pybricks.common.System: Implement reset_storage.
1 parent fc37994 commit a062942

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
### Added
88

99
- Added optional `calibrated=True` parameter to `acceleration()` and `up()` and
10-
`angular_velocity()` methods of the IMU ([support#943]).
10+
`angular_velocity()` and `rotation` methods of the IMU ([support#943]).
1111
- Implemented `hub.imu.orientation()` to give the rotation matrix of the hub or
1212
robot with respect to the inertial frame.
1313
- Added calibration parameters that can be set for angular velocity offset and
1414
scale and acceleration offset and scale.
15+
- Added `hub.system.reset_storage` to restore storage and settings to default
16+
state.
1517

1618
### Changed
1719

lib/pbio/sys/storage.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ void pbsys_storage_request_write(void) {
176176

177177
/**
178178
* Erases user data, erases user program meta data and restores user settings
179-
* to default.
179+
* to default. Does not actually erase program data, but it becomes
180+
* inaccessible, thus effectively erased.
180181
*
181182
* This resets the data in RAM, which will be saved during power off just as
182183
* with any other storage operations.
@@ -188,6 +189,8 @@ void pbsys_storage_request_write(void) {
188189
void pbsys_storage_reset_storage(void) {
189190
// Reset storage except for program data. It is sufficient to set its
190191
// size to 0, which is what happens here since it is in the map.
192+
// The program data itself is not overwritten with zeros because the user
193+
// may be calling this while a program using this data is running.
191194
memset(map, 0, sizeof(pbsys_storage_data_map_t));
192195

193196
// Apply default settings.

pybricks/common/pb_type_system.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ static mp_obj_t pb_type_System_storage(size_t n_args, const mp_obj_t *pos_args,
142142
}
143143
static MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_System_storage_obj, 0, pb_type_System_storage);
144144

145+
static mp_obj_t pb_type_System_reset_storage(void) {
146+
pbsys_storage_reset_storage();
147+
return mp_const_none;
148+
}
149+
static MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_reset_storage_obj, pb_type_System_reset_storage);
150+
145151
#endif // PBIO_CONFIG_ENABLE_SYS
146152

147153
// dir(pybricks.common.System)
@@ -153,6 +159,7 @@ static const mp_rom_map_elem_t common_System_locals_dict_table[] = {
153159
#endif // PBDRV_CONFIG_RESET
154160
#if PBIO_CONFIG_ENABLE_SYS
155161
{ MP_ROM_QSTR(MP_QSTR_set_stop_button), MP_ROM_PTR(&pb_type_System_set_stop_button_obj) },
162+
{ MP_ROM_QSTR(MP_QSTR_reset_storage), MP_ROM_PTR(&pb_type_System_reset_storage_obj) },
156163
{ MP_ROM_QSTR(MP_QSTR_shutdown), MP_ROM_PTR(&pb_type_System_shutdown_obj) },
157164
{ MP_ROM_QSTR(MP_QSTR_storage), MP_ROM_PTR(&pb_type_System_storage_obj) },
158165
#endif

0 commit comments

Comments
 (0)