I’m trying to dig into advanced scripts, and I’m trying to achieve a few step sequence for my project as post action:
execute pio run -t buildfs for each environment.
copy out the folders generated for each env from .pio/build BUT keep only the .bin files in them into one main output/build folder.
execute esptool.py to merge firmware and filesystem binaries into one in every folder copied out above.
Since I never used scripts before, I’m wondering how is it possible to debug a script in work, without executing pio run every time? Where can I find more information about what can be accessed "env" and "projenv", beside of this blog? (here I’m referring to relative paths for example)
Do you have some hints for these steps from above?
Thank you very much,
Sorry if I’m duplicating a topic.
Hello again. I went through some example scripts, like tasmota’s scripts, and I realized that there a lot of APIs, which are not documented. So with a more python-based solution I came up with this script.
Import("env")
import os
import shutil
import esptool
def post_program_action(source, target, env):
targetfile = os.path.abspath(target[0].get_abspath())
filename = os.path.basename(targetfile)
startpath = os.path.dirname(targetfile)
basedir = os.path.basename(startpath)
destpath = os.path.normpath(os.path.join(startpath, '../../../../build', basedir))
print("\nCopying " + filename + " file to the build directory...\n")
print("Target file: " + targetfile)
print("Destination directory: " + destpath)
# create directories if they don't exist
if not os.path.exists(destpath):
os.makedirs(destpath)
# copy the target file to the destination, if it exist
if os.path.exists(targetfile):
shutil.copy(targetfile, destpath)
os.chdir(destpath)
command = ['--chip', 'esp8266', 'merge_bin', '-o', 'combined.bin', '0x0', 'firmware.bin', '0x300000', 'littlefs.bin']
print('Using esptool.py %s' % ' '.join(command))
esptool.main(command)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", post_program_action)
env.AddPostAction("$BUILD_DIR/littlefs.bin", post_program_action)
This will grab your firmware.bin or littlefs.bin file from your workspace, and copy it to the destpath varaible’s location. It will also create directories, if they’re missing.
To combine the scripts, I ran pip install esptool, because the esptool.py file downloaded for ESP8266 seems to be older, and does not contain the merge_bin.
Hmm, I didn’t know about this. Initally I was trying to execute %USERPROFILE%\.platformio\packages\framework-arduinoespressif8266\tools\esptool\esptool.py, and making it dynamic with the envar.
How would you add this pio command to the python script?
Ok, so for those who didn’t get this, first you have to define platform variable, which is platform = env.PioPlatform(), then you can get you can access more APIs, one of them can provide the location of esptool.py with this: print(platform.get_package_dir("tool-esptoolpy") or "", "esptool.py").
I still don’t know how to use this extra information. @maxgerhardt if you could make an example by modifying my function would be a HUGE help, thank you.