From 0ac12b9c85f94a3d94fb057b89d44929f893f33b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月18日 20:47:19 +0100 Subject: [PATCH 01/22] Real Time Clock API - first draft --- api/RealTimeClock.cpp | 136 ++++++++++++++++++++++++++++++++++++++++++ api/RealTimeClock.h | 62 +++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 api/RealTimeClock.cpp create mode 100644 api/RealTimeClock.h diff --git a/api/RealTimeClock.cpp b/api/RealTimeClock.cpp new file mode 100644 index 00000000..57064f3b --- /dev/null +++ b/api/RealTimeClock.cpp @@ -0,0 +1,136 @@ +/* + RealTimeClock API + Copyright (c) 2016 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 "RealTimeClock.h" +#include + +#define YEAR_OFFSET 1900 +#define MONTH_OFFSET 1 + +RealTimeClock *arduinoSystemRTC; + +unsigned long now() +{ + if (arduinoSystemRTC) { + return arduinoSystemRTC->getTime(); + } else { + return 0; + } +} + +static struct tm* toTm(unsigned long t) +{ + time_t time = t; + return gmtime(&time); +} + +int year(unsigned long t) +{ + struct tm* tm = toTm(t); + return (tm->tm_year + YEAR_OFFSET); +} + +int year() +{ + return year(now()); +} + +int month(unsigned long t) +{ + struct tm* tm = toTm(t); + return (tm->tm_mon + MONTH_OFFSET); +} + +int month() +{ + return month(now()); +} + +int day(unsigned long t) +{ + struct tm* tm = toTm(t); + return tm->tm_mday; +} + +int day() +{ + return day(now()); +} + +int hour(unsigned long t) +{ + struct tm* tm = toTm(t); + return tm->tm_hour; +} + +int hour() +{ + return hour(now()); +} + +int minute(unsigned long t) +{ + struct tm* tm = toTm(t); + return tm->tm_min; +} + +int minute() +{ + return minute(now()); +} + +int second(unsigned long t) +{ + struct tm* tm = toTm(t); + return tm->tm_sec; +} + +int second() +{ + return second(now()); +} + +void setTime(unsigned long t) +{ + if (arduinoSystemRTC) { + arduinoSystemRTC->setTime(t); + } +} + +unsigned long convertTime(int hour, int minute, int second, int day, int month, int year) +{ + struct tm tm; + + tm.tm_year = year - YEAR_OFFSET; + tm.tm_mon = month - MONTH_OFFSET; + tm.tm_mday = day; + tm.tm_hour = hour; + tm.tm_min = minute; + tm.tm_sec = second; + tm.tm_isdst = -1; + + return mktime(&tm); +} + +void setTime(int hour, int minute, int second, int day, int month, int year) +{ + time_t t = convertTime(hour, minute, second, day, month, year); + + setTime(t); +} diff --git a/api/RealTimeClock.h b/api/RealTimeClock.h new file mode 100644 index 00000000..c465dee2 --- /dev/null +++ b/api/RealTimeClock.h @@ -0,0 +1,62 @@ +/* + RealTimeClock API + Copyright (c) 2016 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 ARDUINO_REAL_TIME_CLOCK_H +#define ARDUINO_REAL_TIME_CLOCK_H + +#define ARDUINO_REAL_TIME_CLOCK_API_VERSION 10000 // version 1.0.0 + +// Functions to get and set current time + +int year(); // current year as an integer +int month(); // current month as an integer (1 - 12) +int day(); // current day as an integer (1 - 31) +int hour(); // current hour as an integer (0 - 23) +int minute(); // current minute as an integer (0 - 59) +int second(); // current second as an integer (0 - 59) +void setTime(int hour, int minute, int second, int day, int month, int year); // set the current time + +// Functions to get ans set current time with unix-timestamps + +unsigned long now(); // current time as seconds since Jan 1 1970 +void setTime(unsigned long t); // set the current time from seconds since Jan 1 1970 + +// Functions to convert time from unix-timestamp to human readable format and viceversa + +int year(unsigned long t); // year of t as an integer +int month(unsigned long t); // month of t as an integer (1 - 12) +int day(unsigned long t); // day of t as an integer (1 - 31) +int hour(unsigned long t); // hour of t as an integer (0 - 23) +int minute(unsigned long t); // minute of t as an integer (0 - 59) +int second(unsigned long t); // second of t as an integer (0 - 59) + +unsigned long convertTime(int hour, int minute, int second, int day, int month, int year); + +// Real Time Clock interface +// Time sources should implement this interfaces +class RealTimeClock +{ +public: + virtual unsigned long getTime() = 0; + virtual bool setTime(unsigned long t) = 0; +}; + +extern RealTimeClock *arduinoSystemRTC; + +#endif From dfe0136e8d4d75a8067b5beedd640ac37561f8f8 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月24日 11:25:28 +0100 Subject: [PATCH 02/22] Added motion interfaces --- api/Motion.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 api/Motion.h diff --git a/api/Motion.h b/api/Motion.h new file mode 100644 index 00000000..4d669d2e --- /dev/null +++ b/api/Motion.h @@ -0,0 +1,35 @@ +/* + MotionSense API + Copyright (c) 2016 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 ARDUINO_MOTION_SENSE_H +#define ARDUINO_MOTION_SENSE_H + +class Accelerometer { + virtual bool readAcceleration(float &x, float &y, float &z) = 0; + virtual unsigned int availableAcceleration() = 0; + virtual unsigned long sampleRateAcceleration() = 0; +} + +class Gyro { + virtual bool readGyro(float &x, float &y, float &z) = 0; + virtual unsigned int availableGyro() = 0; + virtual unsigned long sampleRateGyro() = 0; +} + +#endif From 0e1a28b0500c4eaffcc1c7b6215c7071b1fd092b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月24日 11:25:35 +0100 Subject: [PATCH 03/22] Fixed some typos --- api/RealTimeClock.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/RealTimeClock.h b/api/RealTimeClock.h index c465dee2..8f678fd5 100644 --- a/api/RealTimeClock.h +++ b/api/RealTimeClock.h @@ -32,7 +32,7 @@ int minute(); // current minute as an integer (0 - 59) int second(); // current second as an integer (0 - 59) void setTime(int hour, int minute, int second, int day, int month, int year); // set the current time -// Functions to get ans set current time with unix-timestamps +// Functions to get and set current time with unix-timestamps unsigned long now(); // current time as seconds since Jan 1 1970 void setTime(unsigned long t); // set the current time from seconds since Jan 1 1970 @@ -49,7 +49,7 @@ int second(unsigned long t); // second of t as an integer (0 - 59) unsigned long convertTime(int hour, int minute, int second, int day, int month, int year); // Real Time Clock interface -// Time sources should implement this interfaces +// Time sources should implement this interface class RealTimeClock { public: From 67b89145c41613360e601ca26a5ed0cc2b05ae90 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月24日 15:27:51 +0100 Subject: [PATCH 04/22] Added Rotation and Quaternion structures --- api/Motion.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/api/Motion.h b/api/Motion.h index 4d669d2e..446e8b40 100644 --- a/api/Motion.h +++ b/api/Motion.h @@ -24,12 +24,30 @@ class Accelerometer { virtual bool readAcceleration(float &x, float &y, float &z) = 0; virtual unsigned int availableAcceleration() = 0; virtual unsigned long sampleRateAcceleration() = 0; -} +}; class Gyro { virtual bool readGyro(float &x, float &y, float &z) = 0; virtual unsigned int availableGyro() = 0; virtual unsigned long sampleRateGyro() = 0; -} +}; + +struct Rotation { + float yaw; + float pitch; + float roll; +}; + +struct Quaternion { + union { + float q[4]; + struct { + float x; + float y; + float z; + float w; + }; + }; +}; #endif From 9924ebc098bca34ad131d003f18aed41d7a34f1d Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月24日 16:08:02 +0100 Subject: [PATCH 05/22] Renamed Motion.h -> MotionSense.h. Added some comments --- api/{Motion.h => MotionSense.h} | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) rename api/{Motion.h => MotionSense.h} (63%) diff --git a/api/Motion.h b/api/MotionSense.h similarity index 63% rename from api/Motion.h rename to api/MotionSense.h index 446e8b40..e3a891f6 100644 --- a/api/Motion.h +++ b/api/MotionSense.h @@ -20,16 +20,31 @@ #ifndef ARDUINO_MOTION_SENSE_H #define ARDUINO_MOTION_SENSE_H +// Sensors that doesn't implement a FIFO can use the default +// implementation of availableXxx() and sampleRateXxx() + +// Base class for accelerometers class Accelerometer { + // read an acceleration sample from the FIFO or wait until one is available virtual bool readAcceleration(float &x, float &y, float &z) = 0; - virtual unsigned int availableAcceleration() = 0; - virtual unsigned long sampleRateAcceleration() = 0; + + // number of samples in the FIFO + virtual unsigned int availableAcceleration() { return 1; } + + // sampling rate of the sensor + virtual unsigned long sampleRateAcceleration() { return 0; } }; +// Base class for gyro class Gyro { + // read a gyro sample from the FIFO or wait until one is available virtual bool readGyro(float &x, float &y, float &z) = 0; - virtual unsigned int availableGyro() = 0; - virtual unsigned long sampleRateGyro() = 0; + + // number of samples in the FIFO + virtual unsigned int availableGyro() { return 1; } + + // sampling rate of the sensor + virtual unsigned long sampleRateGyro() { return 0; } }; struct Rotation { From 3eef53de4d612dac276e86886692b4adedb763c2 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月24日 16:08:47 +0100 Subject: [PATCH 06/22] added Magnetometer class --- api/MotionSense.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/api/MotionSense.h b/api/MotionSense.h index e3a891f6..d8e370ec 100644 --- a/api/MotionSense.h +++ b/api/MotionSense.h @@ -47,6 +47,18 @@ class Gyro { virtual unsigned long sampleRateGyro() { return 0; } }; +// Base class for magnetometers +class Magnetometer { + // read a magnetometer sample from the FIFO or wait until one is available + virtual bool readMagneticField(float &x, float &y, float &z) = 0; + + // Number of samples in the FIFO. + virtual unsigned int availableMagneticField() { return 1; } + + // Sampling rate of the sensor. + virtual unsigned long sampleRateMagneticField() { return 0; } +}; + struct Rotation { float yaw; float pitch; From b66a1bb0b9259ed11d4a26d9be656c56450aed9c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月25日 12:12:29 +0100 Subject: [PATCH 07/22] Added some comments about units. Renamed Gyro -> Gyroscope --- api/MotionSense.h | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/api/MotionSense.h b/api/MotionSense.h index d8e370ec..4a02a90d 100644 --- a/api/MotionSense.h +++ b/api/MotionSense.h @@ -21,35 +21,39 @@ #define ARDUINO_MOTION_SENSE_H // Sensors that doesn't implement a FIFO can use the default -// implementation of availableXxx() and sampleRateXxx() +// implementation of availableXxx() and sampleRateXxx() and +// always return the last valid sample. // Base class for accelerometers class Accelerometer { - // read an acceleration sample from the FIFO or wait until one is available + // Read an acceleration sample from the FIFO or wait until one is available. + // Results are in G (earth gravity). virtual bool readAcceleration(float &x, float &y, float &z) = 0; - // number of samples in the FIFO + // Number of samples in the FIFO. virtual unsigned int availableAcceleration() { return 1; } - // sampling rate of the sensor + // Sampling rate of the sensor. virtual unsigned long sampleRateAcceleration() { return 0; } }; -// Base class for gyro -class Gyro { - // read a gyro sample from the FIFO or wait until one is available - virtual bool readGyro(float &x, float &y, float &z) = 0; +// Base class for gyroscope +class Gyroscope { + // Read a gyro sample from the FIFO or wait until one is available. + // Results are in degrees/second. + virtual bool readGyroscope(float &x, float &y, float &z) = 0; - // number of samples in the FIFO - virtual unsigned int availableGyro() { return 1; } + // Number of samples in the FIFO. + virtual unsigned int availableGyroscope() { return 1; } - // sampling rate of the sensor - virtual unsigned long sampleRateGyro() { return 0; } + // Sampling rate of the sensor. + virtual unsigned long sampleRateGyroscope() { return 0; } }; // Base class for magnetometers class Magnetometer { - // read a magnetometer sample from the FIFO or wait until one is available + // Read a magnetometer sample from the FIFO or wait until one is available. + // Results are in uT (micro Tesla). virtual bool readMagneticField(float &x, float &y, float &z) = 0; // Number of samples in the FIFO. From 4c1e94f8c706ea22b2036f65acab864cd965ad6c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月25日 12:14:05 +0100 Subject: [PATCH 08/22] Correct quaternion order --- api/MotionSense.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/MotionSense.h b/api/MotionSense.h index 4a02a90d..403bccee 100644 --- a/api/MotionSense.h +++ b/api/MotionSense.h @@ -73,10 +73,10 @@ struct Quaternion { union { float q[4]; struct { + float w; float x; float y; float z; - float w; }; }; }; From 0a9804fe66bce745a416543a1630beca608b575c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月25日 12:14:52 +0100 Subject: [PATCH 09/22] EulerAngles is a more appropriate name --- api/MotionSense.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/MotionSense.h b/api/MotionSense.h index 403bccee..97726848 100644 --- a/api/MotionSense.h +++ b/api/MotionSense.h @@ -63,7 +63,7 @@ class Magnetometer { virtual unsigned long sampleRateMagneticField() { return 0; } }; -struct Rotation { +struct EulerAngles { float yaw; float pitch; float roll; From 6e0ca14846f2d95e2223fdf752218519fd902ba8 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月25日 12:25:48 +0100 Subject: [PATCH 10/22] Added Magnetometer.expectedMagneticFieldStrength() method --- api/MotionSense.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api/MotionSense.h b/api/MotionSense.h index 97726848..c9366cfa 100644 --- a/api/MotionSense.h +++ b/api/MotionSense.h @@ -56,6 +56,12 @@ class Magnetometer { // Results are in uT (micro Tesla). virtual bool readMagneticField(float &x, float &y, float &z) = 0; + // Returns the expected field strength. Filter algorithms can use this value + // to detect and ignore when a strong magnetic field or nearby ferrous metal + // objects interfere with the magnetometer readings. + // Defaults to 50.0 uT for sensors that lacks this calibration info. + virtual float expectedMagneticFieldStrength() { return 50.0f; } + // Number of samples in the FIFO. virtual unsigned int availableMagneticField() { return 1; } From 0a96c9a3b5926418977a5e4c2870f8d5fc14125b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月25日 12:28:50 +0100 Subject: [PATCH 11/22] Added comment on EulerAngles units --- api/MotionSense.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/MotionSense.h b/api/MotionSense.h index c9366cfa..076fc3db 100644 --- a/api/MotionSense.h +++ b/api/MotionSense.h @@ -69,6 +69,11 @@ class Magnetometer { virtual unsigned long sampleRateMagneticField() { return 0; } }; +// EulerAngles represents a rotation in the most commonly accepted +// NED (North, East, Down) right hand rule, with: +// yaw from 0 to 360 degrees +// pitch from -90 to 90 degrees +// roll from -180 to 180 degrees struct EulerAngles { float yaw; float pitch; From 9be6359073834067261cdf208594a2490e8a4ebc Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: 2016年3月25日 15:36:45 +0100 Subject: [PATCH 12/22] No defaults FIFO methods for gyroscope. The data provided from gyroscope has no sense without timing information. This means that a correct FIFO or rate-limiting must be implemented from the sub-class. Close #1 --- api/MotionSense.h | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/api/MotionSense.h b/api/MotionSense.h index 076fc3db..bdcb6813 100644 --- a/api/MotionSense.h +++ b/api/MotionSense.h @@ -20,11 +20,25 @@ #ifndef ARDUINO_MOTION_SENSE_H #define ARDUINO_MOTION_SENSE_H -// Sensors that doesn't implement a FIFO can use the default -// implementation of availableXxx() and sampleRateXxx() and -// always return the last valid sample. +// Base class for gyroscope +class Gyroscope { + // Read a gyro sample from the FIFO or wait until one is available. + // Results are in degrees/second. + virtual bool readGyroscope(float &x, float &y, float &z) = 0; + + // Number of samples in the FIFO. + // Sensors without a FIFO should return 1 only if the sensor has + // produced a new measurement since the most recent read. + virtual unsigned int availableGyroscope() = 0; + + // Sampling rate of the sensor. + virtual unsigned long sampleRateGyroscope() = 0; +}; // Base class for accelerometers +// Sensors without a FIFO can use the default implementation of +// availableXxx() and sampleRateXxx() and always return the last +// valid measurement. class Accelerometer { // Read an acceleration sample from the FIFO or wait until one is available. // Results are in G (earth gravity). @@ -37,20 +51,10 @@ class Accelerometer { virtual unsigned long sampleRateAcceleration() { return 0; } }; -// Base class for gyroscope -class Gyroscope { - // Read a gyro sample from the FIFO or wait until one is available. - // Results are in degrees/second. - virtual bool readGyroscope(float &x, float &y, float &z) = 0; - - // Number of samples in the FIFO. - virtual unsigned int availableGyroscope() { return 1; } - - // Sampling rate of the sensor. - virtual unsigned long sampleRateGyroscope() { return 0; } -}; - // Base class for magnetometers +// Sensors without a FIFO can use the default implementation of +// availableXxx() and sampleRateXxx() and always return the last +// valid measurement. class Magnetometer { // Read a magnetometer sample from the FIFO or wait until one is available. // Results are in uT (micro Tesla). From bde41a3517675ab48f32eff135b5c7b28f6ea3bb Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 8 Apr 2016 15:50:49 +0200 Subject: [PATCH 13/22] Added TimeProvider and related functions --- api/RealTimeClock.h | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/api/RealTimeClock.h b/api/RealTimeClock.h index 8f678fd5..f554b6d0 100644 --- a/api/RealTimeClock.h +++ b/api/RealTimeClock.h @@ -22,6 +22,9 @@ #define ARDUINO_REAL_TIME_CLOCK_API_VERSION 10000 // version 1.0.0 +class RealTimeClock; +class TimeProvider; + // Functions to get and set current time int year(); // current year as an integer @@ -45,18 +48,36 @@ int day(unsigned long t); // day of t as an integer (1 - 31) int hour(unsigned long t); // hour of t as an integer (0 - 23) int minute(unsigned long t); // minute of t as an integer (0 - 59) int second(unsigned long t); // second of t as an integer (0 - 59) - unsigned long convertTime(int hour, int minute, int second, int day, int month, int year); +// Synchronize the clock with a time provider +// Return true if the synchronization is successful, false otherwise. +bool setTime(TimeProvider &provider); + // Real Time Clock interface -// Time sources should implement this interface +// Clocks should implement this interface class RealTimeClock { public: + // Get the current time as unix-timestamp. + // Return 0 if time has not been set. virtual unsigned long getTime() = 0; + + // Set the current time as unix-timestamp virtual bool setTime(unsigned long t) = 0; }; extern RealTimeClock *arduinoSystemRTC; +// Time Provider interface +// Class that performs time synchronization with a master time (atomic clock, GPS, +// NTP, etc.) should implement this interface. +class TimeProvider +{ +public: + // Query the time provider for the current real time and return it as unix-timestamp. + // Returns 0 if the query fails. + virtual unsigned long getTime() = 0; +}; + #endif From 58dae9e92e0bab2bc3057177f398f2c973c7273d Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 8 Apr 2016 16:06:06 +0200 Subject: [PATCH 14/22] Added basic SoftwareRTC implementation. See #3 --- api/SoftwareRTC.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ api/SoftwareRTC.h | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 api/SoftwareRTC.cpp create mode 100644 api/SoftwareRTC.h diff --git a/api/SoftwareRTC.cpp b/api/SoftwareRTC.cpp new file mode 100644 index 00000000..676974d9 --- /dev/null +++ b/api/SoftwareRTC.cpp @@ -0,0 +1,46 @@ +/* + Software RealTimeClock + Copyright (c) 2016 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 "Arduino.h" +#include "SoftwareRTC.h" + +SoftwareRTC::SoftwareRTC() : lastTimestamp(0), lastMillis(0) +{ + // Empty +} + +unsigned long SoftwareRTC::getTime() +{ + if (lastTimestamp == 0) return 0; + unsigned long now = millis(); + unsigned long delta = now - lastMillis; + if (delta>= 1000) { + delta /= 1000; // Determine the number of seconds elapsed + lastMillis += delta * 1000; + lastTimestamp += delta; + } + return lastTimestamp; +} + +bool SoftwareRTC::setTime(unsigned long t) +{ + lastTimestamp = t; + lastMillis = millis(); +} + diff --git a/api/SoftwareRTC.h b/api/SoftwareRTC.h new file mode 100644 index 00000000..74762e31 --- /dev/null +++ b/api/SoftwareRTC.h @@ -0,0 +1,41 @@ +/* + Software RealTimeClock + Copyright (c) 2016 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 ARDUINO_SOFTWARE_RTC_H +#define ARDUINO_SOFTWARE_RTC_H + +#include "RealTimeClock.h" + +class SoftwareRTC : public RealTimeClock { +public: + SoftwareRTC(); + + // Get the current time as unix-timestamp. + // Return 0 if time has not been set. + virtual unsigned long getTime(); + + // Set the current time as unix-timestamp + virtual bool setTime(unsigned long t); + +private: + unsigned long lastTimestamp; + unsigned long lastMillis; +}; + +#endif From c40049519544de6c6aaa0422ce04fb67e794301b Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: 2016年4月14日 15:17:50 -0400 Subject: [PATCH 15/22] Add initial BLE API based on the BLEPeripheral library --- api/BLE/ArduinoBLE.h | 27 ++++ api/BLE/BLEAttribute.h | 47 +++++++ api/BLE/BLECharacteristic.h | 220 ++++++++++++++++++++++++++++++ api/BLE/BLEDescriptor.h | 38 ++++++ api/BLE/BLELocalAttribute.h | 31 +++++ api/BLE/BLEPeripheral.h | 70 ++++++++++ api/BLE/BLERemoteAttribute.h | 31 +++++ api/BLE/BLERemoteCentral.h | 35 +++++ api/BLE/BLERemoteCharacteristic.h | 59 ++++++++ api/BLE/BLERemoteService.h | 31 +++++ api/BLE/BLEService.h | 31 +++++ 11 files changed, 620 insertions(+) create mode 100644 api/BLE/ArduinoBLE.h create mode 100644 api/BLE/BLEAttribute.h create mode 100644 api/BLE/BLECharacteristic.h create mode 100644 api/BLE/BLEDescriptor.h create mode 100644 api/BLE/BLELocalAttribute.h create mode 100644 api/BLE/BLEPeripheral.h create mode 100644 api/BLE/BLERemoteAttribute.h create mode 100644 api/BLE/BLERemoteCentral.h create mode 100644 api/BLE/BLERemoteCharacteristic.h create mode 100644 api/BLE/BLERemoteService.h create mode 100644 api/BLE/BLEService.h diff --git a/api/BLE/ArduinoBLE.h b/api/BLE/ArduinoBLE.h new file mode 100644 index 00000000..28bca1a6 --- /dev/null +++ b/api/BLE/ArduinoBLE.h @@ -0,0 +1,27 @@ +/* + BLE API + Copyright (c) 2016 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 ARDUINO_BLE_H +#define ARDUINO_BLE_H + +#define ARDUINO_BLE_API_VERSION 10000 // version 1.0.0 + +#include + +#endif diff --git a/api/BLE/BLEAttribute.h b/api/BLE/BLEAttribute.h new file mode 100644 index 00000000..3465042b --- /dev/null +++ b/api/BLE/BLEAttribute.h @@ -0,0 +1,47 @@ +/* + BLE Attribute API + Copyright (c) 2016 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 ARDUINO_BLE_ATTRIBUTE_H +#define ARDUINO_BLE_ATTRIBUTE_H + +enum BLEAttributeType { + BLETypeService = 0x2800, + BLETypeCharacteristic = 0x2803, + BLETypeDescriptor = 0x2900 +}; + +enum BLEProperty { + BLEBroadcast = 0x01, + BLERead = 0x02, + BLEWriteWithoutResponse = 0x04, + BLEWrite = 0x08, + BLENotify = 0x10, + BLEIndicate = 0x20 +}; + +class BLEAttribute +{ +public: + BLEAttribute(const char* uuid, enum BLEAttributeType type); + const char* uuid() const; // get the UUID as a string + + enum BLEAttributeType type() const; +}; + +#endif diff --git a/api/BLE/BLECharacteristic.h b/api/BLE/BLECharacteristic.h new file mode 100644 index 00000000..5877079a --- /dev/null +++ b/api/BLE/BLECharacteristic.h @@ -0,0 +1,220 @@ +/* + BLE Characteristic API + Copyright (c) 2016 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 ARDUINO_BLE_CHARACTERISTIC_H +#define ARDUINO_BLE_CHARACTERISTIC_H + +#include "BLELocalAttribute.h" + +enum BLECharacteristicEvent { + BLEWritten = 0, + BLESubscribed = 1, + BLEUnsubscribed = 2 +}; + +typedef void (*BLECharacteristicEventHandler)(BLECentral& central, BLECharacteristic& characteristic); + +class BLECharacteristic : public BLELocalAttribute +{ +public: + BLECharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize); // create a characteristic with specified value size + BLECharacteristic(const char* uuid, unsigned char properties, const char* value); // create a characteristic with string value + + virtual ~BLECharacteristic(); + + unsigned char properties() const; // returns the property mask of the characteristic + + virtual unsigned char valueSize() const; // returns the maximum size of the value + virtual const unsigned char* value() const; // returns the value buffer + virtual unsigned char valueLength() const; // returns the current length of the value + virtual unsigned char operator[] (int offset) const; // returns a byte of the value at the specified offset + + virtual bool fixedLength() const; // does the characteristic have a fixed length (valueSize always equals length) + + virtual bool setValue(const unsigned char value[], unsigned char length); // set the value of the characteristc + virtual bool setValue(const char* value); // set the value of the characteristic from a string + + bool broadcast(); // broadcast the characteristic value in the advertisement data + + bool written(); // has the central written a new value + bool subscribed(); // is the central subscribed + bool canNotify(); // can a notification be sent to the central + bool canIndicate(); // can a indication be sent to the central + + void setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler eventHandler); // set an event handler (callback) +}; + + +// Fixed length characteristic, value size is constant +class BLEFixedLengthCharacteristic : public BLECharacteristic { +public: + BLEFixedLengthCharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize); + BLEFixedLengthCharacteristic(const char* uuid, unsigned char properties, const char* value); + + virtual bool fixedLength() const; +}; + +#endif + +// Characteristic type to represent a bool type +class BLEBoolCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEBoolCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(bool value); + bool value(); +}; + +// Characteristic type to represent a char type +class BLECharCharacteristic : public BLETypedCharacteristic { +public: + BLECharCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(char value); + char value(); +}; + +// Characteristic type to represent an unsigned char type +class BLEUnsignedCharCharacteristic : BLEFixedLengthCharacteristic { +public: + BLEUnsignedCharCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(unsigned char value); + unsigned char value(); +}; + +// Characteristic type to represent a short type +class BLEShortCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEShortCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(short value); + short value(); + + bool setValueLE(short value); + short valueLE(); + + bool setValueBE(short value); + short valueBE(); +}; + +// Characteristic type to represent a unsigned short type +class BLEUnsignedShortCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEUnsignedShortCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(unsigned short value); + unsigned short value(); + + bool setValueLE(unsigned short value); + unsigned short valueLE(); + + bool setValueBE(unsigned short value); + unsigned short valueBE(); +}; + +// Characteristic type to represent a int type +class BLEIntCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEIntCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(int value); + int value(); + + bool setValueLE(int value); + int valueLE(); + + bool setValueBE(int value); + int valueBE(); +}; + +// Characteristic type to represent a unsigned int type +class BLEUnsignedIntCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEUnsignedIntCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(unsigned int value); + unsigned int value(); + + bool setValueLE(unsigned int value); + unsigned int valueLE(); + + bool setValueBE(unsigned int value); + unsigned int valueBE(); +}; + +// Characteristic type to represent a long type +class BLELongCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLELongCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(long value); + long value(); + + bool setValueLE(long value); + long valueLE(); + + bool setValueBE(long value); + long valueBE(); +}; + +// Characteristic type to represent a unsigned long type +class BLEUnsignedLongCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEUnsignedLongCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(unsigned long value); + unsigned long value(); + + bool setValueLE(unsigned long value); + unsigned long valueLE(); + + bool setValueBE(unsigned long value); + unsigned long valueBE(); +}; + +// Characteristic type to represent a float type +class BLEFloatCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEFloatCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(float value); + float value(); + + bool setValueLE(float value); + float valueLE(); + + bool setValueBE(float value); + float valueBE(); +}; + +// Characteristic type to represent a double type +class BLEDoubleCharacteristic : public BLEFixedLengthCharacteristic { +public: + BLEDoubleCharacteristic(const char* uuid, unsigned char properties); + + bool setValue(double value); + double value(); + + bool setValueLE(double value); + double valueLE(); + + bool setValueBE(double value); + double valueBE(); +}; diff --git a/api/BLE/BLEDescriptor.h b/api/BLE/BLEDescriptor.h new file mode 100644 index 00000000..7487b859 --- /dev/null +++ b/api/BLE/BLEDescriptor.h @@ -0,0 +1,38 @@ +/* + BLE Descriptor API + Copyright (c) 2016 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 ARDUINO_BLE_DESCRIPTOR_H +#define ARDUINO_BLE_DESCRIPTOR_H + +#include "BLELocalAttribute.h" + +class BLEDescriptor : public BLELocalAttribute +{ + public: + BLEDescriptor(const char* uuid, const unsigned char value[], unsigned char valueLength); // create a descriptor the specified uuid and value + BLEDescriptor(const char* uuid, const char* value); // create a descriptor the specified uuid and string value + + virtual ~BLEDescriptor(); + + virtual const unsigned char* value() const; // returns the value buffer + virtual unsigned char valueLength() const; // returns the current length of the value + virtual unsigned char operator[] (int offset) const; // returns a byte of the value at the specified offset +}; + +#endif diff --git a/api/BLE/BLELocalAttribute.h b/api/BLE/BLELocalAttribute.h new file mode 100644 index 00000000..b8d19c71 --- /dev/null +++ b/api/BLE/BLELocalAttribute.h @@ -0,0 +1,31 @@ +/* + BLE Local Attribute API + Copyright (c) 2016 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 ARDUINO_BLE_LOCAL_ATTRIBUTE_H +#define ARDUINO_BLE_LOCAL_ATTRIBUTE_H + +#include "BLEAttribute.h" + +class BLELocalAttribute : public BLEAttribute +{ +public: + BLELocalAttribute(const char* uuid, enum BLEAttributeType type); +}; + +#endif diff --git a/api/BLE/BLEPeripheral.h b/api/BLE/BLEPeripheral.h new file mode 100644 index 00000000..df8d286f --- /dev/null +++ b/api/BLE/BLEPeripheral.h @@ -0,0 +1,70 @@ +/* + BLE Peripheral API + Copyright (c) 2016 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 ARDUINO_BLE_PERIPHERAL_H +#define ARDUINO_BLE_PERIPHERAL_H + +#include +#include +#include + +enum BLEPeripheralEvent { + BLEConnected = 0, + BLEDisconnected = 1, + BLEBonded = 2, + BLERemoteServicesDiscovered = 2 +}; + +typedef void (*BLEPeripheralEventHandler)(BLERemoteCentral& central); + +class BLEPeripheral +{ +public: + BLEPeripheral(); + virtual ~BLEPeripheral(); + + void begin(); // initiliaze hardware, setup attributes, and start advertising + void poll(); // poll for events + void end(); // stop advertising and initiliaze hardware + + void setAdvertisedServiceUuid(const char* advertisedServiceUuid); // set the service UUID that is advertised + void setServiceSolicitationUuid(const char* serviceSolicitationUuid); // set the service that is solicited in the advertisement + void setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength); // set the manufacturer data that is advertised + void setLocalName(const char *localName); // set the local name that is advertised + + void setAdvertisingInterval(unsigned short advertisingInterval); // set the advertising interval in ms + void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval); // set the min and max connection interval in 1.25ms increments + bool setTxPower(int txPower); // set the TX power of the radio in dBM + void setConnectable(bool connectable); // control if the peripheral is connectable + + void setDeviceName(const char* deviceName); // set the value of the device name characteristic + void setAppearance(unsigned short appearance); // set the value of the appearance characteristic + + void addLocalAttribute(BLELocalAttribute& localAttribute); // add a local attribute (service, characteristic, descriptor) + void addRemoteAttribute(BLERemoteAttribute& remoteAttribute); // add a remove attribute (service, characteristic, descriptor) + + void disconnect(); // disconnect the central that is connected + + BLERemoteCentral central(); // returns the currently connected central + bool connected(); // is the peripheral connected to a central now + + void setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler); // set an event handler (callback) +}; + +#endif diff --git a/api/BLE/BLERemoteAttribute.h b/api/BLE/BLERemoteAttribute.h new file mode 100644 index 00000000..ccd3e61c --- /dev/null +++ b/api/BLE/BLERemoteAttribute.h @@ -0,0 +1,31 @@ +/* + BLE Remote Attribute API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_ATTRIBUTE_H +#define ARDUINO_BLE_REMOTE_ATTRIBUTE_H + +#include "BLEAttribute.h" + +class BLERemoteAttribute : public BLEAttribute +{ +public: + BLERemoteAttribute(const char* uuid, enum BLEAttributeType type); +}; + +#endif diff --git a/api/BLE/BLERemoteCentral.h b/api/BLE/BLERemoteCentral.h new file mode 100644 index 00000000..35158fc7 --- /dev/null +++ b/api/BLE/BLERemoteCentral.h @@ -0,0 +1,35 @@ +/* + BLE Centrel API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_CENTRAL_H +#define ARDUINO_BLE_REMOTE_CENTRAL_H + +class BLERemoteCentral +{ +public: + operator bool() const; // is the central connected + + bool connected(); // is the central connected + const char* address() const; // Bluetooth address of the central as a string + void poll(); // poll the central for events + + void disconnect(); // disconnect the central +}; + +#endif diff --git a/api/BLE/BLERemoteCharacteristic.h b/api/BLE/BLERemoteCharacteristic.h new file mode 100644 index 00000000..0d9a3a91 --- /dev/null +++ b/api/BLE/BLERemoteCharacteristic.h @@ -0,0 +1,59 @@ +/* + BLE Remote Characteristic API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_CHARACTERISTIC_H +#define ARDUINO_BLE_REMOTE_CHARACTERISTIC_H + +#include "BLERemoteAttribute.h" +#include "BLEDeviceLimits.h" + +enum BLERemoteCharacteristicEvent { + BLEValueUpdated = 0 +}; + +typedef void (*BLERemoteCharacteristicEventHandler)(BLERemoteCentral& central, BLERemoteCharacteristic& characteristic); + +class BLERemoteCharacteristic : public BLERemoteAttribute +{ +public: + BLERemoteCharacteristic(const char* uuid, unsigned char properties); + + virtual ~BLERemoteCharacteristic(); + + unsigned char properties() const; // returns the property mask of the characteristic + + const unsigned char* value() const; // returns the value buffer + unsigned char valueLength() const; // returns the current length of the value + virtual unsigned char operator[] (int offset) const; // returns a byte of the value at the specified offset + + bool canRead(); // can the remote characteristic be read + bool read(); // send a read request + bool canWrite(); // can the remote characteristic be written + bool write(const unsigned char value[], unsigned char length); // send a write request + bool canSubscribe(); // can the remote characteristic be subscribed to + bool subscribe(); // subscribe to the remote characteristic + bool canUnsubscribe(); // can the remote characteristic be unsubscribed from + bool unsubscribe(); // unsubscribe from the remote characteristic + + bool valueUpdated(); // has the peripheral updated a new value + + void setEventHandler(BLERemoteCharacteristicEvent event, BLERemoteCharacteristicEventHandler eventHandler); // set an event handler (callback) +}; + +#endif diff --git a/api/BLE/BLERemoteService.h b/api/BLE/BLERemoteService.h new file mode 100644 index 00000000..4f44190b --- /dev/null +++ b/api/BLE/BLERemoteService.h @@ -0,0 +1,31 @@ +/* + BLE Remote Service API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_SERVICE_H +#define ARDUINO_BLE_REMOTE_SERVICE_H + +#include "BLERemoteAttribute.h" + +class BLERemoteService : public BLERemoteAttribute +{ +public: + BLERemoteService(const char* uuid); +}; + +#endif diff --git a/api/BLE/BLEService.h b/api/BLE/BLEService.h new file mode 100644 index 00000000..a448d862 --- /dev/null +++ b/api/BLE/BLEService.h @@ -0,0 +1,31 @@ +/* + BLE Service API + Copyright (c) 2016 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 ARDUINO_BLE_SERVICE_H +#define ARDUINO_BLE_SERVICE_H + +#include "BLELocalAttribute.h" + +class BLEService : public BLELocalAttribute +{ +public: + BLEService(const char* uuid); +} + +#endif From 38424e21a92457ab9403c5b30bec4829a1103c69 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: 2016年4月26日 17:20:06 -0400 Subject: [PATCH 16/22] Move BLE peripheral API into own folder --- api/BLE/{ => peripheral}/ArduinoBLE.h | 0 api/BLE/{ => peripheral}/BLEAttribute.h | 0 api/BLE/{ => peripheral}/BLECharacteristic.h | 0 api/BLE/{ => peripheral}/BLEDescriptor.h | 0 api/BLE/{ => peripheral}/BLELocalAttribute.h | 0 api/BLE/{ => peripheral}/BLEPeripheral.h | 0 api/BLE/{ => peripheral}/BLERemoteAttribute.h | 0 api/BLE/{ => peripheral}/BLERemoteCentral.h | 0 api/BLE/{ => peripheral}/BLERemoteCharacteristic.h | 0 api/BLE/{ => peripheral}/BLERemoteService.h | 0 api/BLE/{ => peripheral}/BLEService.h | 0 11 files changed, 0 insertions(+), 0 deletions(-) rename api/BLE/{ => peripheral}/ArduinoBLE.h (100%) rename api/BLE/{ => peripheral}/BLEAttribute.h (100%) rename api/BLE/{ => peripheral}/BLECharacteristic.h (100%) rename api/BLE/{ => peripheral}/BLEDescriptor.h (100%) rename api/BLE/{ => peripheral}/BLELocalAttribute.h (100%) rename api/BLE/{ => peripheral}/BLEPeripheral.h (100%) rename api/BLE/{ => peripheral}/BLERemoteAttribute.h (100%) rename api/BLE/{ => peripheral}/BLERemoteCentral.h (100%) rename api/BLE/{ => peripheral}/BLERemoteCharacteristic.h (100%) rename api/BLE/{ => peripheral}/BLERemoteService.h (100%) rename api/BLE/{ => peripheral}/BLEService.h (100%) diff --git a/api/BLE/ArduinoBLE.h b/api/BLE/peripheral/ArduinoBLE.h similarity index 100% rename from api/BLE/ArduinoBLE.h rename to api/BLE/peripheral/ArduinoBLE.h diff --git a/api/BLE/BLEAttribute.h b/api/BLE/peripheral/BLEAttribute.h similarity index 100% rename from api/BLE/BLEAttribute.h rename to api/BLE/peripheral/BLEAttribute.h diff --git a/api/BLE/BLECharacteristic.h b/api/BLE/peripheral/BLECharacteristic.h similarity index 100% rename from api/BLE/BLECharacteristic.h rename to api/BLE/peripheral/BLECharacteristic.h diff --git a/api/BLE/BLEDescriptor.h b/api/BLE/peripheral/BLEDescriptor.h similarity index 100% rename from api/BLE/BLEDescriptor.h rename to api/BLE/peripheral/BLEDescriptor.h diff --git a/api/BLE/BLELocalAttribute.h b/api/BLE/peripheral/BLELocalAttribute.h similarity index 100% rename from api/BLE/BLELocalAttribute.h rename to api/BLE/peripheral/BLELocalAttribute.h diff --git a/api/BLE/BLEPeripheral.h b/api/BLE/peripheral/BLEPeripheral.h similarity index 100% rename from api/BLE/BLEPeripheral.h rename to api/BLE/peripheral/BLEPeripheral.h diff --git a/api/BLE/BLERemoteAttribute.h b/api/BLE/peripheral/BLERemoteAttribute.h similarity index 100% rename from api/BLE/BLERemoteAttribute.h rename to api/BLE/peripheral/BLERemoteAttribute.h diff --git a/api/BLE/BLERemoteCentral.h b/api/BLE/peripheral/BLERemoteCentral.h similarity index 100% rename from api/BLE/BLERemoteCentral.h rename to api/BLE/peripheral/BLERemoteCentral.h diff --git a/api/BLE/BLERemoteCharacteristic.h b/api/BLE/peripheral/BLERemoteCharacteristic.h similarity index 100% rename from api/BLE/BLERemoteCharacteristic.h rename to api/BLE/peripheral/BLERemoteCharacteristic.h diff --git a/api/BLE/BLERemoteService.h b/api/BLE/peripheral/BLERemoteService.h similarity index 100% rename from api/BLE/BLERemoteService.h rename to api/BLE/peripheral/BLERemoteService.h diff --git a/api/BLE/BLEService.h b/api/BLE/peripheral/BLEService.h similarity index 100% rename from api/BLE/BLEService.h rename to api/BLE/peripheral/BLEService.h From 49795dfec68a8e4913c87f2a6d0a5569de7ad936 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: 2016年4月26日 18:37:31 -0400 Subject: [PATCH 17/22] Correct typo --- api/BLE/peripheral/BLERemoteCentral.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/BLE/peripheral/BLERemoteCentral.h b/api/BLE/peripheral/BLERemoteCentral.h index 35158fc7..2cc277f1 100644 --- a/api/BLE/peripheral/BLERemoteCentral.h +++ b/api/BLE/peripheral/BLERemoteCentral.h @@ -1,5 +1,5 @@ /* - BLE Centrel API + BLE Remote Central API Copyright (c) 2016 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or From 43c0daafc4160541cdf58a36bd8531ceed854b64 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: 2016年4月26日 18:38:52 -0400 Subject: [PATCH 18/22] First draft of BLE central API --- api/BLE/central/ArduinoBLECentral.h | 27 +++ api/BLE/central/BLECentral.h | 53 ++++++ api/BLE/central/BLERemotePeripheral.h | 83 ++++++++ .../central/BLERemotePeripheralAttribute.h | 31 +++ .../BLERemotePeripheralAttributeWithValue.h | 66 +++++++ .../BLERemotePeripheralCharacteristic.h | 66 +++++++ .../central/BLERemotePeripheralDescriptor.h | 38 ++++ api/BLE/central/BLERemotePeripheralService.h | 43 +++++ .../examples/led_control/led_control.ino | 121 ++++++++++++ .../peripheral_explorer.ino | 178 ++++++++++++++++++ api/BLE/central/examples/scan/scan.ino | 73 +++++++ .../examples/scan_callback/scan_callback.ino | 76 ++++++++ .../sensortag_button/sensortag_button.ino | 118 ++++++++++++ 13 files changed, 973 insertions(+) create mode 100644 api/BLE/central/ArduinoBLECentral.h create mode 100644 api/BLE/central/BLECentral.h create mode 100644 api/BLE/central/BLERemotePeripheral.h create mode 100644 api/BLE/central/BLERemotePeripheralAttribute.h create mode 100644 api/BLE/central/BLERemotePeripheralAttributeWithValue.h create mode 100644 api/BLE/central/BLERemotePeripheralCharacteristic.h create mode 100644 api/BLE/central/BLERemotePeripheralDescriptor.h create mode 100644 api/BLE/central/BLERemotePeripheralService.h create mode 100644 api/BLE/central/examples/led_control/led_control.ino create mode 100644 api/BLE/central/examples/peripheral_explorer/peripheral_explorer.ino create mode 100644 api/BLE/central/examples/scan/scan.ino create mode 100644 api/BLE/central/examples/scan_callback/scan_callback.ino create mode 100644 api/BLE/central/examples/sensortag_button/sensortag_button.ino diff --git a/api/BLE/central/ArduinoBLECentral.h b/api/BLE/central/ArduinoBLECentral.h new file mode 100644 index 00000000..3c78249d --- /dev/null +++ b/api/BLE/central/ArduinoBLECentral.h @@ -0,0 +1,27 @@ +/* + BLE Central API + Copyright (c) 2016 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 ARDUINO_BLE_CENTRAL_H +#define ARDUINO_BLE_CENTRAL_H + +#define ARDUINO_BLE_API_VERSION 10000 // version 1.0.0 + +#include + +#endif diff --git a/api/BLE/central/BLECentral.h b/api/BLE/central/BLECentral.h new file mode 100644 index 00000000..43a62007 --- /dev/null +++ b/api/BLE/central/BLECentral.h @@ -0,0 +1,53 @@ +/* + BLE Central API + Copyright (c) 2016 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 ARDUINO_BLE_CENTRAL_H +#define ARDUINO_BLE_CENTRAL_H + +#include + +#include "BLERemotePeripheral.h" +#include "BLERemotePeripheralCharacteristic.h" + +enum BLECentralEvent { + BLEDiscovered = 0 +}; + +typedef void (*BLECentralEventHandler)(BLERemotePeripheral& peripheral); + +class BLECentral +{ + public: + BLECentral(); + virtual ~BLECentral(); + + void begin(); // initiliaze hardware + void poll(); // poll for events + void end(); // deinitiliaze hardware + + void startScanning(); // start scanning for peripherals + void startScanningWithDuplicates(); // start scanning for peripherals, and report all duplicates + void stopScanning(); // stop scanning for peripherals + + BLERemotePeripheral available(); // retrieve a discovered peripheral + + void setEventHandler(BLECentralEvent event, BLECentralEventHandler eventHandler); // set an event handler (callback) +}; + +#endif diff --git a/api/BLE/central/BLERemotePeripheral.h b/api/BLE/central/BLERemotePeripheral.h new file mode 100644 index 00000000..45c202fe --- /dev/null +++ b/api/BLE/central/BLERemotePeripheral.h @@ -0,0 +1,83 @@ +/* + BLE Remote Peripheral API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_PERIPHERAL_H +#define ARDUINO_BLE_REMOTE_PERIPHERAL_H + +#include + +#include "BLERemotePeripheralService.h" +#include "BLERemotePeripheralCharacteristic.h" +#include "BLERemotePeripheralDescriptor.h" + +enum BLERemotePeripheralEvent { + BLERemoteDisconnected = 0 +}; + +class BLERemotePeripheral; + +typedef void (*BLERemotePeripheralEventHandler)(BLERemotePeripheral& peripheral); + +class BLERemotePeripheral +{ + public: + BLERemotePeripheral(); + virtual ~BLERemotePeripheral(); + + operator bool() const; // is the peripheral valid (discovered) + + String address() const; // returns the BT address of the peripheral as a String + int rssi() const; // returns the RSSI of the peripheral at discovery + + bool hasLocalName() const; // does the peripheral advertise a local name + bool hasAdvertisedService() const; // does the peripheral advertise a service + bool hasAdvertisedService(int index) const; // does the peripheral advertise a service n + + String localName() const; // returns the advertised local name as a String + String advertisedService() const; // returns the advertised service as a UUID String + String advertisedService(int index) const; // returns the nth advertised service as a UUID String + + bool connect(); // connect to the peripheral + bool disconnect(); // disconnect from the peripheral + bool connected(); // is the peripheral connected + + bool discoverAttributes(); // discover the peripherals attributes (services, characteristic, and descriptors) + + String deviceName(); // read the device name attribute of the peripheral, and return String value + unsigned short appearance(); // read the appearance attribute of the peripheral and return value as int + + void setEventHandler(BLERemotePeripheralEvent event, BLERemotePeripheralEventHandler eventHandler); // set an event handler (callback) + + int serviceCount() const; // returns the number of services the peripheral has + bool hasService(const char* uuid) const; // does the peripheral have a service with the specified UUID + bool hasService(const char* uuid, int index) const; // does the peripheral have an nth service with the specified UUID + BLERemotePeripheralService service(int index) const; // return the nth service of the peripheral + BLERemotePeripheralService service(const char * uuid) const; // return the service with the specified UUID + BLERemotePeripheralService service(const char * uuid, int index) const; // return the nth service with the specified UUID + + int characteristicCount() const; // returns the number of characteristics the peripheral has + bool hasCharacteristic(const char* uuid) const; // does the peripheral have a characteristic with the specified UUID + bool hasCharacteristic(const char* uuid, int index) const; // does the peripheral have an nth characteristic with the specified UUID + BLERemotePeripheralCharacteristic characteristic(int index) const; // return the nth characteristic of the peripheral + BLERemotePeripheralCharacteristic characteristic(const char * uuid) const; // return the characteristic with the specified UUID + BLERemotePeripheralCharacteristic characteristic(const char * uuid, int index) const; // return the nth characteristic with the specified UUID +}; + +#endif diff --git a/api/BLE/central/BLERemotePeripheralAttribute.h b/api/BLE/central/BLERemotePeripheralAttribute.h new file mode 100644 index 00000000..52dd3aed --- /dev/null +++ b/api/BLE/central/BLERemotePeripheralAttribute.h @@ -0,0 +1,31 @@ +/* + BLE Remote Peripheral Attribute API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_PERIPHERAL_ATTRIBUTE_H +#define ARDUINO_BLE_REMOTE_PERIPHERAL_ATTRIBUTE_H + +class BLERemotePeripheralAttribute +{ + public: + BLERemotePeripheralAttribute(); + + String uuid() const; // returns the UUID of the attribute as a String +}; + +#endif diff --git a/api/BLE/central/BLERemotePeripheralAttributeWithValue.h b/api/BLE/central/BLERemotePeripheralAttributeWithValue.h new file mode 100644 index 00000000..7c898cbc --- /dev/null +++ b/api/BLE/central/BLERemotePeripheralAttributeWithValue.h @@ -0,0 +1,66 @@ +/* + BLE Remote Peripheral Attribute with value API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_PERIPHERAL_ATTRIBUTE_H +#define ARDUINO_BLE_REMOTE_PERIPHERAL_ATTRIBUTE_H + +#include "BLERemotePeripheralAttribute.h" + + +class BLERemotePeripheralAttributeWithValue : public BLERemotePeripheralAttribute +{ + public: + BLERemotePeripheralAttributeWithValue(); + + virtual bool read(); // read the attribute value from the peripheral + virtual bool write(const unsigned char* value, int length); // write the specific value to the attribute of the peripheral + + int valueLength() const; // returns the length of the attribute value + const unsigned char* value() const; // returns the value of the attribute array + unsigned char operator[] (int offset) const; // access an attribute value at the specified offset + + // intepret the value of the attribute with the specified type + String stringValue() const; + char charValue() const; + unsigned char unsignedCharValue() const; + short shortValue() const; + unsigned short unsignedShortValue() const; + int intValue() const; + unsigned int unsignedIntValue() const; + long longValue() const; + unsigned long unsignedLongValue() const; + float floatValue() const; + double doubleValue() const; + + // write the value of the attribute with the specified type + bool writeString(const String& s); + bool writeString(const char* s); + bool writeChar(char c); + bool writeUnsignedChar(unsigned char c); + bool writeShort(short s); + bool writeUnsignedShort(unsigned short s); + bool writeInt(int i); + bool writeUnsignedInt(unsigned int i); + bool writeLong(long l); + bool writeUnsignedLong(unsigned int l); + bool writeFloat(float f); + bool writeDouble(double d); +}; + +#endif diff --git a/api/BLE/central/BLERemotePeripheralCharacteristic.h b/api/BLE/central/BLERemotePeripheralCharacteristic.h new file mode 100644 index 00000000..16eccb75 --- /dev/null +++ b/api/BLE/central/BLERemotePeripheralCharacteristic.h @@ -0,0 +1,66 @@ +/* + BLE Remote Peripheral Characteristic API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_PERIPHERAL_CHARACTERISTIC_H +#define ARDUINO_BLE_REMOTE_PERIPHERAL_CHARACTERISTIC_H + +#include "BLERemotePeripheralAttributeWithValue.h" +#include "BLERemotePeripheralDescriptor.h" + +enum BLERemotePeripheralCharacteristicEvent { + BLERemoteValueUpdated = 0 +}; + +class BLERemotePeripheralCharacteristic; + +typedef void (*BLERemotePeripheralCharacteristicEventHandler)(BLERemotePeripheral& peripheral, BLERemotePeripheralCharacteristic& characteristic); + +class BLERemotePeripheralCharacteristic : public BLERemotePeripheralAttributeWithValue +{ + public: + BLERemotePeripheralCharacteristic(); + + operator bool() const; // is the characteristic valid (discovered from peripheral) + + unsigned char properties() const; // returns the properties of the characteristic + + bool canRead(); // can the characteristic be read (based on properties) + bool canWrite(); // can the characteristic be written (based on properties) + bool canSubscribe(); // can the characteristic be subscribed to (based on properties) + bool canUnsubscribe(); // can the characteristic be unsubscribed to (based on properties) + + virtual bool read(); // read the characteristic value + virtual bool write(const unsigned char* value, int length); // write the charcteristic value + + bool subscribe(); // subscribe to the characteristic + bool unsubscribe(); // unsubscribe to the characteristic + + bool valueUpdated(); // has the characteristic value been updated + + void setEventHandler(BLERemotePeripheralCharacteristicEvent event, BLERemotePeripheralCharacteristicEventHandler eventHandler); // set an event handler (callback) + + int descriptorCount() const; // returns the number of descriptors the characteristic has + bool hasDescriptor(const char* uuid) const; // does the characteristic have a descriptor with the specified UUID + bool hasDescriptor(const char* uuid, int index) const; // does the characteristic have an nth descriptor with the specified UUID + BLERemotePeripheralDescriptor descriptor(int index) const; // return the nth descriptor of the characteristic + BLERemotePeripheralDescriptor descriptor(const char * uuid) const; // return the descriptor with the specified UUID + BLERemotePeripheralDescriptor descriptor(const char * uuid, int index) const; // return the nth descriptor with the specified UUID +}; + +#endif diff --git a/api/BLE/central/BLERemotePeripheralDescriptor.h b/api/BLE/central/BLERemotePeripheralDescriptor.h new file mode 100644 index 00000000..bad8ec72 --- /dev/null +++ b/api/BLE/central/BLERemotePeripheralDescriptor.h @@ -0,0 +1,38 @@ +/* + BLE Remote Peripheral Descriptor API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_PERIPHERAL_DESCRIPTOR_H +#define ARDUINO_BLE_REMOTE_PERIPHERAL_DESCRIPTOR_H + +#include "BLERemotePeripheralAttributeWithValue.h" + +class BLERemotePeripheral; + +class BLERemotePeripheralDescriptor : public BLERemotePeripheralAttributeWithValue +{ + public: + BLERemotePeripheralDescriptor(); + + operator bool() const; // is the descriptor valid (discovered from peripheral) + + virtual bool read(); // read the descriptor value + virtual bool write(const unsigned char* value, int length); // write the descriptor value +}; + +#endif diff --git a/api/BLE/central/BLERemotePeripheralService.h b/api/BLE/central/BLERemotePeripheralService.h new file mode 100644 index 00000000..be077d91 --- /dev/null +++ b/api/BLE/central/BLERemotePeripheralService.h @@ -0,0 +1,43 @@ +/* + BLE Remote Peripheral Service API + Copyright (c) 2016 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 ARDUINO_BLE_REMOTE_PERIPHERAL_SERVICE_H +#define ARDUINO_BLE_REMOTE_PERIPHERAL_SERVICE_H + +#include "BLERemotePeripheralAttribute.h" +#include "BLERemotePeripheralCharacteristic.h" + +class BLERemotePeripheral; + +class BLERemotePeripheralService : public BLERemotePeripheralAttribute +{ + public: + BLERemotePeripheralService(); + + operator bool() const; // is the service valid (discovered from peripheral) + + int characteristicCount() const; // returns the number of characteristics the service has + bool hasCharacteristic(const char* uuid) const; // does the service have a characteristic with the specified UUID + bool hasCharacteristic(const char* uuid, int index) const; // does the service have an nth characteristic with the specified UUID + BLERemotePeripheralCharacteristic characteristic(int index) const; // return the nth characteristic of the service + BLERemotePeripheralCharacteristic characteristic(const char * uuid) const; // return the characteristic with the specified UUID + BLERemotePeripheralCharacteristic characteristic(const char * uuid, int index) const; // return the nth characteristic with the specified UUID +}; + +#endif diff --git a/api/BLE/central/examples/led_control/led_control.ino b/api/BLE/central/examples/led_control/led_control.ino new file mode 100644 index 00000000..ce6cf570 --- /dev/null +++ b/api/BLE/central/examples/led_control/led_control.ino @@ -0,0 +1,121 @@ +/* + Arduino BLE Central LED Control example + Copyright (c) 2016 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 + +// variables for button +const int buttonPin = 2; +int oldButtonState = LOW; + +// create central +BLECentral bleCentral; + +void setup() { + Serial.begin(9600); + + // configure the button pin as input + pinMode(buttonPin, INPUT_PULLDOWN); + + // initialize the central + bleCentral.begin(); + + Serial.println("BLE Central - LED control"); + + // start scanning for peripherals + bleCentral.startScanning(); +} + +void loop() { + // check if a peripheral has been discovered + BLERemotePeripheral peripheral = bleCentral.available(); + + if (peripheral) { + // discovered a peripheral, print out address, local name, and advertised service + Serial.print("Found "); + Serial.print(peripheral.address()); + Serial.print(" '"); + Serial.print(peripheral.localName()); + Serial.print("' "); + Serial.print(peripheral.advertisedService()); + Serial.println(); + + // see if peripheral is advertising the LED service + if (peripheral.advertisedService() == "19b10000-e8f2-537e-4f6c-d104768a1214") { + // stop scanning + bleCentral.stopScanning(); + + controlLed(peripheral); + + // peripheral disconnected, start scanning again + bleCentral.startScanning(); + } + } +} + +void controlLed(BLERemotePeripheral& peripheral) { + // connect to the peripheral + Serial.println("Connecting ..."); + + if (peripheral.connect()) { + Serial.println("Connected"); + } else { + Serial.println("Failed to connect!"); + return; + } + + // discover peripheral attributes + Serial.println("Discovering attributes ..."); + if (peripheral.discoverAttributes()) { + Serial.println("Attributes discovered"); + } else { + Serial.println("Attribute discovery failed!"); + peripheral.disconnect(); + return; + } + + // retrieve the LED characteristic + BLERemotePeripheralCharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214"); + + while (peripheral.connected()) { + // while the peripheral is connection + + // read the button pin + int buttonState = digitalRead(buttonPin); + + if (oldButtonState != buttonState) { + // button changed + oldButtonState = buttonState; + + if (buttonState) { + Serial.println("button pressed"); + + // button is pressed, write 0x01 to turn the LED on + ledCharacteristic.writeChar(0x01); + } else { + Serial.println("button released"); + + // button is released, write 0x00 to turn the LED of + ledCharacteristic.writeChar(0x00); + } + } + } +} + + diff --git a/api/BLE/central/examples/peripheral_explorer/peripheral_explorer.ino b/api/BLE/central/examples/peripheral_explorer/peripheral_explorer.ino new file mode 100644 index 00000000..e8254082 --- /dev/null +++ b/api/BLE/central/examples/peripheral_explorer/peripheral_explorer.ino @@ -0,0 +1,178 @@ +/* + Arduino BLE Central peripheral explorer example + Copyright (c) 2016 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 + +// create central +BLECentral bleCentral; + +void setup() { + Serial.begin(9600); + + // initialize the central + bleCentral.begin(); + + Serial.println("BLE Central - Peripheral Explorer"); + + // start scanning for peripherals + bleCentral.startScanning(); +} + +void loop() { + // check if a peripheral has been discovered + BLERemotePeripheral peripheral = bleCentral.available(); + + if (peripheral) { + // discovered a peripheral, print out address, local name, and advertised service + Serial.print("Found "); + Serial.print(peripheral.address()); + Serial.print(" '"); + Serial.print(peripheral.localName()); + Serial.print("' "); + Serial.print(peripheral.advertisedService()); + Serial.println(); + + // see if peripheral is a SensorTag + if (peripheral.localName() == "SensorTag") { + // stop scanning + bleCentral.stopScanning(); + + explorerPeripheral(peripheral); + + // peripheral disconnected, we are done + while (1) { + // do nothing + } + } + } +} + +void explorerPeripheral(BLERemotePeripheral& peripheral) { + // connect to the peripheral + Serial.println("Connecting ..."); + + if (peripheral.connect()) { + Serial.println("Connected"); + } else { + Serial.println("Failed to connect!"); + return; + } + + // discover peripheral attributes + Serial.println("Discovering attributes ..."); + if (peripheral.discoverAttributes()) { + Serial.println("Attributes discovered"); + } else { + Serial.println("Attribute discovery failed!"); + peripheral.disconnect() + return; + } + + // read and print device name of peripheral + Serial.println(); + Serial.print("Device name: "); + Serial.println(peripheral.deviceName()); + + // read and print appearance of peripheral + Serial.print("Appearance: "); + Serial.println(peripheral.appearance()); + Serial.println(); + + // loop the services of the peripheral and explore each + for (int i = 0; i < peripheral.serviceCount(); i++) { + BLERemotePeripheralService service = peripheral.service(i); + + exploreService(service); + } + + Serial.println(); + + // we are done exploring, disconnect + Serial.println("Disconnecting ..."); + peripheral.disconnect(); + Serial.println("Disconnected"); +} + +void exploreService(BLERemotePeripheralService& service) { + // print the UUID of the service + Serial.print("Service "); + Serial.println(service.uuid()); + + // loop the characteristics of the service and explore each + for (int i = 0; i < service.characteristicCount(); i++) { + BLERemotePeripheralCharacteristic characteristic = service.characteristic(i); + + exploreCharacteristic(characteristic); + } +} + +void exploreCharacteristic(BLERemotePeripheralCharacteristic& characteristic) { + // print the UUID and properies of the characteristic + Serial.print("\tCharacteristic "); + Serial.print(characteristic.uuid()); + Serial.print(", properties 0x"); + Serial.print(characteristic.properties()); + + // check if the characteristic is readable + if (characteristic.canRead()) { + // read the characteristic value + characteristic.read(); + + // print out the value of the characteristic + Serial.print(", value 0x"); + printData(characteristic.value(), characteristic.valueLength()); + } + + Serial.println(); + + // loop the descriptors of the characteristic and explore each + for (int i = 0; i < characteristic.descriptorCount(); i++) { + BLERemotePeripheralDescriptor descriptor = characteristic.descriptor(i); + + exploreDescriptor(descriptor); + } +} + +void exploreDescriptor(BLERemotePeripheralDescriptor& descriptor) { + // print the UUID of the descriptor + Serial.print("\t\tDescriptor "); + Serial.print(descriptor.uuid()); + + // read the descriptor value + descriptor.read(); + + // print out the value of the descriptor + Serial.print(", value 0x"); + printData(descriptor.value(), descriptor.valueLength()); + + Serial.println(); +} + +void printData(const unsigned char data[], int length) { + for (int i = 0; i < length; i++) { + unsigned char b = data[i]; + + if (b < 16) { + Serial.print("0"); + } + + Serial.print(b, HEX); + } +} + diff --git a/api/BLE/central/examples/scan/scan.ino b/api/BLE/central/examples/scan/scan.ino new file mode 100644 index 00000000..79c827e3 --- /dev/null +++ b/api/BLE/central/examples/scan/scan.ino @@ -0,0 +1,73 @@ +/* + Arduino BLE Central scan example + Copyright (c) 2016 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 + +// create central +BLECentral bleCentral; + +void setup() { + Serial.begin(9600); + + // initialize the central + bleCentral.begin(); + + Serial.println("BLE Central scan"); + + // start scanning for peripheral + bleCentral.startScanning(); +} + +void loop() { + // check if a peripheral has been discovered + BLERemotePeripheral peripheral = bleCentral.available(); + + if (peripheral) { + // discovered a peripheral + Serial.println("Discovered a peripheral"); + Serial.println("-----------------------"); + + // print address + Serial.print("Address: "); + Serial.println(peripheral.address()); + + // print the local name, if present + if (peripheral.hasLocalName()) { + Serial.print("Local Name: "); + Serial.println(peripheral.localName()); + } + + // print the advertised service UUID's, if present + if (peripheral.hasAdvertisedService()) { + Serial.print("Service UUID's: "); + for (int i = 0; peripheral.hasAdvertisedService(i); i++) { + Serial.print(peripheral.advertisedService(i)); + Serial.print(" "); + } + Serial.println(); + } + + // print the RSSI + Serial.print("RSSI: "); + Serial.println(peripheral.rssi()); + + Serial.println(); + } +} + diff --git a/api/BLE/central/examples/scan_callback/scan_callback.ino b/api/BLE/central/examples/scan_callback/scan_callback.ino new file mode 100644 index 00000000..99710503 --- /dev/null +++ b/api/BLE/central/examples/scan_callback/scan_callback.ino @@ -0,0 +1,76 @@ +/* + Arduino BLE Central scan callback example + Copyright (c) 2016 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 + +// create central +BLECentral bleCentral; + +void setup() { + Serial.begin(9600); + + // initialize the central + bleCentral.begin(); + + Serial.println("BLE Central scan callback"); + + // set the discovered event handle + bleCentral.setEventHandler(BLEDiscovered, bleCentralDiscoverHandler); + + // start scanning for peripherals with duplicates + bleCentral.startScanningWithDuplicates(); +} + +void loop() { + // poll the central for events + bleCentral.poll(); +} + +void bleCentralDiscoverHandler(BLERemotePeripheral& peripheral) { + // discovered a peripheral + Serial.println("Discovered a peripheral"); + Serial.println("-----------------------"); + + // print address + Serial.print("Address: "); + Serial.println(peripheral.address()); + + // print the local name, if present + if (peripheral.hasLocalName()) { + Serial.print("Local Name: "); + Serial.println(peripheral.localName()); + } + + // print the advertised service UUID's, if present + if (peripheral.hasAdvertisedService()) { + Serial.print("Service UUID's: "); + for (int i = 0; peripheral.hasAdvertisedService(i); i++) { + Serial.print(peripheral.advertisedService(i)); + Serial.print(" "); + } + Serial.println(); + } + + // print the RSSI + Serial.print("RSSI: "); + Serial.println(peripheral.rssi()); + + Serial.println(); +} + diff --git a/api/BLE/central/examples/sensortag_button/sensortag_button.ino b/api/BLE/central/examples/sensortag_button/sensortag_button.ino new file mode 100644 index 00000000..cf03321a --- /dev/null +++ b/api/BLE/central/examples/sensortag_button/sensortag_button.ino @@ -0,0 +1,118 @@ +/* + Arduino BLE Central SensorTag button example + Copyright (c) 2016 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 + +// create central +BLECentral bleCentral; + +void setup() { + Serial.begin(9600); + + // initialize the central + bleCentral.begin(); + + Serial.println("BLE Central - SensorTag button"); + + // start scanning for peripheral + bleCentral.startScanning(); +} + +void loop() { + // check if a peripheral has been discovered + BLERemotePeripheral peripheral = bleCentral.available(); + + if (peripheral) { + // discovered a peripheral, print out address, local name, and advertised service + Serial.print("Found "); + Serial.print(peripheral.address()); + Serial.print(" '"); + Serial.print(peripheral.localName()); + Serial.print("' "); + Serial.print(peripheral.advertisedService()); + Serial.println(); + + // see if peripheral is a SensorTag + if (peripheral.localName() == "SensorTag") { + // stop scanning + bleCentral.stopScanning(); + + monitorSensorTagButtons(peripheral); + + // peripheral disconnected, start scanning again + bleCentral.startScanning(); + } + } +} + +void monitorSensorTagButtons(BLERemotePeripheral& peripheral) { + // connect to the peripheral + Serial.println("Connecting ..."); + if (peripheral.connect()) { + Serial.println("Connected"); + } else { + Serial.println("Failed to connect!"); + return; + } + + // discover peripheral attributes + Serial.println("Discovering attributes ..."); + if (peripheral.discoverAttributes()) { + Serial.println("Attributes discovered"); + } else { + Serial.println("Attribute discovery failed!"); + peripheral.disconnect(); + return; + } + + // retrieve the simple key characteristic + BLERemotePeripheralCharacteristic simpleKeyCharacteristic = peripheral.characteristic("ffe1"); + + // subscribe to the simple key characteristic + Serial.println("Subscribing to simple key characteristic ..."); + if ( simpleKeyCharacteristic.subscribe()) { + Serial.println("Subscribed"); + } else { + Serial.println("subscription failed!"); + peripheral.disconnect(); + return; + } + + while (peripheral.connected()) { + // while the peripheral is connected + + // check if the value of the simple key characteristic has been updated + if (simpleKeyCharacteristic.valueUpdated()) { + // yes, get the value, characteristic is 1 byte so use char value + int value = simpleKeyCharacteristic.charValue(); + + if (value & 0x01) { + // first bit corresponds to the right button + Serial.println("Right button pressed"); + } + + if (value & 0x02) { + // second bit corresponds to the left button + Serial.println("Left button pressed"); + } + } + } +} + + From 4827027c9ef9da63603bcad8a7b1254fa0dcd463 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 9 Jun 2016 17:26:30 +0200 Subject: [PATCH 19/22] Added a generic ArduinoAPI.h include folder with API version definitions --- api/ArduinoAPI.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 api/ArduinoAPI.h diff --git a/api/ArduinoAPI.h b/api/ArduinoAPI.h new file mode 100644 index 00000000..c9b1b3f1 --- /dev/null +++ b/api/ArduinoAPI.h @@ -0,0 +1,26 @@ +/* + Arduino API main include + Copyright (c) 2016 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 ARDUINO_API_H +#define ARDUINO_API_H + +// version 1.0.0 +#define ARDUINO_API_VERSION 10000 + +#endif From 4f8b4b24d15fa78145f9fc2d00a5300574c79f10 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 9 Jun 2016 17:26:44 +0200 Subject: [PATCH 20/22] Removed ARDUINO_REAL_TIME_CLOCK_API_VERSION it's now implicit from general API version --- api/RealTimeClock.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/api/RealTimeClock.h b/api/RealTimeClock.h index f554b6d0..e517b225 100644 --- a/api/RealTimeClock.h +++ b/api/RealTimeClock.h @@ -20,8 +20,6 @@ #ifndef ARDUINO_REAL_TIME_CLOCK_H #define ARDUINO_REAL_TIME_CLOCK_H -#define ARDUINO_REAL_TIME_CLOCK_API_VERSION 10000 // version 1.0.0 - class RealTimeClock; class TimeProvider; From e255e3de5c5dbd2054f8de7c1f57133d0798a95f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 9 Jun 2016 17:27:21 +0200 Subject: [PATCH 21/22] Fixed return value on SoftwareRTC::setTime --- api/SoftwareRTC.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/api/SoftwareRTC.cpp b/api/SoftwareRTC.cpp index 676974d9..c8934334 100644 --- a/api/SoftwareRTC.cpp +++ b/api/SoftwareRTC.cpp @@ -42,5 +42,6 @@ bool SoftwareRTC::setTime(unsigned long t) { lastTimestamp = t; lastMillis = millis(); + return true; } From 5bfc0cb902cc17586c4e957a1a984b022671d8f8 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 1 Jul 2016 11:44:47 +0200 Subject: [PATCH 22/22] Add generic uint8_t RingBuffer class * cloned from samd core * implement addStorage() API to allow static/dynamic size increase --- api/RingBuffer.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++ api/RingBuffer.h | 57 ++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 api/RingBuffer.cpp create mode 100644 api/RingBuffer.h diff --git a/api/RingBuffer.cpp b/api/RingBuffer.cpp new file mode 100644 index 00000000..be29afe4 --- /dev/null +++ b/api/RingBuffer.cpp @@ -0,0 +1,99 @@ +/* + 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 +*/ + +#include "RingBuffer.h" +#include + +RingBuffer::RingBuffer( void ) +{ + memset( _aucBuffer, 0, RINGBUFFER_SIZE ) ; + clear(); +} + +void RingBuffer::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 ) + { + if (_iHead < RINGBUFFER_SIZE) { + _aucBuffer[_iHead] = c ; + } else { + additionalBuffer[_iHead - RINGBUFFER_SIZE] = c; + } + _iHead = i ; + } +} + +void RingBuffer::clear() +{ + _iHead = 0; + _iTail = 0; +} + +int RingBuffer::read_char() +{ + if(_iTail == _iHead) + return -1; + + uint8_t value; + if (_iTail < RINGBUFFER_SIZE) { + value = _aucBuffer[_iTail]; + } else { + value = additionalBuffer[_iTail - RINGBUFFER_SIZE]; + } + _iTail = nextIndex(_iTail); + + return value; +} + +int RingBuffer::available() +{ + int delta = _iHead - _iTail; + + if(delta < 0) + return RINGBUFFER_SIZE + additionalSize + delta; + else + return delta; +} + +int RingBuffer::peek() +{ + if(_iTail == _iHead) + return -1; + + if (_iTail < RINGBUFFER_SIZE) { + return _aucBuffer[_iTail]; + } else { + return additionalBuffer[_iTail - RINGBUFFER_SIZE]; + } +} + +int RingBuffer::nextIndex(int index) +{ + return (uint32_t)(index + 1) % (RINGBUFFER_SIZE + additionalSize); +} + +bool RingBuffer::isFull() +{ + return (nextIndex(_iHead) == _iTail); +} diff --git a/api/RingBuffer.h b/api/RingBuffer.h new file mode 100644 index 00000000..99dd674e --- /dev/null +++ b/api/RingBuffer.h @@ -0,0 +1,57 @@ +/* + 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 +*/ + +#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 RINGBUFFER_SIZE 64 + +class RingBuffer +{ + public: + uint8_t _aucBuffer[RINGBUFFER_SIZE] ; + int _iHead ; + int _iTail ; + + public: + RingBuffer( void ) ; + void store_char( uint8_t c ) ; + void clear(); + int read_char(); + int available(); + int peek(); + bool isFull(); + void addStorage(uint8_t* _buffer, int _size) { + additionalSize = _size; + additionalBuffer = _buffer; + }; + + private: + int nextIndex(int index); + uint8_t* additionalBuffer; + int additionalSize = 0; +}; + +#endif /* _RING_BUFFER_ */

AltStyle によって変換されたページ (->オリジナル) /