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 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"); + } + } + } +} + + diff --git a/api/BLE/peripheral/ArduinoBLE.h b/api/BLE/peripheral/ArduinoBLE.h new file mode 100644 index 00000000..28bca1a6 --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLEAttribute.h b/api/BLE/peripheral/BLEAttribute.h new file mode 100644 index 00000000..3465042b --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLECharacteristic.h b/api/BLE/peripheral/BLECharacteristic.h new file mode 100644 index 00000000..5877079a --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLEDescriptor.h b/api/BLE/peripheral/BLEDescriptor.h new file mode 100644 index 00000000..7487b859 --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLELocalAttribute.h b/api/BLE/peripheral/BLELocalAttribute.h new file mode 100644 index 00000000..b8d19c71 --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLEPeripheral.h b/api/BLE/peripheral/BLEPeripheral.h new file mode 100644 index 00000000..df8d286f --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLERemoteAttribute.h b/api/BLE/peripheral/BLERemoteAttribute.h new file mode 100644 index 00000000..ccd3e61c --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLERemoteCentral.h b/api/BLE/peripheral/BLERemoteCentral.h new file mode 100644 index 00000000..2cc277f1 --- /dev/null +++ b/api/BLE/peripheral/BLERemoteCentral.h @@ -0,0 +1,35 @@ +/* + BLE Remote 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_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/peripheral/BLERemoteCharacteristic.h b/api/BLE/peripheral/BLERemoteCharacteristic.h new file mode 100644 index 00000000..0d9a3a91 --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLERemoteService.h b/api/BLE/peripheral/BLERemoteService.h new file mode 100644 index 00000000..4f44190b --- /dev/null +++ b/api/BLE/peripheral/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/peripheral/BLEService.h b/api/BLE/peripheral/BLEService.h new file mode 100644 index 00000000..a448d862 --- /dev/null +++ b/api/BLE/peripheral/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 diff --git a/api/MotionSense.h b/api/MotionSense.h new file mode 100644 index 00000000..bdcb6813 --- /dev/null +++ b/api/MotionSense.h @@ -0,0 +1,99 @@ +/* + 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 + +// 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). + virtual bool readAcceleration(float &x, float &y, float &z) = 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 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). + 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; } + + // Sampling rate of the sensor. + 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; + float roll; +}; + +struct Quaternion { + union { + float q[4]; + struct { + float w; + float x; + float y; + float z; + }; + }; +}; + +#endif 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..e517b225 --- /dev/null +++ b/api/RealTimeClock.h @@ -0,0 +1,81 @@ +/* + 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 + +class RealTimeClock; +class TimeProvider; + +// 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 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 + +// 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); + +// Synchronize the clock with a time provider +// Return true if the synchronization is successful, false otherwise. +bool setTime(TimeProvider &provider); + +// Real Time Clock 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 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_ */ diff --git a/api/SoftwareRTC.cpp b/api/SoftwareRTC.cpp new file mode 100644 index 00000000..c8934334 --- /dev/null +++ b/api/SoftwareRTC.cpp @@ -0,0 +1,47 @@ +/* + 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(); + return true; +} + 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