Skip to content

Commit 6ae9e26

Browse files
committed
some code cleanup
1 parent c7ce0d0 commit 6ae9e26

File tree

8 files changed

+158
-181
lines changed

8 files changed

+158
-181
lines changed

builder/__init__.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,20 @@ def update_mphalport(target):
6666
data = f.read().decode('utf-8')
6767

6868
if '#ifndef _MPHALPORT_H_' not in data:
69-
data = f'#ifndef _MPHALPORT_H_\n#define _MPHALPORT_H_\n{data}\n#endif /* _MPHALPORT_H_ */\n'
69+
data = (
70+
f'#ifndef _MPHALPORT_H_\n'
71+
f'#define _MPHALPORT_H_\n'
72+
f'{data}\n'
73+
f'#endif /* _MPHALPORT_H_ */\n'
74+
)
7075
with open(mphalport_path, 'wb') as f:
7176
f.write(data.encode('utf-8'))
7277

7378

74-
def generate_manifest(script_dir, lvgl_api, manifest_path, displays, indevs, frozen_manifest, *addl_manifest_files):
79+
def generate_manifest(
80+
script_dir, lvgl_api, manifest_path, displays,
81+
indevs, frozen_manifest, *addl_manifest_files
82+
):
7583
if not os.path.exists('build'):
7684
os.mkdir('build')
7785

@@ -98,8 +106,14 @@ def generate_manifest(script_dir, lvgl_api, manifest_path, displays, indevs, fro
98106
f'{api_path}/frozen/indev/pointer_framework.py',
99107
f'{api_path}/fs_driver.py',
100108
f'{script_dir}/api_drivers/common_api_drivers/frozen/other/i2c.py',
101-
f'{script_dir}/api_drivers/common_api_drivers/frozen/other/io_expander_framework.py',
102-
f'{script_dir}/api_drivers/common_api_drivers/frozen/other/task_handler.py'
109+
(
110+
f'{script_dir}/api_drivers/common_api_drivers/'
111+
f'frozen/other/io_expander_framework.py'
112+
),
113+
(
114+
f'{script_dir}/api_drivers/common_api_drivers/'
115+
f'frozen/other/task_handler.py'
116+
)
103117
]
104118

105119
for file in frozen_manifest_files:
@@ -122,7 +136,9 @@ def generate_manifest(script_dir, lvgl_api, manifest_path, displays, indevs, fro
122136

123137
for file in indevs:
124138
if not os.path.exists(file):
125-
tmp_file = f'{script_dir}/api_drivers/common_api_drivers/indev/{file}.py'
139+
tmp_file = (
140+
f'{script_dir}/api_drivers/common_api_drivers/indev/{file}.py'
141+
)
126142

127143
if not os.path.exists(tmp_file):
128144
raise RuntimeError(f'File not found "{file}"')
@@ -144,7 +160,9 @@ def generate_manifest(script_dir, lvgl_api, manifest_path, displays, indevs, fro
144160

145161
for file in displays:
146162
if not os.path.exists(file):
147-
tmp_file = f'{script_dir}/api_drivers/common_api_drivers/display/{file}.py'
163+
tmp_file = (
164+
f'{script_dir}/api_drivers/common_api_drivers/display/{file}.py'
165+
)
148166

149167
if not os.path.exists(tmp_file):
150168
raise RuntimeError(f'File not found "{file}"')
@@ -440,7 +458,9 @@ def mpy_cross():
440458
sys.exit(return_code)
441459

442460

443-
def build_manifest(target, script_dir, lvgl_api, displays, indevs, frozen_manifest):
461+
def build_manifest(
462+
target, script_dir, lvgl_api, displays, indevs, frozen_manifest
463+
):
444464
update_mphalport(target)
445465
if target == 'teensy':
446466
manifest_path = f'lib/micropython/ports/{target}/manifest.py'
@@ -449,7 +469,10 @@ def build_manifest(target, script_dir, lvgl_api, displays, indevs, frozen_manife
449469
if not os.path.exists(manifest_path):
450470
raise RuntimeError(f'Unable to locate manifest file "{manifest_path}"')
451471

452-
generate_manifest(script_dir, lvgl_api, manifest_path, displays, indevs, frozen_manifest)
472+
generate_manifest(
473+
script_dir, lvgl_api, manifest_path,
474+
displays, indevs, frozen_manifest
475+
)
453476

454477

455478
def parse_args(extra_args, lv_cflags, board):

builder/esp32.py

Lines changed: 62 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,11 @@
11
import os
22
import sys
33
from argparse import ArgumentParser
4-
from . import spawn as _spawn
4+
from . import spawn
55
from . import generate_manifest
66
from . import update_mphalport
77

88

9-
spawn = _spawn
10-
11-
12-
install_ps_script = '''\
13-
build\\esp-idf-tools-setup-offline-5.0.4.exe /VERYSILENT /SUPPRESSMSGBOXES /SP- /NOCANCEL /IDFVERSION=5.0.4 /IDFDIR=C:\\esp-idf
14-
$InstallerProcess = Get-Process esp-idf-tools-setup
15-
Wait-Process -Id $InstallerProcess.id
16-
'''
17-
18-
idf_env_bat_script = '''
19-
@echo off
20-
21-
set "IDF_PATH=C:\\esp-idf\\frameworks\\esp-idf-v5.0.4"
22-
set "IDF_TOOLS_PATH=C:\\esp-idf\\frameworks\\esp-idf-v5.0.4\\tools"
23-
set "PATH=%IDF_TOOLS_PATH%;%PATH%"
24-
idf-env config get --property python --idf-path %IDF_PATH%\\ > build\\idf-python-path.txt
25-
set /P "IDF_PYTHON=< build\\idf-python-path.txt"
26-
idf-env config get --property gitPath > build\\idf-git-path.txt
27-
set /P "IDF_GIT=< build\\idf-git-path.txt"
28-
set "PREFIX=%IDF_PYTHON% %IDF_PATH%"
29-
DOSKEY idf.py=%PREFIX%\\tools\\idf.py $*
30-
DOSKEY esptool.py=%PREFIX%\\components\\esptool_py\\esptool\\esptool.py $*
31-
DOSKEY espefuse.py=%PREFIX%\\components\\esptool_py\\esptool\\espefuse.py $*
32-
DOSKEY espsecure.py=%PREFIX%\\components\\esptool_py\\esptool\\espsecure.py $*
33-
DOSKEY otatool.py=%PREFIX%\\components\\app_update\\otatool.py $*
34-
DOSKEY parttool.py=%PREFIX%\\components\\partition_table\\parttool.py $*
35-
set PYTHONPATH=
36-
set PYTHONHOME=
37-
set PYTHONNOUSERSITE=True
38-
set "IDF_PYTHON_DIR=%IDF_PYTHON%"
39-
set "IDF_GIT_DIR=%IDF_GIT%"
40-
set "PATH=%IDF_PYTHON_DIR%;%IDF_GIT_DIR%;%PATH%"'
41-
%IDF_PATH%\\export.bat'
42-
'''
43-
44-
45-
def get_idf_build_environment():
46-
global spawn
47-
48-
if sys.platform.startswith('win'):
49-
50-
if not os.path.exists('build'):
51-
os.mkdir('build')
52-
53-
import requests
54-
55-
print('downloading esp-idf toolkit for windows')
56-
57-
url = 'https://dl.espressif.com/dl/idf-installer/esp-idf-tools-setup-offline-5.0.4.exe'
58-
59-
response = requests.get(url, stream=True)
60-
with open("build/esp-idf-tools-setup-offline-5.0.4.exe", mode="wb") as file:
61-
for chunk in response.iter_content(chunk_size=10 * 1024):
62-
file.write(chunk)
63-
64-
with open('build/install_esp-idf.ps1', 'w') as file:
65-
file.write(install_ps_script)
66-
67-
with open('build/setup_environment.bat', 'w') as file:
68-
file.write(idf_env_bat_script)
69-
70-
cmd_ = ['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -NonInteractive -File "build\\install_esp-idf.ps1"']
71-
result, _ = spawn(cmd_)
72-
if result != 0:
73-
sys.exit(result)
74-
75-
old_spawn = spawn
76-
77-
def _new_spawn(cmds, *args, **kwargs):
78-
framework_cmds = [
79-
['build\\setup_environment.bat']
80-
]
81-
82-
if isinstance(cmds[0], str):
83-
cmds = [cmds[:]]
84-
85-
framework_cmds.extend(cmds)
86-
87-
return old_spawn(framework_cmds, *args, **kwargs)
88-
89-
spawn = _new_spawn
90-
91-
929
def get_partition_file_name(otp):
9310
if 'Running cmake in directory ' in otp:
9411
build_path = otp.split('Running cmake in directory ', 1)[-1]
@@ -351,13 +268,18 @@ def build_commands(_, extra_args, __, lv_cflags, ___):
351268

352269
def get_idf_version():
353270
if 'ESP_IDF_VERSION' in os.environ:
354-
exit_code, data = spawn(['python3', 'idf.py', '--version'], out_to_screen=False)
271+
exit_code, data = spawn(
272+
['python3', 'idf.py', '--version'],
273+
out_to_screen=False
274+
)
355275
version = data.split('v')[-1].split('-')[0]
356276
if version:
357277
return version
358278

359279

360-
def build_manifest(target, script_dir, lvgl_api, displays, indevs, frozen_manifest):
280+
def build_manifest(
281+
target, script_dir, lvgl_api, displays, indevs, frozen_manifest
282+
):
361283
update_mphalport(target)
362284

363285
with open(f'lib/micropython/ports/esp32/boards/sdkconfig.base', 'r') as f:
@@ -374,7 +296,10 @@ def build_manifest(target, script_dir, lvgl_api, displays, indevs, frozen_manife
374296

375297
manifest_path = 'lib/micropython/ports/esp32/boards/manifest.py'
376298

377-
generate_manifest(script_dir, lvgl_api, manifest_path, displays, indevs, frozen_manifest)
299+
generate_manifest(
300+
script_dir, lvgl_api, manifest_path,
301+
displays, indevs, frozen_manifest
302+
)
378303

379304

380305
def clean():
@@ -400,13 +325,19 @@ def setup_idf_environ():
400325

401326
if os.path.exists(os.path.join(idf_path, 'export.sh')):
402327

403-
# this removes any IDF environment variable that may exist if the user
404-
# has the ESP-IDF installed
405-
env = {k: v for k, v in os.environ.items() if not k.startswith('IDF')}
328+
# this removes any IDF environment variable that may
329+
# exist if the user has the ESP-IDF installed
330+
env = {
331+
k: v for k, v in os.environ.items() if not k.startswith('IDF')
332+
}
406333
py_path = os.path.split(sys.executable)[0]
407334
idf_path = os.path.abspath(idf_path)
408335
idf_tools_path = os.path.join(idf_path, 'tools')
409-
env['PATH'] = py_path + os.pathsep + os.pathsep + idf_tools_path + os.pathsep + env.get('PATH', '')
336+
env['PATH'] = (
337+
py_path + os.pathsep +
338+
os.pathsep + idf_tools_path +
339+
os.pathsep + env.get('PATH', '')
340+
)
410341
env['IDF_PATH'] = idf_path
411342

412343
cmds = [
@@ -419,12 +350,15 @@ def setup_idf_environ():
419350
if 'GITHUB_RUN_ID' in env:
420351
if sys.platform.startswith('win'):
421352
env_cmds = [
422-
['echo', f"{py_path}", '|', 'Out-File', '-Append',
423-
'-FilePath', '$env:GITHUB_PATH', '-Encoding', 'utf8'],
424-
['echo', f"{idf_path}", '|', 'Out-File', '-Append',
425-
'-FilePath', '$env:GITHUB_PATH', '-Encoding', 'utf8'],
426-
['echo', f"{idf_tools_path}", '|', 'Out-File', '-Append',
427-
'-FilePath', '$env:GITHUB_PATH', '-Encoding', 'utf8']
353+
['echo', f"{py_path}", '|', 'Out-File',
354+
'-Append', '-FilePath', '$env:GITHUB_PATH',
355+
'-Encoding', 'utf8'],
356+
['echo', f"{idf_path}", '|', 'Out-File',
357+
'-Append', '-FilePath', '$env:GITHUB_PATH',
358+
'-Encoding', 'utf8'],
359+
['echo', f"{idf_tools_path}", '|', 'Out-File',
360+
'-Append', '-FilePath', '$env:GITHUB_PATH',
361+
'-Encoding', 'utf8']
428362
]
429363
else:
430364
env_cmds = [
@@ -485,21 +419,24 @@ def submodules():
485419
cmds = [
486420
[f'export "IDF_PATH={os.path.abspath(idf_path)}"'],
487421
['cd', idf_path],
488-
['git', 'submodule', 'update', '--init',
489-
'components/bt/host/nimble/nimble',
490-
'components/esp_wifi',
491-
'components/esptool_py/esptool',
492-
'components/lwip/lwip',
493-
'components/mbedtls/mbedtls',
494-
'components/bt/controller/lib_esp32',
495-
'components/bt/controller/lib_esp32c3_family'
496-
], ['./install.sh', 'all']]
422+
[
423+
'git', 'submodule', 'update', '--init',
424+
'components/bt/host/nimble/nimble',
425+
'components/esp_wifi',
426+
'components/esptool_py/esptool',
427+
'components/lwip/lwip',
428+
'components/mbedtls/mbedtls',
429+
'components/bt/controller/lib_esp32',
430+
'components/bt/controller/lib_esp32c3_family'
431+
], ['./install.sh', 'all']]
497432

498433
print('setting up ESP-IDF v5.0.4')
499434
print('this might take a bit...')
500435
print()
501436

502-
env = {k: v for k, v in os.environ.items() if not k.startswith('IDF')}
437+
env = {
438+
k: v for k, v in os.environ.items() if not k.startswith('IDF')
439+
}
503440
env['IDF_PATH'] = os.path.abspath(idf_path)
504441

505442
result, _ = spawn(cmds, env=env)
@@ -519,9 +456,7 @@ def submodules():
519456
submodules_cmd
520457
]
521458
else:
522-
get_idf_build_environment()
523-
cmds = submodules_cmd
524-
env = None
459+
raise RuntimeError('compiling on windows is not supported at this time')
525460

526461
return_code, _ = spawn(cmds, env=env)
527462
if return_code != 0:
@@ -587,7 +522,11 @@ def compile(): # NOQA
587522

588523
base_config = '\n'.join(base_config)
589524

590-
with open('lib/micropython/ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board', 'w') as f:
525+
sdkconfig_board_path = (
526+
'lib/micropython/ports/esp32/'
527+
'boards/ESP32_GENERIC_S3/sdkconfig.board'
528+
)
529+
with open(sdkconfig_board_path, 'w') as f:
591530
f.write(base_config + '\n')
592531

593532
if board in ('ESP32_GENERIC_S2', 'ESP32_GENERIC_S3'):
@@ -597,7 +536,10 @@ def compile(): # NOQA
597536
with open(mphalport_path, 'rb') as f:
598537
data = f.read().decode('utf-8')
599538

600-
data = data.replace('#elif CONFIG_USB_OTG_SUPPORTED', '#elif MP_USB_OTG')
539+
data = data.replace(
540+
'#elif CONFIG_USB_OTG_SUPPORTED',
541+
'#elif MP_USB_OTG'
542+
)
601543

602544
with open(mphalport_path, 'wb') as f:
603545
f.write(data.encode('utf-8'))
@@ -615,7 +557,10 @@ def compile(): # NOQA
615557
with open(main_path, 'wb') as f:
616558
f.write(data.encode('utf-8'))
617559

618-
mpconfigboard_path = f'lib/micropython/ports/esp32/boards/{board}/mpconfigboard.h'
560+
mpconfigboard_path = (
561+
f'lib/micropython/ports/esp32/boards/{board}/mpconfigboard.h'
562+
)
563+
619564
with open(mpconfigboard_path, 'rb') as f:
620565
data = f.read().decode('utf-8')
621566

@@ -768,7 +713,10 @@ def compile(): # NOQA
768713

769714
esp_tool_path, output = output.split('esptool.py ', 1)
770715
esp_tool_path += 'esptool.py'
771-
esp_tool_path = esp_tool_path.replace('../../../', os.getcwd() + '/lib/')
716+
esp_tool_path = esp_tool_path.replace(
717+
'../../../',
718+
os.getcwd() + '/lib/'
719+
)
772720

773721
out_cmd = []
774722

@@ -830,9 +778,6 @@ def compile(): # NOQA
830778
print(
831779
python_path, esp_tool_path, '-p (PORT) -b 460800 erase_flash'
832780
)
833-
# print()
834-
# print(python_path, esp_tool_path, out_cmd.replace('-b 460800', '-b 921600'))
835-
# print()
836781

837782
cmd = f'{python_path} {esp_tool_path} {out_cmd}'
838783
cmd = cmd.split('write_flash', 1)[0]

0 commit comments

Comments
 (0)