diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2019-02-28 17:04:22 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2019-02-28 17:04:22 -0500 |
commit | d6869d1ec4bd24cd2c3eafa534f0849b25ec5607 (patch) | |
tree | 79e54ed27b39c31864895535d11399708d5a45c0 /arduino/libraries/Bluefruit52Lib/examples/Peripheral | |
parent | 614ee97bf3a2270c413527a7f35c54cbecd9e601 (diff) |
added basic code
Diffstat (limited to 'arduino/libraries/Bluefruit52Lib/examples/Peripheral')
26 files changed, 5388 insertions, 0 deletions
diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/LICENSE.txt b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/LICENSE.txt new file mode 100755 index 0000000..77cec6d --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino new file mode 100755 index 0000000..5cfa943 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino @@ -0,0 +1,916 @@ +/* + Firmata is a generic protocol for communicating with microcontrollers + from software on a host computer. It is intended to work with + any host computer software package. + + To download a host software package, please click on the following link + to open the list of Firmata client libraries in your default browser. + + https://github.com/firmata/arduino#firmata-client-libraries + + Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (C) 2010-2011 Paul Stoffregen. All rights reserved. + Copyright (C) 2009 Shigeru Kobayashi. All rights reserved. + Copyright (C) 2009-2016 Jeff Hoefs. All rights 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. + + See file LICENSE.txt for further informations on licensing terms. + + Last updated October 16th, 2016 +*/ + +// Adafruit nRF52 Boards require Firmata at least 2.5.7 + +#include <bluefruit.h> +#include <Servo.h> +#include <Wire.h> +#include <Firmata.h> + +#define I2C_WRITE B00000000 +#define I2C_READ B00001000 +#define I2C_READ_CONTINUOUSLY B00010000 +#define I2C_STOP_READING B00011000 +#define I2C_READ_WRITE_MODE_MASK B00011000 +#define I2C_10BIT_ADDRESS_MODE_MASK B00100000 +#define I2C_END_TX_MASK B01000000 +#define I2C_STOP_TX 1 +#define I2C_RESTART_TX 0 +#define I2C_MAX_QUERIES 8 +#define I2C_REGISTER_NOT_SPECIFIED -1 + +// the minimum interval for sampling analog input +#define MINIMUM_SAMPLING_INTERVAL 1 + +// Adafruit +uint8_t ANALOG_TO_PIN(uint8_t n) +{ + switch (n) + { + case 0 : return PIN_A0; + case 1 : return PIN_A1; + case 2 : return PIN_A2; + case 3 : return PIN_A3; + case 4 : return PIN_A4; + case 5 : return PIN_A5; + case 6 : return PIN_A6; + case 7 : return PIN_A7; + } + + return 127; +} + + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +#ifdef FIRMATA_SERIAL_FEATURE +SerialFirmata serialFeature; +#endif + +BLEUart bleuart; + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +unsigned int samplingInterval = 19; // how often to run the main loop (in ms) + +/* i2c data */ +struct i2c_device_info { + byte addr; + int reg; + byte bytes; + byte stopTX; +}; + +/* for i2c read continuous more */ +i2c_device_info query[I2C_MAX_QUERIES]; + +byte i2cRxData[64]; +boolean isI2CEnabled = false; +signed char queryIndex = -1; +// default delay time between i2c read request and Wire.requestFrom() +unsigned int i2cReadDelayTime = 0; + +Servo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte detachedServos[MAX_SERVOS]; +byte detachedServoCount = 0; +byte servoCount = 0; + +boolean isResetting = false; + +// Forward declare a few functions to avoid compiler errors with older versions +// of the Arduino IDE. +void setPinModeCallback(byte, int); +void reportAnalogCallback(byte analogPin, int value); +void sysexCallback(byte, byte, byte*); + +/* utility functions */ +void wireWrite(byte data) +{ +#if ARDUINO >= 100 + Wire.write((byte)data); +#else + Wire.send(data); +#endif +} + +byte wireRead(void) +{ +#if ARDUINO >= 100 + return Wire.read(); +#else + return Wire.receive(); +#endif +} + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void attachServo(byte pin, int minPulse, int maxPulse) +{ + if (servoCount < MAX_SERVOS) { + // reuse indexes of detached servos until all have been reallocated + if (detachedServoCount > 0) { + servoPinMap[pin] = detachedServos[detachedServoCount - 1]; + if (detachedServoCount > 0) detachedServoCount--; + } else { + servoPinMap[pin] = servoCount; + servoCount++; + } + if (minPulse > 0 && maxPulse > 0) { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + } else { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + } + } else { + Firmata.sendString("Max servos attached"); + } +} + +void detachServo(byte pin) +{ + servos[servoPinMap[pin]].detach(); + // if we're detaching the last servo, decrement the count + // otherwise store the index of the detached servo + if (servoPinMap[pin] == servoCount && servoCount > 0) { + servoCount--; + } else if (servoCount > 0) { + // keep track of detached servos because we want to reuse their indexes + // before incrementing the count of attached servos + detachedServoCount++; + detachedServos[detachedServoCount - 1] = servoPinMap[pin]; + } + + servoPinMap[pin] = 255; +} + +void enableI2CPins() +{ + byte i; + // is there a faster way to do this? would probaby require importing + // Arduino.h to get SCL and SDA pins + for (i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_I2C(i)) { + // mark pins as i2c so they are ignore in non i2c data requests + setPinModeCallback(i, PIN_MODE_I2C); + } + } + + isI2CEnabled = true; + + Wire.begin(); +} + +/* disable the i2c pins so they can be used for other functions */ +void disableI2CPins() { + isI2CEnabled = false; + // disable read continuous mode for all devices + queryIndex = -1; +} + +void readAndReportData(byte address, int theRegister, byte numBytes, byte stopTX) { + // allow I2C requests that don't require a register read + // for example, some devices using an interrupt pin to signify new data available + // do not always require the register read so upon interrupt you call Wire.requestFrom() + if (theRegister != I2C_REGISTER_NOT_SPECIFIED) { + Wire.beginTransmission(address); + wireWrite((byte)theRegister); + Wire.endTransmission(stopTX); // default = true + // do not set a value of 0 + if (i2cReadDelayTime > 0) { + // delay is necessary for some devices such as WiiNunchuck + delayMicroseconds(i2cReadDelayTime); + } + } else { + theRegister = 0; // fill the register with a dummy value + } + + Wire.requestFrom(address, numBytes); // all bytes are returned in requestFrom + + // check to be sure correct number of bytes were returned by slave + if (numBytes < Wire.available()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("I2C: Too few bytes received"); + } + + i2cRxData[0] = address; + i2cRxData[1] = theRegister; + + for (int i = 0; i < numBytes && Wire.available(); i++) { + i2cRxData[2 + i] = wireRead(); + } + + // send slave address, register and received bytes + Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData); +} + +void outputPort(byte portNumber, byte portValue, byte forceSend) +{ + // pins not configured as INPUT are cleared to zeros + portValue = portValue & portConfigInputs[portNumber]; + // only send if the value is different than previously sent + if (forceSend || previousPINs[portNumber] != portValue) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(void) +{ + /* Using non-looping code allows constants to be given to readPort(). + * The compiler will apply substantial optimizations if the inputs + * to readPort() are compile-time constants. */ + if (TOTAL_PORTS > 0 && reportPINs[0]) outputPort(0, readPort(0, portConfigInputs[0]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +// ----------------------------------------------------------------------------- +/* sets the pin mode to the correct state and sets the relevant bits in the + * two bit-arrays that track Digital I/O and PWM status + */ +void setPinModeCallback(byte pin, int mode) +{ + if (Firmata.getPinMode(pin) == PIN_MODE_IGNORE) + return; + + if (Firmata.getPinMode(pin) == PIN_MODE_I2C && isI2CEnabled && mode != PIN_MODE_I2C) { + // disable i2c so pins can be used for other functions + // the following if statements should reconfigure the pins properly + disableI2CPins(); + } + if (IS_PIN_DIGITAL(pin) && mode != PIN_MODE_SERVO) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + } + if (IS_PIN_ANALOG(pin)) { + reportAnalogCallback(PIN_TO_ANALOG(pin), mode == PIN_MODE_ANALOG ? 1 : 0); // turn on/off reporting + } + if (IS_PIN_DIGITAL(pin)) { + if (mode == INPUT || mode == PIN_MODE_PULLUP) { + portConfigInputs[pin / 8] |= (1 << (pin & 7)); + } else { + portConfigInputs[pin / 8] &= ~(1 << (pin & 7)); + } + } + Firmata.setPinState(pin, 0); + switch (mode) { + case PIN_MODE_ANALOG: + if (IS_PIN_ANALOG(pin)) { + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + } + Firmata.setPinMode(pin, PIN_MODE_ANALOG); + } + break; + case INPUT: +// Adafruit: Input without pull up cause pin state changes randomly --> lots of transmission data +// if (IS_PIN_DIGITAL(pin)) { +// pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +//#if ARDUINO <= 100 +// // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 +// digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +//#endif +// Firmata.setPinMode(pin, INPUT); +// } +// break; + case PIN_MODE_PULLUP: + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT_PULLUP); + Firmata.setPinMode(pin, PIN_MODE_PULLUP); + Firmata.setPinState(pin, 1); + } + break; + case OUTPUT: + if (IS_PIN_DIGITAL(pin)) { + if (Firmata.getPinMode(pin) == PIN_MODE_PWM) { + // Disable PWM if pin mode was previously set to PWM. + digitalWrite(PIN_TO_DIGITAL(pin), LOW); + } + pinMode(PIN_TO_DIGITAL(pin), OUTPUT); + Firmata.setPinMode(pin, OUTPUT); + } + break; + case PIN_MODE_PWM: + if (IS_PIN_PWM(pin)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + Firmata.setPinMode(pin, PIN_MODE_PWM); + } + break; + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) { + Firmata.setPinMode(pin, PIN_MODE_SERVO); + if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) { + // pass -1 for min and max pulse values to use default values set + // by Servo library + attachServo(pin, -1, -1); + } + } + break; + case PIN_MODE_I2C: + if (IS_PIN_I2C(pin)) { + // mark the pin as i2c + // the user must call I2C_CONFIG to enable I2C for a device + Firmata.setPinMode(pin, PIN_MODE_I2C); + } + break; + case PIN_MODE_SERIAL: +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handlePinMode(pin, PIN_MODE_SERIAL); +#endif + break; + default: + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } + // TODO: save status to EEPROM here, if changed +} + +/* + * Sets the value of an individual pin. Useful if you want to set a pin value but + * are not tracking the digital port state. + * Can only be used on pins configured as OUTPUT. + * Cannot be used to enable pull-ups on Digital INPUT pins. + */ +void setPinValueCallback(byte pin, int value) +{ + if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) { + if (Firmata.getPinMode(pin) == OUTPUT) { + Firmata.setPinState(pin, value); + digitalWrite(PIN_TO_DIGITAL(pin), value); + } + } +} + +void analogWriteCallback(byte pin, int value) +{ + if (pin < TOTAL_PINS) { + switch (Firmata.getPinMode(pin)) { + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) + servos[servoPinMap[pin]].write(value); + Firmata.setPinState(pin, value); + break; + case PIN_MODE_PWM: + if (IS_PIN_PWM(pin)) + analogWrite(PIN_TO_PWM(pin), value); + Firmata.setPinState(pin, value); + break; + } + } +} + +void digitalWriteCallback(byte port, int value) +{ + byte pin, lastPin, pinValue, mask = 1, pinWriteMask = 0; + + if (port < TOTAL_PORTS) { + // create a mask of the pins on this port that are writable. + lastPin = port * 8 + 8; + if (lastPin > TOTAL_PINS) lastPin = TOTAL_PINS; + for (pin = port * 8; pin < lastPin; pin++) { + // do not disturb non-digital pins (eg, Rx & Tx) + if (IS_PIN_DIGITAL(pin)) { + // do not touch pins in PWM, ANALOG, SERVO or other modes + if (Firmata.getPinMode(pin) == OUTPUT || Firmata.getPinMode(pin) == INPUT) { + pinValue = ((byte)value & mask) ? 1 : 0; + if (Firmata.getPinMode(pin) == OUTPUT) { + pinWriteMask |= mask; + } else if (Firmata.getPinMode(pin) == INPUT && pinValue == 1 && Firmata.getPinState(pin) != 1) { + // only handle INPUT here for backwards compatibility +#if ARDUINO > 100 + pinMode(pin, INPUT_PULLUP); +#else + // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier + pinWriteMask |= mask; +#endif + } + Firmata.setPinState(pin, pinValue); + } + } + mask = mask << 1; + } + writePort(port, (byte)value, pinWriteMask); + } +} + + +// ----------------------------------------------------------------------------- +/* sets bits in a bit array (int) to toggle the reporting of the analogIns + */ +//void FirmataClass::setAnalogPinReporting(byte pin, byte state) { +//} +void reportAnalogCallback(byte analogPin, int value) +{ + if (analogPin < TOTAL_ANALOG_PINS) { + if (value == 0) { + analogInputsToReport = analogInputsToReport & ~ (1 << analogPin); + } else { + analogInputsToReport = analogInputsToReport | (1 << analogPin); + // prevent during system reset or all analog pin values will be reported + // which may report noise for unconnected analog pins + if (!isResetting) { + // Send pin value immediately. This is helpful when connected via + // ethernet, wi-fi or bluetooth so pin states can be known upon + // reconnecting. + Firmata.sendAnalog(analogPin, analogRead( ANALOG_TO_PIN(analogPin) ) ); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)value; + // Send port value immediately. This is helpful when connected via + // ethernet, wi-fi or bluetooth so pin states can be known upon + // reconnecting. + if (value) outputPort(port, readPort(port, portConfigInputs[port]), true); + } + // do not disable analog reporting on these 8 pins, to allow some + // pins used for digital, others analog. Instead, allow both types + // of reporting to be enabled, but check if the pin is configured + // as analog when sampling the analog inputs. Likewise, while + // scanning digital pins, portConfigInputs will mask off values from any + // pins configured as analog +} + +/*============================================================================== + * SYSEX-BASED commands + *============================================================================*/ + +void sysexCallback(byte command, byte argc, byte *argv) +{ + byte mode; + byte stopTX; + byte slaveAddress; + byte data; + int slaveRegister; + unsigned int delayTime; + + switch (command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("10-bit addressing not supported"); + return; + } + else { + slaveAddress = argv[0]; + } + + // need to invert the logic here since 0 will be default for client + // libraries that have not updated to add support for restart tx + if (argv[1] & I2C_END_TX_MASK) { + stopTX = I2C_RESTART_TX; + } + else { + stopTX = I2C_STOP_TX; // default + } + + switch (mode) { + case I2C_WRITE: + Wire.beginTransmission(slaveAddress); + for (byte i = 2; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + wireWrite(data); + } + Wire.endTransmission(); + delayMicroseconds(70); + break; + case I2C_READ: + if (argc == 6) { + // a slave register is specified + slaveRegister = argv[2] + (argv[3] << 7); + data = argv[4] + (argv[5] << 7); // bytes to read + } + else { + // a slave register is NOT specified + slaveRegister = I2C_REGISTER_NOT_SPECIFIED; + data = argv[2] + (argv[3] << 7); // bytes to read + } + readAndReportData(slaveAddress, (int)slaveRegister, data, stopTX); + break; + case I2C_READ_CONTINUOUSLY: + if ((queryIndex + 1) >= I2C_MAX_QUERIES) { + // too many queries, just ignore + Firmata.sendString("too many queries"); + break; + } + if (argc == 6) { + // a slave register is specified + slaveRegister = argv[2] + (argv[3] << 7); + data = argv[4] + (argv[5] << 7); // bytes to read + } + else { + // a slave register is NOT specified + slaveRegister = (int)I2C_REGISTER_NOT_SPECIFIED; + data = argv[2] + (argv[3] << 7); // bytes to read + } + queryIndex++; + query[queryIndex].addr = slaveAddress; + query[queryIndex].reg = slaveRegister; + query[queryIndex].bytes = data; + query[queryIndex].stopTX = stopTX; + break; + case I2C_STOP_READING: + byte queryIndexToSkip; + // if read continuous mode is enabled for only 1 i2c device, disable + // read continuous reporting for that device + if (queryIndex <= 0) { + queryIndex = -1; + } else { + queryIndexToSkip = 0; + // if read continuous mode is enabled for multiple devices, + // determine which device to stop reading and remove it's data from + // the array, shifiting other array data to fill the space + for (byte i = 0; i < queryIndex + 1; i++) { + if (query[i].addr == slaveAddress) { + queryIndexToSkip = i; + break; + } + } + + for (byte i = queryIndexToSkip; i < queryIndex + 1; i++) { + if (i < I2C_MAX_QUERIES) { + query[i].addr = query[i + 1].addr; + query[i].reg = query[i + 1].reg; + query[i].bytes = query[i + 1].bytes; + query[i].stopTX = query[i + 1].stopTX; + } + } + queryIndex--; + } + break; + default: + break; + } + break; + case I2C_CONFIG: + delayTime = (argv[0] + (argv[1] << 7)); + + if (delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_DIGITAL(pin)) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + attachServo(pin, minPulse, maxPulse); + setPinModeCallback(pin, PIN_MODE_SERVO); + } + } + break; + case SAMPLING_INTERVAL: + if (argc > 1) { + samplingInterval = argv[0] + (argv[1] << 7); + if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) { + samplingInterval = MINIMUM_SAMPLING_INTERVAL; + } + } else { + //Firmata.sendString("Not enough data"); + } + break; + case EXTENDED_ANALOG: + if (argc > 1) { + int val = argv[1]; + if (argc > 2) val |= (argv[2] << 7); + if (argc > 3) val |= (argv[3] << 14); + analogWriteCallback(argv[0], val); + } + break; + case CAPABILITY_QUERY: + Firmata.write(START_SYSEX); + Firmata.write(CAPABILITY_RESPONSE); + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_DIGITAL(pin)) { + Firmata.write((byte)INPUT); + Firmata.write(1); + Firmata.write((byte)PIN_MODE_PULLUP); + Firmata.write(1); + Firmata.write((byte)OUTPUT); + Firmata.write(1); + } + if (IS_PIN_ANALOG(pin)) { + Firmata.write(PIN_MODE_ANALOG); + Firmata.write(10); // 10 = 10-bit resolution + } + if (IS_PIN_PWM(pin)) { + Firmata.write(PIN_MODE_PWM); + Firmata.write(DEFAULT_PWM_RESOLUTION); + } + if (IS_PIN_DIGITAL(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); + } + if (IS_PIN_I2C(pin)) { + Firmata.write(PIN_MODE_I2C); + Firmata.write(1); // TODO: could assign a number to map to SCL or SDA + } +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handleCapability(pin); +#endif + Firmata.write(127); + } + Firmata.write(END_SYSEX); + break; + case PIN_STATE_QUERY: + if (argc > 0) { + byte pin = argv[0]; + Firmata.write(START_SYSEX); + Firmata.write(PIN_STATE_RESPONSE); + Firmata.write(pin); + if (pin < TOTAL_PINS) { + Firmata.write(Firmata.getPinMode(pin)); + Firmata.write((byte)Firmata.getPinState(pin) & 0x7F); + if (Firmata.getPinState(pin) & 0xFF80) Firmata.write((byte)(Firmata.getPinState(pin) >> 7) & 0x7F); + if (Firmata.getPinState(pin) & 0xC000) Firmata.write((byte)(Firmata.getPinState(pin) >> 14) & 0x7F); + } + Firmata.write(END_SYSEX); + } + break; + case ANALOG_MAPPING_QUERY: + Firmata.write(START_SYSEX); + Firmata.write(ANALOG_MAPPING_RESPONSE); + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + break; + + case SERIAL_MESSAGE: +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handleSysex(command, argc, argv); +#endif + break; + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + isResetting = true; + + // initialize a defalt state + // TODO: option to load config from EEPROM instead of default + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.reset(); +#endif + + if (isI2CEnabled) { + disableI2CPins(); + } + + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + + for (byte i = 0; i < TOTAL_PINS; i++) { + // pins with analog capability default to analog input + // otherwise, pins default to digital output + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, PIN_MODE_ANALOG); + } else if (IS_PIN_DIGITAL(i)) { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, OUTPUT); + } + + servoPinMap[i] = 255; + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + detachedServoCount = 0; + servoCount = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ + isResetting = false; +} + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Standard Firmata via BLEUART Example"); + Serial.println("------------------------------------------------\n"); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + + // try to go as fast as possible, could be rejected by some central, increase it if needed + // iOS won't negotitate and will mostly use 30ms + Bluefruit.setConnInterval(9, 24); // min = 9*1.25=11.25 ms, max = 23*1.25=30ms + + // Configure and Start BLE Uart Service + // Firmata use several small write(1) --> buffering TXD is required to run smoothly + // Enable buffering TXD + bleuart.begin(); + bleuart.bufferTXD(true); + + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // use bleuart as transportation layer + Firmata.begin(bleuart); + + // to use a port other than Serial, such as Serial1 on an Arduino Leonardo or Mega, + // Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this: + // Serial1.begin(57600); + // Firmata.begin(Serial1); + // However do not do this if you are using SERIAL_MESSAGE + + //Firmata.begin(57600); + //while (!Serial) { + // ; // wait for serial port to connect. Needed for ATmega32u4-based boards and Arduino 101 + //} + + systemResetCallback(); // reset to default config + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + // Skip if not connected and bleuart notification is enabled + if ( !(Bluefruit.connected() && bleuart.notifyEnabled()) ) + { + // go to low power mode since there is nothing to do + waitForEvent(); + return; + } + + byte pin, analogPin; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * FTDI buffer using Serial.print() */ + checkDigitalInputs(); + + /* STREAMREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) + Firmata.processInput(); + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead( ANALOG_TO_PIN(analogPin) )); + } + } + } + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } + } + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.update(); +#endif +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/adv_advanced/adv_advanced.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/adv_advanced/adv_advanced.ino new file mode 100755 index 0000000..7332a2d --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/adv_advanced/adv_advanced.ino @@ -0,0 +1,91 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketches demontrates the Bluefruit.Advertising API(). When powered up, + * the Bluefruit module will start advertising for ADV_TIMEOUT seconds (by + * default 30 seconds in fast mode, the remaining time slow mode) and then + * stop advertising completely. The module will start advertising again if + * PIN_ADV is grounded. + */ +#include <bluefruit.h> + +#define PIN_ADV A0 +#define ADV_TIMEOUT 60 // seconds + +void setup() +{ + // configure PIN_ADV as input with a pullup (pin is active low) + pinMode(PIN_ADV, INPUT_PULLUP); + + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Advanced Advertising Example"); + Serial.println("----------------------------------------\n"); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // Set up and start advertising + startAdv(); + + Serial.println("Advertising is started"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.setStopCallback(adv_stop_callback); + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in units of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(ADV_TIMEOUT); // Stop advertising entirely after ADV_TIMEOUT seconds +} + +void loop() +{ + // Only check pin when advertising has already stopped + if ( !Bluefruit.Advertising.isRunning() ) + { + // Check if Pin is grounded + if ( digitalRead(PIN_ADV) == 0 ) + { + Bluefruit.Advertising.start(ADV_TIMEOUT); + Serial.println("Advertising is started"); + } + } +} + +/** + * Callback invoked when advertising is stopped by timeout + */ +void adv_stop_callback(void) +{ + Serial.println("Advertising time passed, advertising will now stop."); +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino new file mode 100755 index 0000000..5b4e48a --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino @@ -0,0 +1,217 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch demonstrate the BLEAncs service. After uploading, go to + * iOS setting and connect to Bluefruit, and then press PAIR. Bluefruit + * will print out any notification meesages to Serial Monitor + */ + +#include <bluefruit.h> + +// BLE Client Service +BLEClientDis bleClientDis; +BLEAncs bleancs; + +char buffer[128]; + +// Check BLEAncs.h for AncsNotification_t +const char* EVENT_STR[] = { "Added", "Modified", "Removed" }; +const char* CAT_STR [] = +{ + "Other" , "Incoming Call" , "Missed Call", "Voice Mail" , + "Social" , "Schedule" , "Email" , "News" , + "Health and Fitness", "Business and Finance", "Location" , "Entertainment" +}; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 BLE ANCS Example"); + Serial.println("----------------------------\n"); + + Serial.println("Go to iOS's Bluetooth settings and connect to Bluefruit52"); + Serial.println("It may appear up as 'Accessory' depending on your OS version."); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure DIS client + bleClientDis.begin(); + + // Configure ANCS client + bleancs.begin(); + bleancs.setNotificationCallback(ancs_notification_callback); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include ANCS 128-bit uuid + Bluefruit.Advertising.addService(bleancs); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Not connected, wait for a connection + if ( !Bluefruit.connected() ) return; + + // If service is not yet discovered + if ( !bleancs.discovered() ) return; + + // Your code here +} + +void connect_callback(uint16_t conn_handle) +{ + Serial.println("Connected"); + + Serial.print("Discovering DIS ... "); + if ( bleClientDis.discover(conn_handle) ) + { + Serial.println("Discovered"); + + // Read and print Manufacturer string + memset(buffer, 0, sizeof(buffer)); + if ( bleClientDis.getManufacturer(buffer, sizeof(buffer)) ) + { + Serial.print("Manufacturer: "); + Serial.println(buffer); + } + + // Read and print Model Number string + memset(buffer, 0, sizeof(buffer)); + if ( bleClientDis.getModel(buffer, sizeof(buffer)) ) + { + Serial.print("Model: "); + Serial.println(buffer); + } + + Serial.println(); + } + + Serial.print("Discovering ANCS ... "); + if ( bleancs.discover(conn_handle) ) + { + Serial.println("Discovered"); + + // ANCS requires pairing to work, it makes sense to request security here as well + Serial.print("Attempting to PAIR with the iOS device, please press PAIR on your phone ... "); + if ( Bluefruit.requestPairing() ) + { + Serial.println("Done"); + Serial.println("Enabling notifications"); + Serial.println(); + bleancs.enableNotification(); + + Serial.println("| Event | Category (count) | Title | Message | App ID | App Name |"); + Serial.println("---------------------------------------------------------------------------------------------------------------"); + } + } +} + +void ancs_notification_callback(AncsNotification_t* notif) +{ + int n; + Serial.printf("| %-8s | ", EVENT_STR[notif->eventID]); + + // Print Category with padding + n = Serial.printf("%s (%d)", CAT_STR[notif->categoryID], notif->categoryCount); + for (int i=n; i<20; i++) Serial.print(' '); + Serial.print(" | "); + + // Get notification Title + // iDevice often includes Unicode "Bidirection Text Control" in the Title. + // Most strings have U+202D at the beginning and U+202C at the end. You may + // want to remove them. + // U+202D is E2-80-AD, U+202C is E2-80-AC in UTF-8 + memset(buffer, 0, sizeof(buffer)); + bleancs.getAttribute(notif->uid, ANCS_ATTR_TITLE, buffer, sizeof(buffer)); + Serial.printf("%-14s | ", buffer); + + // Get notification Message + memset(buffer, 0, sizeof(buffer)); + bleancs.getAttribute(notif->uid, ANCS_ATTR_MESSAGE, buffer, sizeof(buffer)); + Serial.printf("%-15s | ", buffer); + + // Get App ID and store in the app_id variable + char app_id[64] = { 0 }; + memset(buffer, 0, sizeof(buffer)); + bleancs.getAttribute(notif->uid, ANCS_ATTR_APP_IDENTIFIER, buffer, sizeof(buffer)); + strcpy(app_id, buffer); + Serial.printf("%-20s | ", app_id); + + // Get Application Name + memset(buffer, 0, sizeof(buffer)); + bleancs.getAppAttribute(app_id, ANCS_APP_ATTR_DISPLAY_NAME, buffer, sizeof(buffer)); + Serial.printf("%-15s | ", buffer); + + Serial.println(); + + // Automatically accept incoming calls using 'performAction' + if ( notif->categoryID == ANCS_CAT_INCOMING_CALL && notif->eventID == ANCS_EVT_NOTIFICATION_ADDED) + { + Serial.println("Incoming call accepted"); + bleancs.performAction(notif->uid, ANCS_ACTION_POSITIVE); + } +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) reason; + + Serial.println(); + Serial.println("Disconnected"); +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/ancs_oled/ancs_oled.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/ancs_oled/ancs_oled.ino new file mode 100755 index 0000000..ce91a31 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/ancs_oled/ancs_oled.ino @@ -0,0 +1,458 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* + * This sketch is similar to 'ancs', but it also uses a Feather OLED + * Wing to display incoming ANCS alerts: + * https://www.adafruit.com/product/2900 + * + * BUTTON A: Up or accept call + * BUTTON B: Not used since it is hard to press + * BUTTON C: Down or decline call + */ +#include <Wire.h> +#include <Adafruit_GFX.h> +#include <Adafruit_SSD1306.h> +#include <bluefruit.h> + +/*------------- OLED and Buttons -------------*/ +#if defined ARDUINO_NRF52_FEATHER +// Feather nRF52832 +#define BUTTON_A 31 +#define BUTTON_B 30 +#define BUTTON_C 27 + +#elif defined ARDUINO_NRF52840_FEATHER +// Feather nRF52840 +#define BUTTON_A 9 +#define BUTTON_B 6 +#define BUTTON_C 5 + +#else +#error board not supported +#endif + + +#define OLED_RESET 4 // TODO remove ? +Adafruit_SSD1306 oled(OLED_RESET); + + +/*------------- Notification List -------------*/ +#define MAX_COUNT 20 +#define BUFSIZE 64 + +typedef struct +{ + AncsNotification_t ntf; + char title[BUFSIZE]; + char message[BUFSIZE]; + char app_name[BUFSIZE]; +} MyNotif_t; + +MyNotif_t myNotifs[MAX_COUNT] = { 0 }; + +// Number of notifications +int notifCount = 0; + +/*------------- Display Management -------------*/ +#define ONSCREEN_TIME 5000 // On-screen time for each notification + +int activeIndex = 0; // Index of currently displayed notification +int displayIndex = -1; // Index of notification about to display + +uint32_t drawTime = 0; // Last time oled display notification + +/*------------- BLE Client Service-------------*/ +BLEAncs bleancs; + +void setup() +{ + // Button configured + pinMode(BUTTON_A, INPUT_PULLUP); + pinMode(BUTTON_B, INPUT_PULLUP); + pinMode(BUTTON_C, INPUT_PULLUP); + + // init with the I2C addr 0x3C (for the 128x32) and show splashscreen + oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); + oled.display(); + + oled.setTextSize(1);// max is 4 line, 21 chars each + oled.setTextColor(WHITE); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + //Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure and Start Service + bleancs.begin(); + bleancs.setNotificationCallback(ancs_notification_callback); + + // Set up and start advertising + startAdv(); + + // splash screen effect + delay(100); + + oled.clearDisplay(); + oled.setCursor(0, 0); + oled.println("Not connected"); + oled.display(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include ANCS 128-bit uuid + Bluefruit.Advertising.addService(bleancs); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // If service is not yet discovered + if ( !bleancs.discovered() ) return; + + // No notifications, do nothing + if ( notifCount == 0 ) return; + + // Check buttons + uint32_t presedButtons = readPressedButtons(); + + if ( myNotifs[activeIndex].ntf.categoryID == ANCS_CAT_INCOMING_CALL ) + { + /* Incoming call event + * - Button A to accept call + * - Button C to decline call + */ + if ( presedButtons & bit(BUTTON_A) ) + { + bleancs.actPositive(myNotifs[activeIndex].ntf.uid); + } + + if ( presedButtons & bit(BUTTON_C) ) + { + bleancs.actNegative(myNotifs[activeIndex].ntf.uid); + } + } + else + { + /* Normal events navigation (wrap around) + * - Button A to display previous notification + * - Button C to display next notification + * + * When a notification is display ONSCREEN_TIME, + * we will display the next one + */ + if ( presedButtons & bit(BUTTON_A) ) + { + displayIndex = (activeIndex != 0) ? (activeIndex-1) : (notifCount-1) ; + } + + if ( presedButtons & bit(BUTTON_C) ) + { + displayIndex = (activeIndex != (notifCount-1)) ? (activeIndex + 1) : 0; + } + + // Display requested notification + if ( displayIndex >= 0 ) + { + activeIndex = displayIndex; + displayIndex = -1; + + displayNotification(activeIndex); + drawTime = millis(); // Save time we draw + } + // Display next notification if time is up + else if ( drawTime + ONSCREEN_TIME < millis() ) + { + activeIndex = (activeIndex+1)%notifCount; + + displayNotification(activeIndex); + drawTime = millis(); // Save time we draw + } + } +} + +/** + * Display notification contents to oled screen + * @param index index of notification + */ +void displayNotification(int index) +{ + // safeguard + if ( index < 0 || (index >= notifCount) ) return; + + // let's Turn on and off RED LED when we draw to get attention + digitalWrite(LED_RED, HIGH); + + /*------------- Display to OLED -------------*/ + MyNotif_t* myNtf = &myNotifs[index]; + + oled.clearDisplay(); + oled.setCursor(0, 0); + + // Incoming call event, display a bit differently + if ( myNtf->ntf.categoryID == ANCS_CAT_INCOMING_CALL ) + { + oled.println(myNtf->title); + oled.println(" is calling"); + oled.println(" Btn A to ACCEPT"); + oled.println(" Btn C to DECLINE"); + }else + { + // Text size = 1, max char is 21. Text size = 2, max char is 10 + char tempbuf[22]; + sprintf(tempbuf, "%-15s %02d/%02d", myNtf->app_name, index+1, notifCount); + + oled.println(tempbuf); + oled.println(myNtf->title); + + oled.print(" "); + oled.print(myNtf->message); + } + + oled.display(); + + digitalWrite(LED_RED, LOW); +} + +/** + * Connect Callback + * Perform ANCS discovering, request Pairing + */ +void connect_callback(uint16_t conn_handle) +{ + oled.clearDisplay(); + oled.setCursor(0, 0); + oled.println("Connected."); + oled.print("Discovering ... "); + oled.display(); + + if ( bleancs.discover( conn_handle ) ) + { + oled.println("OK"); + + // ANCS requires pairing to work + oled.print("Paring ... "); + + oled.display(); + + if ( Bluefruit.requestPairing() ) + { + oled.println("OK"); + + bleancs.enableNotification(); + oled.println("Receiving ..."); + }else + { + oled.println("Failed"); + } + }else + { + oled.println("Failed"); + } + + oled.display(); +} + +/** + * Notification callback + * @param notif Notification from iDevice + * + * Save/Modify notification into myNotifs struct to display later + */ +void ancs_notification_callback(AncsNotification_t* notif) +{ + if (notif->eventID == ANCS_EVT_NOTIFICATION_ADDED ) + { + myNotifs[ notifCount ].ntf = *notif; + + /*------------- Retrieve Title, Message, App Name -------------*/ + MyNotif_t* myNtf = &myNotifs[notifCount]; + uint32_t uid = myNtf->ntf.uid; + + // iDevice often include Unicode "Bidirection Text Control" in the Title. + // Mostly are U+202D as beginning and U+202C as ending. Let's remove them + if ( bleancs.getTitle (uid, myNtf->title , BUFSIZE) ) + { + char u202D[3] = { 0xE2, 0x80, 0xAD }; // U+202D in UTF-8 + char u202C[3] = { 0xE2, 0x80, 0xAC }; // U+202C in UTF-8 + + int len = strlen(myNtf->title); + + if ( 0 == memcmp(&myNtf->title[len-3], u202C, 3) ) + { + len -= 3; + myNtf->title[len] = 0; // chop ending U+202C + } + + if ( 0 == memcmp(myNtf->title, u202D, 3) ) + { + memmove(myNtf->title, myNtf->title+3, len-2); // move null-terminator as well + } + } + + bleancs.getMessage(uid, myNtf->message , BUFSIZE); + bleancs.getAppName(uid, myNtf->app_name, BUFSIZE); + + displayIndex = notifCount++; // display new notification + }else if (notif->eventID == ANCS_EVT_NOTIFICATION_REMOVED ) + { + for(int i=0; i<notifCount; i++) + { + if ( notif->uid == myNotifs[i].ntf.uid ) + { + // remove by swapping with the last one + notifCount--; + myNotifs[i] = myNotifs[notifCount]; + + // Invalid removed data + memset(&myNotifs[notifCount], 0, sizeof(MyNotif_t)); + + if (activeIndex == notifCount) + { + // If remove the last notification, adjust display index + displayIndex = notifCount-1; + }else if (activeIndex == i) + { + // Re-draw if remove currently active one + displayIndex = activeIndex; + } + + break; + } + } + }else + { + // Modification + for(int i=0; i<notifCount; i++) + { + if ( notif->uid == myNotifs[i].ntf.uid ) + { + // Display modification + displayIndex = i; + break; + } + } + } +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + // reset notification array + notifCount = 0; + activeIndex = 0; + displayIndex = -1; + + memset(myNotifs, 0, sizeof(myNotifs)); + + oled.clearDisplay(); + oled.setCursor(0, 0); + oled.println("Not connected"); + oled.display(); +} + +/** + * Check if button A,B,C state are pressed, include some software + * debouncing. + * + * Note: Only set bit when Button is state change from + * idle -> pressed. Press and hold only report 1 time, release + * won't report as well + * + * @return Bitmask of pressed buttons e.g If BUTTON_A is pressed + * bit 31 will be set. + */ +uint32_t readPressedButtons(void) +{ + // must be exponent of 2 + enum { MAX_CHECKS = 8, SAMPLE_TIME = 10 }; + + /* Array that maintains bounce status/, which is sampled + * 10 ms each. Debounced state is regconized if all the values + * of a button has the same value (bit set or clear) + */ + static uint32_t lastReadTime = 0; + static uint32_t states[MAX_CHECKS] = { 0 }; + static uint32_t index = 0; + + // Last Debounced state, used to detect changed + static uint32_t lastDebounced = 0; + + // Too soon, nothing to do + if (millis() - lastReadTime < SAMPLE_TIME ) return 0; + + lastReadTime = millis(); + + // Take current read and masked with BUTTONs + // Note: Bitwise inverted since buttons are active (pressed) LOW + uint32_t debounced = ~(*portInputRegister( digitalPinToPort(0) )); + debounced &= (bit(BUTTON_A) | bit(BUTTON_B) | bit(BUTTON_C)); + + // Copy current state into array + states[ (index & (MAX_CHECKS-1)) ] = debounced; + index++; + + // Bitwise And all the state in the array together to get the result + // This means pin must stay at least MAX_CHECKS time to be realized as changed + for(int i=0; i<MAX_CHECKS; i++) + { + debounced &= states[i]; + } + + // result is button changed and current debounced is set + // Mean button is pressed (idle previously) + uint32_t result = (debounced ^ lastDebounced) & debounced; + + lastDebounced = debounced; + + return result; +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/beacon/beacon.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/beacon/beacon.ino new file mode 100755 index 0000000..5897eb8 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/beacon/beacon.ino @@ -0,0 +1,93 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include <bluefruit.h> + +// Beacon uses the Manufacturer Specific Data field in the advertising +// packet, which means you must provide a valid Manufacturer ID. Update +// the field below to an appropriate value. For a list of valid IDs see: +// https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers +// 0x004C is Apple (for example) +#define MANUFACTURER_ID 0x004C + +// AirLocate UUID: E2C56DB5-DFFB-48D2-B060-D0F5A71096E0 +uint8_t beaconUuid[16] = +{ + 0xE2, 0xC5, 0x6D, 0xB5, 0xDF, 0xFB, 0x48, 0xD2, + 0xB0, 0x60, 0xD0, 0xF5, 0xA7, 0x10, 0x96, 0xE0, +}; + +// A valid Beacon packet consists of the following information: +// UUID, Major, Minor, RSSI @ 1M +BLEBeacon beacon(beaconUuid, 0x0000, 0x0000, -54); + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Beacon Example"); + Serial.println("--------------------------\n"); + + Bluefruit.begin(); + + // off Blue LED for lowest power consumption + Bluefruit.autoConnLed(false); + + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(0); + Bluefruit.setName("Bluefruit52"); + + // Manufacturer ID is required for Manufacturer Specific Data + beacon.setManufacturer(MANUFACTURER_ID); + + // Setup the advertising packet + startAdv(); + + Serial.println("Broadcasting beacon, open your beacon app to test"); + + // Suspend Loop() to save power, since we didn't have any code there + suspendLoop(); +} + +void startAdv(void) +{ + // Advertising packet + // Set the beacon payload using the BLEBeacon class populated + // earlier in this example + Bluefruit.Advertising.setBeacon(beacon); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * Apple Beacon specs + * - Type: Non connectable, undirected + * - Fixed interval: 100 ms -> fast = slow = 100 ms + */ + //Bluefruit.Advertising.setType(BLE_GAP_ADV_TYPE_ADV_NONCONN_IND); + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(160, 160); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // loop is already suspended, CPU will not run loop() at all +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino new file mode 100755 index 0000000..9e09bb4 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino @@ -0,0 +1,182 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* For BLE MIDI Setup + * https://learn.adafruit.com/wireless-untztrument-using-ble-midi/overview + */ + + +#include <bluefruit.h> +#include <MIDI.h> + +BLEDis bledis; +BLEMidi blemidi; + +// Create a new instance of the Arduino MIDI Library, +// and attach BluefruitLE MIDI as the transport. +MIDI_CREATE_BLE_INSTANCE(blemidi); + +// Variable that holds the current position in the sequence. +int position = 0; + +// Store example melody as an array of note values +byte note_sequence[] = { + 74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78, + 74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61, + 56,61,64,68,74,78,81,86,90,93,98,102 +}; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Adafruit Bluefruit52 MIDI over Bluetooth LE Example"); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52 MIDI"); + Bluefruit.setTxPower(4); + + // Setup the on board blue LED to be enabled on CONNECT + Bluefruit.autoConnLed(true); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Initialize MIDI, and listen to all MIDI channels + // This will also call blemidi service's begin() + MIDI.begin(MIDI_CHANNEL_OMNI); + + // Attach the handleNoteOn function to the MIDI Library. It will + // be called whenever the Bluefruit receives MIDI Note On messages. + MIDI.setHandleNoteOn(handleNoteOn); + + // Do the same for MIDI Note Off messages. + MIDI.setHandleNoteOff(handleNoteOff); + + // Set up and start advertising + startAdv(); + + // Start MIDI read loop + Scheduler.startLoop(midiRead); +} + +void startAdv(void) +{ + // Set General Discoverable Mode flag + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + + // Advertise TX Power + Bluefruit.Advertising.addTxPower(); + + // Advertise BLE MIDI Service + Bluefruit.Advertising.addService(blemidi); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void handleNoteOn(byte channel, byte pitch, byte velocity) +{ + // Log when a note is pressed. + Serial.printf("Note on: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity); + Serial.println(); +} + +void handleNoteOff(byte channel, byte pitch, byte velocity) +{ + // Log when a note is released. + Serial.printf("Note off: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity); + Serial.println(); +} + +void loop() +{ + // Don't continue if we aren't connected. + if (! Bluefruit.connected()) { + return; + } + + // Don't continue if the connected device isn't ready to receive messages. + if (! blemidi.notifyEnabled()) { + return; + } + + // Setup variables for the current and previous + // positions in the note sequence. + int current = position; + int previous = position - 1; + + // If we currently are at position 0, set the + // previous position to the last note in the sequence. + if (previous < 0) { + previous = sizeof(note_sequence) - 1; + } + + // Send Note On for current position at full velocity (127) on channel 1. + MIDI.sendNoteOn(note_sequence[current], 127, 1); + + // Send Note Off for previous note. + MIDI.sendNoteOff(note_sequence[previous], 0, 1); + + // Increment position + position++; + + // If we are at the end of the sequence, start over. + if (position >= sizeof(note_sequence)) { + position = 0; + } + + delay(286); + +} + +void midiRead() +{ + // Don't continue if we aren't connected. + if (! Bluefruit.connected()) { + return; + } + + // Don't continue if the connected device isn't ready to receive messages. + if (! blemidi.notifyEnabled()) { + return; + } + + // read any new MIDI messages + MIDI.read(); +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino new file mode 100755 index 0000000..55d2b77 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino @@ -0,0 +1,142 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include <bluefruit.h> + +// BLE Service +BLEDis bledis; // device information +BLEUart bleuart; // uart over ble +BLEBas blebas; // battery + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 BLEUART Example"); + Serial.println("---------------------------\n"); + + // Setup the BLE LED to be enabled on CONNECT + // Note: This is actually the default behaviour, but provided + // here in case you want to control this LED manually via PIN 19 + Bluefruit.autoConnLed(true); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Configure and Start BLE Uart Service + bleuart.begin(); + + // Start BLE Battery Service + blebas.begin(); + blebas.write(100); + + // Set up and start advertising + startAdv(); + + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); + Serial.println("Once connected, enter character(s) that you wish to send"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Forward data from HW Serial to BLEUART + while (Serial.available()) + { + // Delay to wait for enough input, since we have a limited transmission buffer + delay(2); + + uint8_t buf[64]; + int count = Serial.readBytes(buf, sizeof(buf)); + bleuart.write( buf, count ); + } + + // Forward from BLEUART to HW Serial + while ( bleuart.available() ) + { + uint8_t ch; + ch = (uint8_t) bleuart.read(); + Serial.write(ch); + } + + // Request CPU to enter low-power mode until an event/interrupt occurs + waitForEvent(); +} + +// callback invoked when central connects +void connect_callback(uint16_t conn_handle) +{ + char central_name[32] = { 0 }; + Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println(); + Serial.println("Disconnected"); +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/blinky_ota/blinky_ota.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/blinky_ota/blinky_ota.ino new file mode 100755 index 0000000..85cd5f0 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/blinky_ota/blinky_ota.ino @@ -0,0 +1,63 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include <bluefruit.h> + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Blinky Example"); + Serial.println("--------------------------\n"); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Toggle both LEDs every 1 second + digitalToggle(LED_RED); + + delay(1000); +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/clearbonds/clearbonds.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/clearbonds/clearbonds.ino new file mode 100755 index 0000000..d6770d4 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/clearbonds/clearbonds.ino @@ -0,0 +1,54 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch remove the folder that contains the bonding information + * used by Bluefruit which is "/adafruit/bond" + */ + +#include <bluefruit.h> +#include <utility/bonding.h> + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Clear Bonds Example"); + Serial.println("-------------------------------\n"); + + Bluefruit.begin(); + + Serial.println(); + Serial.println("----- Before -----\n"); + bond_print_list(BLE_GAP_ROLE_PERIPH); + bond_print_list(BLE_GAP_ROLE_CENTRAL); + + Bluefruit.clearBonds(); + Bluefruit.Central.clearBonds(); + + Serial.println(); + Serial.println("----- After -----\n"); + + bond_print_list(BLE_GAP_ROLE_PERIPH); + bond_print_list(BLE_GAP_ROLE_CENTRAL); +} + +void loop() +{ + // Toggle both LEDs every 1 second + digitalToggle(LED_RED); + + delay(1000); +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino new file mode 100755 index 0000000..ca145b1 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino @@ -0,0 +1,176 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch demonstrates the client Current Time Service using the + * BLEClientCts API(). After uploading, go to iOS setting and connect + * to Bluefruit52, and then press PAIR. + * + * Note: Currently only iOS act as a CTS server, Android does not. The + * easiest way to test this sketch is using an iOS device. + * + * Current Time Service info: + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml + */ + +#include <bluefruit.h> + +// BLE Client Current Time Service +BLEClientCts bleCTime; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 BLE Client Current Time Example"); + Serial.println("-------------------------------------------\n"); + + Serial.println("Go to iOS's Bluetooth settings and connect to Bluefruit52"); + Serial.println("It may appear up as 'Accessory' depending on your iOS version."); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure CTS client + bleCTime.begin(); + + // Callback invoked when iOS device time changes + // To test this go to Setting -> Date & Time -> Toggle Time Zone "Set Automatically" + // Or change the time manually etc ... + bleCTime.setAdjustCallback(cts_adjust_callback); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_GENERIC_CLOCK); + + // Include CTS client UUID + Bluefruit.Advertising.addService(bleCTime); + + // Includes name + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // If service is not yet discovered + if ( !bleCTime.discovered() && !Bluefruit.connPaired() ) return; + + // Get Time from iOS once per second + // Note it is not advised to update this quickly + // Application should use local clock and update time after + // a long period (e.g an hour or day) + bleCTime.getCurrentTime(); + + printTime(); + + delay(1000); +} + +void connect_callback(uint16_t conn_handle) +{ + Serial.println("Connected"); + + Serial.print("Discovering CTS ... "); + if ( bleCTime.discover(conn_handle) ) + { + Serial.println("Discovered"); + + // iOS requires pairing to work, it makes sense to request security here as well + Serial.print("Attempting to PAIR with the iOS device, please press PAIR on your phone ... "); + if ( Bluefruit.requestPairing() ) + { + Serial.println("Done"); + Serial.println("Enabling Time Adjust Notify"); + bleCTime.enableAdjust(); + + Serial.print("Get Current Time chars value"); + bleCTime.getCurrentTime(); + + Serial.print("Get Local Time Info chars value"); + bleCTime.getLocalTimeInfo(); + + Serial.println(); + } + + Serial.println(); + } +} + +void cts_adjust_callback(uint8_t reason) +{ + const char * reason_str[] = { "Manual", "External Reference", "Change of Time Zone", "Change of DST" }; + + Serial.println("iOS Device time changed due to "); + Serial.println( reason_str[reason] ); +} + +void printTime(void) +{ + const char * day_of_week_str[] = { "n/a", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; + + Serial.printf("%04d-%02d-%02d ", bleCTime.Time.year, bleCTime.Time.month, bleCTime.Time.day); + Serial.printf("%02d:%02d:%02d ", bleCTime.Time.hour, bleCTime.Time.minute, bleCTime.Time.second); + Serial.print(day_of_week_str[bleCTime.Time.weekday]); + + int utc_offset = bleCTime.LocalInfo.timezone*15; // in 15 minutes unit + Serial.printf(" (UTC %+d:%02d, ", utc_offset/60, utc_offset%60); + Serial.printf("DST %+.1f)", ((float) bleCTime.LocalInfo.dst_offset*15)/60 ); + Serial.println(); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) reason; + + Serial.println(); + Serial.println("Disconnected"); +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/client_cts_oled/client_cts_oled.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/client_cts_oled/client_cts_oled.ino new file mode 100755 index 0000000..f9a220c --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/client_cts_oled/client_cts_oled.ino @@ -0,0 +1,193 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch demonstrates the client Current Time Service using the + * BLEClientCts API(). After uploading, go to iOS setting and connect + * to Bluefruit52, and then press PAIR. + * + * Note: Currently only iOS act as a CTS server, Android does not. The + * easiest way to test this sketch is using an iOS device. + * + * This sketch is similar to client_cts but also uses a Feather OLED Wing + * to display time https://www.adafruit.com/product/2900 + * + * Current Time Service info: + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml + */ + +#include <bluefruit.h> +#include <Adafruit_GFX.h> +#include <Adafruit_SSD1306.h> + +#define OLED_RESET 4 +Adafruit_SSD1306 oled(OLED_RESET); + +// BLE Client Current Time Service +BLEClientCts bleCTime; + +void setup() +{ + // init with the I2C addr 0x3C (for the 128x32) and show splashscreen + oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); + oled.display(); + + oled.setTextSize(1);// max is 4 line, 21 chars each + oled.setTextColor(WHITE); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure CTS client + bleCTime.begin(); + + // Set up and start advertising + startAdv(); + + // splash screen effect + delay(100); + + oled.clearDisplay(); + oled.setCursor(0, 0); + oled.println("Waiting to connect"); + oled.display(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_GENERIC_CLOCK); + + // Include CTS client UUID + Bluefruit.Advertising.addService(bleCTime); + + // Includes name + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // If service is not yet discovered + if ( !bleCTime.discovered() && !Bluefruit.connPaired() ) return; + + // Get Time from iOS once per second + // Note it is not advised to update this quickly + // Application should use local clock and update time after + // a long period (e.g an hour or day) + bleCTime.getCurrentTime(); + bleCTime.getLocalTimeInfo(); + + printTime(); + + delay(1000); +} + +void connect_callback(uint16_t conn_handle) +{ + oled.clearDisplay(); + oled.setCursor(0, 0); + oled.println("Connected."); + oled.print("Discovering ... "); + oled.display(); + + if ( bleCTime.discover(conn_handle) ) + { + oled.println("OK"); + + // ANCS requires pairing to work + oled.print("Paring ... "); + + oled.display(); + + if ( Bluefruit.requestPairing() ) + { + oled.println("OK"); + + bleCTime.enableAdjust(); + + oled.println("Receiving Time..."); + bleCTime.getCurrentTime(); + bleCTime.getLocalTimeInfo(); + } + } + + oled.display(); +} + +void printTime(void) +{ + const char * day_of_week_str[] = { "n/a", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; + const char * month_str[] = { "na", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + + oled.clearDisplay(); + oled.setCursor(0, 0); + + oled.print(day_of_week_str[bleCTime.Time.weekday]); + oled.printf(" %s %02d %04d\n", month_str[bleCTime.Time.month], bleCTime.Time.day, bleCTime.Time.year); + + + oled.setTextSize(2); + oled.printf(" %02d:%02d:%02d\n", bleCTime.Time.hour, bleCTime.Time.minute, bleCTime.Time.second); + + int utc_offset = bleCTime.LocalInfo.timezone*15; // in 15 minutes unit + + oled.setTextSize(1); + oled.printf("UTC %+d:%02d, ", utc_offset/60, utc_offset%60); + oled.printf("DST %+.1f", ((float) bleCTime.LocalInfo.dst_offset*15)/60 ); + oled.println(); + + oled.display(); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) reason; + + oled.clearDisplay(); + oled.setCursor(0, 0); + oled.println("Waiting to connect"); + oled.display(); +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/controller/controller.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/controller/controller.ino new file mode 100755 index 0000000..5db6696 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/controller/controller.ino @@ -0,0 +1,182 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include <bluefruit.h> + +BLEUart bleuart; + +// Function prototypes for packetparser.cpp +uint8_t readPacket (BLEUart *ble_uart, uint16_t timeout); +float parsefloat (uint8_t *buffer); +void printHex (const uint8_t * data, const uint32_t numBytes); + +// Packet buffer +extern uint8_t packetbuffer[]; + +void setup(void) +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println(F("Adafruit Bluefruit52 Controller App Example")); + Serial.println(F("-------------------------------------------")); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // Configure and start the BLE Uart service + bleuart.begin(); + + // Set up and start advertising + startAdv(); + + Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode")); + Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!")); + Serial.println(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include the BLE UART (AKA 'NUS') 128-bit UUID + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +/**************************************************************************/ +/*! + @brief Constantly poll for new command or response data +*/ +/**************************************************************************/ +void loop(void) +{ + // Wait for new data to arrive + uint8_t len = readPacket(&bleuart, 500); + if (len == 0) return; + + // Got a packet! + // printHex(packetbuffer, len); + + // Color + if (packetbuffer[1] == 'C') { + uint8_t red = packetbuffer[2]; + uint8_t green = packetbuffer[3]; + uint8_t blue = packetbuffer[4]; + Serial.print ("RGB #"); + if (red < 0x10) Serial.print("0"); + Serial.print(red, HEX); + if (green < 0x10) Serial.print("0"); + Serial.print(green, HEX); + if (blue < 0x10) Serial.print("0"); + Serial.println(blue, HEX); + } + + // Buttons + if (packetbuffer[1] == 'B') { + uint8_t buttnum = packetbuffer[2] - '0'; + boolean pressed = packetbuffer[3] - '0'; + Serial.print ("Button "); Serial.print(buttnum); + if (pressed) { + Serial.println(" pressed"); + } else { + Serial.println(" released"); + } + } + + // GPS Location + if (packetbuffer[1] == 'L') { + float lat, lon, alt; + lat = parsefloat(packetbuffer+2); + lon = parsefloat(packetbuffer+6); + alt = parsefloat(packetbuffer+10); + Serial.print("GPS Location\t"); + Serial.print("Lat: "); Serial.print(lat, 4); // 4 digits of precision! + Serial.print('\t'); + Serial.print("Lon: "); Serial.print(lon, 4); // 4 digits of precision! + Serial.print('\t'); + Serial.print(alt, 4); Serial.println(" meters"); + } + + // Accelerometer + if (packetbuffer[1] == 'A') { + float x, y, z; + x = parsefloat(packetbuffer+2); + y = parsefloat(packetbuffer+6); + z = parsefloat(packetbuffer+10); + Serial.print("Accel\t"); + Serial.print(x); Serial.print('\t'); + Serial.print(y); Serial.print('\t'); + Serial.print(z); Serial.println(); + } + + // Magnetometer + if (packetbuffer[1] == 'M') { + float x, y, z; + x = parsefloat(packetbuffer+2); + y = parsefloat(packetbuffer+6); + z = parsefloat(packetbuffer+10); + Serial.print("Mag\t"); + Serial.print(x); Serial.print('\t'); + Serial.print(y); Serial.print('\t'); + Serial.print(z); Serial.println(); + } + + // Gyroscope + if (packetbuffer[1] == 'G') { + float x, y, z; + x = parsefloat(packetbuffer+2); + y = parsefloat(packetbuffer+6); + z = parsefloat(packetbuffer+10); + Serial.print("Gyro\t"); + Serial.print(x); Serial.print('\t'); + Serial.print(y); Serial.print('\t'); + Serial.print(z); Serial.println(); + } + + // Quaternions + if (packetbuffer[1] == 'Q') { + float x, y, z, w; + x = parsefloat(packetbuffer+2); + y = parsefloat(packetbuffer+6); + z = parsefloat(packetbuffer+10); + w = parsefloat(packetbuffer+14); + Serial.print("Quat\t"); + Serial.print(x); Serial.print('\t'); + Serial.print(y); Serial.print('\t'); + Serial.print(z); Serial.print('\t'); + Serial.print(w); Serial.println(); + } +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/controller/packetParser.cpp b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/controller/packetParser.cpp new file mode 100755 index 0000000..8f0194f --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/controller/packetParser.cpp @@ -0,0 +1,134 @@ +#include <string.h> +#include <Arduino.h> +#include <bluefruit.h> + + +#define PACKET_ACC_LEN (15) +#define PACKET_GYRO_LEN (15) +#define PACKET_MAG_LEN (15) +#define PACKET_QUAT_LEN (19) +#define PACKET_BUTTON_LEN (5) +#define PACKET_COLOR_LEN (6) +#define PACKET_LOCATION_LEN (15) + +// READ_BUFSIZE Size of the read buffer for incoming packets +#define READ_BUFSIZE (20) + + +/* Buffer to hold incoming characters */ +uint8_t packetbuffer[READ_BUFSIZE+1]; + +/**************************************************************************/ +/*! + @brief Casts the four bytes at the specified address to a float +*/ +/**************************************************************************/ +float parsefloat(uint8_t *buffer) +{ + float f; + memcpy(&f, buffer, 4); + return f; +} + +/**************************************************************************/ +/*! + @brief Prints a hexadecimal value in plain characters + @param data Pointer to the byte data + @param numBytes Data length in bytes +*/ +/**************************************************************************/ +void printHex(const uint8_t * data, const uint32_t numBytes) +{ + uint32_t szPos; + for (szPos=0; szPos < numBytes; szPos++) + { + Serial.print(F("0x")); + // Append leading 0 for small values + if (data[szPos] <= 0xF) + { + Serial.print(F("0")); + Serial.print(data[szPos] & 0xf, HEX); + } + else + { + Serial.print(data[szPos] & 0xff, HEX); + } + // Add a trailing space if appropriate + if ((numBytes > 1) && (szPos != numBytes - 1)) + { + Serial.print(F(" ")); + } + } + Serial.println(); +} + +/**************************************************************************/ +/*! + @brief Waits for incoming data and parses it +*/ +/**************************************************************************/ +uint8_t readPacket(BLEUart *ble_uart, uint16_t timeout) +{ + uint16_t origtimeout = timeout, replyidx = 0; + + memset(packetbuffer, 0, READ_BUFSIZE); + + while (timeout--) { + if (replyidx >= 20) break; + if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) + break; + if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) + break; + if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN)) + break; + if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN)) + break; + if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN)) + break; + if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN)) + break; + if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN)) + break; + + while (ble_uart->available()) { + char c = ble_uart->read(); + if (c == '!') { + replyidx = 0; + } + packetbuffer[replyidx] = c; + replyidx++; + timeout = origtimeout; + } + + if (timeout == 0) break; + delay(1); + } + + packetbuffer[replyidx] = 0; // null term + + if (!replyidx) // no data or timeout + return 0; + if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning + return 0; + + // check checksum! + uint8_t xsum = 0; + uint8_t checksum = packetbuffer[replyidx-1]; + + for (uint8_t i=0; i<replyidx-1; i++) { + xsum += packetbuffer[i]; + } + xsum = ~xsum; + + // Throw an error message if the checksum's don't match + if (xsum != checksum) + { + Serial.print("Checksum mismatch in packet : "); + printHex(packetbuffer, replyidx+1); + return 0; + } + + // checksum passed! + return replyidx; +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_hrm/custom_hrm.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_hrm/custom_hrm.ino new file mode 100755 index 0000000..ea7f63d --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_hrm/custom_hrm.ino @@ -0,0 +1,224 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include <bluefruit.h> + +/* HRM Service Definitions + * Heart Rate Monitor Service: 0x180D + * Heart Rate Measurement Char: 0x2A37 + * Body Sensor Location Char: 0x2A38 + */ +BLEService hrms = BLEService(UUID16_SVC_HEART_RATE); +BLECharacteristic hrmc = BLECharacteristic(UUID16_CHR_HEART_RATE_MEASUREMENT); +BLECharacteristic bslc = BLECharacteristic(UUID16_CHR_BODY_SENSOR_LOCATION); + +BLEDis bledis; // DIS (Device Information Service) helper class instance +BLEBas blebas; // BAS (Battery Service) helper class instance + +uint8_t bps = 0; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 HRM Example"); + Serial.println("-----------------------\n"); + + // Initialise the Bluefruit module + Serial.println("Initialise the Bluefruit nRF52 module"); + Bluefruit.begin(); + + // Set the advertised device name (keep it short!) + Serial.println("Setting Device Name to 'Feather52 HRM'"); + Bluefruit.setName("Bluefruit52 HRM"); + + // Set the connect/disconnect callback handlers + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure and Start the Device Information Service + Serial.println("Configuring the Device Information Service"); + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Start the BLE Battery Service and set it to 100% + Serial.println("Configuring the Battery Service"); + blebas.begin(); + blebas.write(100); + + // Setup the Heart Rate Monitor service using + // BLEService and BLECharacteristic classes + Serial.println("Configuring the Heart Rate Monitor Service"); + setupHRM(); + + // Setup the advertising packet(s) + Serial.println("Setting up the advertising payload(s)"); + startAdv(); + + Serial.println("Ready Player One!!!"); + Serial.println("\nAdvertising"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include HRM Service UUID + Bluefruit.Advertising.addService(hrms); + + // Include Name + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void setupHRM(void) +{ + // Configure the Heart Rate Monitor service + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.heart_rate.xml + // Supported Characteristics: + // Name UUID Requirement Properties + // ---------------------------- ------ ----------- ---------- + // Heart Rate Measurement 0x2A37 Mandatory Notify + // Body Sensor Location 0x2A38 Optional Read + // Heart Rate Control Point 0x2A39 Conditional Write <-- Not used here + hrms.begin(); + + // Note: You must call .begin() on the BLEService before calling .begin() on + // any characteristic(s) within that service definition.. Calling .begin() on + // a BLECharacteristic will cause it to be added to the last BLEService that + // was 'begin()'ed! + + // Configure the Heart Rate Measurement characteristic + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.heart_rate_measurement.xml + // Properties = Notify + // Min Len = 1 + // Max Len = 8 + // B0 = UINT8 - Flag (MANDATORY) + // b5:7 = Reserved + // b4 = RR-Internal (0 = Not present, 1 = Present) + // b3 = Energy expended status (0 = Not present, 1 = Present) + // b1:2 = Sensor contact status (0+1 = Not supported, 2 = Supported but contact not detected, 3 = Supported and detected) + // b0 = Value format (0 = UINT8, 1 = UINT16) + // B1 = UINT8 - 8-bit heart rate measurement value in BPM + // B2:3 = UINT16 - 16-bit heart rate measurement value in BPM + // B4:5 = UINT16 - Energy expended in joules + // B6:7 = UINT16 - RR Internal (1/1024 second resolution) + hrmc.setProperties(CHR_PROPS_NOTIFY); + hrmc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + hrmc.setFixedLen(2); + hrmc.setCccdWriteCallback(cccd_callback); // Optionally capture CCCD updates + hrmc.begin(); + uint8_t hrmdata[2] = { 0b00000110, 0x40 }; // Set the characteristic to use 8-bit values, with the sensor connected and detected + hrmc.notify(hrmdata, 2); // Use .notify instead of .write! + + // Configure the Body Sensor Location characteristic + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml + // Properties = Read + // Min Len = 1 + // Max Len = 1 + // B0 = UINT8 - Body Sensor Location + // 0 = Other + // 1 = Chest + // 2 = Wrist + // 3 = Finger + // 4 = Hand + // 5 = Ear Lobe + // 6 = Foot + // 7:255 = Reserved + bslc.setProperties(CHR_PROPS_READ); + bslc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + bslc.setFixedLen(1); + bslc.begin(); + bslc.write8(2); // Set the characteristic to 'Wrist' (2) +} + +void connect_callback(uint16_t conn_handle) +{ + char central_name[32] = { 0 }; + Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println("Disconnected"); + Serial.println("Advertising!"); +} + +void cccd_callback(BLECharacteristic& chr, uint16_t cccd_value) +{ + // Display the raw request packet + Serial.print("CCCD Updated: "); + //Serial.printBuffer(request->data, request->len); + Serial.print(cccd_value); + Serial.println(""); + + // Check the characteristic this CCCD update is associated with in case + // this handler is used for multiple CCCD records. + if (chr.uuid == hrmc.uuid) { + if (chr.notifyEnabled()) { + Serial.println("Heart Rate Measurement 'Notify' enabled"); + } else { + Serial.println("Heart Rate Measurement 'Notify' disabled"); + } + } +} + +void loop() +{ + digitalToggle(LED_RED); + + if ( Bluefruit.connected() ) { + uint8_t hrmdata[2] = { 0b00000110, bps++ }; // Sensor connected, increment BPS value + + // Note: We use .notify instead of .write! + // If it is connected but CCCD is not enabled + // The characteristic's value is still updated although notification is not sent + if ( hrmc.notify(hrmdata, sizeof(hrmdata)) ){ + Serial.print("Heart Rate Measurement updated to: "); Serial.println(bps); + }else{ + Serial.println("ERROR: Notify not set in the CCCD or not connected!"); + } + } + + // Only send update once per second + delay(1000); +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp new file mode 100755 index 0000000..6b372bd --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp @@ -0,0 +1,106 @@ +/**************************************************************************/ +/*! + @file IEEE11073float.h +*/ +/**************************************************************************/ + +/** + * \file bytelib.c + * \brief Byte manipulation module implementation. + * Copyright (C) 2010 Signove Tecnologia Corporation. + * All rights reserved. + * Contact: Signove Tecnologia Corporation (contact@signove.com) + * + * $LICENSE_TEXT:BEGIN$ + * 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 and appearing + * in the file LICENSE included in the packaging of this file; 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * $LICENSE_TEXT:END$ + * + * \author Walter Guerra, Mateus Lima + * \date Jun 14, 2010 + */ + +#include <Arduino.h> +#include "IEEE11073float.h" + +uint32_t float2IEEE11073(double data, uint8_t output[4]) +{ + uint32_t result = MDER_NaN; + + + if (isnan(data)) { + goto finally; + }/* else if (data > MDER_FLOAT_MAX) { + result = MDER_POSITIVE_INFINITY; + goto finally; + } else if (data < MDER_FLOAT_MIN) { + result = MDER_NEGATIVE_INFINITY; + goto finally; + } else if (data >= -MDER_FLOAT_EPSILON && + data <= MDER_FLOAT_EPSILON) { + result = 0; + goto finally; + }*/ + + double sgn; sgn = data > 0 ? +1 : -1; + double mantissa; mantissa = fabs(data); + int32_t exponent; exponent = 0; // Note: 10**x exponent, not 2**x + + // scale up if number is too big + while (mantissa > MDER_FLOAT_MANTISSA_MAX) { + mantissa /= 10.0; + ++exponent; + if (exponent > MDER_FLOAT_EXPONENT_MAX) { + // argh, should not happen + if (sgn > 0) { + result = MDER_POSITIVE_INFINITY; + } else { + result = MDER_NEGATIVE_INFINITY; + } + goto finally; + } + } + + // scale down if number is too small + while (mantissa < 1) { + mantissa *= 10; + --exponent; + if (exponent < MDER_FLOAT_EXPONENT_MIN) { + // argh, should not happen + result = 0; + goto finally; + } + } + + // scale down if number needs more precision + double smantissa; smantissa = round(mantissa * MDER_FLOAT_PRECISION); + double rmantissa; rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; + double mdiff; mdiff = abs(smantissa - rmantissa); + while (mdiff > 0.5 && exponent > MDER_FLOAT_EXPONENT_MIN && + (mantissa * 10) <= MDER_FLOAT_MANTISSA_MAX) { + mantissa *= 10; + --exponent; + smantissa = round(mantissa * MDER_FLOAT_PRECISION); + rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; + mdiff = abs(smantissa - rmantissa); + } + + uint32_t int_mantissa; int_mantissa = (int) round(sgn * mantissa); + result = (exponent << 24) | (int_mantissa & 0xFFFFFF); + +finally: + if ( output ) memcpy(output, &result, 4); + return result; +}
diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.h b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.h new file mode 100755 index 0000000..b5dec8f --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.h @@ -0,0 +1,89 @@ +/**************************************************************************/ +/*! + @file IEEE11073float.h +*/ +/**************************************************************************/ + +/** + * \file bytelib.c + * \brief Byte manipulation module implementation. + * Copyright (C) 2010 Signove Tecnologia Corporation. + * All rights reserved. + * Contact: Signove Tecnologia Corporation (contact@signove.com) + * + * $LICENSE_TEXT:BEGIN$ + * 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 and appearing + * in the file LICENSE included in the packaging of this file; 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * $LICENSE_TEXT:END$ + * + * \author Walter Guerra, Mateus Lima + * \date Jun 14, 2010 + */ + +#ifndef _IEEE11073FLOAT_H_ +#define _IEEE11073FLOAT_H_ + +#include <stdint.h> + +typedef enum { + MDER_POSITIVE_INFINITY = 0x007FFFFE, + MDER_NaN = 0x007FFFFF, + MDER_NRes = 0x00800000, + MDER_RESERVED_VALUE = 0x00800001, + MDER_NEGATIVE_INFINITY = 0x00800002 +} ReservedFloatValues; +static const int32_t FIRST_RESERVED_VALUE = MDER_POSITIVE_INFINITY; + +// (2 ** 23 - 3) +#define MDER_FLOAT_MANTISSA_MAX 0x007FFFFD +// 2 ** 7 - 1 +#define MDER_FLOAT_EXPONENT_MAX 127 +#define MDER_FLOAT_EXPONENT_MIN -128 +// (2 ** 23 - 3) * 10 ** 127 +#define MDER_FLOAT_MAX 8.388604999999999e+133 +// -(2 ** 23 - 3) * 10 ** 127 +#define MDER_FLOAT_MIN (-MDER_FLOAT_MAX) +// 10 ** -128 +#define MDER_FLOAT_EPSILON 1e-128 +// 10 ** upper(23 * log(2) / log(10)) +// precision for a number 1.0000xxx +#define MDER_FLOAT_PRECISION 10000000 + +typedef enum { + MDER_S_POSITIVE_INFINITY = 0x07FE, + MDER_S_NaN = 0x07FF, + MDER_S_NRes = 0x0800, + MDER_S_RESERVED_VALUE = 0x0801, + MDER_S_NEGATIVE_INFINITY = 0x0802 +} ReservedSFloatValues; +static const uint32_t FIRST_S_RESERVED_VALUE = MDER_S_POSITIVE_INFINITY; + +// (2 ** 11 - 3) +#define MDER_SFLOAT_MANTISSA_MAX 0x07FD +// 2 ** 3 - 1 +#define MDER_SFLOAT_EXPONENT_MAX 7 +#define MDER_SFLOAT_EXPONENT_MIN -8 +// (2 ** 11 - 3) * 10 ** 7 +#define MDER_SFLOAT_MAX 20450000000.0 +// -(2 ** 11 - 3) * 10 ** 7 +#define MDER_SFLOAT_MIN (-MDER_SFLOAT_MAX) +// 10 ** -8 +#define MDER_SFLOAT_EPSILON 1e-8 +// 10 ** upper(11 * log(2) / log(10)) +#define MDER_SFLOAT_PRECISION 10000 + +uint32_t float2IEEE11073(double data, uint8_t output[4]); + +#endif /* _IEEE11073FLOAT_H_ */
diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/custom_htm.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/custom_htm.ino new file mode 100755 index 0000000..ba360fc --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/custom_htm.ino @@ -0,0 +1,219 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include <bluefruit.h> +#include "IEEE11073float.h" + +/* Health Thermometer Service Definitions + * Health Thermometer Service: 0x1809 + * Temperature Measurement Char: 0x2A1C + */ +BLEService htms = BLEService(UUID16_SVC_HEALTH_THERMOMETER); +BLECharacteristic htmc = BLECharacteristic(UUID16_CHR_TEMPERATURE_MEASUREMENT); + +BLEDis bledis; // DIS (Device Information Service) helper class instance + +double tempvalue = 0; + +// Advanced function prototypes +void startAdv(void); +void setupHTM(void); +void connect_callback(uint16_t conn_handle); +void disconnect_callback(uint16_t conn_handle, uint8_t reason); + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Heath Thermometer Example"); + Serial.println("-------------------------------------\n"); + + // Initialise the Bluefruit module + Serial.println("Initialise the Bluefruit nRF52 module"); + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + + // Set the connect/disconnect callback handlers + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure and Start the Device Information Service + Serial.println("Configuring the Device Information Service"); + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin();; + + // Setup the Heath Thermometer service using + // BLEService and BLECharacteristic classes + Serial.println("Configuring the Heath Thermometer Service"); + setupHTM(); + + // Setup the advertising packet(s) + Serial.println("Setting up the advertising payload(s)"); + startAdv(); + + Serial.println("Ready Player One!!!"); + Serial.println("\nAdvertising"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include HTM Service UUID + Bluefruit.Advertising.addService(htms); + + // Include Name + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void setupHTM(void) +{ + // Configure the Health Thermometer service + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.health_thermometer.xml + // Supported Characteristics: + // Name UUID Requirement Properties + // ---------------------------- ------ ----------- ---------- + // Temperature Measurement 0x2A1C Mandatory Indicate + // + // Temperature Type 0x2A1D Optional Read <-- Not used here + // Intermediate Temperature 0x2A1E Optional Read, Notify <-- Not used here + // Measurement Interval 0x2A21 Optional Read, Write, Indicate <-- Not used here + htms.begin(); + + // Note: You must call .begin() on the BLEService before calling .begin() on + // any characteristic(s) within that service definition.. Calling .begin() on + // a BLECharacteristic will cause it to be added to the last BLEService that + // was 'begin()'ed! + + // Configure the Temperature Measurement characteristic + // See:https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.temperature_measurement.xml + // Properties = Indicte + // Min Len = 6 + // Max Len = 6 + // B0 = UINT8 - Flag (MANDATORY) + // b3:7 = Reserved + // b2 = Temperature Type Flag (0 = Not present, 1 = Present) + // b1 = Timestamp Flag (0 = Not present, 1 = Present) + // b0 = Unit Flag (0 = Celsius, 1 = Fahrenheit) + // B4:1 = FLOAT - IEEE-11073 32-bit FLOAT measurement value + // B5 = Temperature Type + htmc.setProperties(CHR_PROPS_INDICATE); + htmc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + htmc.setFixedLen(6); + htmc.setCccdWriteCallback(cccd_callback); // Optionally capture CCCD updates + htmc.begin(); + uint8_t htmdata[6] = { 0b00000101, 0, 0 ,0 ,0, 2 }; // Set the characteristic to use Fahrenheit, with type (body) but no timestamp field + htmc.write(htmdata, sizeof(htmdata)); // Use .write for init data + + // Temperature Type Value + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.temperature_type.xml + // B0 = UINT8 - Temperature Type + // 0 = Reserved + // 1 = Armpit + // 2 = Body (general) + // 3 = Ear (usually ear lobe) + // 4 = Finger + // 5 = Gastro-intestinal Tract + // 6 = Mouth + // 7 = Rectum + // 8 = Toe + // 9 = Tympanum (ear drum) + // 10:255 = Reserved +} + +void connect_callback(uint16_t conn_handle) +{ + char central_name[32] = { 0 }; + Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println("Disconnected"); + Serial.println("Advertising!"); +} + +void cccd_callback(BLECharacteristic& chr, uint16_t cccd_value) +{ + // Display the raw request packet + Serial.print("CCCD Updated: "); + //Serial.printBuffer(request->data, request->len); + Serial.print(cccd_value); + Serial.println(""); + + // Check the characteristic this CCCD update is associated with in case + // this handler is used for multiple CCCD records. + if (chr.uuid == htmc.uuid) { + if (chr.indicateEnabled()) { + Serial.println("Temperature Measurement 'Indicate' enabled"); + } else { + Serial.println("Temperature Measurement 'Indicate' disabled"); + } + } +} + +void loop() +{ + digitalToggle(LED_RED); + + if ( Bluefruit.connected() ) { + uint8_t htmdata[6] = { 0b00000101, 0,0,0,0, 2 }; // Fahrenheit unit, temperature type = body (2) + + float2IEEE11073(tempvalue, &htmdata[1]); + + // Note: We use .indicate instead of .write! + // If it is connected but CCCD is not enabled + // The characteristic's value is still updated although notification is not sent + if ( htmc.indicate(htmdata, sizeof(htmdata)) ){ + Serial.print("Temperature Measurement updated to: "); Serial.println(tempvalue); + }else{ + Serial.println("ERROR: Indicate not set in the CCCD or not connected!"); + } + + tempvalue += 0.5F; + } + + // Only send update once per second + delay(1000); +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/eddystone_url/eddystone_url.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/eddystone_url/eddystone_url.ino new file mode 100755 index 0000000..15b068f --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/eddystone_url/eddystone_url.ino @@ -0,0 +1,73 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include <bluefruit.h> + +#define URL "https://www.adafruit.com" + +// Create an EddyStone URL with rssi at 0m = -40 and URL as defined above +EddyStoneUrl eddyUrl(-40, URL); + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 EddyStone URL Example"); + Serial.println("---------------------------------\n"); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // Setup the advertising packet + startAdv(); + + Serial.println("Broadcasting EddyStone URL, open Google Physical Web app to test"); +} + +void startAdv(void) +{ + // Advertising packet + // Set the beacon payload using the BLEBeacon class populated + // earlier in this example + Bluefruit.Advertising.setBeacon(eddyUrl); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * Apple Beacon specs + * - Type: Non connectable, undirected + * - Fixed interval: 100 ms -> fast = slow = 100 ms + */ + //Bluefruit.Advertising.setType(BLE_GAP_ADV_TYPE_ADV_NONCONN_IND); + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(160, 160); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Toggle both LEDs every second + digitalToggle(LED_RED); + delay(1000); +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino new file mode 100755 index 0000000..1b056b4 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino @@ -0,0 +1,131 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* + * This sketch uses the HID Consumer Key API to send the Volume Down + * key when PIN_SHUTTER is grounded. This will cause your mobile device + * to capture a photo when you are in the camera app + */ +#include <bluefruit.h> + +BLEDis bledis; +BLEHidAdafruit blehid; + +#define PIN_SHUTTER A0 + +void setup() +{ + pinMode(PIN_SHUTTER, INPUT_PULLUP); + + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 HID Camera Shutter Example"); + Serial.println("--------------------------------------\n"); + + Serial.println(); + Serial.println("Go to your phone's Bluetooth settings to pair your device"); + Serial.println("then open the camera application"); + + Serial.println(); + Serial.printf("Set pin %d to GND to capture a photo\n", PIN_SHUTTER); + Serial.println(); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // Configure and start DIS (Device Information Service) + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather 52"); + bledis.begin(); + + /* Start BLE HID + * Note: Apple requires BLE devices to have a min connection interval >= 20m + * (The smaller the connection interval the faster we can send data). + * However, for HID and MIDI device Apple will accept a min connection + * interval as low as 11.25 ms. Therefore BLEHidAdafruit::begin() will try to + * set the min and max connection interval to 11.25 ms and 15 ms respectively + * for the best performance. + */ + blehid.begin(); + + /* Set connection interval (min, max) to your perferred value. + * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms + * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms + */ + /* Bluefruit.setConnInterval(9, 12); */ + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD); + + // Include BLE HID service + Bluefruit.Advertising.addService(blehid); + + // There is enough room for the dev name in the advertising packet + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Make sure you are connected and bonded/paired + if ( Bluefruit.connected() && Bluefruit.connPaired() ) + { + // Check if pin GND'ed + if ( digitalRead(PIN_SHUTTER) == 0 ) + { + // Turn on red LED when we start sending data + digitalWrite(LED_RED, 1); + + // Send the 'volume down' key press + // Check BLEHidGeneric.h for a list of valid consumer usage codes + blehid.consumerKeyPress(HID_USAGE_CONSUMER_VOLUME_DECREMENT); + + // Delay a bit between reports + delay(10); + + // Send key release + blehid.consumerKeyRelease(); + + // Turn off the red LED + digitalWrite(LED_RED, 0); + + // Delay to avoid constant capturing + delay(500); + } + } +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino new file mode 100755 index 0000000..9b44693 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino @@ -0,0 +1,146 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include <bluefruit.h> + +BLEDis bledis; +BLEHidAdafruit blehid; + +bool hasKeyPressed = false; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 HID Keyboard Example"); + Serial.println("--------------------------------\n"); + + Serial.println(); + Serial.println("Go to your phone's Bluetooth settings to pair your device"); + Serial.println("then open an application that accepts keyboard input"); + + Serial.println(); + Serial.println("Enter the character(s) to send:"); + Serial.println(); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather 52"); + bledis.begin(); + + /* Start BLE HID + * Note: Apple requires BLE device must have min connection interval >= 20m + * ( The smaller the connection interval the faster we could send data). + * However for HID and MIDI device, Apple could accept min connection interval + * up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max + * connection interval to 11.25 ms and 15 ms respectively for best performance. + */ + blehid.begin(); + + // Set callback for set LED from central + blehid.setKeyboardLedCallback(set_keyboard_led); + + /* Set connection interval (min, max) to your perferred value. + * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms + * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms + */ + /* Bluefruit.setConnInterval(9, 12); */ + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD); + + // Include BLE HID service + Bluefruit.Advertising.addService(blehid); + + // There is enough room for the dev name in the advertising packet + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Only send KeyRelease if previously pressed to avoid sending + // multiple keyRelease reports (that consume memory and bandwidth) + if ( hasKeyPressed ) + { + hasKeyPressed = false; + blehid.keyRelease(); + + // Delay a bit after a report + delay(5); + } + + if (Serial.available()) + { + char ch = (char) Serial.read(); + + // echo + Serial.write(ch); + + blehid.keyPress(ch); + hasKeyPressed = true; + + // Delay a bit after a report + delay(5); + } + + // Request CPU to enter low-power mode until an event/interrupt occurs + waitForEvent(); +} + +/** + * Callback invoked when received Set LED from central. + * Must be set previously with setKeyboardLedCallback() + * + * The LED bit map is as follows: (also defined by KEYBOARD_LED_* ) + * Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0) + */ +void set_keyboard_led(uint8_t led_bitmap) +{ + // light up Red Led if any bits is set + if ( led_bitmap ) + { + ledOn( LED_RED ); + } + else + { + ledOff( LED_RED ); + } +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino new file mode 100755 index 0000000..719b876 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino @@ -0,0 +1,194 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch demonstate how to use BLE HID to scan an array of pin + * and send its keycode. It is essentially an implementation of hid keyboard, + * useful reference if you want to make an BLE keyboard. + */ + +#include <bluefruit.h> + +BLEHidAdafruit blehid; + +// Array of pins and its keycode +// For keycode definition see BLEHidGeneric.h +uint8_t pins[] = { A0, A1, A2, A3, A4, A5 }; +uint8_t hidcode[] = { HID_KEY_A, HID_KEY_B, HID_KEY_C ,HID_KEY_D, HID_KEY_E, HID_KEY_F }; + +uint8_t pincount = sizeof(pins)/sizeof(pins[0]); + +// Modifier keys, only take cares of Shift +// ATL, CTRL, CMD keys are left for user excersie. +uint8_t shiftPin = 11; + +bool keyPressedPreviously = false; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 HID Keyscan Example"); + Serial.println("-------------------------------\n"); + + Serial.println(); + Serial.println("Go to your phone's Bluetooth settings to pair your device"); + Serial.println("then open an application that accepts keyboard input"); + + Serial.println(); + Serial.println("Wire configured Pin to GND to send key"); + Serial.println("Wire Shift Keky to GND if you want to send it in upper case"); + Serial.println(); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // set up pin as input + for (uint8_t i=0; i<pincount; i++) + { + pinMode(pins[i], INPUT_PULLUP); + } + + // set up modifier key + pinMode(shiftPin, INPUT_PULLUP); + + /* Start BLE HID + * Note: Apple requires BLE device must have min connection interval >= 20m + * ( The smaller the connection interval the faster we could send data). + * However for HID and MIDI device, Apple could accept min connection interval + * up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max + * connection interval to 11.25 ms and 15 ms respectively for best performance. + */ + blehid.begin(); + + // Set callback for set LED from central + blehid.setKeyboardLedCallback(set_keyboard_led); + + /* Set connection interval (min, max) to your perferred value. + * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms + * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms + */ + /* Bluefruit.setConnInterval(9, 12); */ + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD); + + // Include BLE HID service + Bluefruit.Advertising.addService(blehid); + + // There is enough room for the dev name in the advertising packet + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + /*-------------- Scan Pin Array and send report ---------------------*/ + bool anyKeyPressed = false; + + uint8_t modifier = 0; + uint8_t count=0; + uint8_t keycode[6] = { 0 }; + + // scan modifier key (only SHIFT), user implement ATL, CTRL, CMD if needed + if ( 0 == digitalRead(shiftPin) ) + { + modifier |= KEYBOARD_MODIFIER_LEFTSHIFT; + } + + // scan normal key and send report + for(uint8_t i=0; i < pincount; i++) + { + if ( 0 == digitalRead(pins[i]) ) + { + // if pin is active (low), add its hid code to key report + keycode[count++] = hidcode[i]; + + // 6 is max keycode per report + if ( count == 6) + { + blehid.keyboardReport(modifier, keycode); + + // reset report + count = 0; + memset(keycode, 0, 6); + } + + // used later + anyKeyPressed = true; + keyPressedPreviously = true; + } + } + + // Send any remaining keys (not accumulated up to 6) + if ( count ) + { + blehid.keyboardReport(modifier, keycode); + } + + // Send All-zero report to indicate there is no keys pressed + // Most of the time, it is, though we don't need to send zero report + // every loop(), only a key is pressed in previous loop() + if ( !anyKeyPressed && keyPressedPreviously ) + { + keyPressedPreviously = false; + blehid.keyRelease(); + } + + // Poll interval + delay(10); +} + +/** + * Callback invoked when received Set LED from central. + * Must be set previously with setKeyboardLedCallback() + * + * The LED bit map is as follows: (also defined by KEYBOARD_LED_* ) + * Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0) + */ +void set_keyboard_led(uint8_t led_bitmap) +{ + // light up Red Led if any bits is set + if ( led_bitmap ) + { + ledOn( LED_RED ); + } + else + { + ledOff( LED_RED ); + } +} diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino new file mode 100755 index 0000000..68dbf48 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino @@ -0,0 +1,146 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include <bluefruit.h> + +BLEDis bledis; +BLEHidAdafruit blehid; + +#define MOVE_STEP 10 + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 HID Mouse Example"); + Serial.println("-----------------------------\n"); + Serial.println("Go to your phone's Bluetooth settings to pair your device"); + Serial.println("then open an application that accepts mouse input"); + Serial.println(); + + Serial.println("Enter following characters"); + Serial.println("- 'WASD' to move mouse (up, left, down, right)"); + Serial.println("- 'LRMBF' to press mouse button(s) (left, right, middle, backward, forward)"); + Serial.println("- 'X' to release mouse button(s)"); + + Bluefruit.begin(); + // HID Device can have a min connection interval of 9*1.25 = 11.25 ms + Bluefruit.setConnInterval(9, 16); // min = 9*1.25=11.25 ms, max = 16*1.25=20ms + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather 52"); + bledis.begin(); + + // BLE HID + blehid.begin(); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_MOUSE); + + // Include BLE HID service + Bluefruit.Advertising.addService(blehid); + + // There is enough room for 'Name' in the advertising packet + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + if (Serial.available()) + { + char ch = (char) Serial.read(); + + // convert to upper case + ch = (char) toupper(ch); + + // echo + Serial.println(ch); + + switch(ch) + { + // WASD to move the mouse + case 'W': + blehid.mouseMove(0, -MOVE_STEP); + break; + + case 'A': + blehid.mouseMove(-MOVE_STEP, 0); + break; + + case 'S': + blehid.mouseMove(0, MOVE_STEP); + break; + + case 'D': + blehid.mouseMove(MOVE_STEP, 0); + break; + + // LRMBF for mouse button(s) + case 'L': + blehid.mouseButtonPress(MOUSE_BUTTON_LEFT); + break; + + case 'R': + blehid.mouseButtonPress(MOUSE_BUTTON_RIGHT); + break; + + case 'M': + blehid.mouseButtonPress(MOUSE_BUTTON_MIDDLE); + break; + + case 'B': + blehid.mouseButtonPress(MOUSE_BUTTON_BACKWARD); + break; + + case 'F': + // This key is not always supported by every OS + blehid.mouseButtonPress(MOUSE_BUTTON_FORWARD); + break; + + case 'X': + // X to release all buttons + blehid.mouseButtonRelease(); + break; + + default: break; + } + } +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/neomatrix/neomatrix.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/neomatrix/neomatrix.ino new file mode 100755 index 0000000..6462a29 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/neomatrix/neomatrix.ino @@ -0,0 +1,168 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* How to run this sketch + * - Connect the Neopixel FeatherWing and load this sketch to your Bluefruit52 + * - Connect to the board using the Bluefruit Connect LE app + * - Send character(s) using BLEUART + * - Bluefruit will render the received character(s) on Neopixel FeatherWing + * + * Note: due to the font being larger than the 4x8 Neopixel Wing, you can + * only see part of the characters in some cases. + * Run the sketch with a larger Neopixel Matrix for a complete demo + */ + +/* NOTE: This sketch required at least version 1.1.0 of Adafruit_Neopixel !!! */ + +#include <Arduino.h> +#include <Adafruit_NeoPixel.h> +#include <Adafruit_GFX.h> +#include <Adafruit_NeoMatrix.h> +#include <bluefruit.h> + +#define PIN 30 /* Pin used to drive the NeoPixels */ + +#define MATRIX_WIDTH 4 +#define MATRIX_HEIGHT 8 +#define MATRIX_LAYOUT (NEO_MATRIX_TOP + NEO_MATRIX_RIGHT + NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE) + +// MATRIX DECLARATION: +// Parameter 1 = width of NeoPixel matrix +// Parameter 2 = height of matrix +// Parameter 3 = pin number (most are valid) +// Parameter 4 = matrix layout flags, add together as needed: +// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT: +// Position of the FIRST LED in the matrix; pick two, e.g. +// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner. +// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal +// rows or in vertical columns, respectively; pick one or the other. +// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed +// in the same order, or alternate lines reverse direction; pick one. +// See example below for these values in action. +// Parameter 5 = pixel type flags, add together as needed: +// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) +// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) +// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) +// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) + +Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(MATRIX_WIDTH, MATRIX_HEIGHT, PIN, + MATRIX_LAYOUT, + NEO_GRB + NEO_KHZ800); + +// BLE Service +BLEDis bledis; +BLEUart bleuart; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Adafruit Bluefruit NeoMatrix"); + Serial.println("----------------------------"); + + Serial.println(); + Serial.println("Please connect using Bluefruit Connect LE application"); + + // Config Neopixels Matrix + matrix.begin(); + matrix.setTextWrap(false); + matrix.setBrightness(40); + matrix.setTextColor( matrix.Color(0, 0, 255) ); // Blue for Bluefruit + + matrix.print('?'); // print out ? + matrix.show(); + + // Init Bluefruit + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Configure and start BLE UART service + bleuart.begin(); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void connect_callback(uint16_t conn_handle) +{ + char central_name[32] = { 0 }; + Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); + + Serial.println("Please select 'Uart' tab and send any characters"); +} + + +void loop() +{ + // Echo received data + if ( Bluefruit.connected() && bleuart.notifyEnabled() ) + { + if ( bleuart.available() ) + { + char ch = (char) bleuart.read(); + + // skip newline + if ( ch != '\r' && ch != '\n' ) + { + matrix.fillScreen(0); + matrix.setCursor(0, 0); + matrix.print(ch); + + matrix.show(); + delay(200); + } + } + } +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino new file mode 100755 index 0000000..f016a29 --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino @@ -0,0 +1,350 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +// This sketch is intended to be used with the NeoPixel control +// surface in Adafruit's Bluefruit LE Connect mobile application. +// +// - Compile and flash this sketch to the nRF52 Feather +// - Open the Bluefruit LE Connect app +// - Switch to the NeoPixel utility +// - Click the 'connect' button to establish a connection and +// send the meta-data about the pixel layout +// - Use the NeoPixel utility to update the pixels on your device + +/* NOTE: This sketch required at least version 1.1.0 of Adafruit_Neopixel !!! */ + +#include <Arduino.h> +#include <Adafruit_NeoPixel.h> +#include <bluefruit.h> + +#define NEOPIXEL_VERSION_STRING "Neopixel v2.0" +#define PIN 30 /* Pin used to drive the NeoPixels */ + +#define MAXCOMPONENTS 4 +uint8_t *pixelBuffer = NULL; +uint8_t width = 0; +uint8_t height = 0; +uint8_t stride; +uint8_t componentsValue; +bool is400Hz; +uint8_t components = 3; // only 3 and 4 are valid values + +Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(); + +// BLE Service +BLEDis bledis; +BLEUart bleuart; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Adafruit Bluefruit Neopixel Test"); + Serial.println("--------------------------------"); + + Serial.println(); + Serial.println("Please connect using the Bluefruit Connect LE application"); + + // Config Neopixels + neopixel.begin(); + + // Init Bluefruit + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Configure and start BLE UART service + bleuart.begin(); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void connect_callback(uint16_t conn_handle) +{ + char central_name[32] = { 0 }; + Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); + + Serial.println("Please select the 'Neopixels' tab, click 'Connect' and have fun"); +} + +void loop() +{ + // Echo received data + if ( Bluefruit.connected() && bleuart.notifyEnabled() ) + { + int command = bleuart.read(); + + switch (command) { + case 'V': { // Get Version + commandVersion(); + break; + } + + case 'S': { // Setup dimensions, components, stride... + commandSetup(); + break; + } + + case 'C': { // Clear with color + commandClearColor(); + break; + } + + case 'B': { // Set Brightness + commandSetBrightness(); + break; + } + + case 'P': { // Set Pixel + commandSetPixel(); + break; + } + + case 'I': { // Receive new image + commandImage(); + break; + } + + } + } +} + +void swapBuffers() +{ + uint8_t *base_addr = pixelBuffer; + int pixelIndex = 0; + for (int j = 0; j < height; j++) + { + for (int i = 0; i < width; i++) { + if (components == 3) { + neopixel.setPixelColor(pixelIndex, neopixel.Color(*base_addr, *(base_addr+1), *(base_addr+2))); + } + else { + neopixel.setPixelColor(pixelIndex, neopixel.Color(*base_addr, *(base_addr+1), *(base_addr+2), *(base_addr+3) )); + } + base_addr+=components; + pixelIndex++; + } + pixelIndex += stride - width; // Move pixelIndex to the next row (take into account the stride) + } + neopixel.show(); + +} + +void commandVersion() { + Serial.println(F("Command: Version check")); + sendResponse(NEOPIXEL_VERSION_STRING); +} + +void commandSetup() { + Serial.println(F("Command: Setup")); + + width = bleuart.read(); + height = bleuart.read(); + stride = bleuart.read(); + componentsValue = bleuart.read(); + is400Hz = bleuart.read(); + + neoPixelType pixelType; + pixelType = componentsValue + (is400Hz ? NEO_KHZ400 : NEO_KHZ800); + + components = (componentsValue == NEO_RGB || componentsValue == NEO_RBG || componentsValue == NEO_GRB || componentsValue == NEO_GBR || componentsValue == NEO_BRG || componentsValue == NEO_BGR) ? 3:4; + + Serial.printf("\tsize: %dx%d\n", width, height); + Serial.printf("\tstride: %d\n", stride); + Serial.printf("\tpixelType %d\n", pixelType); + Serial.printf("\tcomponents: %d\n", components); + + if (pixelBuffer != NULL) { + delete[] pixelBuffer; + } + + uint32_t size = width*height; + pixelBuffer = new uint8_t[size*components]; + neopixel.updateLength(size); + neopixel.updateType(pixelType); + neopixel.setPin(PIN); + + // Done + sendResponse("OK"); +} + +void commandSetBrightness() { + Serial.println(F("Command: SetBrightness")); + + // Read value + uint8_t brightness = bleuart.read(); + + // Set brightness + neopixel.setBrightness(brightness); + + // Refresh pixels + swapBuffers(); + + // Done + sendResponse("OK"); +} + +void commandClearColor() { + Serial.println(F("Command: ClearColor")); + + // Read color + uint8_t color[MAXCOMPONENTS]; + for (int j = 0; j < components;) { + if (bleuart.available()) { + color[j] = bleuart.read(); + j++; + } + } + + // Set all leds to color + int size = width * height; + uint8_t *base_addr = pixelBuffer; + for (int i = 0; i < size; i++) { + for (int j = 0; j < components; j++) { + *base_addr = color[j]; + base_addr++; + } + } + + // Swap buffers + Serial.println(F("ClearColor completed")); + swapBuffers(); + + + if (components == 3) { + Serial.printf("\tclear (%d, %d, %d)\n", color[0], color[1], color[2] ); + } + else { + Serial.printf("\tclear (%d, %d, %d, %d)\n", color[0], color[1], color[2], color[3] ); + } + + // Done + sendResponse("OK"); +} + +void commandSetPixel() { + Serial.println(F("Command: SetPixel")); + + // Read position + uint8_t x = bleuart.read(); + uint8_t y = bleuart.read(); + + // Read colors + uint32_t pixelOffset = y*width+x; + uint32_t pixelDataOffset = pixelOffset*components; + uint8_t *base_addr = pixelBuffer+pixelDataOffset; + for (int j = 0; j < components;) { + if (bleuart.available()) { + *base_addr = bleuart.read(); + base_addr++; + j++; + } + } + + // Set colors + uint32_t neopixelIndex = y*stride+x; + uint8_t *pixelBufferPointer = pixelBuffer + pixelDataOffset; + uint32_t color; + if (components == 3) { + color = neopixel.Color( *pixelBufferPointer, *(pixelBufferPointer+1), *(pixelBufferPointer+2) ); + Serial.printf("\tcolor (%d, %d, %d)\n",*pixelBufferPointer, *(pixelBufferPointer+1), *(pixelBufferPointer+2) ); + } + else { + color = neopixel.Color( *pixelBufferPointer, *(pixelBufferPointer+1), *(pixelBufferPointer+2), *(pixelBufferPointer+3) ); + Serial.printf("\tcolor (%d, %d, %d, %d)\n", *pixelBufferPointer, *(pixelBufferPointer+1), *(pixelBufferPointer+2), *(pixelBufferPointer+3) ); + } + neopixel.setPixelColor(neopixelIndex, color); + neopixel.show(); + + // Done + sendResponse("OK"); +} + +void commandImage() { + Serial.printf("Command: Image %dx%d, %d, %d\n", width, height, components, stride); + + // Receive new pixel buffer + int size = width * height; + uint8_t *base_addr = pixelBuffer; + for (int i = 0; i < size; i++) { + for (int j = 0; j < components;) { + if (bleuart.available()) { + *base_addr = bleuart.read(); + base_addr++; + j++; + } + } + +/* + if (components == 3) { + uint32_t index = i*components; + Serial.printf("\tp%d (%d, %d, %d)\n", i, pixelBuffer[index], pixelBuffer[index+1], pixelBuffer[index+2] ); + } + */ + } + + // Swap buffers + Serial.println(F("Image received")); + swapBuffers(); + + // Done + sendResponse("OK"); +} + +void sendResponse(char const *response) { + Serial.printf("Send Response: %s\n", response); + bleuart.write(response, strlen(response)*sizeof(char)); +} + diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino new file mode 100755 index 0000000..8f88f2a --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino @@ -0,0 +1,183 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include <bluefruit.h> + +// String to send in the throughput test +#define TEST_STRING "01234567899876543210" +const int TEST_STRLEN = strlen(TEST_STRING); + +// Number of total data sent ( 1024 times the test string) +#define TOTAL_BYTES (1024 * strlen(TEST_STRING)) + +BLEDis bledis; +BLEUart bleuart; + +/**************************************************************************/ +/*! + @brief Sets up the HW an the BLE module (this function is called + automatically on startup) +*/ +/**************************************************************************/ +void setup(void) +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Throughput Example"); + Serial.println("------------------------------\n"); + + // Setup the BLE LED to be enabled on CONNECT + // Note: This is actually the default behaviour, but provided + // here in case you want to control this manually via PIN 19 + Bluefruit.autoConnLed(true); + + Bluefruit.begin(); + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(4); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Configure and Start BLE Uart Service + bleuart.begin(); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // There is no room for Name in Advertising packet + // Use Scan response for Name + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void connect_callback(uint16_t conn_handle) +{ + (void) conn_handle; + Serial.println("Connected"); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println(); + Serial.println("Disconnected"); +} + +/**************************************************************************/ +/*! + @brief Constantly poll for new command or response data +*/ +/**************************************************************************/ +void loop(void) +{ + uint32_t start, stop, sent; + uint32_t remaining = TOTAL_BYTES; + start = stop = sent = 0; + + if (Bluefruit.connected() && bleuart.notifyEnabled()) + { + // Wait for user input before trying again + Serial.println("Connected. Send a key and press enter to start test"); + getUserInput(); + + Serial.print("Sending "); + Serial.print(remaining); + Serial.println(" bytes ..."); + + start = millis(); + while ( (remaining > 0) && Bluefruit.connected() && bleuart.notifyEnabled() ) + { + if ( !bleuart.print(TEST_STRING) ) break; + + sent += TEST_STRLEN; + remaining -= TEST_STRLEN; + + // Only print every 100th time + // if ( (sent % (100*TEST_STRLEN) ) == 0 ) + // { + // Serial.print("Sent: "); Serial.print(sent); + // Serial.print(" Remaining: "); Serial.println(remaining); + // } + } + stop = millis() - start; + + Serial.print("Sent "); + Serial.print(sent); + Serial.print(" bytes in "); + Serial.print(stop / 1000.0F, 2); + Serial.println(" seconds."); + + Serial.println("Speed "); + Serial.print( (sent / 1000.0F) / (stop / 1000.0F), 2); + Serial.println(" KB/s.\r\n"); + } +} + +/**************************************************************************/ +/*! + @brief Get user input from Serial +*/ +/**************************************************************************/ +char* getUserInput(void) +{ + static char inputs[64+1]; + memset(inputs, 0, sizeof(inputs)); + + // wait until data is available + while( Serial.available() == 0 ) { delay(1); } + + uint8_t count=0; + do + { + count += Serial.readBytes(inputs+count, 64); + } while( (count < 64) && Serial.available() ); + + return inputs; +} |