scripting the Arduino-cli

I asked this question on Stackoverflow originally (since it’s also partly a Raspberry Pi question) and nobody has yet answered it. So I’ll try here. I have a setup that includes a Raspberry Pi Zero W acting as a host for an Arduino Pro Mini (the little kind with an Atmega328 and a reset button that you have to flash via 3.3v serial). Normally the sketch on the Arduino is used to send the values from analog pins to the Raspberry Pi via I2C, and that all works great. But recently I thought it would be nice to be able to have the Raspberry Pi be able to reflash the Arduino with new versions of the sketch. To do this, I’ve wired up the Rasberry Pi’s serial port to the Arduino’s serial port. I’ve confirmed that this is more-or-less working by monitoring data using Raspberry Pi’s screen command, for example:

screen /dev/serial0 19200

I’m not finding Raspberry Pi Zero W’s PL011-based serial port reliable enough to actually flash the Arduino (any help with that would be appreciated). But there is another issue:

The Arduino mini pro needs to be manually reset just before it will slurp up a new sketch. I can simulate this by togging one of the Raspberry Pi’s GPIO pins attached to the Arduino’s reset pin. But this requires me to run a Python script on the Raspberry Pi just as the cli is sending the sketch. Is there a way to insert calls to scripts into the Arduino-cli to have it do things like this?

Although it's possible to modify the source code of Arduino CLI, and surprisingly easy to compile it from that modified source (instructions), the more common approach would be to modify the boards platform (arduino:avr in this case) to provide the behavior you want.

In the case of wanting to customize compilation, this is made easy by the use of "per and post-build hooks":
https://arduino.github.io/arduino-cli/latest/platform-specification/#pre-and-post-build-hooks-since-arduino-ide-165

Unfortunately, there are no hooks for the upload process. You could still use the last hook that runs during the compilation process (recipe.hooks.objcopy.postobjcopy.NUMBER.pattern) to do the reset, but this will only work for "arduino-cli compile --upload". It won't work for "arduino-cli upload" because that command doesn't run the compilation process, and thus the build hooks are never called.

So the last resort is to modify the upload recipe so it runs a wrapper script instead of running avrdude directly. The wrapper script would do the reset and then run the avrdude command, passing the script's command line arguments along to avrdude. You can see an example of that here:

You could use a platform.local.txt to make the modifications in order to avoid having to directly modify the platform.txt of Arduino AVR Boards:
https://arduino.github.io/arduino-cli/latest/platform-specification/#platformlocaltxt

In the end, I don't think it provides any benefit over just using a wrapper script around "arduino-cli upload", but it is fun to explore using the Arduino boards platform system to do interesting things like this.

Thanks, that helps focus my attention on what is easy and what is hard. In this case, though, it might be easiest to just use an Arduino that has a USB connection if there is a chance the Raspberry Pi will be updating the Arduino's sketch.