diff --git a/hardware/arduino/avr/libraries/Bridge/Bridge.cpp b/hardware/arduino/avr/libraries/Bridge/Bridge.cpp new file mode 100644 index 000000000..b91814b0a --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/Bridge.cpp @@ -0,0 +1,207 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Bridge.h" + +void BridgeClass::begin() { + if (started) + return; + started = true; + + // TODO: A more robust restart + // Bridge startup: + // - If the bridge is not running starts it safely + print(CTRL_C); + print(F("\n")); + delay(500); + // Wait for OpenWRT message + // "Press enter to activate console" + print(F("\ncd /root\n")); + delay(100); + print(F("python bridge.py\n")); + delay(1000); + dropAll(); + + // - If the bridge was already running previous commands + // are ignored as "invalid packets". + + // Reset the brigde + uint8_t cmd[] = {'X','X'}; + transfer(cmd, 2); +} + +uint8_t BridgeClass::runCommand(String &command) { + // TODO: do it in a more efficient way + String cmd = "R" + command; + uint8_t res[1]; + transfer((uint8_t*)cmd.c_str(), cmd.length(), res, 1); + return res[0]; +} + +bool BridgeClass::commandIsRunning(uint8_t handle) { + uint8_t cmd[] = {'r', handle}; + uint8_t res[1]; + transfer(cmd, 2, res, 1); + return (res[0] == 1); +} + +unsigned int BridgeClass::commandExitValue(uint8_t handle) { + uint8_t cmd[] = {'W', handle}; + uint8_t res[2]; + transfer(cmd, 2, res, 2); + return (res[0] << 8) + res[1]; +} + +void BridgeClass::cleanCommand(uint8_t handle) { + uint8_t cmd[] = {'w', handle}; + transfer(cmd, 2); +} + +unsigned int BridgeClass::commandOutputAvailable(uint8_t handle) { + uint8_t cmd[] = {'o', handle}; + uint8_t res[1]; + transfer(cmd, 2, res, 1); + return res[0]; +} + +unsigned int BridgeClass::readCommandOutput(uint8_t handle, + uint8_t *buffer, unsigned int size) { + if (size > 255) + size = 255; + uint8_t cmd[] = {'O', handle, size}; + return transfer(cmd, 3, buffer, size); +} + +void BridgeClass::writeCommandInput(uint8_t handle, + uint8_t *buff, unsigned int size) { + // TODO: do it in a more efficient way + uint8_t *tmp = new uint8_t[size+2]; + tmp[0] = 'I'; + tmp[1] = handle; + memcpy(tmp+2, buff, size); + transfer(tmp, size+2); + delete[] tmp; +} + +void BridgeClass::crcUpdate(uint8_t c) { + CRC = CRC ^ c; + CRC = (CRC >> 8) + (CRC << 8); +} + +void BridgeClass::crcReset() { + CRC = 0xAAAA; +} + +void BridgeClass::crcWrite() { + write((char)(CRC >> 8)); + write((char)(CRC & 0xFF)); +} + +bool BridgeClass::crcCheck(uint16_t _CRC) { + return CRC == _CRC; +} + +uint8_t BridgeClass::transfer(uint8_t *buff, uint8_t len, + uint8_t *rxbuff, uint8_t rxlen) +{ + for ( ; ; delay(100), dropAll() /* Delay for retransmission */) { + // Send packet + crcReset(); + write((char)0xFF); // Start of packet (0xFF) + crcUpdate(0xFF); + write((char)index); // Message index + crcUpdate(index); + write((char)len); // Message length + crcUpdate(len); + for (uint8_t i=0; i rxlen) + return rxlen; + return l; + } +} + +int BridgeClass::timedRead(unsigned int timeout) { + int c; + unsigned long _startMillis = millis(); + do { + c = read(); + if (c >= 0) return c; + } while(millis() - _startMillis < timeout); + return -1; // -1 indicates timeout +} + +void BridgeClass::dropAll() { + while (available() > 0) { + read(); + } +} + +// Bridge instance +#ifdef __AVR_ATmega32U4__ + // Leonardo variants (where HardwareSerial is Serial1) + SerialBridgeClass Bridge(Serial1); +#else + SerialBridgeClass Bridge(Serial); +#endif diff --git a/hardware/arduino/avr/libraries/Bridge/Bridge.h b/hardware/arduino/avr/libraries/Bridge/Bridge.h new file mode 100644 index 000000000..0740d4f44 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/Bridge.h @@ -0,0 +1,99 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef BRIDGE_H_ +#define BRIDGE_H_ + +#include +#include + +class BridgeClass: public Stream { +public: + BridgeClass(Stream &_stream) : index(0), stream(_stream), started(false) { + // Empty + } + + void begin(); + uint8_t runCommand(String &command); + + bool commandIsRunning(uint8_t handle); + + unsigned int commandExitValue(uint8_t handle); + + void cleanCommand(uint8_t handle); + + unsigned int commandOutputAvailable(uint8_t handle); + unsigned int readCommandOutput(uint8_t handle, uint8_t *buff, unsigned int size); + unsigned int readCommandOutput(uint8_t handle, char *buff, unsigned int size) + { return readCommandOutput(handle, reinterpret_cast(buff), size); } + + void writeCommandInput(uint8_t handle, uint8_t *buff, unsigned int size); + void writeCommandInput(uint8_t handle, char *buff, unsigned int size) + { writeCommandInput(handle, reinterpret_cast(buff), size); } + + // Print methods + size_t write(uint8_t c) { return stream.write(c); } + size_t write(const uint8_t *buffer, size_t size) + { return stream.write(buffer, size); } + + // Stream methods + int available() { return stream.available(); } + int read() { return stream.read(); } + int peek() { return stream.peek(); } + void flush() { stream.flush(); } + + uint8_t transfer(uint8_t *buff, uint8_t len, uint8_t *rxbuff=NULL, uint8_t rxlen=0); +private: + uint8_t index; + int timedRead(unsigned int timeout); + void dropAll(); + +private: + void crcUpdate(uint8_t c); + void crcReset(); + void crcWrite(); + bool crcCheck(uint16_t _CRC); + uint16_t CRC; + +private: + static const char CTRL_C = 3; + static const char CMD_RECV = 0x00; + Stream &stream; + bool started; +}; + +// This subclass uses a serial port Stream +class SerialBridgeClass : public BridgeClass { +public: + SerialBridgeClass(HardwareSerial &_serial) + : BridgeClass(_serial), serial(_serial) { + // Empty + } + + void begin() { + serial.begin(115200); + BridgeClass::begin(); + } + +private: + HardwareSerial &serial; +}; + +extern SerialBridgeClass Bridge; + +#endif /* BRIDGE_H_ */ diff --git a/hardware/arduino/avr/libraries/Bridge/Console.cpp b/hardware/arduino/avr/libraries/Bridge/Console.cpp new file mode 100644 index 000000000..76c648190 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/Console.cpp @@ -0,0 +1,152 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +// Default constructor uses global Bridge instance +ConsoleClass::ConsoleClass() : + bridge(Bridge), buffered(0), readPos(0), buffer(NULL) +{ + // Empty +} + +// Constructor with a user provided BridgeClass instance +ConsoleClass::ConsoleClass(BridgeClass &_b) : + bridge(_b), buffered(0), readPos(0), buffer(NULL), + autoFlush(true) +{ + // Empty +} + +ConsoleClass::~ConsoleClass() { + end(); +} + +size_t ConsoleClass::write(uint8_t c) { + if (autoFlush) { + uint8_t tmp[] = { 'P', c }; + bridge.transfer(tmp, 2); + return 1; + } else { + outBuffer[outBuffered++] = c; + if (outBuffered == outBufferSize) + flush(); + } +} + +size_t ConsoleClass::write(const uint8_t *buffer, size_t size) { + if (autoFlush) { + // TODO: do it in a more efficient way + uint8_t *tmp = new uint8_t[size+1]; + tmp[0] = 'P'; + memcpy(tmp+1, buffer, size); + bridge.transfer(tmp, size+1); + delete[] tmp; + return size; + } else { + while (size > 0) { + outBuffer[outBuffered++] = *buffer++; + size--; + if (outBuffered == outBufferSize) + flush(); + } + } +} + +void ConsoleClass::flush() { + if (autoFlush) + return; + + bridge.transfer(outBuffer, outBuffered); + outBuffered = 1; +} + +void ConsoleClass::setBuffer(uint8_t size) { + if (size==0) { + if (!autoFlush) { + delete[] outBuffer; + autoFlush = true; + } + } else { + if (autoFlush) + setBuffer(0); + outBuffer = new uint8_t[size+1]; + outBuffer[0] = 'P'; // WRITE tag + outBufferSize = size+1; + outBuffered = 1; + } +} + +bool ConsoleClass::connected() { + uint8_t tmp = 'a'; + bridge.transfer(&tmp, 1, &tmp, 1); + return tmp==1; +} + +int ConsoleClass::available() { + // Look if there is new data available + doBuffer(); + return buffered; +} + +int ConsoleClass::read() { + doBuffer(); + if (buffered == 0) + return -1; // no chars available + else { + buffered--; + return buffer[readPos++]; + } +} + +int ConsoleClass::peek() { + doBuffer(); + if (buffered == 0) + return -1; // no chars available + else + return buffer[readPos]; +} + +void ConsoleClass::doBuffer() { + // If there are already char in buffer exit + if (buffered > 0) + return; + + // Try to buffer up to 32 characters + readPos = 0; + uint8_t tmp[] = { 'p', BUFFER_SIZE }; + buffered = bridge.transfer(tmp, 2, buffer, BUFFER_SIZE); +} + +void ConsoleClass::begin() { + bridge.begin(); + end(); + buffer = new uint8_t[BUFFER_SIZE]; +} + +void ConsoleClass::end() { + if (autoFlush) { + setBuffer(0); + } + if (buffer) { + delete[] buffer; + buffer = NULL; + } +} + +ConsoleClass Console; diff --git a/hardware/arduino/avr/libraries/Bridge/Console.h b/hardware/arduino/avr/libraries/Bridge/Console.h new file mode 100644 index 000000000..72a396703 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/Console.h @@ -0,0 +1,68 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef CONSOLE_H_ +#define CONSOLE_H_ + +#include + +class ConsoleClass : public Stream { +public: + // Default constructor uses global Bridge instance + ConsoleClass(); + // Constructor with a user provided BridgeClass instance + ConsoleClass(BridgeClass &_b); + ~ConsoleClass(); + + void begin(); + void end(); + + void setBuffer(uint8_t size); + + bool connected(); + + // Stream methods + // (read from console socket) + int available(); + int read(); + int peek(); + // (write to console socket) + size_t write(uint8_t); + size_t write(const uint8_t *buffer, size_t size); + void flush(); + + operator bool () { return connected(); } + +private: + BridgeClass &bridge; + + void doBuffer(); + uint8_t buffered; + uint8_t readPos; + static const int BUFFER_SIZE = 32; + uint8_t *buffer; + + bool autoFlush; + uint8_t outBuffered; + uint8_t outBufferSize; + uint8_t *outBuffer; +}; + +extern ConsoleClass Console; + +#endif diff --git a/hardware/arduino/avr/libraries/Bridge/FileIO.cpp.disabled b/hardware/arduino/avr/libraries/Bridge/FileIO.cpp.disabled new file mode 100644 index 000000000..fc6d3df63 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/FileIO.cpp.disabled @@ -0,0 +1,162 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +File::File() : mode(255), filename(NULL) { + // Empty +} + +File::File(const char *_filename, uint8_t _mode) : mode(_mode) { + filename = new char[strlen(_filename)+1]; + strcpy(filename, _filename); +} + +File::operator bool() { + return (mode != 255); +} + +File::~File() { + if (filename) + delete[] filename; +} + +char toHex(uint8_t c) { + if (c<10) + return '0' + c; + else + return 'A' + c - 10; +} + +size_t File::write(uint8_t c) { + Process echo; + echo.begin("arduino-append"); + echo.addParameter(filename); + char chars[] = { '\\', 'x', toHex(c >> 4), toHex(c & 0x0F), '\0' }; + echo.addParameter(chars); + echo.run(); + return 1; +} + +size_t File::write(const uint8_t *buf, size_t size) { + Process echo; + echo.begin("arduino-append"); + echo.addParameter(filename); + echo.addParameter(" \"", true); + for (unsigned int i=0; i> 4), toHex(c & 0x0F), '\0' }; + echo.addParameter(chars, true); + } + echo.addParameter("\"", true); + echo.run(); + return size; +} + +int File::read() { + return 1; +} + +int File::peek() { + return 1; +} + +int File::available() { + return 1; +} + +void File::flush() { +} + +//int read(void *buf, uint16_t nbyte) +//boolean seek(uint32_t pos) +//uint32_t position() +//uint32_t size() + +void File::close() { + mode = 255; +} + +char *File::name() { + return filename; +} + +//boolean isDirectory(void) +//File openNextFile(uint8_t mode = O_RDONLY); +//void rewindDirectory(void) + + + + + + +boolean SDClass::begin() { + return true; +} + +File SDClass::open(const char *filename, uint8_t mode) { + if (mode == FILE_READ) { + if (exists(filename)) + return File(filename, mode); + } + if (mode == FILE_WRITE) { + Process touch; + touch.begin(">"); + touch.addParameter(filename); + int res = touch.run(); + if (res == 0) + return File(filename, mode); + } + return File(); +} + +boolean SDClass::exists(const char *filepath) { + Process ls; + ls.begin("ls"); + ls.addParameter(filepath); + int res = ls.run(); + return (res == 0); +} + +boolean SDClass::mkdir(const char *filepath) { + Process mk; + mk.begin("mkdir"); + mk.addParameter("-p"); + mk.addParameter(filepath); + int res = mk.run(); + return (res == 0); +} + +boolean SDClass::remove(const char *filepath) { + Process rm; + rm.begin("rm"); + rm.addParameter(filepath); + int res = rm.run(); + return (res == 0); +} + +boolean SDClass::rmdir(const char *filepath) { + Process rm; + rm.begin("rmdir"); + rm.addParameter(filepath); + int res = rm.run(); + return (res == 0); +} + +SDClass SD; diff --git a/hardware/arduino/avr/libraries/Bridge/FileIO.h b/hardware/arduino/avr/libraries/Bridge/FileIO.h new file mode 100644 index 000000000..a90ff46af --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/FileIO.h @@ -0,0 +1,96 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __SD_H__ +#define __SD_H__ + +#include + +//#define FILE_READ O_READ +//#define FILE_WRITE (O_READ | O_WRITE | O_CREAT) + +#define FILE_READ 0 +#define FILE_WRITE 1 + +class File : public Process { + +public: + File(); + File(const char *_filename, uint8_t _mode); + ~File(); + + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buf, size_t size); + virtual int read(); + virtual int peek(); + virtual int available(); + virtual void flush(); + int read(void *buf, uint16_t nbyte); + boolean seek(uint32_t pos); + uint32_t position(); + uint32_t size(); + void close(); + operator bool(); + char * name(); + + boolean isDirectory(void); + //File openNextFile(uint8_t mode = O_RDONLY); + void rewindDirectory(void); + + using Print::write; + +private: + char *filename; + uint8_t mode; +}; + +class SDClass { +public: + SDClass() : bridge(Bridge) { } + SDClass(BridgeClass &_b) : bridge(_b) { } + + // This needs to be called to set up the connection to the SD card + // before other methods are used. + boolean begin(); + + // Open the specified file/directory with the supplied mode (e.g. read or + // write, etc). Returns a File object for interacting with the file. + // Note that currently only one file can be open at a time. + File open(const char *filename, uint8_t mode = FILE_READ); + + // Methods to determine if the requested file path exists. + boolean exists(const char *filepath); + + // Create the requested directory heirarchy--if intermediate directories + // do not exist they will be created. + boolean mkdir(const char *filepath); + + // Delete the file. + boolean remove(const char *filepath); + + boolean rmdir(const char *filepath); + +private: + friend class File; + + BridgeClass &bridge; +}; + +extern SDClass SD; + +#endif diff --git a/hardware/arduino/avr/libraries/Bridge/HttpClient.cpp b/hardware/arduino/avr/libraries/Bridge/HttpClient.cpp new file mode 100644 index 000000000..510af38d1 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/HttpClient.cpp @@ -0,0 +1,53 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "HttpClient.h" + +unsigned int HttpClient::get(String &url) { + begin("curl"); + addParameter(url); + return run(); +} + +unsigned int HttpClient::get(const char *url) { + begin("curl"); + addParameter(url); + return run(); +} + +void HttpClient::getAsynchronously(String &url) { + begin("curl"); + addParameter(url); + runAsynchronously(); +} + +void HttpClient::getAsynchronously(const char *url) { + begin("curl"); + addParameter(url); + runAsynchronously(); +} + +boolean HttpClient::ready() { + return running(); +} + +unsigned int HttpClient::getResult() { + return exitValue(); +} + + diff --git a/hardware/arduino/avr/libraries/Bridge/HttpClient.h b/hardware/arduino/avr/libraries/Bridge/HttpClient.h new file mode 100644 index 000000000..940a66de6 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/HttpClient.h @@ -0,0 +1,36 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef HTTPCLIENT_H_ +#define HTTPCLIENT_H_ + +#include + +class HttpClient : public Process { +public: + + unsigned int get(String &url); + unsigned int get(const char * url); + void getAsynchronously(String &url); + void getAsynchronously(const char * url); + boolean ready(); + unsigned int getResult(); + +}; + +#endif /* HTTPCLIENT_H_ */ diff --git a/hardware/arduino/avr/libraries/Bridge/Process.cpp b/hardware/arduino/avr/libraries/Bridge/Process.cpp new file mode 100644 index 000000000..7265119d5 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/Process.cpp @@ -0,0 +1,115 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +Process::~Process() { + close(); +} + +size_t Process::write(uint8_t c) { + bridge.writeCommandInput(handle, &c, 1); + return 1; +} + +void Process::flush() { +} + +int Process::available() { + // Look if there is new data available + doBuffer(); + return buffered; +} + +int Process::read() { + doBuffer(); + if (buffered == 0) + return -1; // no chars available + else { + buffered--; + return buffer[readPos++]; + } +} + +int Process::peek() { + doBuffer(); + if (buffered == 0) + return -1; // no chars available + else + return buffer[readPos]; +} + +void Process::doBuffer() { + // If there are already char in buffer exit + if (buffered > 0) + return; + + // Try to buffer up to 32 characters + readPos = 0; + buffered = bridge.readCommandOutput(handle, buffer, sizeof(buffer)); +} + +void Process::begin(String &command) { + close(); + cmdline = new String(command); +} + +void Process::begin(const char *command) { + close(); + cmdline = new String(command); +} + +void Process::addParameter(const char *param) { + *cmdline += "\xFE"; + *cmdline += param; +} + +void Process::addParameter(String ¶m) { + *cmdline += "\xFE"; + *cmdline += param; +} + +void Process::runAsynchronously() { + handle = bridge.runCommand(*cmdline); + delete cmdline; + cmdline = NULL; + + started = true; +} + +boolean Process::running() { + return bridge.commandIsRunning(handle); +} + +unsigned int Process::exitValue() { + return bridge.commandExitValue(handle); +} + +unsigned int Process::run() { + runAsynchronously(); + while (running()) + delay(100); + return exitValue(); +} + +void Process::close() { + if (started) + bridge.cleanCommand(handle); + started = false; +} + diff --git a/hardware/arduino/avr/libraries/Bridge/Process.h b/hardware/arduino/avr/libraries/Bridge/Process.h new file mode 100644 index 000000000..b48e04651 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/Process.h @@ -0,0 +1,66 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef PROCESS_H_ +#define PROCESS_H_ + +#include + +class Process : public Stream { +public: + // Default constructor uses global Bridge instance + Process() : bridge(Bridge), started(false), buffered(0), readPos(0) { } + // Constructor with a user provided BridgeClass instance + Process(BridgeClass &_b) : bridge(_b), started(false), buffered(0), readPos(0) { } + ~Process(); + + void begin(String &command); + void begin(const char *command); + void addParameter(String ¶m); + void addParameter(const char *param); + unsigned int run(); + void runAsynchronously(); + boolean running(); + unsigned int exitValue(); + void close(); + + // Stream methods + // (read from process stdout) + int available(); + int read(); + int peek(); + // (write to process stdin) + size_t write(uint8_t); + void flush(); + +private: + BridgeClass &bridge; + unsigned int handle; + String *cmdline; + boolean started; + +private: + void doBuffer(); + uint8_t buffered; + uint8_t readPos; + static const int BUFFER_SIZE = 64; + uint8_t buffer[BUFFER_SIZE]; + +}; + +#endif diff --git a/hardware/arduino/avr/libraries/Bridge/examples/Bridge/Bridge.ino b/hardware/arduino/avr/libraries/Bridge/examples/Bridge/Bridge.ino new file mode 100644 index 000000000..848cc4efb --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/examples/Bridge/Bridge.ino @@ -0,0 +1,32 @@ + +#include + +void brk() { + Bridge.print((char)3); +} + +void setup() { + pinMode(13,OUTPUT); + digitalWrite(13, LOW); + Bridge.begin(); + digitalWrite(13, HIGH); + + delay(2000); + int handle = Bridge.beginCommand("curl"); + Bridge.commandAddEscapedParam("http://arduino.cc/asciilogo.txt"); + Bridge.endCommand(); + + while (Bridge.commandIsRunning(handle)) + delay(250); + + int size = Bridge.commandOutputSize(handle); + char buff[20]; + Bridge.readCommandOutput(handle, 0, size, buff); + buff[size]=0; + Bridge.print(buff);brk(); +} + +void loop() { +} + + diff --git a/hardware/arduino/avr/libraries/Bridge/examples/FileTest/FileTest.ino b/hardware/arduino/avr/libraries/Bridge/examples/FileTest/FileTest.ino new file mode 100644 index 000000000..3fd7ce980 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/examples/FileTest/FileTest.ino @@ -0,0 +1,50 @@ + +#include + +void setup() { + Bridge.begin(); + SD.begin(); + + boolean r; + r=SD.exists("/arduino/test"); + if (r) error("1"); + r=SD.exists("/arduino"); + if (!r) error("2"); + + r=SD.mkdir("/arduino/test"); + if (!r) error("3"); + r=SD.exists("/arduino/test"); + if (!r) error("4"); + + File f = SD.open("/arduino/test/bla", FILE_WRITE); + if (!f) error("5"); + f.println("CIAO!"); + f.close(); + + delay(10000); + + r=SD.rmdir("/arduino/test"); + if (r) error("6"); + r=SD.remove("/arduino/test"); + if (r) error("7"); + + r=SD.remove("/arduino/test/bla"); + if (!r) error("8"); + r=SD.rmdir("/arduino/test"); + if (!r) error("9"); + r=SD.exists("/arduino/test"); + if (r) error("10"); +} + + +void error(const char *s) { + Bridge.print("# ERROR "); + Bridge.println(s); + while (true); +} + +void loop() { +} + + + diff --git a/hardware/arduino/avr/libraries/Bridge/examples/HttpClient/HttpClient.ino b/hardware/arduino/avr/libraries/Bridge/examples/HttpClient/HttpClient.ino new file mode 100644 index 000000000..bf5e8ff21 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/examples/HttpClient/HttpClient.ino @@ -0,0 +1,23 @@ + +#include + +void setup() { + pinMode(13, OUTPUT); + digitalWrite(13, LOW); + Bridge.begin(); +} + +void loop() { + HttpClient client; + client.get("http://my.server.address/file.php"); + + char c = client.read(); + if (c=='1') + digitalWrite(13, HIGH); + if (c=='0') + digitalWrite(13, LOW); + + delay(5000); +} + + diff --git a/hardware/arduino/avr/libraries/Bridge/examples/Process/Process.ino b/hardware/arduino/avr/libraries/Bridge/examples/Process/Process.ino new file mode 100644 index 000000000..f954646ab --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/examples/Process/Process.ino @@ -0,0 +1,24 @@ +#include + +void brk() { + Bridge.print((char)3); + Bridge.find("#"); +} + +void setup() { + Bridge.begin(); + + Process p; + p.begin("curl"); + p.addParameter("http://arduino.cc/asciilogo.txt"); + p.run(); + + String res = ""; + while (p.IO.available()>0) { + p.IO.read(); + } +} + +void loop() { +} +