From 406c20222affa4940c4b90b42e8cc22cbe10c5e2 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 12 Jun 2013 13:28:24 +0200 Subject: Added Process.runShellCommand*() methods. --- libraries/Bridge/Process.cpp | 14 ++++++++++++ libraries/Bridge/Process.h | 3 +++ .../examples/ShellCommands/ShellCommands.ino | 26 ++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 libraries/Bridge/examples/ShellCommands/ShellCommands.ino diff --git a/libraries/Bridge/Process.cpp b/libraries/Bridge/Process.cpp index 78f168f..219922a 100644 --- a/libraries/Bridge/Process.cpp +++ b/libraries/Bridge/Process.cpp @@ -118,6 +118,20 @@ void Process::close() { started = false; } +unsigned int Process::runShellCommand(const String &command) { + runShellCommandAsynchronously(command); + while (running()) + delay(100); + return exitValue(); +} + +void Process::runShellCommandAsynchronously(const String &command) { + begin("/bin/ash"); + addParameter("-c"); + addParameter(command); + runAsynchronously(); +} + // This method is currently unused //static unsigned int __commandOutputAvailable(uint8_t handle) { // uint8_t cmd[] = {'o', handle}; diff --git a/libraries/Bridge/Process.h b/libraries/Bridge/Process.h index cf950d4..cacf516 100644 --- a/libraries/Bridge/Process.h +++ b/libraries/Bridge/Process.h @@ -36,6 +36,9 @@ public: unsigned int exitValue(); void close(); + unsigned int runShellCommand(const String &command); + void runShellCommandAsynchronously(const String &command); + operator bool () { return started; } // Stream methods diff --git a/libraries/Bridge/examples/ShellCommands/ShellCommands.ino b/libraries/Bridge/examples/ShellCommands/ShellCommands.ino new file mode 100644 index 0000000..a362ed8 --- /dev/null +++ b/libraries/Bridge/examples/ShellCommands/ShellCommands.ino @@ -0,0 +1,26 @@ + +/* Demonstrate shell commands */ + +#include + +void setup() { + Bridge.begin(); + Console.begin(); + Console.buffer(64); +} + +void loop() { + Process p; + // This command line prints the number of bytes received and transmitted from WLAN + p.runShellCommand(F("ifconfig wlan0 | grep \"RX bytes\" | tr ':' ' ' | awk \"{ print \\$3 \\\" \\\" \\$8 }\"\n")); + + // Read command output + while (p.available()) { + char c = p.read(); + Console.print(c); + } + Console.flush(); + + delay(5000); +} + -- cgit v1.2.3-18-g5258