11import os
22import sys
33from argparse import ArgumentParser
4- from .import spawn as _spawn
4+ from .import spawn
55from .import generate_manifest
66from .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-
929def get_partition_file_name (otp ):
9310if 'Running cmake in directory ' in otp :
9411build_path = otp .split ('Running cmake in directory ' ,1 )[- 1 ]
@@ -351,13 +268,18 @@ def build_commands(_, extra_args, __, lv_cflags, ___):
351268
352269def get_idf_version ():
353270if '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+ )
355275version = data .split ('v' )[- 1 ].split ('-' )[0 ]
356276if version :
357277return 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+ ):
361283update_mphalport (target )
362284
363285with 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
375297manifest_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
380305def clean ():
@@ -400,13 +325,19 @@ def setup_idf_environ():
400325
401326if 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+ }
406333py_path = os .path .split (sys .executable )[0 ]
407334idf_path = os .path .abspath (idf_path )
408335idf_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+ )
410341env ['IDF_PATH' ]= idf_path
411342
412343cmds = [
@@ -419,12 +350,15 @@ def setup_idf_environ():
419350if 'GITHUB_RUN_ID' in env :
420351if sys .platform .startswith ('win' ):
421352env_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 ]
429363else :
430364env_cmds = [
@@ -485,21 +419,24 @@ def submodules():
485419cmds = [
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
498433print ('setting up ESP-IDF v5.0.4' )
499434print ('this might take a bit...' )
500435print ()
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+ }
503440env ['IDF_PATH' ]= os .path .abspath (idf_path )
504441
505442result ,_ = spawn (cmds ,env = env )
@@ -519,9 +456,7 @@ def submodules():
519456submodules_cmd
520457 ]
521458else :
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
526461return_code ,_ = spawn (cmds ,env = env )
527462if return_code != 0 :
@@ -587,7 +522,11 @@ def compile(): # NOQA
587522
588523base_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 :
591530f .write (base_config + '\n ' )
592531
593532if board in ('ESP32_GENERIC_S2' ,'ESP32_GENERIC_S3' ):
@@ -597,7 +536,10 @@ def compile(): # NOQA
597536with open (mphalport_path ,'rb' )as f :
598537data = 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
602544with open (mphalport_path ,'wb' )as f :
603545f .write (data .encode ('utf-8' ))
@@ -615,7 +557,10 @@ def compile(): # NOQA
615557with open (main_path ,'wb' )as f :
616558f .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+
619564with open (mpconfigboard_path ,'rb' )as f :
620565data = f .read ().decode ('utf-8' )
621566
@@ -768,7 +713,10 @@ def compile(): # NOQA
768713
769714esp_tool_path ,output = output .split ('esptool.py ' ,1 )
770715esp_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
773721out_cmd = []
774722
@@ -830,9 +778,6 @@ def compile(): # NOQA
830778print (
831779python_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
837782cmd = f'{ python_path } { esp_tool_path } { out_cmd } '
838783cmd = cmd .split ('write_flash' ,1 )[0 ]