diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 3725f92a9..a1f14b43d 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -59,7 +59,7 @@ jobs: - name: WiFi101 - name: WiFiNINA - name: Arduino_MKRMEM - sketch-paths: '"examples/utility/Provisioning" "examples/utility/WiFi_Cloud_Blink"' + sketch-paths: '"examples/utility/Provisioning"' # LoRaWAN boards - board: type: "wan" @@ -83,8 +83,7 @@ jobs: - name: ArduinoECCX08 - name: RTCZero - name: MKRGSM - - name: Arduino_MKRMEM - sketch-paths: '"examples/utility/Provisioning" "examples/utility/GSM_Cloud_Blink"' + sketch-paths: '"examples/utility/Provisioning"' # NB boards - board: type: "nb" @@ -96,8 +95,7 @@ jobs: - name: ArduinoECCX08 - name: RTCZero - name: MKRNB - - name: Arduino_MKRMEM - sketch-paths: '"examples/utility/Provisioning" "examples/utility/NB_Cloud_Blink"' + sketch-paths: '"examples/utility/Provisioning"' # ESP8266 boards - board: type: "esp8266" @@ -106,7 +104,7 @@ jobs: - name: esp8266:esp8266 source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json libraries: - sketch-paths: '"examples/utility/WiFi_Cloud_Blink_with_security_credentials"' + sketch-paths: steps: - name: Checkout diff --git a/examples/utility/GSM_Cloud_Blink/GSM_Cloud_Blink.ino b/examples/utility/GSM_Cloud_Blink/GSM_Cloud_Blink.ino deleted file mode 100644 index 807229701..000000000 --- a/examples/utility/GSM_Cloud_Blink/GSM_Cloud_Blink.ino +++ /dev/null @@ -1,76 +0,0 @@ -/* This sketch is used during the getting started tutorial when - initialising a Arduino cloud-enabled board with the Arduino - cloud for the first time. - - This sketch is compatible with: - - MKR GSM 1400 -*/ - -#include -#include - -#include "arduino_secrets.h" - -String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters -// handles connection to the network -GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_USER_NAME, SECRET_PASSWORD); - -void setup() { - setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4] - Serial.begin(9600); - while (!Serial); // waits for the serial to become available - ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud - pinMode(LED_BUILTIN, OUTPUT); -} - -void loop() { - ArduinoCloud.update(); - // check if there is something waiting to be read - if (ArduinoCloud.connected()) { - if (CloudSerial.available()) { - char character = CloudSerial.read(); - cloudSerialBuffer += character; - // if a \n character has been received, there should be a complete command inside cloudSerialBuffer - if (character == '\n') { - handleString(); - } - } else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check. - handleString(); - } - // Just to be able to simulate the board responses through the serial monitor - if (Serial.available()) { - CloudSerial.write(Serial.read()); - } - } -} -void handleString() { - // Don't proceed if the string is empty - if (cloudSerialBuffer.equals("")) { - return; - } - // Remove leading and trailing whitespaces - cloudSerialBuffer.trim(); - // Make it uppercase; - cloudSerialBuffer.toUpperCase(); - if (cloudSerialBuffer.equals("ON")) { - digitalWrite(LED_BUILTIN, HIGH); - } else if (cloudSerialBuffer.equals("OFF")) { - digitalWrite(LED_BUILTIN, LOW); - } - sendString(cloudSerialBuffer); - // Reset cloudSerialBuffer - cloudSerialBuffer = ""; -} -// sendString sends a string to the Arduino Cloud. -void sendString(String stringToSend) { - // send the characters one at a time - char lastSentChar = 0; - for (unsigned int i = 0; i < stringToSend.length(); i++) { - lastSentChar = stringToSend.charAt(i); - CloudSerial.write(lastSentChar); - } - // if the last sent character wasn't a '\n' add it - if (lastSentChar != '\n') { - CloudSerial.write('\n'); - } -} diff --git a/examples/utility/GSM_Cloud_Blink/arduino_secrets.h b/examples/utility/GSM_Cloud_Blink/arduino_secrets.h deleted file mode 100644 index dd956ba80..000000000 --- a/examples/utility/GSM_Cloud_Blink/arduino_secrets.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef SECRET_PIN - #define SECRET_PIN "" - #warning "You need to define SECRET_PIN in tab/arduino_secrets.h" -#endif - -#ifndef SECRET_APN - #define SECRET_APN "" - #warning "You need to define SECRET_PIN in tab/arduino_secrets.h" -#endif - -#ifndef SECRET_USER_NAME - #define SECRET_USER_NAME "" - #warning "You need to define SECRET_USER_NAME in tab/arduino_secrets.h" -#endif - -#ifndef SECRET_PASSWORD - #define SECRET_PASSWORD "" - #warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h" -#endif diff --git a/examples/utility/NB_Cloud_Blink/NB_Cloud_Blink.ino b/examples/utility/NB_Cloud_Blink/NB_Cloud_Blink.ino deleted file mode 100644 index 3281fbfe1..000000000 --- a/examples/utility/NB_Cloud_Blink/NB_Cloud_Blink.ino +++ /dev/null @@ -1,76 +0,0 @@ -/* This sketch is used during the getting started tutorial when - initialising a Arduino cloud-enabled board with the Arduino - cloud for the first time. - - This sketch is compatible with: - - MKR NB 1500 -*/ - -#include -#include - -#include "arduino_secrets.h" - -String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters -// handles connection to the network -NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_PIN, SECRET_OPTIONAL_APN, SECRET_OPTIONAL_USER_NAME, SECRET_OPTIONAL_PASSWORD); - -void setup() { - setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4] - Serial.begin(9600); - while (!Serial); // waits for the serial to become available - ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud - pinMode(LED_BUILTIN, OUTPUT); -} - -void loop() { - ArduinoCloud.update(); - // check if there is something waiting to be read - if (ArduinoCloud.connected()) { - if (CloudSerial.available()) { - char character = CloudSerial.read(); - cloudSerialBuffer += character; - // if a \n character has been received, there should be a complete command inside cloudSerialBuffer - if (character == '\n') { - handleString(); - } - } else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check. - handleString(); - } - // Just to be able to simulate the board responses through the serial monitor - if (Serial.available()) { - CloudSerial.write(Serial.read()); - } - } -} -void handleString() { - // Don't proceed if the string is empty - if (cloudSerialBuffer.equals("")) { - return; - } - // Remove leading and trailing whitespaces - cloudSerialBuffer.trim(); - // Make it uppercase; - cloudSerialBuffer.toUpperCase(); - if (cloudSerialBuffer.equals("ON")) { - digitalWrite(LED_BUILTIN, HIGH); - } else if (cloudSerialBuffer.equals("OFF")) { - digitalWrite(LED_BUILTIN, LOW); - } - sendString(cloudSerialBuffer); - // Reset cloudSerialBuffer - cloudSerialBuffer = ""; -} -// sendString sends a string to the Arduino Cloud. -void sendString(String stringToSend) { - // send the characters one at a time - char lastSentChar = 0; - for (unsigned int i = 0; i < stringToSend.length(); i++) { - lastSentChar = stringToSend.charAt(i); - CloudSerial.write(lastSentChar); - } - // if the last sent character wasn't a '\n' add it - if (lastSentChar != '\n') { - CloudSerial.write('\n'); - } -} diff --git a/examples/utility/NB_Cloud_Blink/arduino_secrets.h b/examples/utility/NB_Cloud_Blink/arduino_secrets.h deleted file mode 100644 index 2abc7ed01..000000000 --- a/examples/utility/NB_Cloud_Blink/arduino_secrets.h +++ /dev/null @@ -1,4 +0,0 @@ -#define SECRET_OPTIONAL_PIN "" -#define SECRET_OPTIONAL_APN "" -#define SECRET_OPTIONAL_USER_NAME "" -#define SECRET_OPTIONAL_PASSWORD "" \ No newline at end of file diff --git a/examples/utility/WiFi_Cloud_Blink/WiFi_Cloud_Blink.ino b/examples/utility/WiFi_Cloud_Blink/WiFi_Cloud_Blink.ino deleted file mode 100644 index 62484e247..000000000 --- a/examples/utility/WiFi_Cloud_Blink/WiFi_Cloud_Blink.ino +++ /dev/null @@ -1,77 +0,0 @@ -/* This sketch is used during the getting started tutorial when - initialising a Arduino cloud-enabled board with the Arduino - cloud for the first time. - - This sketch is compatible with: - - MKR 1000 - - MKR WIFI 1010 -*/ - -#include -#include - -#include "arduino_secrets.h" - -String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters -// handles connection to the network -WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_NAME, SECRET_PASSWORD); - -void setup() { - setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4] - Serial.begin(9600); - while (!Serial); // waits for the serial to become available - ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud - pinMode(LED_BUILTIN, OUTPUT); -} - -void loop() { - ArduinoCloud.update(); - // check if there is something waiting to be read - if (ArduinoCloud.connected()) { - if (CloudSerial.available()) { - char character = CloudSerial.read(); - cloudSerialBuffer += character; - // if a \n character has been received, there should be a complete command inside cloudSerialBuffer - if (character == '\n') { - handleString(); - } - } else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check. - handleString(); - } - // Just to be able to simulate the board responses through the serial monitor - if (Serial.available()) { - CloudSerial.write(Serial.read()); - } - } -} -void handleString() { - // Don't proceed if the string is empty - if (cloudSerialBuffer.equals("")) { - return; - } - // Remove leading and trailing whitespaces - cloudSerialBuffer.trim(); - // Make it uppercase; - cloudSerialBuffer.toUpperCase(); - if (cloudSerialBuffer.equals("ON")) { - digitalWrite(LED_BUILTIN, HIGH); - } else if (cloudSerialBuffer.equals("OFF")) { - digitalWrite(LED_BUILTIN, LOW); - } - sendString(cloudSerialBuffer); - // Reset cloudSerialBuffer - cloudSerialBuffer = ""; -} -// sendString sends a string to the Arduino Cloud. -void sendString(String stringToSend) { - // send the characters one at a time - char lastSentChar = 0; - for (unsigned int i = 0; i < stringToSend.length(); i++) { - lastSentChar = stringToSend.charAt(i); - CloudSerial.write(lastSentChar); - } - // if the last sent character wasn't a '\n' add it - if (lastSentChar != '\n') { - CloudSerial.write('\n'); - } -} diff --git a/examples/utility/WiFi_Cloud_Blink/arduino_secrets.h b/examples/utility/WiFi_Cloud_Blink/arduino_secrets.h deleted file mode 100644 index 8d400bccb..000000000 --- a/examples/utility/WiFi_Cloud_Blink/arduino_secrets.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef SECRET_WIFI_NAME - #define SECRET_WIFI_NAME "" - #warning "You need to define SECRET_WIFI_NAME in tab/arduino_secrets.h" -#endif - -#ifndef SECRET_PASSWORD - #define SECRET_PASSWORD "" - #warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h" -#endif diff --git a/examples/utility/WiFi_Cloud_Blink_with_security_credentials/WiFi_Cloud_Blink_with_security_credentials.ino b/examples/utility/WiFi_Cloud_Blink_with_security_credentials/WiFi_Cloud_Blink_with_security_credentials.ino deleted file mode 100644 index e4f938f99..000000000 --- a/examples/utility/WiFi_Cloud_Blink_with_security_credentials/WiFi_Cloud_Blink_with_security_credentials.ino +++ /dev/null @@ -1,78 +0,0 @@ -/* This sketch is used during the getting started tutorial when - initialising a Arduino cloud-enabled board with the Arduino - cloud for the first time. - - This sketch is compatible with: - - ESP8266 boards -*/ - -#include -#include - -#include "arduino_secrets.h" - -String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters -// handles connection to the network -WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_NAME, SECRET_PASSWORD); - -void setup() { - setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4] - Serial.begin(9600); - while (!Serial); // waits for the serial to become available - ArduinoCloud.setBoardId(SECRET_BOARD_ID); - ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); - ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud - pinMode(LED_BUILTIN, OUTPUT); -} - -void loop() { - ArduinoCloud.update(); - // check if there is something waiting to be read - if (ArduinoCloud.connected()) { - if (CloudSerial.available()) { - char character = CloudSerial.read(); - cloudSerialBuffer += character; - // if a \n character has been received, there should be a complete command inside cloudSerialBuffer - if (character == '\n') { - handleString(); - } - } else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check. - handleString(); - } - // Just to be able to simulate the board responses through the serial monitor - if (Serial.available()) { - CloudSerial.write(Serial.read()); - } - } -} -void handleString() { - // Don't proceed if the string is empty - if (cloudSerialBuffer.equals("")) { - return; - } - // Remove leading and trailing whitespaces - cloudSerialBuffer.trim(); - // Make it uppercase; - cloudSerialBuffer.toUpperCase(); - if (cloudSerialBuffer.equals("ON")) { - digitalWrite(LED_BUILTIN, HIGH); - } else if (cloudSerialBuffer.equals("OFF")) { - digitalWrite(LED_BUILTIN, LOW); - } - sendString(cloudSerialBuffer); - // Reset cloudSerialBuffer - cloudSerialBuffer = ""; -} -// sendString sends a string to the Arduino Cloud. -void sendString(String stringToSend) { - // send the characters one at a time - char lastSentChar = 0; - for (unsigned int i = 0; i < stringToSend.length(); i++) { - lastSentChar = stringToSend.charAt(i); - CloudSerial.write(lastSentChar); - } - // if the last sent character wasn't a '\n' add it - if (lastSentChar != '\n') { - CloudSerial.write('\n'); - } -} diff --git a/examples/utility/WiFi_Cloud_Blink_with_security_credentials/arduino_secrets.h b/examples/utility/WiFi_Cloud_Blink_with_security_credentials/arduino_secrets.h deleted file mode 100644 index 2cf02f8be..000000000 --- a/examples/utility/WiFi_Cloud_Blink_with_security_credentials/arduino_secrets.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef SECRET_WIFI_NAME - #define SECRET_WIFI_NAME "" - #warning "You need to define SECRET_WIFI_NAME in tab/arduino_secrets.h" -#endif - -#ifndef SECRET_PASSWORD - #define SECRET_PASSWORD "" - #warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h" -#endif - -#ifndef SECRET_BOARD_ID - #define SECRET_BOARD_ID "" - #warning "You need to define SECRET_BOARD_ID in tab/arduino_secrets.h" -#endif - -#ifndef SECRET_DEVICE_KEY - #define SECRET_DEVICE_KEY "" - #warning "You need to define SECRET_DEVICE_KEY in tab/arduino_secrets.h" -#endif diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index af836fd31..d60f5feac 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -38,8 +38,6 @@ #include "property/types/CloudWrapperInt.h" #include "property/types/CloudWrapperString.h" -#include "CloudSerial.h" - /****************************************************************************** TYPEDEF ******************************************************************************/ diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 40b4af507..1fef05472 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -286,9 +286,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length) bytes[i] = _mqttClient.read(); } - if (_stdinTopic == topic) { - CloudSerial.appendStdin((uint8_t*)bytes, length); - } if (_dataTopicIn == topic) { CBORDecoder::decode(_property_container, (uint8_t*)bytes, length); } @@ -347,8 +344,6 @@ ArduinoIoTConnectionStatus ArduinoIoTCloudTCP::checkCloudConnection() if (ret == CONNECT_SUCCESS) { next_iot_status = ArduinoIoTConnectionStatus::CONNECTED; - CloudSerial.begin(9600); - CloudSerial.println("Hello from Cloud Serial!"); } else if (ret == CONNECT_FAILURE_SUBSCRIBE) { @@ -363,8 +358,6 @@ ArduinoIoTConnectionStatus ArduinoIoTCloudTCP::checkCloudConnection() if (reconnect() == CONNECT_SUCCESS) { next_iot_status = ArduinoIoTConnectionStatus::CONNECTED; - CloudSerial.begin(9600); - CloudSerial.println("Hello from Cloud Serial!"); } } break; diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 4c2842f0c..6bc6f0ca0 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -87,9 +87,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass // Clean up existing Mqtt connection, create a new one and initialize it int reconnect(); - friend class CloudSerialClass; - - protected: virtual int connect () override; diff --git a/src/CloudSerial.cpp b/src/CloudSerial.cpp deleted file mode 100644 index 1821a8786..000000000 --- a/src/CloudSerial.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - This file is part of ArduinoIoTCloud. - - Copyright 2019 ARDUINO SA (http://www.arduino.cc/) - - This software is released under the GNU General Public License version 3, - which covers the main part of arduino-cli. - The terms of this license can be found at: - https://www.gnu.org/licenses/gpl-3.0.en.html - - You can be released from the requirements of the above licenses by purchasing - a commercial license. Buying such a license is mandatory if you want to modify or - otherwise use the software for commercial activities involving the Arduino - software without disclosing the source code of your own applications. To purchase - a commercial license, send an email to license@arduino.cc. -*/ - -/****************************************************************************** - * INCLUDE - ******************************************************************************/ - -#include - -#ifndef HAS_LORA - -#include "ArduinoIoTCloud.h" -#include "CloudSerial.h" - -/****************************************************************************** - CTOR/DTOR - ******************************************************************************/ - -CloudSerialClass::CloudSerialClass() -{ - -} - -CloudSerialClass::~CloudSerialClass() -{ - -} - -/****************************************************************************** - * PUBLIC MEMBER FUNCTIONS - ******************************************************************************/ - -void CloudSerialClass::begin(int /*baud*/) -{ - _txBuffer.clear(); - _rxBuffer.clear(); -} - -void CloudSerialClass::end() -{ - -} - -int CloudSerialClass::available() -{ - return _rxBuffer.available(); -} - -int CloudSerialClass::availableForWrite() -{ - return _txBuffer.availableForStore(); -} - -int CloudSerialClass::peek() -{ - return _rxBuffer.peek(); -} - -int CloudSerialClass::read() -{ - return _rxBuffer.read_char(); -} - -void CloudSerialClass::flush() -{ - byte out[CLOUD_SERIAL_TX_BUFFER_SIZE]; - int length = 0; - - while (_txBuffer.available()) { - out[length++] = _txBuffer.read_char(); - } - - ArduinoCloud.write(ArduinoCloud._stdoutTopic, out, length); -} - -size_t CloudSerialClass::write(const uint8_t data) -{ - _txBuffer.store_char(data); - - if (_txBuffer.isFull() || data == '\n') { - flush(); - } - - return 1; -} - -CloudSerialClass::operator bool() -{ - return ArduinoCloud.connected(); -} - -void CloudSerialClass::appendStdin(const uint8_t *buffer, size_t size) -{ - while (!_rxBuffer.isFull() && size--) { - _rxBuffer.store_char(*buffer++); - } -} - -/****************************************************************************** - * EXTERN DEFINITION - ******************************************************************************/ - -CloudSerialClass CloudSerial; - -#endif \ No newline at end of file diff --git a/src/CloudSerial.h b/src/CloudSerial.h deleted file mode 100644 index 76ffe219b..000000000 --- a/src/CloudSerial.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - This file is part of ArduinoIoTCloud. - - Copyright 2019 ARDUINO SA (http://www.arduino.cc/) - - This software is released under the GNU General Public License version 3, - which covers the main part of arduino-cli. - The terms of this license can be found at: - https://www.gnu.org/licenses/gpl-3.0.en.html - - You can be released from the requirements of the above licenses by purchasing - a commercial license. Buying such a license is mandatory if you want to modify or - otherwise use the software for commercial activities involving the Arduino - software without disclosing the source code of your own applications. To purchase - a commercial license, send an email to license@arduino.cc. -*/ - -#ifndef CLOUD_SERIAL_H -#define CLOUD_SERIAL_H - -/****************************************************************************** - * INCLUDE - ******************************************************************************/ - -#include -#if defined(ARDUINO_ESP8266_ESP12) || defined(ARDUINO_ARCH_ESP32) || defined(ESP8266) - #include "utility/RingBuffer.h" -#else - #include -#endif - -/****************************************************************************** - * DEFINES - ******************************************************************************/ - -#define CLOUD_SERIAL_TX_BUFFER_SIZE 32 -#define CLOUD_SERIAL_RX_BUFFER_SIZE 32 - -/****************************************************************************** - * CLASS DECLARATION - ******************************************************************************/ - -class ArduinoIoTCloudTCP; - - -class CloudSerialClass : public Stream -{ - public: - CloudSerialClass(); - ~CloudSerialClass(); - - void begin(int baud); - void end(); - int available(); - int availableForWrite(); - int peek(); - int read(); - void flush(); - size_t write(const uint8_t data); - using Print::write; // pull in write(str) and write(buf, size) from Print - - operator bool(); - - protected: - - friend class ArduinoIoTCloudTCP; - - - - void appendStdin(const uint8_t *buffer, size_t size); - - private: - RingBufferN _txBuffer; - RingBufferN _rxBuffer; -}; - -/****************************************************************************** - * EXTERN DECLARATION - ******************************************************************************/ - -extern CloudSerialClass CloudSerial; - -#endif diff --git a/src/utility/RingBuffer.h b/src/utility/RingBuffer.h deleted file mode 100644 index f9d24e4ca..000000000 --- a/src/utility/RingBuffer.h +++ /dev/null @@ -1,138 +0,0 @@ -/* - Copyright (c) 2014 Arduino. 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 -*/ -#if defined(ARDUINO_ESP8266_ESP12) || defined(ARDUINO_ARCH_ESP32) || defined(ESP8266) - -#ifdef __cplusplus - -#ifndef _RING_BUFFER_ -#define _RING_BUFFER_ - -#include - -// Define constants and variables for buffering incoming serial data. We're -// using a ring buffer (I think), in which head is the index of the location -// to which to write the next incoming character and tail is the index of the -// location from which to read. -#define SERIAL_BUFFER_SIZE 64 - -template -class RingBufferN { - public: - uint8_t _aucBuffer[N] ; - volatile int _iHead ; - volatile int _iTail ; - - public: - RingBufferN(void) ; - void store_char(uint8_t c) ; - void clear(); - int read_char(); - int available(); - int availableForStore(); - int peek(); - bool isFull(); - - private: - int nextIndex(int index); -}; - -typedef RingBufferN RingBuffer; - - -template -RingBufferN::RingBufferN(void) { - memset(_aucBuffer, 0, N) ; - clear(); -} - -template -void RingBufferN::store_char(uint8_t c) { - int i = nextIndex(_iHead); - - // if we should be storing the received character into the location - // just before the tail (meaning that the head would advance to the - // current location of the tail), we're about to overflow the buffer - // and so we don't write the character or advance the head. - if (i != _iTail) { - _aucBuffer[_iHead] = c ; - _iHead = i ; - } -} - -template -void RingBufferN::clear() { - _iHead = 0; - _iTail = 0; -} - -template -int RingBufferN::read_char() { - if (_iTail == _iHead) { - return -1; - } - - uint8_t value = _aucBuffer[_iTail]; - _iTail = nextIndex(_iTail); - - return value; -} - -template -int RingBufferN::available() { - int delta = _iHead - _iTail; - - if (delta < 0) { - return N + delta; - } else { - return delta; - } -} - -template -int RingBufferN::availableForStore() { - if (_iHead >= _iTail) { - return N - 1 - _iHead + _iTail; - } else { - return _iTail - _iHead - 1; - } -} - -template -int RingBufferN::peek() { - if (_iTail == _iHead) { - return -1; - } - - return _aucBuffer[_iTail]; -} - -template -int RingBufferN::nextIndex(int index) { - return (uint32_t)(index + 1) % N; -} - -template -bool RingBufferN::isFull() { - return (nextIndex(_iHead) == _iTail); -} - -#endif /* _RING_BUFFER_ */ - -#endif /* __cplusplus */ - -#endif