-
Notifications
You must be signed in to change notification settings - Fork 0
Usage.md
Ready to make your Arduino a command-line superhero with Bang? This guide shows you how to wield the Bang library to blast shell commands (!), upload new sketches (&), and manage slick user-defined macros (@) via Serial-USB to a PC, Mac, or Linux host. With macros like BANG("echo Hello"), BANG("@add_macro:play:osascript ..."), and BANG("&blink\n"), you’ll be controlling the host, playing music, or reprogramming your Arduino like a pro. Bang’s built to keep things lean for Arduino’s tight memory, so let’s dive in and start dominating!
- Bang is set up on your Arduino and host (see Installation).
-
arduino_exec.pyis running on the host withsudo(e.g.,sudo python3 arduino_exec.py -p /dev/cu.usbserial-00100 -b 38400). - Serial Monitor is open in the Arduino IDE (baud rate: 38400) for viewing output.
- (Optional)
arduino-cliis installed for dynamic sketch uploads.
Bang lets your Arduino send three types of commands to the host, using the Bang class and its handy macros (BANG, BANG_P, BANG_S):
-
Shell Commands (
!): Run host shell commands (e.g.,BANG("echo Hello World")sends!echo Hello World). -
Sketch Uploads (
&): Upload a new sketch, replacing the current one (e.g.,BANG("&blink\n")uploadsblink.ino). -
User-Defined Macros (
@): Define, invoke, or delete macros dynamically (e.g.,BANG("@add_macro:play:osascript ..."),BANG("@play")).
Here’s how to use Bang in your Arduino sketches and manage those @macro commands.
Add the Bang library to your sketch and set up the Bang object:
```cpp
#include <Bang.h>
Bang bang(Serial); // Single Serial stream for commands and output
```
OR, for separate command/output streams (e.g., with an FTDI module):
```cpp
#include <SoftwareSerial.h>
SoftwareSerial command_serial(10, 11); // RX, TX pins
Bang bang(command_serial, Serial); // Commands via command_serial, output via Serial
```
-
Initialize Serial in
setup():void setup() { Serial.begin(38400); while (!Serial) ; // Wait for Serial to connect // If using command_serial, initialize it too // command_serial.begin(38400); }
Use the BANG macros to send shell commands prefixed with !:
-
Basic Command:
BANG("echo Hello World"); // Sends !echo Hello World
- Serial Monitor shows:
Hello World.
- Serial Monitor shows:
-
Flash Memory (PROGMEM):
BANG_P(PSTR("date")); // Sends !date from flash
- Saves RAM for static commands.
-
Dynamic String:
String cmd = "curl https://example.com"; BANG_S(cmd); // Sends !curl https://example.com
- Useful for runtime-generated commands.
-
Examples:
- Get the host’s date:
BANG("date"). - Fetch a webpage:
BANG("curl https://example.com"). - Reboot (requires
sudo):BANG("reboot").
- Get the host’s date:
Use @ commands to define, invoke, or delete macros stored in an in-memory dictionary on the host:
-
Define Macro:
BANG("@add_macro:play:osascript -e 'tell application \"Music\" to play'"); // Defines @play
- Adds
playtomacrosdictionary with the command.
- Adds
-
Invoke Macro:
BANG("@play"); // Runs osascript -e 'tell application "Music" to play'
-
List Macros:
BANG("@list_macros"); // Returns list (e.g., "play": "osascript ...")
- Serial Monitor shows:
Registered Macros: "play": "osascript ...", ....
- Serial Monitor shows:
-
Delete Macro:
BANG("@delete_macro:play"); // Removes @play
-
Notes:
- Macros persist in-memory during
arduino_exec.py’s runtime, resetting on restart (no file-based storage likemacros.json). - Use
sudofor privileged macros (e.g.,BANG("@add_macro:reboot:reboot")). - See Python Agent for advanced macro management.
- Macros persist in-memory during
Use &sketch_name\n to upload a new sketch, replacing the current one:
-
Example:
BANG("&blink\n"); // Sends &blink\n, uploads blink.ino
-
arduino_exec.pyrunsarduino-clito compile/uploadblink.ino, restarting the Arduino.
-
-
Unlimited App Size:
- Each sketch must fit in 32K flash, but you can swap subsystems endlessly.
- If
blink.inoincludes Bang, it can sendBANG("&cmd\n"), continuing the cycle.
-
Setup:
- Ensure
arduino-cliis installed and configured (see Installation). - Place sketch files (e.g.,
blink.ino) wherearduino_exec.pycan access them.
- Ensure
-
Example Flow:
- Sketch sends
BANG("&blink\n"). - Host uploads
blink.ino, which sendsBANG("echo Blink Example"). - Next sketch could send
BANG("&music\n").
- Sketch sends
Here’s a sketch combining all command types:
```cpp
#include <Bang.h>
Bang bang(Serial);
void setup() {
Serial.begin(38400);
while (!Serial) ;
}
void loop() {
BANG("echo Hello World"); // Shell command
delay(1000);
BANG("@add_macro:play:osascript -e 'tell application \"Music\" to play'"); // Define macro
delay(1000);
BANG("@play"); // Play music
delay(5000);
BANG("@list_macros"); // List macros
delay(1000);
BANG("@delete_macro:play"); // Delete macro
delay(1000);
BANG("&blink\n"); // Upload new sketch
delay(10000);
}
```
-
Output (Serial Monitor):
-
Hello Worldfrom!echo Hello World. - Music plays from
@play(if defined). - Macro list from
@list_macros. - Arduino restarts with
blink.inoafter&blink\n.
-
- Open the Serial Monitor (Arduino IDE, 38400 baud) to see command results:
- Shell:
Hello WorldfromBANG("echo Hello World"). - Macro: Confirmation or output (e.g.,
@playlogs “Macro 'play' executed”). - Upload: Upload status or new sketch output.
- Shell:
- Ensure
arduino_exec.pyis running to process commands.
-
Macro Management: Use
@list_macrosto check defined macros,@add_macrofor new ones, and@delete_macroto clean up. -
Upload Safety: Test
&sketch_name\nwith simple sketches first, ensuringarduino-cliworks. -
Sudo: Run
arduino_exec.pywithsudofor!reboot,@reboot, or similar. -
Debugging: Add
Serial.println()to log command results.
-
No Output: Check Serial Monitor baud rate (38400), port, and
arduino_exec.pystatus. -
Macro Fails: Verify
@macro_nameis defined (use@list_macrosor@add_macro). -
Upload Errors: Ensure
arduino-cliis set up and sketch files are accessible. - See Troubleshooting for more fixes.
- Explore Examples to see
blink.ino,music.ino, andupload.inoin action. - Dive into Python Agent for advanced
@macroconfiguration. - Check Dynamic Uploading to master
&sketch_name\n.