Skip to content

Add directory support to fs_driver #399

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
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
38 changes: 38 additions & 0 deletions api_drivers/py_api_drivers/fs_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,41 @@ def _fs_write_cb(drv, fs_file, buf, btw, bw):

return lv.FS_RES.OK

def _fs_dir_open_cb(drv, path):
#print(f"_fs_dir_open_cb for path '{path}'")
try:
import os # for ilistdir()
return {'iterator' : os.ilistdir(path)}
except Exception as e:
print(f"_fs_dir_open_cb exception: {e}")
return None

def _fs_dir_read_cb(drv, lv_fs_dir_t, buf, btr):
try:
iterator = lv_fs_dir_t.__cast__()['iterator']
nextfile = iterator.__next__()
#print(f"nextfile: {nextfile}")
filename = nextfile[0]
entry_type = nextfile[1] # Type field
if entry_type == 0x4000:
#print(f"{filename} is a directory")
filename = f"/{filename}"
# Convert filename to bytes with null terminator
tmp_data_bytes = filename.encode() + b'\x00'
buf.__dereference__(btr)[0:len(tmp_data_bytes)] = tmp_data_bytes
return lv.FS_RES.OK
except StopIteration:
# Clear buffer and return FS_ERR when iteration ends
buf.__dereference__(btr)[0:1] = b'\x00' # Empty string (null byte)
return lv.FS_RES.NOT_EX # Next entry "does not exist"
except Exception as e:
print(f"_fs_dir_read_cb exception: {e}")
return lv.FS_RES.UNKNOWN

def _fs_dir_close_cb(drv, lv_fs_dir_t):
#print(f"_fs_dir_close_cb called")
# No need to cleanup the iterator so nothing to do
return lv.FS_RES.OK

def fs_register(fs_drv, letter, cache_size=500):

Expand All @@ -85,6 +120,9 @@ def fs_register(fs_drv, letter, cache_size=500):
fs_drv.seek_cb = _fs_seek_cb
fs_drv.tell_cb = _fs_tell_cb
fs_drv.close_cb = _fs_close_cb
fs_drv.dir_open_cb = _fs_dir_open_cb
fs_drv.dir_read_cb = _fs_dir_read_cb
#fs_drv.dir_close_cb = _fs_dir_close_cb

if cache_size >= 0:
fs_drv.cache_size = cache_size
Expand Down
17 changes: 12 additions & 5 deletions gen/python_api_gen_mpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2865,12 +2865,18 @@ def create_helper_struct(struct_str):


def build_callback_func_arg(arg, index, func, func_name = None):
arg_type = get_type(arg.type, remove_quals = True)
arg_type = get_type(arg.type, remove_quals = False)
cast = '(void*)' if isinstance(arg.type, c_ast.PtrDecl) else '' # needed when field is const. casting to void overrides it
if arg_type not in lv_to_mp or not lv_to_mp[arg_type]:
try_generate_type(arg.type)

if arg_type == 'char *':
converter = 'ptr_to_mp'
else:
arg_type = get_type(arg.type, remove_quals = True)
if arg_type not in lv_to_mp or not lv_to_mp[arg_type]:
raise MissingConversionException("Callback: Missing conversion to %s" % arg_type)
try_generate_type(arg.type)
if arg_type not in lv_to_mp or not lv_to_mp[arg_type]:
raise MissingConversionException("Callback: Missing conversion to %s" % arg_type)
converter = lv_to_mp[arg_type]

arg_metadata = {'c_type': arg_type, 'py_type': get_py_type(arg_type)}

Expand All @@ -2880,8 +2886,9 @@ def build_callback_func_arg(arg, index, func, func_name = None):
arg_metadata['name'] = None

callback_metadata[func_name]['args'].append(arg_metadata)

return 'mp_args[{i}] = {convertor}({cast}arg{i});'.format(
convertor = lv_to_mp[arg_type],
convertor = converter,
i = index, cast = cast)


Expand Down