-
Notifications
You must be signed in to change notification settings - Fork 0
Examples.md
Ready to see Bang flex its muscles? The Bang library’s examples/ folder is packed with sketches that show off its command-line domination, from firing shell commands (!) to uploading new sketches (&) and managing slick macros (@) via Serial-USB. Whether it’s blinking LEDs, playing music, or reprogramming your Arduino with BANG("&blink\n"), these examples prove Bang’s power while staying lean for Arduino’s tight memory. Let’s dive into the action and get your microcontroller rocking!
-
Purpose: Demonstrate Bang’s features using practical sketches in the
examples/folder. -
Sketches:
-
blink.ino: Sends a shell command and blinks an LED. -
cmd.ino: Fires multiple shell commands. -
music.ino: Controls music playback. -
reboot.ino: Reboots the host. -
upload.ino: Uploads a new sketch for self-reprogramming.
-
-
Features Shown:
- Shell commands:
BANG("echo Hello")sends!echo Hello. - Sketch uploads:
BANG("&blink\n")uploadsblink.ino. - Macros: Dynamic management with
BANG("@add_macro:play:osascript ..."),BANG("@play"),BANG("@delete_macro:play").
- Shell commands:
- Bang is set up (see Installation).
-
arduino_exec.pyis running withsudo(e.g.,sudo python3 arduino_exec.py -p /dev/cu.usbserial-00100 -b 38400). - Serial Monitor is open (Arduino IDE, 38400 baud).
-
arduino-cliis installed for&uploads.
-
Purpose: Sends a shell command and blinks an LED on pin 13.
-
Key Code:
#include <Bang.h> Bang bang(Serial); void setup() { Serial.begin(38400); while (!Serial) ; pinMode(13, OUTPUT); } void loop() { BANG("echo Blink Example"); // Sends !echo Blink Example digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); }
-
Output: Serial Monitor shows
Blink Example, LED blinks every second. -
Use Case: Basic
!command testing with visual feedback.
-
Purpose: Sends multiple shell commands to show versatility.
-
Key Code:
#include <Bang.h> Bang bang(Serial); void setup() { Serial.begin(38400); while (!Serial) ; } void loop() { BANG("echo cmd example"); // Sends !echo cmd example delay(1000); BANG("date"); // Sends !date delay(1000); BANG("curl https://example.com"); // Sends !curl https://example.com delay(5000); }
-
Output: Serial Monitor shows command results (e.g.,
cmd example, current date, webpage content). -
Use Case: Testing multiple
!commands for host interaction.
-
Purpose: Controls music playback on macOS using
osascript. -
Key Code:
#include <Bang.h> Bang bang(Serial); void setup() { Serial.begin(38400); while (!Serial) ; } void loop() { BANG("@add_macro:play:osascript -e 'tell application \"Music\" to play'"); // Define @play delay(1000); BANG("@play"); // Play music delay(5000); BANG("@add_macro:pause:osascript -e 'tell application \"Music\" to pause'"); // Define @pause delay(1000); BANG("@pause"); // Pause music delay(5000); }
-
Output: Music plays/pauses, Serial Monitor may show macro confirmations (e.g.,
Macro 'play' created). -
Use Case: Demonstrates dynamic
@macrodefinition and invocation.
-
Purpose: Reboots the host (requires
sudo). -
Key Code:
#include <Bang.h> Bang bang(Serial); void setup() { Serial.begin(38400); while (!Serial) ; } void loop() { BANG("@add_macro:reboot:reboot"); // Define @reboot delay(1000); BANG("@reboot"); // Reboot host delay(10000); }
-
Output: Host reboots, Serial Monitor may show
Macro 'reboot' created. -
Use Case: Shows
@macrofor privileged commands.
-
Purpose: Uploads a new sketch for self-reprogramming.
-
Key Code:
#include <Bang.h> Bang bang(Serial); void setup() { Serial.begin(38400); while (!Serial) ; } void loop() { BANG("&blink\n"); // Sends &blink\n, uploads blink.ino delay(10000); }
-
Output: Arduino restarts with
blink.ino, Serial Monitor shows new sketch output. -
Use Case: Demonstrates
&for unlimited app size (each sketch ≤ 32K).
- Open a sketch in Arduino IDE (e.g.,
examples/blink/blink.ino). - Connect your Arduino via USB.
- Set Tools > Board (e.g., Arduino Uno) and Tools > Port.
- Upload the sketch.
- Open Serial Monitor (38400 baud) to view output.
- Ensure
arduino_exec.pyis running withsudo.
-
Macros: Use
@list_macrosto check defined macros,@add_macrofor new ones. -
Uploads: Test
&with simple sketches first, verifyarduino-clisetup. -
Debugging: Add
Serial.println()to log results. -
Sudo: Required for
@rebootor!reboot.
-
No Output: Check Serial Monitor baud rate (38400), port, and
arduino_exec.py. -
Macro Fails: Use
BANG("@list_macros")to verify macro exists. -
Upload Errors: Ensure
arduino-cliand sketch files are accessible. - See Troubleshooting for fixes.
- Check Dynamic Uploading for
&sketch_name\ndetails. - Explore Python Agent for
arduino_exec.pycustomization. - See FAQ for macro questions.