Skip to content

Commit e9be6a3

Browse files
committed
extmod/vfs_fat: Object-oriented encapsulation of FatFs VFS.
This implements OO interface based on existing fsusermount code and with minimal changes to it, to serve as a proof of concept of OO interface. Examle of usage: bdev = RAMFS(48) uos.VfsFat.mkfs(bdev) vfs = uos.VfsFat(bdev, "/ramdisk") f = vfs.open("foo", "w") f.write("hello!") f.close()
1 parent dc3eb55 commit e9be6a3

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

extmod/fsusermount.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "lib/fatfs/ff.h"
3535
#include "fsusermount.h"
3636

37-
STATIC mp_obj_t fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) {
37+
fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) {
3838
static const mp_arg_t allowed_args[] = {
3939
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
4040
{ MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
@@ -67,6 +67,7 @@ STATIC mp_obj_t fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_
6767
if (res != FR_OK) {
6868
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount"));
6969
}
70+
return NULL;
7071
} else {
7172
// mount
7273
size_t i = 0;
@@ -128,6 +129,7 @@ STATIC mp_obj_t fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_
128129
goto mkfs_error;
129130
}
130131
MP_STATE_PORT(fs_user_mount)[i] = NULL;
132+
return NULL;
131133
}
132134
} else {
133135
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount"));
@@ -144,12 +146,13 @@ STATIC mp_obj_t fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_
144146
f_getfree(vfs->str, &nclst, &fatfs);
145147
printf(" on %s with %u bytes free\n", vfs->str, (uint)(nclst * fatfs->csize * 512));
146148
*/
149+
return vfs;
147150
}
148-
return mp_const_none;
149151
}
150152

151153
STATIC mp_obj_t fatfs_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
152-
return fatfs_mount_mkfs(n_args, pos_args, kw_args, false);
154+
fatfs_mount_mkfs(n_args, pos_args, kw_args, false);
155+
return mp_const_none;
153156
}
154157
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount);
155158

@@ -191,7 +194,8 @@ STATIC mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in) {
191194
MP_DEFINE_CONST_FUN_OBJ_1(fsuser_umount_obj, fatfs_umount);
192195

193196
STATIC mp_obj_t fatfs_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
194-
return fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
197+
fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
198+
return mp_const_none;
195199
}
196200
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj, 2, fatfs_mkfs);
197201

extmod/fsusermount.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define BP_IOCTL_SEC_SIZE (5)
3838

3939
typedef struct _fs_user_mount_t {
40+
mp_obj_base_t base;
4041
const char *str;
4142
uint16_t len; // length of str
4243
uint16_t flags;
@@ -53,6 +54,8 @@ typedef struct _fs_user_mount_t {
5354
FATFS fatfs;
5455
} fs_user_mount_t;
5556

57+
fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs);
58+
5659
MP_DECLARE_CONST_FUN_OBJ(fsuser_mount_obj);
5760
MP_DECLARE_CONST_FUN_OBJ(fsuser_umount_obj);
5861
MP_DECLARE_CONST_FUN_OBJ(fsuser_mkfs_obj);

extmod/vfs_fat.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Damien P. George
7+
* Copyright (c) 2016 Paul Sokolovsky
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/mpconfig.h"
29+
#if MICROPY_VFS_FAT
30+
31+
#include "py/nlr.h"
32+
#include "py/runtime.h"
33+
#include "lib/fatfs/ff.h"
34+
#include "lib/fatfs/diskio.h"
35+
#include "stmhal/file.h"
36+
#include "fsusermount.h"
37+
38+
#define mp_obj_fat_vfs_t fs_user_mount_t
39+
40+
STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
41+
mp_arg_check_num(n_args, n_kw, 2, 2, false);
42+
mp_obj_fat_vfs_t *vfs = fatfs_mount_mkfs(n_args, args, (mp_map_t*)&mp_const_empty_map, false);
43+
vfs->base.type = type;
44+
return vfs;
45+
}
46+
47+
STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
48+
mp_obj_t args[] = {bdev_in, MP_OBJ_NEW_QSTR(MP_QSTR_mkfs)};
49+
fatfs_mount_mkfs(2, args, (mp_map_t*)&mp_const_empty_map, true);
50+
return mp_const_none;
51+
}
52+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
53+
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
54+
55+
mp_obj_t fat_vfs_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
56+
// Skip self
57+
return fatfs_builtin_open(n_args - 1, args + 1, kwargs);
58+
}
59+
MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fat_vfs_open);
60+
61+
STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
62+
{ MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
63+
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
64+
};
65+
STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
66+
67+
const mp_obj_type_t mp_fat_vfs_type = {
68+
{ &mp_type_type },
69+
.name = MP_QSTR_VfsFat,
70+
.make_new = fat_vfs_make_new,
71+
.locals_dict = (mp_obj_dict_t*)&fat_vfs_locals_dict,
72+
};
73+
74+
#endif // MICROPY_VFS_FAT

0 commit comments

Comments
 (0)