Skip to content

Added utime() to the os library #6923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions extmod/vfs_fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,35 @@ STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);

STATIC mp_obj_t vfs_fat_utime(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t times_in) {
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
const char *path = mp_obj_str_get_str(path_in);
if (!mp_obj_is_tuple_compatible(times_in)) {
mp_raise_type_arg(&mp_type_TypeError, times_in);
}

mp_obj_t *otimes;
mp_obj_get_array_fixed_n(times_in, 2, &otimes);

// Validate that both elements of the tuple are int and discard the second one
int time[2];
time[0] = mp_obj_get_int(otimes[0]);
time[1] = mp_obj_get_int(otimes[1]);
timeutils_struct_time_t tm;
timeutils_seconds_since_epoch_to_struct_time(time[0], &tm);

FILINFO fno;
fno.fdate = (WORD)(((tm.tm_year - 1980) * 512U) | tm.tm_mon * 32U | tm.tm_mday);
fno.ftime = (WORD)(tm.tm_hour * 2048U | tm.tm_min * 32U | tm.tm_sec / 2U);
FRESULT res = f_utime(&self->fatfs, path, &fno);
if (res != FR_OK) {
mp_raise_OSError_fresult(res);
}

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_utime_obj, vfs_fat_utime);

#if MICROPY_FATFS_USE_LABEL
STATIC mp_obj_t vfs_fat_getlabel(mp_obj_t self_in) {
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
Expand Down Expand Up @@ -451,6 +480,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&fat_vfs_utime_obj) },
#if MICROPY_FATFS_USE_LABEL
{ MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) },
#endif
Expand Down
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ CIRCUITPY_BUILD_EXTENSIONS = bin,uf2
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

CIRCUITPY_RAINBOWIO = 0
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ CIRCUITPY_BUILD_EXTENSIONS = bin,uf2
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

CIRCUITPY_RAINBOWIO = 0
5 changes: 3 additions & 2 deletions ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ CIRCUITPY_FULL_BUILD = 0
# A number of modules are removed for RFM69 to make room for frozen libraries.
# Many I/O functions are not available.
CIRCUITPY_ANALOGIO = 1
CIRCUITPY_BUSDEVICE = 1
CIRCUITPY_RAINBOWIO = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_USB_HID = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_BUSDEVICE = 1

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM69
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ CIRCUITPY_BITBANG_APA102 = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CTARGET = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_ONEWIREIO = 0
CIRCUITPY_PARALLELDISPLAY = 0
CIRCUITPY_PIXELBUF = 0
CIRCUITPY_PS2IO = 0
CIRCUITPY_PULSEIO = 0
CIRCUITPY_PWMIO = 0
CIRCUITPY_RAINBOWIO = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
CIRCUITPY_SAMD = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "W25Q32FV"
LONGINT_IMPL = MPZ

CIRCUITPY_RAINBOWIO = 0
12 changes: 12 additions & 0 deletions shared-bindings/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ STATIC mp_obj_t os_urandom(mp_obj_t size_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);

//| def utime(path: str, times: Tuple[int, int]) -> None:
//| """Change the timestamp of a file."""
//| ...
//|
STATIC mp_obj_t os_utime(mp_obj_t path_in, mp_obj_t times_in) {
const char *path = mp_obj_str_get_str(path_in);
common_hal_os_utime(path, times_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(os_utime_obj, os_utime);

STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) },

Expand All @@ -264,6 +275,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&os_stat_obj) },
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&os_statvfs_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&os_remove_obj) }, // unlink aliases to remove
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&os_utime_obj) },

{ MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&os_sync_obj) },

Expand Down
1 change: 1 addition & 0 deletions shared-bindings/os/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void common_hal_os_rename(const char *old_path, const char *new_path);
void common_hal_os_rmdir(const char *path);
mp_obj_t common_hal_os_stat(const char *path);
mp_obj_t common_hal_os_statvfs(const char *path);
void common_hal_os_utime(const char *path, mp_obj_t times);

// Returns true if data was correctly sourced from a true random number generator.
bool common_hal_os_urandom(uint8_t *buffer, mp_uint_t length);
Expand Down
7 changes: 7 additions & 0 deletions shared-module/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,10 @@ mp_obj_t common_hal_os_statvfs(const char *path) {
}
return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out);
}

void common_hal_os_utime(const char *path, mp_obj_t times) {
mp_obj_t args[2];
mp_vfs_mount_t *vfs = lookup_path(path, &args[0]);
args[1] = times;
mp_vfs_proxy_call(vfs, MP_QSTR_utime, 2, args);
}