-
Notifications
You must be signed in to change notification settings - Fork 0
PythonAgent.md
The Bang Python agent, arduino_exec.py, is the host-side genius that makes your Arduino a command-line overlord! Running on a PC, Mac, or Linux, it listens for ! (shell), & (sketch uploads), and @ (macro) commands over Serial-USB, turning your BANG("echo Hello") or BANG("@play") into pure magic. With a slick in-memory dictionary for dynamic @macro management, it’s the key to music playback, sketch swapping, and more, all while keeping your Arduino projects lean. Let’s unpack this powerhouse!
- Purpose: Processes Arduino commands, executing shell commands, uploading sketches, and managing user-defined macros.
-
File:
arduino_exec.py, a Python script usingpyserialfor Serial communication. -
Key Features:
- Executes
!command(e.g.,!echo Hellorunsecho Hello). - Uploads sketches with
&sketch_name\n(e.g.,&blink\nusesarduino-cli). - Manages
@macro_namedynamically via an in-memory dictionary, supporting@add_macro,@delete_macro,@list_macros.
- Executes
-
Requirements: Python 3,
pyserial(pip3 install pyserial),arduino-clifor uploads. -
Run Command:
sudo python3 arduino_exec.py -p /dev/cu.usbserial-00100 -b 38400
-
-p: Serial port (e.g.,COM3,/dev/cu.usbserial-00100). -
-b: Baud rate (default 38400). -
sudo: Needed for privileged commands (e.g.,!reboot,@reboot).
-
-
See Installation for setup details.
arduino_exec.py listens on the serial port for commands from the Arduino (sent via BANG macros or bang.serial()):
-
Shell Commands (
!):-
Example:
BANG("echo Hello World")sends!echo Hello World. -
Script runs
execute_command("echo Hello World"), outputtingHello Worldto Serial.if cmd_id == '!': result = execute_command(command) # Runs shell command
-
-
Sketch Uploads (
&):-
Example:
BANG("&blink\n")sends&blink\n. -
Script runs
compile_and_upload("blink"), usingarduino-clito uploadblink.ino.if cmd_id == '&': result = compile_and_upload(command) # Uploads sketch
-
-
Macros (
@):-
Stored in an in-memory
macrosdictionary (e.g.,macros = {"play": "osascript -e 'tell application \"Music\" to play'"}). -
Invoke:
BANG("@play")runsexecute_command(macros["play"]). -
List:
BANG("@list_macros")returns macro list (e.g.,"play": "osascript ..."). -
Define:
BANG("@add_macro:play:osascript -e 'tell application \"Music\" to play'")adds tomacros. -
Delete:
BANG("@delete_macro:play")removes frommacros.macros = {} # In-memory dictionary if cmd_id == '@': if command in macros: result = execute_command(macros[command]) elif command == "list_macros": macro_list = [f' "{m}": "{macros[m]}"' for m in macros] result = "Registered Macros:\n" + "\n".join(macro_list) elif command.startswith("add_macro:"): _, macro_name, macro_cmd = command.split(":", 2) create_macro(macro_name, macro_cmd, macros) result = f"Macro '{macro_name}' created with command '{macro_cmd}'" elif command.startswith("delete_macro:"): _, macro_name = command.split(":", 1) result = delete_macro(macro_name, macros) else: result = f"unrecognized macro command: @{command}"
-
-
Text Lines (
#): Echoes text (e.g.,BANG("#Hello")returnsHello). -
Output: Results are sent back to the Arduino’s Serial Monitor.
-
In-Memory Storage:
macrosdictionary stores macro names and commands, reset onarduino_exec.pyrestart (no file-based storage likemacros.json). -
Dynamic Management:
- Add:
BANG("@add_macro:weather:curl wttr.in?format=3"). - Invoke:
BANG("@weather"). - Delete:
BANG("@delete_macro:weather"). - List:
BANG("@list_macros").
- Add:
-
Predefining Macros: Add to
arduino_exec.py:macros = { 'play': 'osascript -e "tell application \"Music\" to play"', 'pause': 'osascript -e "tell application \"Music\" to pause"' }
-
Privileges: Use
sudofor macros like@reboot(e.g.,BANG("@add_macro:reboot:reboot")).
-
Extend Macros: Add complex logic in
create_macro()(e.g., validate commands). -
Logging: Enhance
print()statements for debugging. -
Future Storage: Add file-based persistence (e.g., save
macrostomacros.json), not yet implemented.
-
Macro Not Found: Check with
BANG("@list_macros")or define with@add_macro. -
Upload Fails: Verify
arduino-clisetup and sketch file location. -
No Output: Check serial port, baud rate (38400), and
sudo. - See Troubleshooting for fixes.
- Explore Examples for sketches using
!,&, and@. - Check Dynamic Uploading for
&sketch_name\ndetails. - See FAQ for macro questions.