├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── Adafruit_ATParser.cpp ├── Adafruit_ATParser.h ├── Adafruit_BLE.cpp ├── Adafruit_BLE.h ├── Adafruit_BLEBattery.cpp ├── Adafruit_BLEBattery.h ├── Adafruit_BLEEddystone.cpp ├── Adafruit_BLEEddystone.h ├── Adafruit_BLEGatt.cpp ├── Adafruit_BLEGatt.h ├── Adafruit_BLEMIDI.cpp ├── Adafruit_BLEMIDI.h ├── Adafruit_BluefruitLE_SPI.cpp ├── Adafruit_BluefruitLE_SPI.h ├── Adafruit_BluefruitLE_UART.cpp ├── Adafruit_BluefruitLE_UART.h ├── README.md ├── SDEP.md ├── changelog.md ├── changelog_firmware.md ├── examples ├── atcommand │ ├── BluefruitConfig.h │ └── atcommand.ino ├── battery │ ├── BluefruitConfig.h │ └── battery.ino ├── beacon │ ├── BluefruitConfig.h │ └── beacon.ino ├── bleuart_cmdmode │ ├── BluefruitConfig.h │ └── bleuart_cmdmode.ino ├── bleuart_datamode │ ├── BluefruitConfig.h │ └── bleuart_datamode.ino ├── callbacks │ ├── BluefruitConfig.h │ └── callbacks.ino ├── callbacks_dfuirq │ ├── BluefruitConfig.h │ └── callbacks_dfuirq.ino ├── controller │ ├── BluefruitConfig.h │ ├── controller.ino │ └── packetParser.cpp ├── cplay_neopixel_picker │ ├── BluefruitConfig.h │ ├── cplay_neopixel_picker.ino │ └── packetParser.cpp ├── eddystone │ ├── BluefruitConfig.h │ └── eddystone.ino ├── factoryreset │ ├── BluefruitConfig.h │ └── factoryreset.ino ├── feathertester │ ├── BluefruitConfig.h │ └── feathertester.ino ├── healththermometer │ ├── BluefruitConfig.h │ ├── IEEE11073float.cpp │ ├── IEEE11073float.h │ └── healththermometer.ino ├── heartratemonitor │ ├── BluefruitConfig.h │ └── heartratemonitor.ino ├── hidcontrolkey │ ├── BluefruitConfig.h │ └── hidcontrolkey.ino ├── hidkeyboard │ ├── BluefruitConfig.h │ └── hidkeyboard.ino ├── hidmouse │ ├── BluefruitConfig.h │ └── hidmouse.ino ├── midi │ ├── BluefruitConfig.h │ └── midi.ino ├── ndof_bno055 │ ├── BluefruitConfig.h │ ├── README.md │ └── ndof_bno055.ino ├── neopixel │ ├── BluefruitConfig.h │ └── neopixel.ino ├── neopixel_picker │ ├── BluefruitConfig.h │ ├── neopixel_picker.ino │ └── packetParser.cpp ├── nvmdata │ ├── BluefruitConfig.h │ └── nvmdata.ino ├── throughput │ ├── BluefruitConfig.h │ └── throughput.ino └── uribeacon │ ├── BluefruitConfig.h │ └── uribeacon.ino ├── keywords.txt ├── library.properties └── utility ├── Adafruit_FIFO.cpp ├── Adafruit_FIFO.h ├── TimeoutTimer.h ├── common_header.h ├── errors.h └── sdep.h /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /Adafruit_BLEBattery.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BLEBatterry.cpp 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #include "Adafruit_BLEBattery.h" 38 | 39 | /******************************************************************************/ 40 | /*! 41 | @brief Constructor 42 | */ 43 | /******************************************************************************/ 44 | Adafruit_BLEBattery::Adafruit_BLEBattery(Adafruit_BLE& ble) : 45 | _ble(ble) 46 | { 47 | 48 | } 49 | 50 | /******************************************************************************/ 51 | /*! 52 | @brief Enable Battery service if not already enabled 53 | @param reset true will reset Bluefruit 54 | */ 55 | /******************************************************************************/ 56 | bool Adafruit_BLEBattery::begin(bool reset) 57 | { 58 | int32_t enabled = 0; 59 | VERIFY_( _ble.atcommandIntReply( F("AT+BLEBATTEN"), &enabled) ); 60 | if ( enabled ) return true; 61 | 62 | VERIFY_( _ble.atcommand( F("AT+BLEBATTEN=1") ) ); 63 | 64 | // Perform Bluefruit reset if needed 65 | if (reset) _ble.reset(); 66 | 67 | return true; 68 | } 69 | 70 | /******************************************************************************/ 71 | /*! 72 | @brief Stop Battery service if it is enabled 73 | @param reset true will reset Bluefruit 74 | */ 75 | /******************************************************************************/ 76 | bool Adafruit_BLEBattery::stop(bool reset) 77 | { 78 | int32_t enabled = 0; 79 | VERIFY_( _ble.atcommandIntReply( F("AT+BLEBATTEN"), &enabled) ); 80 | if ( !enabled ) return true; 81 | 82 | VERIFY_( _ble.atcommand( F("AT+BLEBATTEN=0") ) ); 83 | 84 | // Perform Bluefruit reset if needed 85 | if (reset) _ble.reset(); 86 | 87 | return true; 88 | } 89 | 90 | /******************************************************************************/ 91 | /*! 92 | @brief Update Battery level value 93 | @param percent Battery value in percentage 0-100 94 | */ 95 | /******************************************************************************/ 96 | bool Adafruit_BLEBattery::update(uint8_t percent) 97 | { 98 | VERIFY_( is_within(0, percent, 100) ); 99 | return _ble.atcommand( F("AT+BLEBATTVAL"), percent ) ; 100 | } 101 | 102 | -------------------------------------------------------------------------------- /Adafruit_BLEBattery.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BLEBatterry.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #ifndef _ADAFRUIT_BLEBATTERY_H_ 38 | #define _ADAFRUIT_BLEBATTERY_H_ 39 | 40 | #include 41 | #include "Adafruit_BLE.h" 42 | 43 | class Adafruit_BLEBattery 44 | { 45 | private: 46 | Adafruit_BLE& _ble; 47 | 48 | public: 49 | Adafruit_BLEBattery(Adafruit_BLE& ble); 50 | 51 | bool begin(bool reset = true); 52 | bool stop (bool reset = true); 53 | 54 | bool update(uint8_t percent); 55 | }; 56 | 57 | #endif /* _ADAFRUIT_BLEBATTERY_H_ */ 58 | -------------------------------------------------------------------------------- /Adafruit_BLEEddystone.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BLEEddystone.cpp 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #include "Adafruit_BLEEddystone.h" 38 | 39 | #define EDDYSTONE_MINIMUM_FIRMWARE_VERSION "0.7.0" 40 | 41 | 42 | /******************************************************************************/ 43 | /*! 44 | @brief Constructor 45 | */ 46 | /******************************************************************************/ 47 | Adafruit_BLEEddystone::Adafruit_BLEEddystone(Adafruit_BLE& ble) : 48 | _ble(ble) 49 | { 50 | } 51 | 52 | /******************************************************************************/ 53 | /*! 54 | @brief Enable Eddystone service if not already enabled 55 | @param reset true will reset Bluefruit 56 | */ 57 | /******************************************************************************/ 58 | bool Adafruit_BLEEddystone::begin(bool reset) 59 | { 60 | VERIFY_( _ble.isVersionAtLeast(EDDYSTONE_MINIMUM_FIRMWARE_VERSION) ); 61 | 62 | int32_t enabled = 0; 63 | VERIFY_( _ble.atcommandIntReply( F("AT+EDDYSTONESERVICEEN"), &enabled) ); 64 | 65 | if ( enabled ) return true; 66 | VERIFY_( _ble.atcommand( F("AT+EDDYSTONESERVICEEN=1") ) ); 67 | 68 | // Perform Bluefruit reset if needed 69 | if (reset) _ble.reset(); 70 | 71 | return true; 72 | } 73 | 74 | /******************************************************************************/ 75 | /*! 76 | @brief Stop Eddystone service if it is enabled 77 | @param reset true will reset Bluefruit 78 | */ 79 | /******************************************************************************/ 80 | bool Adafruit_BLEEddystone::stop(bool reset) 81 | { 82 | int32_t enabled = 0; 83 | VERIFY_( _ble.atcommandIntReply( F("AT+EDDYSTONESERVICEEN"), &enabled) ); 84 | if ( !enabled ) return true; 85 | 86 | VERIFY_( _ble.atcommand( F("AT+EDDYSTONESERVICEEN=0") ) ); 87 | 88 | // Perform Bluefruit reset if needed 89 | if (reset) _ble.reset(); 90 | 91 | return true; 92 | } 93 | 94 | /******************************************************************************/ 95 | /*! 96 | @brief Change Bluefruit's URL setting in NVM 97 | @param url URL to be advertized 98 | @param broadcastEvenConnect Keep broadcasting even Bluefruit is connected 99 | @param rssi_at_0m RSSI value at 0m (check out EddyStone specs) 100 | */ 101 | /******************************************************************************/ 102 | bool Adafruit_BLEEddystone::setURL(const char* url, bool broadcastEvenConnect, int8_t rssi_at_0m) 103 | { 104 | bool result; 105 | uint8_t current_mode = _ble.getMode(); 106 | 107 | // switch mode if necessary to execute command 108 | if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_COMMAND); 109 | 110 | // send command and integer parameters separated by comma 111 | _ble.print( F("AT+EDDYSTONEURL=") ); 112 | _ble.print(url); 113 | 114 | _ble.print(','); _ble.print(broadcastEvenConnect, DEC); 115 | _ble.print(','); _ble.print(rssi_at_0m); 116 | 117 | _ble.println(); // execute command 118 | 119 | result = _ble.waitForOK(); 120 | 121 | // switch back if necessary 122 | if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_DATA); 123 | 124 | return result; 125 | } 126 | 127 | /******************************************************************************/ 128 | /*! 129 | @brief Start Broadcasting (advertising) specified URL 130 | */ 131 | /******************************************************************************/ 132 | bool Adafruit_BLEEddystone::startBroadcast(void) 133 | { 134 | return _ble.atcommand( F("AT+EDDYSTONEBROADCAST=1") ); 135 | } 136 | 137 | /******************************************************************************/ 138 | /*! 139 | @brief Stop Broadcasting (advertising) specified URL 140 | */ 141 | /******************************************************************************/ 142 | bool Adafruit_BLEEddystone::stopBroadcast(void) 143 | { 144 | return _ble.atcommand( F("AT+EDDYSTONEBROADCAST=0") ); 145 | } 146 | 147 | /******************************************************************************/ 148 | /*! 149 | @brief Broadcast (advertising) specified URL 150 | */ 151 | /******************************************************************************/ 152 | bool Adafruit_BLEEddystone::startConfigMode(uint32_t seconds) 153 | { 154 | return _ble.atcommand( F("AT+EDDYSTONECONFIGEN"), (int32_t) seconds ); 155 | } 156 | -------------------------------------------------------------------------------- /Adafruit_BLEEddystone.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BLEEddystone.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #ifndef _ADAFRUIT_BLEEDDYSTONE_H_ 38 | #define _ADAFRUIT_BLEEDDYSTONE_H_ 39 | 40 | #include 41 | #include "Adafruit_BLE.h" 42 | 43 | #define EDDYSTONE_DEFAULT_RSSI0M (-18) 44 | 45 | class Adafruit_BLEEddystone 46 | { 47 | private: 48 | Adafruit_BLE& _ble; 49 | 50 | public: 51 | Adafruit_BLEEddystone(Adafruit_BLE& ble); 52 | 53 | bool begin(bool reset = true); 54 | bool stop (bool reset = true); 55 | 56 | bool setURL(const char* url, bool broadcastEvenConnect = false, int8_t rssi_at_0m = EDDYSTONE_DEFAULT_RSSI0M); 57 | 58 | bool startBroadcast(void); 59 | bool stopBroadcast(void); 60 | 61 | bool startConfigMode(uint32_t seconds); 62 | 63 | }; 64 | 65 | #endif /* _ADAFRUIT_BLEEDDYSTONE_H_ */ 66 | -------------------------------------------------------------------------------- /Adafruit_BLEMIDI.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BLEMIDI.cpp 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #include "Adafruit_BLEMIDI.h" 38 | 39 | #define MIDI_MINIMUM_FIRMWARE_VERSION "0.7.0" 40 | 41 | /******************************************************************************/ 42 | /*! 43 | @brief Constructor 44 | */ 45 | /******************************************************************************/ 46 | Adafruit_BLEMIDI::Adafruit_BLEMIDI(Adafruit_BLE& ble) : 47 | _ble(ble) 48 | { 49 | } 50 | 51 | /******************************************************************************/ 52 | /*! 53 | @brief Set callback 54 | */ 55 | /******************************************************************************/ 56 | void Adafruit_BLEMIDI::setRxCallback(midiRxCallback_t fp) 57 | { 58 | _ble.setBleMidiRxCallback(fp); 59 | } 60 | 61 | 62 | /******************************************************************************/ 63 | /*! 64 | @brief Enable MIDI service if not already enabled 65 | @param reset true will reset Bluefruit 66 | */ 67 | /******************************************************************************/ 68 | bool Adafruit_BLEMIDI::begin(bool reset) 69 | { 70 | VERIFY_( _ble.isVersionAtLeast(MIDI_MINIMUM_FIRMWARE_VERSION) ); 71 | 72 | int32_t enabled = 0; 73 | VERIFY_( _ble.atcommandIntReply( F("AT+BLEMIDIEN"), &enabled) ); 74 | 75 | if ( enabled ) return true; 76 | VERIFY_( _ble.atcommand( F("AT+BLEMIDIEN=1") ) ); 77 | 78 | // Perform Bluefruit reset if needed 79 | if (reset) _ble.reset(); 80 | 81 | return true; 82 | } 83 | 84 | /******************************************************************************/ 85 | /*! 86 | @brief Stop MIDI service if it is enabled 87 | @param reset true will reset Bluefruit 88 | */ 89 | /******************************************************************************/ 90 | bool Adafruit_BLEMIDI::stop(bool reset) 91 | { 92 | int32_t enabled = 0; 93 | VERIFY_( _ble.atcommandIntReply( F("AT+BLEMIDIEN"), &enabled) ); 94 | if ( !enabled ) return true; 95 | 96 | VERIFY_( _ble.atcommand( F("AT+BLEMIDIEN=0") ) ); 97 | 98 | // Perform Bluefruit reset if needed 99 | if (reset) _ble.reset(); 100 | 101 | return true; 102 | } 103 | 104 | /******************************************************************************/ 105 | /*! 106 | @brief Send a MIDI event data 107 | @param bytes MIDI event data 108 | */ 109 | /******************************************************************************/ 110 | bool Adafruit_BLEMIDI::send(const uint8_t bytes[3]) 111 | { 112 | return _ble.atcommand( F("AT+BLEMIDITX"), bytes, 3); 113 | } 114 | 115 | /******************************************************************************/ 116 | /*! 117 | @brief Send multiple MIDI event which shared the same status 118 | @param status MIDI status 119 | @param bytes MIDI events data 120 | @param count number of data in bytes (must be multiple of 2) 121 | 122 | @note count + 1 must less than (20-3) --> count <= 16 123 | */ 124 | /******************************************************************************/ 125 | bool Adafruit_BLEMIDI::send_n(uint8_t status, const uint8_t bytes[], uint8_t count) 126 | { 127 | VERIFY_(count <= 16); 128 | 129 | uint8_t data[17] = { status }; 130 | memcpy(data+1, bytes, count); 131 | 132 | return _ble.atcommand( F("AT+BLEMIDITX"), data, count+1); 133 | } 134 | 135 | /******************************************************************************/ 136 | /*! 137 | @brief 138 | @param 139 | */ 140 | /******************************************************************************/ 141 | void Adafruit_BLEMIDI::processRxCallback(uint8_t data[], uint16_t len, Adafruit_BLE::midiRxCallback_t callback_func) 142 | { 143 | if ( len < 3 ) return; 144 | 145 | // First 3 bytes is always : Header + Timestamp + Status 146 | midi_header_t header; 147 | uint16_t tstamp = 0; 148 | uint8_t status = 0; 149 | 150 | header.byte = *data++; 151 | len--; 152 | 153 | while (len) 154 | { 155 | /* event : 0x00 - 0x7F 156 | status: 0x80 - 0xEF 157 | sysex : 0xF0 - 0xFF 158 | */ 159 | 160 | if ( bitRead(data[0], 7) ) 161 | { 162 | // Start of new full event 163 | midi_timestamp_t timestamp; 164 | timestamp.byte = *data++; 165 | 166 | tstamp = (header.timestamp_hi << 7) | timestamp.timestamp_low; 167 | status = *data++; 168 | 169 | // Status must have 7th-bit set, must have something wrong 170 | if ( !bitRead(status, 7) ) return; 171 | 172 | callback_func( tstamp, status, data[0], data[1]); 173 | 174 | len -= 4; 175 | data += 2; 176 | } 177 | else 178 | { 179 | // Running event 180 | callback_func( tstamp, status, data[0], data[1]); 181 | 182 | len -= 2; 183 | data += 2; 184 | } 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /Adafruit_BLEMIDI.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BLEMIDI.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #ifndef _ADAFRUIT_BLEMIDI_H_ 38 | #define _ADAFRUIT_BLEMIDI_H_ 39 | 40 | #include 41 | #include "Adafruit_BLE.h" 42 | 43 | typedef struct ATTR_PACKED 44 | { 45 | union { 46 | struct { 47 | uint8_t timestamp_hi : 6; 48 | uint8_t reserved : 1; 49 | uint8_t start_bit : 1; 50 | }; 51 | 52 | uint8_t byte; 53 | }; 54 | } midi_header_t; 55 | 56 | ASSERT_STATIC_ ( sizeof(midi_header_t) == 1 ); 57 | 58 | typedef struct ATTR_PACKED 59 | { 60 | union { 61 | struct { 62 | uint8_t timestamp_low : 7; 63 | uint8_t start_bit : 1; 64 | }; 65 | 66 | uint8_t byte; 67 | }; 68 | } midi_timestamp_t; 69 | 70 | ASSERT_STATIC_ ( sizeof(midi_timestamp_t) == 1 ); 71 | 72 | class Adafruit_BLEMIDI 73 | { 74 | private: 75 | Adafruit_BLE& _ble; 76 | 77 | public: 78 | typedef Adafruit_BLE::midiRxCallback_t midiRxCallback_t; 79 | Adafruit_BLEMIDI(Adafruit_BLE& ble); 80 | 81 | bool begin(bool reset = true); 82 | bool stop (bool reset = true); 83 | 84 | bool send(const uint8_t bytes[3]); 85 | 86 | bool send(uint8_t status, const uint8_t bytes[2]) 87 | { 88 | uint8_t buffer[3] = { status, bytes[0], bytes[1] }; 89 | return send(buffer); 90 | } 91 | 92 | bool send(uint8_t status, uint8_t byte1, uint8_t byte2) 93 | { 94 | uint8_t buffer[3] = { status, byte1, byte2 }; 95 | return send(buffer); 96 | } 97 | 98 | bool send_n(uint8_t status, const uint8_t bytes[], uint8_t count); 99 | 100 | void setRxCallback(midiRxCallback_t fp); 101 | 102 | static void processRxCallback(uint8_t data[], uint16_t len, Adafruit_BLE::midiRxCallback_t callback_func); 103 | }; 104 | 105 | #endif /* _ADAFRUIT_BLEMIDI_H_ */ 106 | -------------------------------------------------------------------------------- /Adafruit_BluefruitLE_SPI.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BluefruiLE_SPI.h 4 | @author hathach, ktown (Adafruit Industries) 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2015, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | #ifndef _ADAFRUIT_BLE_SPI_H_ 37 | #define _ADAFRUIT_BLE_SPI_H_ 38 | 39 | #include 40 | #include 41 | #include "utility/Adafruit_FIFO.h" 42 | 43 | #define SPI_CS_ENABLE() digitalWrite(m_cs_pin, LOW) 44 | #define SPI_CS_DISABLE() digitalWrite(m_cs_pin, HIGH) 45 | 46 | #define SPI_IGNORED_BYTE 0xFEu /**< SPI default character. Character clocked out in case of an ignored transaction. */ 47 | #define SPI_OVERREAD_BYTE 0xFFu /**< SPI over-read character. Character clocked out after an over-read of the transmit buffer. */ 48 | #define SPI_DEFAULT_DELAY_US 50 49 | 50 | #define memclr(buffer, size) memset(buffer, 0, size) 51 | 52 | 53 | class Adafruit_BluefruitLE_SPI : public Adafruit_BLE 54 | { 55 | private: 56 | // Hardware Pin 57 | int8_t m_cs_pin; 58 | int8_t m_irq_pin; 59 | int8_t m_rst_pin; 60 | 61 | // software SPI pins 62 | int8_t m_sck_pin; 63 | int8_t m_mosi_pin; 64 | int8_t m_miso_pin; 65 | 66 | // TX 67 | uint8_t m_tx_buffer[SDEP_MAX_PACKETSIZE]; 68 | uint8_t m_tx_count; 69 | 70 | // RX 71 | uint8_t m_rx_buffer[BLE_BUFSIZE]; 72 | Adafruit_FIFO m_rx_fifo; 73 | 74 | bool m_mode_switch_command_enabled; 75 | 76 | // Low level transportation I/O functions 77 | bool sendInitializePattern(void); 78 | bool sendPacket(uint16_t command, const uint8_t* buffer, uint8_t count, uint8_t more_data); 79 | bool getPacket(sdepMsgResponse_t* p_response); 80 | 81 | bool getResponse(void); 82 | void simulateSwitchMode(void); 83 | // bool handleSwitchCmdInDataMode(uint8_t ch); 84 | 85 | uint8_t spixfer(uint8_t x); 86 | void spixfer(void *x, size_t len); 87 | 88 | public: 89 | // Constructor 90 | Adafruit_BluefruitLE_SPI(int8_t csPin, int8_t irqPin, int8_t rstPin = -1); 91 | Adafruit_BluefruitLE_SPI(int8_t clkPin, int8_t misoPin, int8_t mosiPin, int8_t csPin, int8_t irqPin, int8_t rstPin); 92 | 93 | // HW initialisation 94 | bool begin(boolean v = false, boolean blocking = true); 95 | void end(void); 96 | 97 | bool setMode(uint8_t new_mode); 98 | void enableModeSwitchCommand(bool enabled); 99 | 100 | // Class Print virtual function Interface 101 | virtual size_t write(uint8_t c); 102 | virtual size_t write(const uint8_t *buffer, size_t size); 103 | 104 | // pull in write(str) and write(buf, size) from Print 105 | using Print::write; 106 | 107 | // Class Stream interface 108 | virtual int available(void); 109 | virtual int read(void); 110 | virtual void flush(void); 111 | virtual int peek(void); 112 | }; 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /Adafruit_BluefruitLE_UART.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_BluefruitLE_UART.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2015, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #ifndef _ADAFRUIT_BLE_UART_H_ 38 | #define _ADAFRUIT_BLE_UART_H_ 39 | 40 | #include "Arduino.h" 41 | #include 42 | 43 | #define SOFTWARE_SERIAL_AVAILABLE ( ! (defined (_VARIANT_ARDUINO_DUE_X_) || defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_STM32_FEATHER)) ) 44 | 45 | #if SOFTWARE_SERIAL_AVAILABLE 46 | #include 47 | #endif 48 | 49 | class Adafruit_BluefruitLE_UART : public Adafruit_BLE 50 | { 51 | private: 52 | // Hardware Pins 53 | int8_t _mode_pin, _cts_pin, _rts_pin; 54 | Stream *mySerial; 55 | #if SOFTWARE_SERIAL_AVAILABLE 56 | SoftwareSerial *ss; 57 | #endif 58 | HardwareSerial *hs; 59 | boolean _debug; 60 | uint8_t _intercharwritedelay; 61 | 62 | public: 63 | // Software Serial Constructor (0, 1, 2, or 3 pins) 64 | Adafruit_BluefruitLE_UART(HardwareSerial &port, 65 | int8_t mode_pin = -1, 66 | int8_t cts_pin = -1, 67 | int8_t rts_pin = -1); 68 | #if SOFTWARE_SERIAL_AVAILABLE 69 | Adafruit_BluefruitLE_UART(SoftwareSerial &port, 70 | int8_t mode_pin = -1, 71 | int8_t cts_pin = -1, 72 | int8_t rts_pin = -1); 73 | #endif 74 | 75 | void setInterCharWriteDelay(uint8_t x) { _intercharwritedelay = x; }; 76 | 77 | virtual ~Adafruit_BluefruitLE_UART(); 78 | 79 | // HW initialisation 80 | bool begin(boolean debug = false, boolean blocking = true); 81 | void end(void); 82 | 83 | bool setMode(uint8_t new_mode); 84 | 85 | // Class Print virtual function Interface 86 | virtual size_t write(uint8_t c); 87 | 88 | // pull in write(str) and write(buf, size) from Print 89 | using Print::write; 90 | 91 | // Class Stream interface 92 | virtual int available(void); 93 | virtual int read(void); 94 | virtual void flush(void); 95 | virtual int peek(void); 96 | }; 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This library is for all nRF51 based Adafruit Bluefruit LE modules that use SPI or UART. 2 | 3 | Current nRF51 based Bluefruit LE products include: 4 | 5 | * [Bluefruit LE Friend](https://www.adafruit.com/product/2267) 6 | * [Bluefruit LE UART Friend](https://www.adafruit.com/product/2479) 7 | * [Bluefruit LE SPI Friend](https://www.adafruit.com/product/2633) 8 | * [Bluefruit LE Shield](https://www.adafruit.com/products/2746) 9 | * [Bluefruit LE Micro](https://www.adafruit.com/product/2661) 10 | * [Feather 32u4 Bluefruit LE](https://www.adafruit.com/product/2829) 11 | * [Feather M0 Bluefruit LE](https://www.adafruit.com/products/2995) 12 | 13 | # AT Commands 14 | 15 | The Bluefruit LE modules this library talks to use AT-style commands and responses. 16 | 17 | If you are using a UART board, the commands are sent directly as text using a SW serial transport. 18 | 19 | If your are using an SPI board, the AT commands are wrapped in a thin **[SDEP](SDEP.md)** (Simple Data Exchange Protocol) wrapper to transmit and received text data over the binary SPI transport. Details of this SPI transport layer are detailed in [SDEP.md](SDEP.md) in this same folder. 20 | 21 | # Hardware Setup 22 | 23 | There are two variants of the nRF51 Bluefruit LE modules. One uses SPI to communicate, the other uses UART with flow control (TXD, RXD, CTS, RTS). The wiring you use will depend on the module you are trying to connect. 24 | 25 | On both boards, power should be connected as shown below: 26 | 27 | Bluefruit LE | Arduino Uno 28 | -------------|------------ 29 | VIN | 5V (assuming a 5V board) 30 | GND | GND 31 | 32 | ## Software UART Pinout 33 | 34 | If you are using a UART Bluefruit LE board, your Arduino should be connected to the Bluefruit LE UART module using the following pinout: 35 | 36 | Bluefruit LE UART | Arduino Uno 37 | ------------------|------------ 38 | RTS | 8 39 | RXI | 9 40 | TXO | 10 41 | CTS | 11 42 | 43 | Optional Pins 44 | 45 | Bluefruit LE UART | Arduino Uno 46 | ------------------|------------ 47 | MODE | 12 48 | 49 | ## SPI Pinout 50 | 51 | If you are using an SPI Bluefruit LE board, your Arduino should be connected to the Bluefruit LE SPI module using the following pinout: 52 | 53 | Bluefruit LE SPI | Arduino Uno 54 | -----------------|------------ 55 | SCLK | 13 56 | MISO | 12 57 | MOSI | 11 58 | CS | 8 59 | IRQ | 7 60 | 61 | Optional Pins (enable these in the sample sketches) 62 | 63 | Bluefruit LE SPI | Arduino Uno 64 | -----------------|------------ 65 | RESET | 6 66 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Arduino Changelog 2 | 3 | ## 1.10 4 | 5 | - Add non-blokcing option for .reset(blocking) & .factoryReset(blocking). The reset progress can be polled by .resetCompleted() 6 | - Add callback using dfu as irq pin example, .handleDfuIrq() is expected to be call in loop() 7 | 8 | ## 1.9 9 | 10 | ### Features 11 | 12 | - Added **Adafruit_ATParser** helper class to facilitate sending and receiving AT commands: 13 | - `.atcommand()` : Send a command without reply (several variants defined for various input parameters) 14 | - `.atcommandIntReply()` : Send a command with integer reply (several variants defined for various input parameters) 15 | - `.atcommand_full()` : General purpose command execution with a pointer to the reply buffer 16 | - `.printByteArray()` : Outputs a byte array in the `AA-BB-CC` format from a buffer. Useful for executing AT commands. 17 | - `.waitForOK()` : Uses a separate temporary buffer to avoid overwriting response content. 18 | - Callback support in **Adafruit_BLE** class 19 | - Supported Events are: 20 | - **Connect**: Set using `setConnectCallback()` 21 | - **Disconnect**: Set using `setDisconnectCallback()` 22 | - **BLE UART RX**: Set using `.setBleUartRxCallback()` 23 | - **MIDI RX**: Set using `.setBleMidiRxCallback()` 24 | - **GATT Characteristic RX**: Set using `.setBleGattRxCallback()` 25 | - `.update(ms)` must be placed in the loop() function to fire the callbacks, where `ms` is the interval in milliseconds to poll for new events 26 | - See 'examples/callbacks' for more details 27 | - Added **Adafruit_BLEGatt** helper class to make working with custom GATT services and characteristics easier: 28 | - Helpers to add custom GATT services and characteristics 29 | - Helpers to read/write previously defined characteristics 30 | - Callback support for characteristic updates 31 | - Added **User Description** and **Presentation Format** support for GATT characteristics 32 | - Add `BLEDataType_t` typedef for GATT characteristics 33 | - See 'example/healththermometer' for an example of using the Adafruit_BLEGatt class 34 | - Added BLE MIDI service support with the **Adafruit_BLEMIDI** class 35 | - See 'examples/midi' for details 36 | - Added BLE Battery service support via the **Adafruit_BLEBattery** class 37 | - See 'example/battery' for more details 38 | - Added BLE Eddystone helper class to facilitate using Eddystone beacon 39 | - See 'example/eddystone' for more details 40 | - Add a 256 byte user NVM data section that can be accessed via `.writeNVM()` and `.readNVM()` in **Adafruit_BLE**. User can use this small chunk of NVM memory to store application specific data. 41 | - See 'example/nvmdata' for more details 42 | - Additional **Adafruit_BLE** class changes: 43 | - Added a `setAdvData()` helper to advertise custom data payloads 44 | -------------------------------------------------------------------------------- /examples/atcommand/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 160 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/atcommand/atcommand.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | This is an example for our nRF51822 based Bluefruit LE modules 3 | 4 | Pick one up today in the adafruit shop! 5 | 6 | Adafruit invests time and resources providing this open source code, 7 | please support Adafruit and open-source hardware by purchasing 8 | products from Adafruit! 9 | 10 | MIT license, check LICENSE for more information 11 | All text above, and the splash screen below must be included in 12 | any redistribution 13 | *********************************************************************/ 14 | 15 | #include 16 | #include 17 | #include "Adafruit_BLE.h" 18 | #include "Adafruit_BluefruitLE_SPI.h" 19 | #include "Adafruit_BluefruitLE_UART.h" 20 | 21 | #include "BluefruitConfig.h" 22 | 23 | #if SOFTWARE_SERIAL_AVAILABLE 24 | #include 25 | #endif 26 | 27 | /*========================================================================= 28 | APPLICATION SETTINGS 29 | 30 | ? ? FACTORYRESET_ENABLE? ? Perform a factory reset when running this sketch 31 | ? ? 32 | ? ? Enabling this will put your Bluefruit LE module 33 | in a 'known good' state and clear any config 34 | data set in previous sketches or projects, so 35 | ? ? running this at least once is a good idea. 36 | ? ? 37 | ? ? When deploying your project, however, you will 38 | want to disable factory reset by setting this 39 | value to 0.? If you are making changes to your 40 | ? ? Bluefruit LE device via AT commands, and those 41 | changes aren't persisting across resets, this 42 | is the reason why.? Factory reset will erase 43 | the non-volatile memory where config data is 44 | stored, setting it back to factory default 45 | values. 46 | ? ? ? ? 47 | ? ? Some sketches that require you to bond to a 48 | central device (HID mouse, keyboard, etc.) 49 | won't work at all with this feature enabled 50 | since the factory reset will clear all of the 51 | bonding data stored on the chip, meaning the 52 | central device won't be able to reconnect. 53 | -----------------------------------------------------------------------*/ 54 | #define FACTORYRESET_ENABLE 1 55 | /*=========================================================================*/ 56 | 57 | 58 | // Create the bluefruit object, either software serial...uncomment these lines 59 | /* 60 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 61 | 62 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 63 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 64 | */ 65 | 66 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 67 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 68 | 69 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 70 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 71 | 72 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 73 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 74 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 75 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 76 | 77 | 78 | // A small helper 79 | void error(const __FlashStringHelper*err) { 80 | Serial.println(err); 81 | while (1); 82 | } 83 | 84 | /**************************************************************************/ 85 | /*! 86 | @brief Sets up the HW an the BLE module (this function is called 87 | automatically on startup) 88 | */ 89 | /**************************************************************************/ 90 | void setup(void) 91 | { 92 | while (!Serial); // required for Flora & Micro 93 | delay(500); 94 | 95 | Serial.begin(115200); 96 | Serial.println(F("Adafruit Bluefruit AT Command Example")); 97 | Serial.println(F("-------------------------------------")); 98 | 99 | /* Initialise the module */ 100 | Serial.print(F("Initialising the Bluefruit LE module: ")); 101 | 102 | if ( !ble.begin(VERBOSE_MODE) ) 103 | { 104 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 105 | } 106 | Serial.println( F("OK!") ); 107 | 108 | if ( FACTORYRESET_ENABLE ) 109 | { 110 | /* Perform a factory reset to make sure everything is in a known state */ 111 | Serial.println(F("Performing a factory reset: ")); 112 | if ( ! ble.factoryReset() ){ 113 | error(F("Couldn't factory reset")); 114 | } 115 | } 116 | 117 | /* Disable command echo from Bluefruit */ 118 | ble.echo(false); 119 | 120 | Serial.println("Requesting Bluefruit info:"); 121 | /* Print Bluefruit information */ 122 | ble.info(); 123 | } 124 | 125 | /**************************************************************************/ 126 | /*! 127 | @brief Constantly poll for new command or response data 128 | */ 129 | /**************************************************************************/ 130 | void loop(void) 131 | { 132 | // Display command prompt 133 | Serial.print(F("AT > ")); 134 | 135 | // Check for user input and echo it back if anything was found 136 | char command[BUFSIZE+1]; 137 | getUserInput(command, BUFSIZE); 138 | 139 | // Send command 140 | ble.println(command); 141 | 142 | // Check response status 143 | ble.waitForOK(); 144 | } 145 | 146 | /**************************************************************************/ 147 | /*! 148 | @brief Checks for user input (via the Serial Monitor) 149 | */ 150 | /**************************************************************************/ 151 | void getUserInput(char buffer[], uint8_t maxSize) 152 | { 153 | memset(buffer, 0, maxSize); 154 | while( Serial.available() == 0 ) { 155 | delay(1); 156 | } 157 | 158 | uint8_t count=0; 159 | 160 | do 161 | { 162 | count += Serial.readBytes(buffer+count, maxSize); 163 | delay(2); 164 | } while( (count < maxSize) && !(Serial.available() == 0) ); 165 | } 166 | -------------------------------------------------------------------------------- /examples/battery/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/battery/battery.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | This is an example for our nRF51822 based Bluefruit LE modules 3 | 4 | Pick one up today in the adafruit shop! 5 | 6 | Adafruit invests time and resources providing this open source code, 7 | please support Adafruit and open-source hardware by purchasing 8 | products from Adafruit! 9 | 10 | MIT license, check LICENSE for more information 11 | All text above, and the splash screen below must be included in 12 | any redistribution 13 | *********************************************************************/ 14 | 15 | #include 16 | #include 17 | 18 | #include "Adafruit_BLE.h" 19 | #include "Adafruit_BluefruitLE_SPI.h" 20 | #include "Adafruit_BluefruitLE_UART.h" 21 | #include "Adafruit_BLEBattery.h" 22 | #include "BluefruitConfig.h" 23 | 24 | #if SOFTWARE_SERIAL_AVAILABLE 25 | #include 26 | #endif 27 | 28 | 29 | /*========================================================================= 30 | APPLICATION SETTINGS 31 | 32 |     FACTORYRESET_ENABLE    Perform a factory reset when running this sketch 33 |     34 |     Enabling this will put your Bluefruit LE module 35 | in a 'known good' state and clear any config 36 | data set in previous sketches or projects, so 37 |     running this at least once is a good idea. 38 |     39 |     When deploying your project, however, you will 40 | want to disable factory reset by setting this 41 | value to 0.  If you are making changes to your 42 |     Bluefruit LE device via AT commands, and those 43 | changes aren't persisting across resets, this 44 | is the reason why.  Factory reset will erase 45 | the non-volatile memory where config data is 46 | stored, setting it back to factory default 47 | values. 48 |         49 |     Some sketches that require you to bond to a 50 | central device (HID mouse, keyboard, etc.) 51 | won't work at all with this feature enabled 52 | since the factory reset will clear all of the 53 | bonding data stored on the chip, meaning the 54 | central device won't be able to reconnect. 55 | -----------------------------------------------------------------------*/ 56 | #define FACTORYRESET_ENABLE 1 57 | /*=========================================================================*/ 58 | 59 | 60 | // Create the bluefruit object, either software serial...uncomment these lines 61 | /* 62 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 63 | 64 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 65 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 66 | */ 67 | 68 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 69 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 70 | 71 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 72 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 73 | 74 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 75 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 76 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 77 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 78 | 79 | Adafruit_BLEBattery battery(ble); 80 | 81 | // A small helper 82 | void error(const __FlashStringHelper*err) { 83 | Serial.println(err); 84 | while (1); 85 | } 86 | 87 | int value = 100; 88 | 89 | /**************************************************************************/ 90 | /*! 91 | @brief Sets up the HW an the BLE module (this function is called 92 | automatically on startup) 93 | */ 94 | /**************************************************************************/ 95 | void setup(void) 96 | { 97 | while (!Serial); // required for Flora & Micro 98 | delay(500); 99 | 100 | Serial.begin(115200); 101 | Serial.println(F("Adafruit Bluefruit AT Command Example")); 102 | Serial.println(F("-------------------------------------")); 103 | 104 | /* Initialise the module */ 105 | Serial.print(F("Initialising the Bluefruit LE module: ")); 106 | 107 | if ( !ble.begin(VERBOSE_MODE) ) 108 | { 109 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 110 | } 111 | Serial.println( F("OK!") ); 112 | 113 | if ( FACTORYRESET_ENABLE ) 114 | { 115 | /* Perform a factory reset to make sure everything is in a known state */ 116 | Serial.println(F("Performing a factory reset: ")); 117 | if ( ! ble.factoryReset() ){ 118 | error(F("Couldn't factory reset")); 119 | } 120 | } 121 | 122 | /* Disable command echo from Bluefruit */ 123 | ble.echo(false); 124 | 125 | Serial.println("Requesting Bluefruit info:"); 126 | /* Print Bluefruit information */ 127 | ble.info(); 128 | 129 | // Enable Battery service and reset Bluefruit 130 | battery.begin(true); 131 | } 132 | 133 | /**************************************************************************/ 134 | /*! 135 | @brief Constantly poll for new command or response data 136 | */ 137 | /**************************************************************************/ 138 | void loop(void) 139 | { 140 | // Should get Battery value from LIPO and update 141 | Serial.print("Update battery level = "); 142 | Serial.println(value); 143 | 144 | battery.update(value); 145 | 146 | value--; 147 | if (value == 0) value = 100; 148 | 149 | delay(5000); 150 | } 151 | 152 | -------------------------------------------------------------------------------- /examples/beacon/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/beacon/beacon.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | This is an example for our nRF51822 based Bluefruit LE modules 3 | 4 | Pick one up today in the adafruit shop! 5 | 6 | Adafruit invests time and resources providing this open source code, 7 | please support Adafruit and open-source hardware by purchasing 8 | products from Adafruit! 9 | 10 | MIT license, check LICENSE for more information 11 | All text above, and the splash screen below must be included in 12 | any redistribution 13 | *********************************************************************/ 14 | 15 | #include 16 | #include 17 | #include "Adafruit_BLE.h" 18 | #include "Adafruit_BluefruitLE_SPI.h" 19 | #include "Adafruit_BluefruitLE_UART.h" 20 | 21 | #include "BluefruitConfig.h" 22 | 23 | #if SOFTWARE_SERIAL_AVAILABLE 24 | #include 25 | #endif 26 | 27 | /*========================================================================= 28 | APPLICATION SETTINGS 29 | 30 |     FACTORYRESET_ENABLE    Perform a factory reset when running this sketch 31 |     32 |     Enabling this will put your Bluefruit LE module 33 | in a 'known good' state and clear any config 34 | data set in previous sketches or projects, so 35 |     running this at least once is a good idea. 36 |     37 |     When deploying your project, however, you will 38 | want to disable factory reset by setting this 39 | value to 0.  If you are making changes to your 40 |     Bluefruit LE device via AT commands, and those 41 | changes aren't persisting across resets, this 42 | is the reason why.  Factory reset will erase 43 | the non-volatile memory where config data is 44 | stored, setting it back to factory default 45 | values. 46 |         47 |     Some sketches that require you to bond to a 48 | central device (HID mouse, keyboard, etc.) 49 | won't work at all with this feature enabled 50 | since the factory reset will clear all of the 51 | bonding data stored on the chip, meaning the 52 | central device won't be able to reconnect. 53 | 54 | BEACON_MANUFACTURER_ID Company Identifier assigned by Bluetooth SIG 55 | Full list of Manufacturer ID can be found here 56 | https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers 57 | BEACON_UUID 16-bytes UUID in hex format AA-BB-... 58 | BEACON_MAJOR 16-bit major nunber 59 | BEACON_MINOR 16-bit minor nunber 60 | BEACON_RSSI_1M 61 | -----------------------------------------------------------------------*/ 62 | #define FACTORYRESET_ENABLE 1 63 | 64 | #define MANUFACTURER_APPLE "0x004C" 65 | #define MANUFACTURER_NORDIC "0x0059" 66 | 67 | #define BEACON_MANUFACTURER_ID MANUFACTURER_APPLE 68 | #define BEACON_UUID "01-12-23-34-45-56-67-78-89-9A-AB-BC-CD-DE-EF-F0" 69 | #define BEACON_MAJOR "0x0000" 70 | #define BEACON_MINOR "0x0000" 71 | #define BEACON_RSSI_1M "-54" 72 | /*=========================================================================*/ 73 | 74 | // Create the bluefruit object, either software serial...uncomment these lines 75 | /* 76 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 77 | 78 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 79 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 80 | */ 81 | 82 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 83 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 84 | 85 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 86 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 87 | 88 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 89 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 90 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 91 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 92 | 93 | 94 | // A small helper 95 | void error(const __FlashStringHelper*err) { 96 | Serial.println(err); 97 | while (1); 98 | } 99 | 100 | /**************************************************************************/ 101 | /*! 102 | @brief Sets up the HW an the BLE module (this function is called 103 | automatically on startup) 104 | */ 105 | /**************************************************************************/ 106 | void setup(void) 107 | { 108 | while (!Serial); // required for Flora & Micro 109 | delay(500); 110 | 111 | Serial.begin(115200); 112 | Serial.println(F("Adafruit Bluefruit Beacon Example")); 113 | Serial.println(F("---------------------------------")); 114 | 115 | /* Initialise the module */ 116 | Serial.print(F("Initialising the Bluefruit LE module: ")); 117 | 118 | if ( !ble.begin(VERBOSE_MODE) ) 119 | { 120 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 121 | } 122 | Serial.println( F("OK!") ); 123 | 124 | if ( FACTORYRESET_ENABLE ) 125 | { 126 | /* Perform a factory reset to make sure everything is in a known state */ 127 | Serial.println(F("Performing a factory reset: ")); 128 | if ( ! ble.factoryReset() ){ 129 | error(F("Couldn't factory reset")); 130 | } 131 | } 132 | 133 | /* Disable command echo from Bluefruit */ 134 | ble.echo(false); 135 | 136 | Serial.println("Requesting Bluefruit info:"); 137 | /* Print Bluefruit information */ 138 | ble.info(); 139 | 140 | Serial.println(F("Setting beacon configuration details: ")); 141 | 142 | // AT+BLEBEACON=0x004C,01-12-23-34-45-56-67-78-89-9A-AB-BC-CD-DE-EF-F0,0x0000,0x0000,-54 143 | ble.print("AT+BLEBEACON=" ); 144 | ble.print(BEACON_MANUFACTURER_ID ); ble.print(','); 145 | ble.print(BEACON_UUID ); ble.print(','); 146 | ble.print(BEACON_MAJOR ); ble.print(','); 147 | ble.print(BEACON_MINOR ); ble.print(','); 148 | ble.print(BEACON_RSSI_1M ); 149 | ble.println(); // print line causes the command to execute 150 | 151 | // check response status 152 | if (! ble.waitForOK() ) { 153 | error(F("Didn't get the OK")); 154 | } 155 | 156 | Serial.println(); 157 | Serial.println(F("Open your beacon app to test")); 158 | } 159 | 160 | /**************************************************************************/ 161 | /*! 162 | @brief Constantly poll for new command or response data 163 | */ 164 | /**************************************************************************/ 165 | void loop(void) 166 | { 167 | } 168 | -------------------------------------------------------------------------------- /examples/bleuart_cmdmode/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/bleuart_datamode/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/callbacks/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/callbacks_dfuirq/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/controller/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | #define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response 8 | 9 | 10 | // SOFTWARE UART SETTINGS 11 | // ---------------------------------------------------------------------------------------------- 12 | // The following macros declare the pins that will be used for 'SW' serial. 13 | // You should use this option if you are connecting the UART Friend to an UNO 14 | // ---------------------------------------------------------------------------------------------- 15 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 16 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 17 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 18 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 19 | 20 | 21 | // HARDWARE UART SETTINGS 22 | // ---------------------------------------------------------------------------------------------- 23 | // The following macros declare the HW serial port you are using. Uncomment 24 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 25 | // ---------------------------------------------------------------------------------------------- 26 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 27 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 28 | #endif 29 | 30 | 31 | // SHARED UART SETTINGS 32 | // ---------------------------------------------------------------------------------------------- 33 | // The following sets the optional Mode pin, its recommended but not required 34 | // ---------------------------------------------------------------------------------------------- 35 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 36 | 37 | 38 | // SHARED SPI SETTINGS 39 | // ---------------------------------------------------------------------------------------------- 40 | // The following macros declare the pins to use for HW and SW SPI communication. 41 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 42 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 43 | // that use SPI (Bluefruit LE SPI Friend). 44 | // ---------------------------------------------------------------------------------------------- 45 | #define BLUEFRUIT_SPI_CS 8 46 | #define BLUEFRUIT_SPI_IRQ 7 47 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 48 | 49 | // SOFTWARE SPI SETTINGS 50 | // ---------------------------------------------------------------------------------------------- 51 | // The following macros declare the pins to use for SW SPI communication. 52 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 53 | // (Bluefruit LE SPI Friend). 54 | // ---------------------------------------------------------------------------------------------- 55 | #define BLUEFRUIT_SPI_SCK 13 56 | #define BLUEFRUIT_SPI_MISO 12 57 | #define BLUEFRUIT_SPI_MOSI 11 58 | -------------------------------------------------------------------------------- /examples/controller/packetParser.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) && not defined(__SAMD51__) 5 | #include 6 | #endif 7 | 8 | #include "Adafruit_BLE.h" 9 | #include "Adafruit_BluefruitLE_SPI.h" 10 | #include "Adafruit_BluefruitLE_UART.h" 11 | 12 | 13 | #define PACKET_ACC_LEN (15) 14 | #define PACKET_GYRO_LEN (15) 15 | #define PACKET_MAG_LEN (15) 16 | #define PACKET_QUAT_LEN (19) 17 | #define PACKET_BUTTON_LEN (5) 18 | #define PACKET_COLOR_LEN (6) 19 | #define PACKET_LOCATION_LEN (15) 20 | 21 | // READ_BUFSIZE Size of the read buffer for incoming packets 22 | #define READ_BUFSIZE (20) 23 | 24 | 25 | /* Buffer to hold incoming characters */ 26 | uint8_t packetbuffer[READ_BUFSIZE+1]; 27 | 28 | /**************************************************************************/ 29 | /*! 30 | @brief Casts the four bytes at the specified address to a float 31 | */ 32 | /**************************************************************************/ 33 | float parsefloat(uint8_t *buffer) 34 | { 35 | float f; 36 | memcpy(&f, buffer, 4); 37 | return f; 38 | } 39 | 40 | /**************************************************************************/ 41 | /*! 42 | @brief Prints a hexadecimal value in plain characters 43 | @param data Pointer to the byte data 44 | @param numBytes Data length in bytes 45 | */ 46 | /**************************************************************************/ 47 | void printHex(const uint8_t * data, const uint32_t numBytes) 48 | { 49 | uint32_t szPos; 50 | for (szPos=0; szPos < numBytes; szPos++) 51 | { 52 | Serial.print(F("0x")); 53 | // Append leading 0 for small values 54 | if (data[szPos] <= 0xF) 55 | { 56 | Serial.print(F("0")); 57 | Serial.print(data[szPos] & 0xf, HEX); 58 | } 59 | else 60 | { 61 | Serial.print(data[szPos] & 0xff, HEX); 62 | } 63 | // Add a trailing space if appropriate 64 | if ((numBytes > 1) && (szPos != numBytes - 1)) 65 | { 66 | Serial.print(F(" ")); 67 | } 68 | } 69 | Serial.println(); 70 | } 71 | 72 | /**************************************************************************/ 73 | /*! 74 | @brief Waits for incoming data and parses it 75 | */ 76 | /**************************************************************************/ 77 | uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) 78 | { 79 | uint16_t origtimeout = timeout, replyidx = 0; 80 | 81 | memset(packetbuffer, 0, READ_BUFSIZE); 82 | 83 | while (timeout--) { 84 | if (replyidx >= 20) break; 85 | if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) 86 | break; 87 | if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) 88 | break; 89 | if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN)) 90 | break; 91 | if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN)) 92 | break; 93 | if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN)) 94 | break; 95 | if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN)) 96 | break; 97 | if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN)) 98 | break; 99 | 100 | while (ble->available()) { 101 | char c = ble->read(); 102 | if (c == '!') { 103 | replyidx = 0; 104 | } 105 | packetbuffer[replyidx] = c; 106 | replyidx++; 107 | timeout = origtimeout; 108 | } 109 | 110 | if (timeout == 0) break; 111 | delay(1); 112 | } 113 | 114 | packetbuffer[replyidx] = 0; // null term 115 | 116 | if (!replyidx) // no data or timeout 117 | return 0; 118 | if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning 119 | return 0; 120 | 121 | // check checksum! 122 | uint8_t xsum = 0; 123 | uint8_t checksum = packetbuffer[replyidx-1]; 124 | 125 | for (uint8_t i=0; i 25 | #include 26 | #include 27 | 28 | #include "Adafruit_BLE.h" 29 | #include "Adafruit_BluefruitLE_SPI.h" 30 | #include "Adafruit_BluefruitLE_UART.h" 31 | #include "BluefruitConfig.h" 32 | 33 | #if SOFTWARE_SERIAL_AVAILABLE 34 | #include 35 | #endif 36 | 37 | #include 38 | 39 | // Configuration (you don't need to change these, but can!): 40 | #define FACTORYRESET_ENABLE 1 // Set to 1 to factory reset the Bluefruit LE 41 | // module. In general this is a smart idea to 42 | // put the module into a known good state. 43 | // A value of 1 means perform a factory reset 44 | // on start, and 0 means no factory reset. 45 | 46 | #define NUMPIXELS 10 // Number of NeoPixels on the board. 47 | 48 | #define BRIGHTNESS 255 // NeoPixel brightness (0...255, low to max brightness) 49 | 50 | /*=========================================================================*/ 51 | 52 | // Create the bluefruit object, for Circuit Playground this needs to be 53 | // hardware serial. 54 | Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 55 | 56 | // A small helper 57 | void error(const __FlashStringHelper*err) { 58 | Serial.println(err); 59 | while (1); 60 | } 61 | 62 | // function prototypes over in packetparser.cpp 63 | uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout); 64 | float parsefloat(uint8_t *buffer); 65 | void printHex(const uint8_t * data, const uint32_t numBytes); 66 | 67 | // the packet buffer 68 | extern uint8_t packetbuffer[]; 69 | 70 | // Offset that controls which pixels are turned on/off. 71 | // Pressing the left/right controller button moves this offset 72 | // up/down and 'slides' the chain of 10 pixels around. 73 | int currentOffset = 0; 74 | 75 | // Last selected color. 76 | uint8_t currentRed, currentGreen, currentBlue; 77 | 78 | // Light up a chain of 10 pixels starting at offset global variable value. 79 | void lightPixels(uint8_t red, uint8_t green, uint8_t blue, int offset) { 80 | CircuitPlayground.strip.clear(); 81 | for (int i = offset; i < offset+NUMPIXELS; ++i) { 82 | if ((i >= 0) && (i < NUMPIXELS)) { 83 | CircuitPlayground.strip.setPixelColor(i, red, green, blue); 84 | } 85 | } 86 | CircuitPlayground.strip.show(); 87 | } 88 | 89 | void setup() { 90 | // Wait for serial port before starting. Not required but helps with debugging. 91 | //while (!Serial); 92 | //delay(500); 93 | 94 | // Initialize Circuit Playground library and turn off the Pixels. 95 | CircuitPlayground.begin(BRIGHTNESS); 96 | CircuitPlayground.clearPixels(); 97 | 98 | // Initialize serial output. 99 | Serial.begin(115200); 100 | Serial.println(F("Adafruit Circuit Playground Bluefruit Neopixel Color Picker Example")); 101 | Serial.println(F("-------------------------------------------------------------------")); 102 | 103 | // Initialise the module 104 | Serial.print(F("Initialising the Bluefruit LE module: ")); 105 | 106 | if ( !ble.begin(VERBOSE_MODE) ) 107 | { 108 | error(F("Couldn't find Bluefruit, make sure it's in DATA mode & check wiring....")); 109 | } 110 | Serial.println( F("OK!") ); 111 | 112 | if ( FACTORYRESET_ENABLE ) 113 | { 114 | // Perform a factory reset to make sure everything is in a known state 115 | Serial.println(F("Performing a factory reset: ")); 116 | if (!ble.factoryReset()) { 117 | Serial.println(F("Couldn't factory reset, making another attempt...")); 118 | delay(1000); 119 | if (!ble.factoryReset()) { 120 | error(F("Couldn't factory reset!")); 121 | } 122 | } 123 | } 124 | 125 | // Disable command echo from Bluefruit 126 | ble.echo(false); 127 | 128 | Serial.println("Requesting Bluefruit info:"); 129 | // Print Bluefruit information 130 | ble.info(); 131 | 132 | // Change advertised name to Circuit_Playground_BLE 133 | ble.println("AT+GAPDEVNAME=CPlay_BLE"); 134 | delay(100); 135 | ble.println("ATZ"); 136 | delay(100); 137 | 138 | Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode")); 139 | Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!")); 140 | Serial.println(); 141 | 142 | ble.verbose(false); // debug info is a little annoying after this point! 143 | 144 | // Wait for connection 145 | while (! ble.isConnected()) { 146 | delay(500); 147 | } 148 | 149 | Serial.println(F("***********************")); 150 | 151 | // Set Bluefruit to DATA mode 152 | Serial.println( F("Switching to DATA mode!") ); 153 | ble.setMode(BLUEFRUIT_MODE_DATA); 154 | 155 | Serial.println(F("***********************")); 156 | 157 | } 158 | 159 | void loop() 160 | { 161 | /* Wait for new data to arrive */ 162 | uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT); 163 | if (len == 0) { 164 | return; 165 | } 166 | 167 | /* Got a packet! */ 168 | //printHex(packetbuffer, len); 169 | 170 | // Color 171 | if (packetbuffer[1] == 'C') { 172 | // Parse out the red, green, blue values and save them. 173 | uint8_t red = packetbuffer[2]; 174 | uint8_t green = packetbuffer[3]; 175 | uint8_t blue = packetbuffer[4]; 176 | // Change the color. 177 | currentRed = red; 178 | currentGreen = green; 179 | currentBlue = blue; 180 | } 181 | 182 | // Left/right adjust the offset that turns on/off pixels. 183 | if (packetbuffer[1] == 'B') { 184 | if (memcmp(packetbuffer, "!B705", 5) == 0) { 185 | // Left button release. 186 | currentOffset -= 1; 187 | } 188 | else if (memcmp(packetbuffer, "!B804", 5) == 0) { 189 | // Right button release. 190 | currentOffset += 1; 191 | } 192 | currentOffset = constrain(currentOffset, -NUMPIXELS, NUMPIXELS); 193 | } 194 | 195 | // Update the pixels again since either the color or offset position 196 | // must have changed above. 197 | lightPixels(currentRed, currentGreen, currentBlue, currentOffset); 198 | } 199 | -------------------------------------------------------------------------------- /examples/cplay_neopixel_picker/packetParser.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) 5 | #include 6 | #endif 7 | 8 | #include "Adafruit_BLE.h" 9 | #include "Adafruit_BluefruitLE_SPI.h" 10 | #include "Adafruit_BluefruitLE_UART.h" 11 | 12 | 13 | #define PACKET_ACC_LEN (15) 14 | #define PACKET_GYRO_LEN (15) 15 | #define PACKET_MAG_LEN (15) 16 | #define PACKET_QUAT_LEN (19) 17 | #define PACKET_BUTTON_LEN (5) 18 | #define PACKET_COLOR_LEN (6) 19 | #define PACKET_LOCATION_LEN (15) 20 | 21 | // READ_BUFSIZE Size of the read buffer for incoming packets 22 | #define READ_BUFSIZE (20) 23 | 24 | 25 | /* Buffer to hold incoming characters */ 26 | uint8_t packetbuffer[READ_BUFSIZE+1]; 27 | 28 | /**************************************************************************/ 29 | /*! 30 | @brief Casts the four bytes at the specified address to a float 31 | */ 32 | /**************************************************************************/ 33 | float parsefloat(uint8_t *buffer) 34 | { 35 | float f = ((float *)buffer)[0]; 36 | return f; 37 | } 38 | 39 | /**************************************************************************/ 40 | /*! 41 | @brief Prints a hexadecimal value in plain characters 42 | @param data Pointer to the byte data 43 | @param numBytes Data length in bytes 44 | */ 45 | /**************************************************************************/ 46 | void printHex(const uint8_t * data, const uint32_t numBytes) 47 | { 48 | uint32_t szPos; 49 | for (szPos=0; szPos < numBytes; szPos++) 50 | { 51 | Serial.print(F("0x")); 52 | // Append leading 0 for small values 53 | if (data[szPos] <= 0xF) 54 | { 55 | Serial.print(F("0")); 56 | Serial.print(data[szPos] & 0xf, HEX); 57 | } 58 | else 59 | { 60 | Serial.print(data[szPos] & 0xff, HEX); 61 | } 62 | // Add a trailing space if appropriate 63 | if ((numBytes > 1) && (szPos != numBytes - 1)) 64 | { 65 | Serial.print(F(" ")); 66 | } 67 | } 68 | Serial.println(); 69 | } 70 | 71 | /**************************************************************************/ 72 | /*! 73 | @brief Waits for incoming data and parses it 74 | */ 75 | /**************************************************************************/ 76 | uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) 77 | { 78 | uint16_t origtimeout = timeout, replyidx = 0; 79 | 80 | memset(packetbuffer, 0, READ_BUFSIZE); 81 | 82 | while (timeout--) { 83 | if (replyidx >= 20) break; 84 | if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) 85 | break; 86 | if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) 87 | break; 88 | if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN)) 89 | break; 90 | if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN)) 91 | break; 92 | if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN)) 93 | break; 94 | if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN)) 95 | break; 96 | if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN)) 97 | break; 98 | 99 | while (ble->available()) { 100 | char c = ble->read(); 101 | if (c == '!') { 102 | replyidx = 0; 103 | } 104 | packetbuffer[replyidx] = c; 105 | replyidx++; 106 | timeout = origtimeout; 107 | } 108 | 109 | if (timeout == 0) break; 110 | delay(1); 111 | } 112 | 113 | packetbuffer[replyidx] = 0; // null term 114 | 115 | if (!replyidx) // no data or timeout 116 | return 0; 117 | if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning 118 | return 0; 119 | 120 | // check checksum! 121 | uint8_t xsum = 0; 122 | uint8_t checksum = packetbuffer[replyidx-1]; 123 | 124 | for (uint8_t i=0; i 16 | #include 17 | #include "Adafruit_BLE.h" 18 | #include "Adafruit_BluefruitLE_SPI.h" 19 | #include "Adafruit_BluefruitLE_UART.h" 20 | 21 | #include "BluefruitConfig.h" 22 | 23 | #if SOFTWARE_SERIAL_AVAILABLE 24 | #include 25 | #endif 26 | 27 | // Create the bluefruit object, either software serial...uncomment these lines 28 | /* 29 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 30 | 31 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 32 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 33 | */ 34 | 35 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 36 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 37 | 38 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 39 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 40 | 41 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 42 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 43 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 44 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 45 | 46 | 47 | // A small helper 48 | void error(const __FlashStringHelper*err) { 49 | Serial.println(err); 50 | while (1); 51 | } 52 | 53 | /**************************************************************************/ 54 | /*! 55 | @brief Sets up the HW an the BLE module (this function is called 56 | automatically on startup) 57 | */ 58 | /**************************************************************************/ 59 | void setup(void) 60 | { 61 | while (!Serial); // required for Flora & Micro 62 | delay(500); 63 | 64 | Serial.begin(115200); 65 | Serial.println(F("Adafruit Factory Reset Example")); 66 | Serial.println(F("------------------------------------------------")); 67 | 68 | /* Initialise the module */ 69 | Serial.print(F("Initialising the Bluefruit LE module: ")); 70 | 71 | if ( !ble.begin(VERBOSE_MODE) ) 72 | { 73 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 74 | } 75 | Serial.println( F("OK!") ); 76 | 77 | /* Perform a factory reset to make sure everything is in a known state */ 78 | Serial.println(F("Performing a factory reset: ")); 79 | if (! ble.factoryReset() ){ 80 | error(F("Couldn't factory reset")); 81 | } 82 | 83 | Serial.println("Requesting Bluefruit info:"); 84 | /* Print Bluefruit information */ 85 | ble.info(); 86 | 87 | Serial.println(F("DONE!")); 88 | } 89 | 90 | /**************************************************************************/ 91 | /*! 92 | @brief Constantly poll for new command or response data 93 | */ 94 | /**************************************************************************/ 95 | void loop(void) 96 | { 97 | } 98 | -------------------------------------------------------------------------------- /examples/feathertester/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 160 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE false // If set to 'true' enables debug output 7 | 8 | // SHARED SPI SETTINGS 9 | // ---------------------------------------------------------------------------------------------- 10 | // The following macros declare the pins to use for HW and SW SPI communication. 11 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 12 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 13 | // that use SPI (Bluefruit LE SPI Friend). 14 | // ---------------------------------------------------------------------------------------------- 15 | #define BLUEFRUIT_SPI_CS 8 16 | #define BLUEFRUIT_SPI_IRQ 7 17 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 18 | 19 | // SOFTWARE SPI SETTINGS 20 | // ---------------------------------------------------------------------------------------------- 21 | // The following macros declare the pins to use for SW SPI communication. 22 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 23 | // (Bluefruit LE SPI Friend). 24 | // ---------------------------------------------------------------------------------------------- 25 | #define BLUEFRUIT_SPI_SCK 13 26 | #define BLUEFRUIT_SPI_MISO 12 27 | #define BLUEFRUIT_SPI_MOSI 11 28 | -------------------------------------------------------------------------------- /examples/feathertester/feathertester.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Bluefruit Feather Tester 3 | 4 | This sketch provides a simple tester for Bluefruit Feather boards from Adafruit 5 | 6 | created 31 Jan. 2016 7 | by K. Townsend (KTOWN) 8 | */ 9 | 10 | #include 11 | #include 12 | #include "Adafruit_BLE.h" 13 | #include "Adafruit_BluefruitLE_SPI.h" 14 | #include "BluefruitConfig.h" 15 | 16 | #if SOFTWARE_SERIAL_AVAILABLE 17 | #include 18 | #endif 19 | 20 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 21 | 22 | /**************************************************************************/ 23 | /*! 24 | 25 | */ 26 | /**************************************************************************/ 27 | void error(const __FlashStringHelper*err) { 28 | Serial.println(err); 29 | while (1); 30 | } 31 | 32 | /**************************************************************************/ 33 | /*! 34 | 35 | */ 36 | /**************************************************************************/ 37 | void setup() 38 | { 39 | Serial.begin(115200); 40 | 41 | // Wait for the Serial Monitor to open 42 | while (!Serial) { yield(); } 43 | 44 | // Initialise the BLE module 45 | if ( !ble.begin(VERBOSE_MODE) ) 46 | { 47 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 48 | } 49 | 50 | // Display the test suite selection menu 51 | display_menu(); 52 | } 53 | 54 | /**************************************************************************/ 55 | /*! 56 | Reads and prints the AT response buffer until we reach OK or ERROR 57 | 58 | Returns 'true' if an error occurred, otherwise 'false' 59 | */ 60 | /**************************************************************************/ 61 | bool display_response(void) 62 | { 63 | // Read the response until we get OK or ERROR 64 | while (ble.readline()) 65 | { 66 | Serial.print(ble.buffer); 67 | if ( strcmp(ble.buffer, "OK") == 0 ) 68 | { 69 | return false; 70 | } 71 | if ( strcmp(ble.buffer, "ERROR") == 0 ) 72 | { 73 | return true; 74 | } 75 | } 76 | 77 | return true; 78 | } 79 | 80 | /**************************************************************************/ 81 | /*! 82 | 83 | */ 84 | /**************************************************************************/ 85 | void display_menu() 86 | { 87 | delay(500); // Short delay for cosmetic reasons 88 | Serial.println(""); 89 | Serial.println("Bluefruit Feather Tester"); 90 | Serial.println("-------------------------------------------------------------------------------"); 91 | Serial.println("Select a menu option below:"); 92 | Serial.println(""); 93 | Serial.println("[1] - System Info"); 94 | Serial.println("[2] - AT+HELP"); 95 | Serial.println(""); 96 | Serial.println("Enter your selection in the Serial Monitor and press "); 97 | Serial.println(""); 98 | } 99 | 100 | /**************************************************************************/ 101 | /*! 102 | 103 | */ 104 | /**************************************************************************/ 105 | void loop() 106 | { 107 | String user_input = ""; 108 | int selection = 0; 109 | 110 | // Wait for user feedback, then parse feedback one byte at a time 111 | while((Serial.available()) && !selection) 112 | { 113 | char incoming = Serial.read(); 114 | if (isdigit(incoming)) 115 | { 116 | // Append the current digit to the string placeholder 117 | user_input += (char)incoming; 118 | } 119 | // Parse the string on new-line 120 | if (incoming == '\n') 121 | { 122 | selection = user_input.toInt(); 123 | } 124 | delay(2); 125 | } 126 | 127 | // Run the appropriate test suite if we have a number 128 | if (selection) 129 | { 130 | bool error = false; 131 | switch(selection) 132 | { 133 | case 1: 134 | ble.info(); 135 | break; 136 | case 2: 137 | ble.println("AT+HELP"); 138 | error = display_response(); 139 | break; 140 | default: 141 | Serial.print("Invalid selection: "); 142 | Serial.println(selection); 143 | break; 144 | } 145 | 146 | // Catch any error responses here 147 | if (error) Serial.println("ERROR!"); 148 | 149 | // Display the main menu again 150 | Serial.println(""); 151 | display_menu(); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /examples/healththermometer/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN 8 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/healththermometer/IEEE11073float.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file IEEE11073float.h 4 | */ 5 | /**************************************************************************/ 6 | 7 | /** 8 | * \file bytelib.c 9 | * \brief Byte manipulation module implementation. 10 | * Copyright (C) 2010 Signove Tecnologia Corporation. 11 | * All rights reserved. 12 | * Contact: Signove Tecnologia Corporation (contact@signove.com) 13 | * 14 | * $LICENSE_TEXT:BEGIN$ 15 | * This library is free software; you can redistribute it and/or 16 | * modify it under the terms of the GNU Lesser General Public 17 | * License as published by the Free Software Foundation and appearing 18 | * in the file LICENSE included in the packaging of this file; either 19 | * version 2.1 of the License, or (at your option) any later version. 20 | * 21 | * This library is distributed in the hope that it will be useful, 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * Lesser General Public License for more details. 25 | * 26 | * You should have received a copy of the GNU Lesser General Public 27 | * License along with this library; if not, write to the Free Software 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 29 | * $LICENSE_TEXT:END$ 30 | * 31 | * \author Walter Guerra, Mateus Lima 32 | * \date Jun 14, 2010 33 | */ 34 | 35 | #include 36 | #include "IEEE11073float.h" 37 | 38 | uint32_t float2IEEE11073(double data, uint8_t output[4]) 39 | { 40 | uint32_t result = MDER_NaN; 41 | 42 | 43 | if (isnan(data)) { 44 | goto finally; 45 | }/* else if (data > MDER_FLOAT_MAX) { 46 | result = MDER_POSITIVE_INFINITY; 47 | goto finally; 48 | } else if (data < MDER_FLOAT_MIN) { 49 | result = MDER_NEGATIVE_INFINITY; 50 | goto finally; 51 | } else if (data >= -MDER_FLOAT_EPSILON && 52 | data <= MDER_FLOAT_EPSILON) { 53 | result = 0; 54 | goto finally; 55 | }*/ 56 | 57 | double sgn; sgn = data > 0 ? +1 : -1; 58 | double mantissa; mantissa = fabs(data); 59 | int32_t exponent; exponent = 0; // Note: 10**x exponent, not 2**x 60 | 61 | // scale up if number is too big 62 | while (mantissa > MDER_FLOAT_MANTISSA_MAX) { 63 | mantissa /= 10.0; 64 | ++exponent; 65 | if (exponent > MDER_FLOAT_EXPONENT_MAX) { 66 | // argh, should not happen 67 | if (sgn > 0) { 68 | result = MDER_POSITIVE_INFINITY; 69 | } else { 70 | result = MDER_NEGATIVE_INFINITY; 71 | } 72 | goto finally; 73 | } 74 | } 75 | 76 | // scale down if number is too small 77 | while (mantissa < 1) { 78 | mantissa *= 10; 79 | --exponent; 80 | if (exponent < MDER_FLOAT_EXPONENT_MIN) { 81 | // argh, should not happen 82 | result = 0; 83 | goto finally; 84 | } 85 | } 86 | 87 | // scale down if number needs more precision 88 | double smantissa; smantissa = round(mantissa * MDER_FLOAT_PRECISION); 89 | double rmantissa; rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; 90 | double mdiff; mdiff = abs(smantissa - rmantissa); 91 | while (mdiff > 0.5 && exponent > MDER_FLOAT_EXPONENT_MIN && 92 | (mantissa * 10) <= MDER_FLOAT_MANTISSA_MAX) { 93 | mantissa *= 10; 94 | --exponent; 95 | smantissa = round(mantissa * MDER_FLOAT_PRECISION); 96 | rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; 97 | mdiff = abs(smantissa - rmantissa); 98 | } 99 | 100 | uint32_t int_mantissa; int_mantissa = (int) round(sgn * mantissa); 101 | result = (exponent << 24) | (int_mantissa & 0xFFFFFF); 102 | 103 | finally: 104 | if ( output ) memcpy(output, &result, 4); 105 | return result; 106 | } 107 | -------------------------------------------------------------------------------- /examples/healththermometer/IEEE11073float.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file IEEE11073float.h 4 | */ 5 | /**************************************************************************/ 6 | 7 | /** 8 | * \file bytelib.c 9 | * \brief Byte manipulation module implementation. 10 | * Copyright (C) 2010 Signove Tecnologia Corporation. 11 | * All rights reserved. 12 | * Contact: Signove Tecnologia Corporation (contact@signove.com) 13 | * 14 | * $LICENSE_TEXT:BEGIN$ 15 | * This library is free software; you can redistribute it and/or 16 | * modify it under the terms of the GNU Lesser General Public 17 | * License as published by the Free Software Foundation and appearing 18 | * in the file LICENSE included in the packaging of this file; either 19 | * version 2.1 of the License, or (at your option) any later version. 20 | * 21 | * This library is distributed in the hope that it will be useful, 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * Lesser General Public License for more details. 25 | * 26 | * You should have received a copy of the GNU Lesser General Public 27 | * License along with this library; if not, write to the Free Software 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 29 | * $LICENSE_TEXT:END$ 30 | * 31 | * \author Walter Guerra, Mateus Lima 32 | * \date Jun 14, 2010 33 | */ 34 | 35 | #ifndef _IEEE11073FLOAT_H_ 36 | #define _IEEE11073FLOAT_H_ 37 | 38 | #include 39 | 40 | typedef enum { 41 | MDER_POSITIVE_INFINITY = 0x007FFFFE, 42 | MDER_NaN = 0x007FFFFF, 43 | MDER_NRes = 0x00800000, 44 | MDER_RESERVED_VALUE = 0x00800001, 45 | MDER_NEGATIVE_INFINITY = 0x00800002 46 | } ReservedFloatValues; 47 | static const int32_t FIRST_RESERVED_VALUE = MDER_POSITIVE_INFINITY; 48 | 49 | // (2 ** 23 - 3) 50 | #define MDER_FLOAT_MANTISSA_MAX 0x007FFFFD 51 | // 2 ** 7 - 1 52 | #define MDER_FLOAT_EXPONENT_MAX 127 53 | #define MDER_FLOAT_EXPONENT_MIN -128 54 | // (2 ** 23 - 3) * 10 ** 127 55 | #define MDER_FLOAT_MAX 8.388604999999999e+133 56 | // -(2 ** 23 - 3) * 10 ** 127 57 | #define MDER_FLOAT_MIN (-MDER_FLOAT_MAX) 58 | // 10 ** -128 59 | #define MDER_FLOAT_EPSILON 1e-128 60 | // 10 ** upper(23 * log(2) / log(10)) 61 | // precision for a number 1.0000xxx 62 | #define MDER_FLOAT_PRECISION 10000000 63 | 64 | typedef enum { 65 | MDER_S_POSITIVE_INFINITY = 0x07FE, 66 | MDER_S_NaN = 0x07FF, 67 | MDER_S_NRes = 0x0800, 68 | MDER_S_RESERVED_VALUE = 0x0801, 69 | MDER_S_NEGATIVE_INFINITY = 0x0802 70 | } ReservedSFloatValues; 71 | static const uint32_t FIRST_S_RESERVED_VALUE = MDER_S_POSITIVE_INFINITY; 72 | 73 | // (2 ** 11 - 3) 74 | #define MDER_SFLOAT_MANTISSA_MAX 0x07FD 75 | // 2 ** 3 - 1 76 | #define MDER_SFLOAT_EXPONENT_MAX 7 77 | #define MDER_SFLOAT_EXPONENT_MIN -8 78 | // (2 ** 11 - 3) * 10 ** 7 79 | #define MDER_SFLOAT_MAX 20450000000.0 80 | // -(2 ** 11 - 3) * 10 ** 7 81 | #define MDER_SFLOAT_MIN (-MDER_SFLOAT_MAX) 82 | // 10 ** -8 83 | #define MDER_SFLOAT_EPSILON 1e-8 84 | // 10 ** upper(11 * log(2) / log(10)) 85 | #define MDER_SFLOAT_PRECISION 10000 86 | 87 | uint32_t float2IEEE11073(double data, uint8_t output[4]); 88 | 89 | #endif /* _IEEE11073FLOAT_H_ */ 90 | -------------------------------------------------------------------------------- /examples/healththermometer/healththermometer.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | This is an example for our nRF51822 based Bluefruit LE modules 3 | 4 | Pick one up today in the adafruit shop! 5 | 6 | Adafruit invests time and resources providing this open source code, 7 | please support Adafruit and open-source hardware by purchasing 8 | products from Adafruit! 9 | 10 | MIT license, check LICENSE for more information 11 | All text above, and the splash screen below must be included in 12 | any redistribution 13 | *********************************************************************/ 14 | 15 | /* 16 | Please note the long strings of data sent mean the *RTS* pin is 17 | required with UART to slow down data sent to the Bluefruit LE! 18 | */ 19 | 20 | #include 21 | #include 22 | #include "Adafruit_BLE.h" 23 | #include "Adafruit_BluefruitLE_SPI.h" 24 | #include "Adafruit_BluefruitLE_UART.h" 25 | #include "Adafruit_BLEGatt.h" 26 | #include "IEEE11073float.h" 27 | 28 | #include "BluefruitConfig.h" 29 | 30 | #if SOFTWARE_SERIAL_AVAILABLE 31 | #include 32 | #endif 33 | 34 | 35 | // Create the bluefruit object, either software serial...uncomment these lines 36 | /* 37 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 38 | 39 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 40 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 41 | */ 42 | 43 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 44 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 45 | 46 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 47 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 48 | 49 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 50 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 51 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 52 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 53 | 54 | Adafruit_BLEGatt gatt(ble); 55 | 56 | // A small helper 57 | void error(const __FlashStringHelper*err) { 58 | Serial.println(err); 59 | while (1); 60 | } 61 | 62 | /* The service information */ 63 | 64 | int32_t htsServiceId; 65 | int32_t htsMeasureCharId; 66 | 67 | /**************************************************************************/ 68 | /*! 69 | @brief Sets up the HW an the BLE module (this function is called 70 | automatically on startup) 71 | */ 72 | /**************************************************************************/ 73 | void setup(void) 74 | { 75 | while (!Serial); // required for Flora & Micro 76 | delay(500); 77 | 78 | boolean success; 79 | 80 | Serial.begin(115200); 81 | Serial.println(F("Adafruit Bluefruit Health Thermometer Example")); 82 | Serial.println(F("--------------------------------------------")); 83 | 84 | randomSeed(micros()); 85 | 86 | /* Initialise the module */ 87 | Serial.print(F("Initialising the Bluefruit LE module: ")); 88 | 89 | if ( !ble.begin(VERBOSE_MODE) ) 90 | { 91 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 92 | } 93 | Serial.println( F("OK!") ); 94 | 95 | /* Perform a factory reset to make sure everything is in a known state */ 96 | Serial.println(F("Performing a factory reset: ")); 97 | if (! ble.factoryReset() ){ 98 | error(F("Couldn't factory reset")); 99 | } 100 | 101 | /* Disable command echo from Bluefruit */ 102 | ble.echo(false); 103 | 104 | Serial.println("Requesting Bluefruit info:"); 105 | /* Print Bluefruit information */ 106 | ble.info(); 107 | 108 | // this line is particularly required for Flora, but is a good idea 109 | // anyways for the super long lines ahead! 110 | // ble.setInterCharWriteDelay(5); // 5 ms 111 | 112 | /* Add the Heart Rate Service definition */ 113 | /* Service ID should be 1 */ 114 | Serial.println(F("Adding the Health Thermometer Service definition (UUID = 0x1809): ")); 115 | htsServiceId = gatt.addService(0x1809); 116 | if (htsServiceId == 0) { 117 | error(F("Could not add Thermometer service")); 118 | } 119 | 120 | /* Add the Temperature Measurement characteristic which is composed of 121 | * 1 byte flags + 4 float */ 122 | /* Chars ID for Measurement should be 1 */ 123 | Serial.println(F("Adding the Temperature Measurement characteristic (UUID = 0x2A1C): ")); 124 | htsMeasureCharId = gatt.addCharacteristic(0x2A1C, GATT_CHARS_PROPERTIES_INDICATE, 5, 5, BLE_DATATYPE_BYTEARRAY); 125 | if (htsMeasureCharId == 0) { 126 | error(F("Could not add Temperature characteristic")); 127 | } 128 | 129 | /* Add the Health Thermometer Service to the advertising data (needed for Nordic apps to detect the service) */ 130 | Serial.print(F("Adding Health Thermometer Service UUID to the advertising payload: ")); 131 | uint8_t advdata[] { 0x02, 0x01, 0x06, 0x05, 0x02, 0x09, 0x18, 0x0a, 0x18 }; 132 | ble.setAdvData( advdata, sizeof(advdata) ); 133 | 134 | /* Reset the device for the new service setting changes to take effect */ 135 | Serial.print(F("Performing a SW reset (service changes require a reset): ")); 136 | ble.reset(); 137 | 138 | Serial.println(); 139 | } 140 | 141 | /** Send randomized heart rate data continuously **/ 142 | void loop(void) 143 | { 144 | double temp = random(0, 100) / 10.0; 145 | 146 | Serial.print(F("Updating Temperature value to ")); 147 | Serial.print(temp); 148 | Serial.println(F(" Fahrenheit")); 149 | 150 | // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml 151 | // Chars value is 1 flag + 4 float value. Tempearature is in Fahrenheit unit 152 | uint8_t temp_measurement [5] = { bit(0) }; 153 | 154 | float2IEEE11073(temp, temp_measurement+1); 155 | 156 | // TODO temperature is not correct due to Bluetooth use IEEE-11073 format 157 | gatt.setChar(htsMeasureCharId, temp_measurement, 5); 158 | 159 | /* Delay before next measurement update */ 160 | delay(1000); 161 | } 162 | -------------------------------------------------------------------------------- /examples/heartratemonitor/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN 8 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/heartratemonitor/heartratemonitor.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | This is an example for our nRF51822 based Bluefruit LE modules 3 | 4 | Pick one up today in the adafruit shop! 5 | 6 | Adafruit invests time and resources providing this open source code, 7 | please support Adafruit and open-source hardware by purchasing 8 | products from Adafruit! 9 | 10 | MIT license, check LICENSE for more information 11 | All text above, and the splash screen below must be included in 12 | any redistribution 13 | *********************************************************************/ 14 | 15 | /* 16 | Please note the long strings of data sent mean the *RTS* pin is 17 | required with UART to slow down data sent to the Bluefruit LE! 18 | */ 19 | 20 | #include 21 | #include 22 | #include "Adafruit_BLE.h" 23 | #include "Adafruit_BluefruitLE_SPI.h" 24 | #include "Adafruit_BluefruitLE_UART.h" 25 | 26 | #include "BluefruitConfig.h" 27 | 28 | #if SOFTWARE_SERIAL_AVAILABLE 29 | #include 30 | #endif 31 | 32 | // Create the bluefruit object, either software serial...uncomment these lines 33 | /* 34 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 35 | 36 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 37 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 38 | */ 39 | 40 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 41 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 42 | 43 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 44 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 45 | 46 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 47 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 48 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 49 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 50 | 51 | 52 | // A small helper 53 | void error(const __FlashStringHelper*err) { 54 | Serial.println(err); 55 | while (1); 56 | } 57 | 58 | /* The service information */ 59 | 60 | int32_t hrmServiceId; 61 | int32_t hrmMeasureCharId; 62 | int32_t hrmLocationCharId; 63 | /**************************************************************************/ 64 | /*! 65 | @brief Sets up the HW an the BLE module (this function is called 66 | automatically on startup) 67 | */ 68 | /**************************************************************************/ 69 | void setup(void) 70 | { 71 | while (!Serial); // required for Flora & Micro 72 | delay(500); 73 | 74 | boolean success; 75 | 76 | Serial.begin(115200); 77 | Serial.println(F("Adafruit Bluefruit Heart Rate Monitor (HRM) Example")); 78 | Serial.println(F("---------------------------------------------------")); 79 | 80 | randomSeed(micros()); 81 | 82 | /* Initialise the module */ 83 | Serial.print(F("Initialising the Bluefruit LE module: ")); 84 | 85 | if ( !ble.begin(VERBOSE_MODE) ) 86 | { 87 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 88 | } 89 | Serial.println( F("OK!") ); 90 | 91 | /* Perform a factory reset to make sure everything is in a known state */ 92 | Serial.println(F("Performing a factory reset: ")); 93 | if (! ble.factoryReset() ){ 94 | error(F("Couldn't factory reset")); 95 | } 96 | 97 | /* Disable command echo from Bluefruit */ 98 | ble.echo(false); 99 | 100 | Serial.println("Requesting Bluefruit info:"); 101 | /* Print Bluefruit information */ 102 | ble.info(); 103 | 104 | // this line is particularly required for Flora, but is a good idea 105 | // anyways for the super long lines ahead! 106 | // ble.setInterCharWriteDelay(5); // 5 ms 107 | 108 | /* Change the device name to make it easier to find */ 109 | Serial.println(F("Setting device name to 'Bluefruit HRM': ")); 110 | 111 | if (! ble.sendCommandCheckOK(F("AT+GAPDEVNAME=Bluefruit HRM")) ) { 112 | error(F("Could not set device name?")); 113 | } 114 | 115 | /* Add the Heart Rate Service definition */ 116 | /* Service ID should be 1 */ 117 | Serial.println(F("Adding the Heart Rate Service definition (UUID = 0x180D): ")); 118 | success = ble.sendCommandWithIntReply( F("AT+GATTADDSERVICE=UUID=0x180D"), &hrmServiceId); 119 | if (! success) { 120 | error(F("Could not add HRM service")); 121 | } 122 | 123 | /* Add the Heart Rate Measurement characteristic */ 124 | /* Chars ID for Measurement should be 1 */ 125 | Serial.println(F("Adding the Heart Rate Measurement characteristic (UUID = 0x2A37): ")); 126 | success = ble.sendCommandWithIntReply( F("AT+GATTADDCHAR=UUID=0x2A37, PROPERTIES=0x10, MIN_LEN=2, MAX_LEN=3, VALUE=00-40"), &hrmMeasureCharId); 127 | if (! success) { 128 | error(F("Could not add HRM characteristic")); 129 | } 130 | 131 | /* Add the Body Sensor Location characteristic */ 132 | /* Chars ID for Body should be 2 */ 133 | Serial.println(F("Adding the Body Sensor Location characteristic (UUID = 0x2A38): ")); 134 | success = ble.sendCommandWithIntReply( F("AT+GATTADDCHAR=UUID=0x2A38, PROPERTIES=0x02, MIN_LEN=1, VALUE=3"), &hrmLocationCharId); 135 | if (! success) { 136 | error(F("Could not add BSL characteristic")); 137 | } 138 | 139 | /* Add the Heart Rate Service to the advertising data (needed for Nordic apps to detect the service) */ 140 | Serial.print(F("Adding Heart Rate Service UUID to the advertising payload: ")); 141 | ble.sendCommandCheckOK( F("AT+GAPSETADVDATA=02-01-06-05-02-0d-18-0a-18") ); 142 | 143 | /* Reset the device for the new service setting changes to take effect */ 144 | Serial.print(F("Performing a SW reset (service changes require a reset): ")); 145 | ble.reset(); 146 | 147 | Serial.println(); 148 | } 149 | 150 | /** Send randomized heart rate data continuously **/ 151 | void loop(void) 152 | { 153 | int heart_rate = random(50, 100); 154 | 155 | Serial.print(F("Updating HRM value to ")); 156 | Serial.print(heart_rate); 157 | Serial.println(F(" BPM")); 158 | 159 | /* Command is sent when \n (\r) or println is called */ 160 | /* AT+GATTCHAR=CharacteristicID,value */ 161 | ble.print( F("AT+GATTCHAR=") ); 162 | ble.print( hrmMeasureCharId ); 163 | ble.print( F(",00-") ); 164 | ble.println(heart_rate, HEX); 165 | 166 | /* Check if command executed OK */ 167 | if ( !ble.waitForOK() ) 168 | { 169 | Serial.println(F("Failed to get response!")); 170 | } 171 | 172 | /* Delay before next measurement update */ 173 | delay(1000); 174 | } 175 | -------------------------------------------------------------------------------- /examples/hidcontrolkey/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // HARDWARE SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. 41 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI. 42 | // ---------------------------------------------------------------------------------------------- 43 | #define BLUEFRUIT_SPI_CS 8 44 | #define BLUEFRUIT_SPI_IRQ 7 45 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 46 | -------------------------------------------------------------------------------- /examples/hidkeyboard/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // HARDWARE SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. 41 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI. 42 | // ---------------------------------------------------------------------------------------------- 43 | #define BLUEFRUIT_SPI_CS 8 44 | #define BLUEFRUIT_SPI_IRQ 7 45 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 46 | -------------------------------------------------------------------------------- /examples/hidmouse/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // HARDWARE SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. 41 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI. 42 | // ---------------------------------------------------------------------------------------------- 43 | #define BLUEFRUIT_SPI_CS 8 44 | #define BLUEFRUIT_SPI_IRQ 7 45 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 46 | -------------------------------------------------------------------------------- /examples/midi/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN 12 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN -1 // Set to -1 if unused 35 | 36 | 37 | // HARDWARE SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. 41 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI. 42 | // ---------------------------------------------------------------------------------------------- 43 | #define BLUEFRUIT_SPI_CS 8 44 | #define BLUEFRUIT_SPI_IRQ 7 45 | #define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused 46 | -------------------------------------------------------------------------------- /examples/midi/midi.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Adafruit_BLE.h" 4 | #include "Adafruit_BluefruitLE_SPI.h" 5 | #include "Adafruit_BluefruitLE_UART.h" 6 | #include "Adafruit_BLEMIDI.h" 7 | #if SOFTWARE_SERIAL_AVAILABLE 8 | #include 9 | #endif 10 | 11 | #include "BluefruitConfig.h" 12 | 13 | #define FACTORYRESET_ENABLE 1 14 | #define MINIMUM_FIRMWARE_VERSION "0.7.0" 15 | 16 | // This app was tested on iOS with the following apps: 17 | // 18 | // https://itunes.apple.com/us/app/midimittr/id925495245?mt=8 19 | // https://itunes.apple.com/us/app/igrand-piano-free-for-ipad/id562914032?mt=8 20 | // 21 | // To test: 22 | // - Run this sketch and open the Serial Monitor 23 | // - Open the iGrand Piano Free app 24 | // - Open the midimittr app on your phone and under Clients select "Adafruit Bluefruit LE" 25 | // - When you see the 'Connected' label switch to the Routing panel 26 | // - Set the Destination to 'iGrand Piano' 27 | // - Switch to the iGrand Piano Free app and you should see notes playing one by one 28 | 29 | // Create the bluefruit object, either software serial...uncomment these lines 30 | /* 31 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 32 | 33 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 34 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 35 | */ 36 | 37 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 38 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 39 | 40 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 41 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 42 | 43 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 44 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 45 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 46 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 47 | 48 | Adafruit_BLEMIDI midi(ble); 49 | 50 | bool isConnected = false; 51 | int current_note = 60; 52 | 53 | // A small helper 54 | void error(const __FlashStringHelper*err) { 55 | Serial.println(err); 56 | while (1); 57 | } 58 | 59 | // callback 60 | void connected(void) 61 | { 62 | isConnected = true; 63 | 64 | Serial.println(F(" CONNECTED!")); 65 | delay(1000); 66 | 67 | } 68 | 69 | void disconnected(void) 70 | { 71 | Serial.println("disconnected"); 72 | isConnected = false; 73 | } 74 | 75 | void BleMidiRX(uint16_t timestamp, uint8_t status, uint8_t byte1, uint8_t byte2) 76 | { 77 | Serial.print("[MIDI "); 78 | Serial.print(timestamp); 79 | Serial.print(" ] "); 80 | 81 | Serial.print(status, HEX); Serial.print(" "); 82 | Serial.print(byte1 , HEX); Serial.print(" "); 83 | Serial.print(byte2 , HEX); Serial.print(" "); 84 | 85 | Serial.println(); 86 | } 87 | 88 | void setup(void) 89 | { 90 | while (!Serial); // required for Flora & Micro 91 | delay(500); 92 | 93 | Serial.begin(115200); 94 | Serial.println(F("Adafruit Bluefruit MIDI Example")); 95 | Serial.println(F("---------------------------------------")); 96 | 97 | /* Initialise the module */ 98 | Serial.print(F("Initialising the Bluefruit LE module: ")); 99 | 100 | if ( !ble.begin(VERBOSE_MODE) ) 101 | { 102 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 103 | } 104 | Serial.println( F("OK!") ); 105 | 106 | if ( FACTORYRESET_ENABLE ) 107 | { 108 | /* Perform a factory reset to make sure everything is in a known state */ 109 | Serial.println(F("Performing a factory reset: ")); 110 | if ( ! ble.factoryReset() ) { 111 | error(F("Couldn't factory reset")); 112 | } 113 | } 114 | 115 | //ble.sendCommandCheckOK(F("AT+uartflow=off")); 116 | ble.echo(false); 117 | 118 | Serial.println("Requesting Bluefruit info:"); 119 | /* Print Bluefruit information */ 120 | ble.info(); 121 | 122 | /* Set BLE callbacks */ 123 | ble.setConnectCallback(connected); 124 | ble.setDisconnectCallback(disconnected); 125 | 126 | // Set MIDI RX callback 127 | midi.setRxCallback(BleMidiRX); 128 | 129 | Serial.println(F("Enable MIDI: ")); 130 | if ( ! midi.begin(true) ) 131 | { 132 | error(F("Could not enable MIDI")); 133 | } 134 | 135 | ble.verbose(false); 136 | Serial.print(F("Waiting for a connection...")); 137 | } 138 | 139 | void loop(void) 140 | { 141 | // interval for each scanning ~ 500ms (non blocking) 142 | ble.update(500); 143 | 144 | // bail if not connected 145 | if (! isConnected) 146 | return; 147 | 148 | Serial.print("Sending pitch "); 149 | Serial.println(current_note, HEX); 150 | 151 | // send note on 152 | midi.send(0x90, current_note, 0x64); 153 | delay(500); 154 | 155 | // send note off 156 | midi.send(0x80, current_note, 0x64); 157 | delay(500); 158 | 159 | // increment note pitch 160 | current_note++; 161 | 162 | // only do one octave 163 | if(current_note > 72) 164 | current_note = 60; 165 | 166 | } 167 | -------------------------------------------------------------------------------- /examples/ndof_bno055/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/ndof_bno055/README.md: -------------------------------------------------------------------------------- 1 | # NDOF BNO055 Example 2 | 3 | This example is intended to be used with a board like the [Bluefruit LE Micro](https://www.adafruit.com/product/2661). 4 | 5 | It will read data samples from the [BNO055](https://www.adafruit.com/products/2472) 6 | at a fixed rate, and transmit the data over the air using the BLE UART service. 7 | 8 | # Requirements 9 | 10 | To use this example, you will also need the following libraries installed on 11 | your system: 12 | 13 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 14 | - [Adafruit_BNO055](https://github.com/adafruit/Adafruit_BNO055) 15 | 16 | ## Wiring 17 | 18 | Connect your Bluefruit LE Micro to the BNO055 breakout using I2C as follows: 19 | 20 | - VIN on the BNO055 to USB on the Bluefruit LE Micro 21 | - GND on the BNO055 to GND on the Bluefruit LE Micro 22 | - SDA on the BNO055 to SDA on the Bluefruit LE Micro 23 | - SCL on the BNO055 to SCL on the Bluefruit LE Micro 24 | -------------------------------------------------------------------------------- /examples/neopixel/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | #define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response 8 | 9 | 10 | // SOFTWARE UART SETTINGS 11 | // ---------------------------------------------------------------------------------------------- 12 | // The following macros declare the pins that will be used for 'SW' serial. 13 | // You should use this option if you are connecting the UART Friend to an UNO 14 | // ---------------------------------------------------------------------------------------------- 15 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 16 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 17 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 18 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 19 | 20 | 21 | // HARDWARE UART SETTINGS 22 | // ---------------------------------------------------------------------------------------------- 23 | // The following macros declare the HW serial port you are using. Uncomment 24 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 25 | // ---------------------------------------------------------------------------------------------- 26 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 27 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 28 | #endif 29 | 30 | 31 | // SHARED UART SETTINGS 32 | // ---------------------------------------------------------------------------------------------- 33 | // The following sets the optional Mode pin, its recommended but not required 34 | // ---------------------------------------------------------------------------------------------- 35 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 36 | 37 | 38 | // SHARED SPI SETTINGS 39 | // ---------------------------------------------------------------------------------------------- 40 | // The following macros declare the pins to use for HW and SW SPI communication. 41 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 42 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 43 | // that use SPI (Bluefruit LE SPI Friend). 44 | // ---------------------------------------------------------------------------------------------- 45 | #define BLUEFRUIT_SPI_CS 8 46 | #define BLUEFRUIT_SPI_IRQ 7 47 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 48 | 49 | // SOFTWARE SPI SETTINGS 50 | // ---------------------------------------------------------------------------------------------- 51 | // The following macros declare the pins to use for SW SPI communication. 52 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 53 | // (Bluefruit LE SPI Friend). 54 | // ---------------------------------------------------------------------------------------------- 55 | #define BLUEFRUIT_SPI_SCK 13 56 | #define BLUEFRUIT_SPI_MISO 12 57 | #define BLUEFRUIT_SPI_MOSI 11 58 | -------------------------------------------------------------------------------- /examples/neopixel_picker/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | #define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response 8 | 9 | 10 | // SOFTWARE UART SETTINGS 11 | // ---------------------------------------------------------------------------------------------- 12 | // The following macros declare the pins that will be used for 'SW' serial. 13 | // You should use this option if you are connecting the UART Friend to an UNO 14 | // ---------------------------------------------------------------------------------------------- 15 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 16 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 17 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 18 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 19 | 20 | 21 | // HARDWARE UART SETTINGS 22 | // ---------------------------------------------------------------------------------------------- 23 | // The following macros declare the HW serial port you are using. Uncomment 24 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 25 | // ---------------------------------------------------------------------------------------------- 26 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 27 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 28 | #endif 29 | 30 | 31 | // SHARED UART SETTINGS 32 | // ---------------------------------------------------------------------------------------------- 33 | // The following sets the optional Mode pin, its recommended but not required 34 | // ---------------------------------------------------------------------------------------------- 35 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 36 | 37 | 38 | // SHARED SPI SETTINGS 39 | // ---------------------------------------------------------------------------------------------- 40 | // The following macros declare the pins to use for HW and SW SPI communication. 41 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 42 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 43 | // that use SPI (Bluefruit LE SPI Friend). 44 | // ---------------------------------------------------------------------------------------------- 45 | #define BLUEFRUIT_SPI_CS 8 46 | #define BLUEFRUIT_SPI_IRQ 7 47 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 48 | 49 | // SOFTWARE SPI SETTINGS 50 | // ---------------------------------------------------------------------------------------------- 51 | // The following macros declare the pins to use for SW SPI communication. 52 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 53 | // (Bluefruit LE SPI Friend). 54 | // ---------------------------------------------------------------------------------------------- 55 | #define BLUEFRUIT_SPI_SCK 13 56 | #define BLUEFRUIT_SPI_MISO 12 57 | #define BLUEFRUIT_SPI_MOSI 11 58 | -------------------------------------------------------------------------------- /examples/neopixel_picker/packetParser.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) 5 | #include 6 | #endif 7 | 8 | #include "Adafruit_BLE.h" 9 | #include "Adafruit_BluefruitLE_SPI.h" 10 | #include "Adafruit_BluefruitLE_UART.h" 11 | 12 | 13 | #define PACKET_ACC_LEN (15) 14 | #define PACKET_GYRO_LEN (15) 15 | #define PACKET_MAG_LEN (15) 16 | #define PACKET_QUAT_LEN (19) 17 | #define PACKET_BUTTON_LEN (5) 18 | #define PACKET_COLOR_LEN (6) 19 | #define PACKET_LOCATION_LEN (15) 20 | 21 | // READ_BUFSIZE Size of the read buffer for incoming packets 22 | #define READ_BUFSIZE (20) 23 | 24 | 25 | /* Buffer to hold incoming characters */ 26 | uint8_t packetbuffer[READ_BUFSIZE+1]; 27 | 28 | /**************************************************************************/ 29 | /*! 30 | @brief Casts the four bytes at the specified address to a float 31 | */ 32 | /**************************************************************************/ 33 | float parsefloat(uint8_t *buffer) 34 | { 35 | float f = ((float *)buffer)[0]; 36 | return f; 37 | } 38 | 39 | /**************************************************************************/ 40 | /*! 41 | @brief Prints a hexadecimal value in plain characters 42 | @param data Pointer to the byte data 43 | @param numBytes Data length in bytes 44 | */ 45 | /**************************************************************************/ 46 | void printHex(const uint8_t * data, const uint32_t numBytes) 47 | { 48 | uint32_t szPos; 49 | for (szPos=0; szPos < numBytes; szPos++) 50 | { 51 | Serial.print(F("0x")); 52 | // Append leading 0 for small values 53 | if (data[szPos] <= 0xF) 54 | { 55 | Serial.print(F("0")); 56 | Serial.print(data[szPos] & 0xf, HEX); 57 | } 58 | else 59 | { 60 | Serial.print(data[szPos] & 0xff, HEX); 61 | } 62 | // Add a trailing space if appropriate 63 | if ((numBytes > 1) && (szPos != numBytes - 1)) 64 | { 65 | Serial.print(F(" ")); 66 | } 67 | } 68 | Serial.println(); 69 | } 70 | 71 | /**************************************************************************/ 72 | /*! 73 | @brief Waits for incoming data and parses it 74 | */ 75 | /**************************************************************************/ 76 | uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) 77 | { 78 | uint16_t origtimeout = timeout, replyidx = 0; 79 | 80 | memset(packetbuffer, 0, READ_BUFSIZE); 81 | 82 | while (timeout--) { 83 | if (replyidx >= 20) break; 84 | if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) 85 | break; 86 | if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) 87 | break; 88 | if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN)) 89 | break; 90 | if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN)) 91 | break; 92 | if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN)) 93 | break; 94 | if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN)) 95 | break; 96 | if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN)) 97 | break; 98 | 99 | while (ble->available()) { 100 | char c = ble->read(); 101 | if (c == '!') { 102 | replyidx = 0; 103 | } 104 | packetbuffer[replyidx] = c; 105 | replyidx++; 106 | timeout = origtimeout; 107 | } 108 | 109 | if (timeout == 0) break; 110 | delay(1); 111 | } 112 | 113 | packetbuffer[replyidx] = 0; // null term 114 | 115 | if (!replyidx) // no data or timeout 116 | return 0; 117 | if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning 118 | return 0; 119 | 120 | // check checksum! 121 | uint8_t xsum = 0; 122 | uint8_t checksum = packetbuffer[replyidx-1]; 123 | 124 | for (uint8_t i=0; i 16 | #include 17 | #include "Adafruit_BLE.h" 18 | #include "Adafruit_BluefruitLE_SPI.h" 19 | #include "Adafruit_BluefruitLE_UART.h" 20 | #if SOFTWARE_SERIAL_AVAILABLE 21 | #include 22 | #endif 23 | 24 | #include "BluefruitConfig.h" 25 | 26 | // Create the bluefruit object, either software serial...uncomment these lines 27 | /* 28 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 29 | 30 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 31 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 32 | */ 33 | 34 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 35 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 36 | 37 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 38 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 39 | 40 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 41 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 42 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 43 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 44 | 45 | 46 | // A small helper 47 | void error(const __FlashStringHelper*err) { 48 | Serial.println(err); 49 | while (1); 50 | } 51 | 52 | /* This demo perform factory reset on the first run then 53 | * - Write a defined magic number at offset 0 54 | * - Write a DATE string at offset 16 55 | * - Dump the whole NVM section, User's NVM is started at 0x0640 56 | */ 57 | 58 | #define MAGIC_NUMBER 0xC0FFEE 59 | #define MAGIC_STRING __DATE__ 60 | 61 | /**************************************************************************/ 62 | /*! 63 | @brief Sets up the HW an the BLE module (this function is called 64 | automatically on startup) 65 | */ 66 | /**************************************************************************/ 67 | void setup(void) 68 | { 69 | while (!Serial); // required for Flora & Micro 70 | delay(500); 71 | 72 | Serial.begin(115200); 73 | Serial.println(F("Adafruit Bluefruit NVM DATA Example")); 74 | Serial.println(F("-------------------------------------")); 75 | 76 | /* Initialise the module */ 77 | Serial.print(F("Initialising the Bluefruit LE module: ")); 78 | 79 | if ( !ble.begin(VERBOSE_MODE) ) 80 | { 81 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 82 | } 83 | Serial.println( F("OK!") ); 84 | 85 | /* Disable command echo from Bluefruit */ 86 | // ble.echo(false); 87 | 88 | Serial.println("Requesting Bluefruit info:"); 89 | /* Print Bluefruit information */ 90 | ble.info(); 91 | 92 | int32_t magic_number; 93 | ble.readNVM(0, &magic_number); 94 | 95 | if ( magic_number != MAGIC_NUMBER ) 96 | { 97 | /* Perform a factory reset to make sure everything is in a known state */ 98 | Serial.println(F("Magic not found: performing a factory reset: ")); 99 | if ( ! ble.factoryReset() ){ 100 | error(F("Couldn't factory reset")); 101 | } 102 | 103 | // Write data to NVM 104 | Serial.println( F("Write defined data to NVM") ); 105 | ble.writeNVM(0 , MAGIC_NUMBER); 106 | ble.writeNVM(16, MAGIC_STRING); 107 | }else 108 | { 109 | Serial.println(F("Magic found")); 110 | } 111 | 112 | // Read from NVM and print out 113 | ble.readNVM(0, &magic_number); 114 | Serial.print( F("Magic Number: ") ); 115 | Serial.println( magic_number ); 116 | 117 | char magic_str[32]; 118 | ble.readNVM(16, magic_str, sizeof(magic_str)); 119 | Serial.print( F("Magic String: ") ); 120 | Serial.println( magic_str ); 121 | 122 | // Dump the whole NVM section 123 | Serial.println(); 124 | Serial.println("Dumping the whole NVM contents"); 125 | ble.atcommand(F("AT+DBGNVMRD")); 126 | Serial.println("User NVM data is at offset 0x0640"); 127 | } 128 | 129 | /**************************************************************************/ 130 | /*! 131 | @brief Constantly poll for new command or response data 132 | */ 133 | /**************************************************************************/ 134 | void loop(void) 135 | { 136 | // Display command prompt 137 | Serial.print(F("AT > ")); 138 | 139 | // Check for user input and echo it back if anything was found 140 | char command[BUFSIZE+1]; 141 | getUserInput(command, BUFSIZE); 142 | 143 | // Send command 144 | ble.atcommand(command); 145 | } 146 | 147 | /**************************************************************************/ 148 | /*! 149 | @brief Checks for user input (via the Serial Monitor) 150 | */ 151 | /**************************************************************************/ 152 | void getUserInput(char buffer[], uint8_t maxSize) 153 | { 154 | memset(buffer, 0, maxSize); 155 | while( Serial.available() == 0 ) { 156 | delay(1); 157 | } 158 | 159 | uint8_t count=0; 160 | 161 | do 162 | { 163 | count += Serial.readBytes(buffer+count, maxSize); 164 | delay(2); 165 | } while( (count < maxSize) && !(Serial.available() == 0) ); 166 | } 167 | -------------------------------------------------------------------------------- /examples/throughput/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/uribeacon/BluefruitConfig.h: -------------------------------------------------------------------------------- 1 | // COMMON SETTINGS 2 | // ---------------------------------------------------------------------------------------------- 3 | // These settings are used in both SW UART, HW UART and SPI mode 4 | // ---------------------------------------------------------------------------------------------- 5 | #define BUFSIZE 128 // Size of the read buffer for incoming data 6 | #define VERBOSE_MODE true // If set to 'true' enables debug output 7 | 8 | 9 | // SOFTWARE UART SETTINGS 10 | // ---------------------------------------------------------------------------------------------- 11 | // The following macros declare the pins that will be used for 'SW' serial. 12 | // You should use this option if you are connecting the UART Friend to an UNO 13 | // ---------------------------------------------------------------------------------------------- 14 | #define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! 15 | #define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! 16 | #define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! 17 | #define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused 18 | 19 | 20 | // HARDWARE UART SETTINGS 21 | // ---------------------------------------------------------------------------------------------- 22 | // The following macros declare the HW serial port you are using. Uncomment 23 | // this line if you are connecting the BLE to Leonardo/Micro or Flora 24 | // ---------------------------------------------------------------------------------------------- 25 | #ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 26 | #define BLUEFRUIT_HWSERIAL_NAME Serial1 27 | #endif 28 | 29 | 30 | // SHARED UART SETTINGS 31 | // ---------------------------------------------------------------------------------------------- 32 | // The following sets the optional Mode pin, its recommended but not required 33 | // ---------------------------------------------------------------------------------------------- 34 | #define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused 35 | 36 | 37 | // SHARED SPI SETTINGS 38 | // ---------------------------------------------------------------------------------------------- 39 | // The following macros declare the pins to use for HW and SW SPI communication. 40 | // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when 41 | // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules 42 | // that use SPI (Bluefruit LE SPI Friend). 43 | // ---------------------------------------------------------------------------------------------- 44 | #define BLUEFRUIT_SPI_CS 8 45 | #define BLUEFRUIT_SPI_IRQ 7 46 | #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused 47 | 48 | // SOFTWARE SPI SETTINGS 49 | // ---------------------------------------------------------------------------------------------- 50 | // The following macros declare the pins to use for SW SPI communication. 51 | // This should be used with nRF51822 based Bluefruit LE modules that use SPI 52 | // (Bluefruit LE SPI Friend). 53 | // ---------------------------------------------------------------------------------------------- 54 | #define BLUEFRUIT_SPI_SCK 13 55 | #define BLUEFRUIT_SPI_MISO 12 56 | #define BLUEFRUIT_SPI_MOSI 11 57 | -------------------------------------------------------------------------------- /examples/uribeacon/uribeacon.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | This is an example for our nRF51822 based Bluefruit LE modules 3 | 4 | Pick one up today in the adafruit shop! 5 | 6 | Adafruit invests time and resources providing this open source code, 7 | please support Adafruit and open-source hardware by purchasing 8 | products from Adafruit! 9 | 10 | MIT license, check LICENSE for more information 11 | All text above, and the splash screen below must be included in 12 | any redistribution 13 | *********************************************************************/ 14 | 15 | #include 16 | #include 17 | #include "Adafruit_BLE.h" 18 | #include "Adafruit_BluefruitLE_SPI.h" 19 | #include "Adafruit_BluefruitLE_UART.h" 20 | #if SOFTWARE_SERIAL_AVAILABLE 21 | #include 22 | #endif 23 | 24 | #include "BluefruitConfig.h" 25 | 26 | /*========================================================================= 27 | APPLICATION SETTINGS 28 | 29 | FACTORYRESET_ENABLE Perform a factory reset when running this sketch 30 | 31 | Enabling this will put your Bluefruit LE module 32 | in a 'known good' state and clear any config 33 | data set in previous sketches or projects, so 34 | running this at least once is a good idea. 35 | 36 | When deploying your project, however, you will 37 | want to disable factory reset by setting this 38 | value to 0.  If you are making changes to your 39 | Bluefruit LE device via AT commands, and those 40 | changes aren't persisting across resets, this 41 | is the reason why.  Factory reset will erase 42 | the non-volatile memory where config data is 43 | stored, setting it back to factory default 44 | values. 45 | -----------------------------------------------------------------------*/ 46 | #define FACTORYRESET_ENABLE 1 47 | #define URL "http://www.adafruit.com" 48 | /*=========================================================================*/ 49 | 50 | 51 | // Create the bluefruit object, either software serial...uncomment these lines 52 | /* 53 | SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); 54 | 55 | Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, 56 | BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); 57 | */ 58 | 59 | /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ 60 | // Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); 61 | 62 | /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ 63 | Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 64 | 65 | /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ 66 | //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, 67 | // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, 68 | // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 69 | 70 | // A small helper 71 | void error(const __FlashStringHelper*err) { 72 | Serial.println(err); 73 | while (1); 74 | } 75 | 76 | /**************************************************************************/ 77 | /*! 78 | @brief Sets up the HW an the BLE module (this function is called 79 | automatically on startup) 80 | */ 81 | /**************************************************************************/ 82 | void setup(void) 83 | { 84 | while (!Serial); // required for Flora & Micro 85 | delay(500); 86 | 87 | Serial.begin(115200); 88 | Serial.println(F("Adafruit Bluefruit UriBeacon Example")); 89 | Serial.println(F("------------------------------------")); 90 | 91 | /* Initialise the module */ 92 | Serial.print(F("Initialising the Bluefruit LE module: ")); 93 | 94 | if ( !ble.begin(VERBOSE_MODE) ) 95 | { 96 | error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 97 | } 98 | Serial.println( F("OK!") ); 99 | 100 | if ( FACTORYRESET_ENABLE ) 101 | { 102 | /* Perform a factory reset to make sure everything is in a known state */ 103 | Serial.println(F("Performing a factory reset: ")); 104 | if ( ! ble.factoryReset() ){ 105 | error(F("Couldn't factory reset")); 106 | } 107 | } 108 | 109 | /* Disable command echo from Bluefruit */ 110 | ble.echo(false); 111 | 112 | Serial.println("Requesting Bluefruit info:"); 113 | /* Print Bluefruit information */ 114 | ble.info(); 115 | 116 | // Prompt user to migrate to EddyStone 117 | Serial.println(F("Please consider to migrate to EddyStone since Google has retired uribeacon")); 118 | 119 | /* Set EddyStone URL beacon data */ 120 | Serial.println(F("Setting uri beacon to Adafruit website: ")); 121 | 122 | // Older firmware use AT+BLEURIBEACON command 123 | if (! ble.sendCommandCheckOK(F( "AT+BLEURIBEACON=" URL ))) { 124 | error(F("Couldnt set, is URL too long !?")); 125 | } 126 | 127 | Serial.println(F("**************************************************")); 128 | Serial.println(F("Please use Google Physical Web application to test")); 129 | Serial.println(F("**************************************************")); 130 | } 131 | 132 | /**************************************************************************/ 133 | /*! 134 | @brief Constantly poll for new command or response data 135 | */ 136 | /**************************************************************************/ 137 | void loop(void) 138 | { 139 | 140 | } 141 | 142 | /**************************************************************************/ 143 | /*! 144 | @brief Checks for user input (via the Serial Monitor) 145 | */ 146 | /**************************************************************************/ 147 | void getUserInput(char buffer[], uint8_t maxSize) 148 | { 149 | memset(buffer, 0, maxSize); 150 | while( Serial.available() == 0 ) { 151 | delay(1); 152 | } 153 | 154 | uint8_t count=0; 155 | 156 | do 157 | { 158 | count += Serial.readBytes(buffer+count, maxSize); 159 | delay(2); 160 | } while( (count < maxSize) && !(Serial.available() == 0) ); 161 | } 162 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Bluefruit 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Adafruit_BLE KEYWORD1 10 | Adafruit_BluefruitLE_SPI KEYWORD1 11 | Adafruit_BluefruitLE_UART KEYWORD1 12 | Adafruit_ATParser KEYWORD1 13 | 14 | Adafruit_BLEBattery KEYWORD1 15 | Adafruit_BLEEddystone KEYWORD1 16 | Adafruit_BLEGatt KEYWORD1 17 | Adafruit_BLEMIDI KEYWORD1 18 | 19 | Adafruit_FIFO KEYWORD1 20 | TimeoutTimer KEYWORD1 21 | 22 | ####################################### 23 | # Adafruit_BLE 24 | ####################################### 25 | reset KEYWORD2 26 | factoryReset KEYWORD2 27 | info KEYWORD2 28 | echo KEYWORD2 29 | isConnected KEYWORD2 30 | isVersionAtLeast KEYWORD2 31 | disconnect KEYWORD2 32 | 33 | setAdvData KEYWORD2 34 | writeNVM KEYWORD2 35 | readNVM KEYWORD2 36 | update KEYWORD2 37 | writeBLEUart KEYWORD2 38 | readBLEUart KEYWORD2 39 | setDisconnectCallback KEYWORD2 40 | setConnectCallback KEYWORD2 41 | setBleUartRxCallback KEYWORD2 42 | setBleMidiRxCallback KEYWORD2 43 | setBleGattRxCallback KEYWORD2 44 | 45 | sendCommandCheckOK KEYWORD2 46 | sendCommandWithIntReply KEYWORD2 47 | 48 | ####################################### 49 | # Adafruit_ATParser 50 | ####################################### 51 | atcommand KEYWORD2 52 | atcommandIntReply KEYWORD2 53 | atcommand_full KEYWORD2 54 | getMode KEYWORD2 55 | setMode KEYWORD2 56 | verbose KEYWORD2 57 | readline KEYWORD2 58 | readline_parseInt KEYWORD2 59 | waitForOK KEYWORD2 60 | readraw KEYWORD2 61 | printByteArray KEYWORD2 62 | 63 | ####################################### 64 | # Adafruit_BLEBattery 65 | ####################################### 66 | update KEYWORD2 67 | 68 | ####################################### 69 | # Adafruit_BLEEddystone 70 | ####################################### 71 | setURL KEYWORD2 72 | startBroadcast KEYWORD2 73 | stopBroadcast KEYWORD2 74 | startConfigMode KEYWORD2 75 | 76 | ####################################### 77 | # Adafruit_BLEGatt 78 | ####################################### 79 | addService KEYWORD2 80 | addCharacteristic KEYWORD2 81 | getChar KEYWORD2 82 | getCharInt8 KEYWORD2 83 | getCharInt16 KEYWORD2 84 | getCharInt32 KEYWORD2 85 | getCharStr KEYWORD2 86 | setChar KEYWORD2 87 | 88 | # Constants (LITERAL1) 89 | GATT_CHARS_PROPERTIES_BROADCAST LITERAL1 90 | GATT_CHARS_PROPERTIES_READ LITERAL1 91 | GATT_CHARS_PROPERTIES_WRITE_WO_RESP LITERAL1 92 | GATT_CHARS_PROPERTIES_WRITE LITERAL1 93 | GATT_CHARS_PROPERTIES_NOTIFY LITERAL1 94 | GATT_CHARS_PROPERTIES_INDICATE LITERAL1 95 | 96 | ####################################### 97 | # Adafruit_BLEMIDI 98 | ####################################### 99 | send KEYWORD2 100 | send_n KEYWORD2 101 | setRxCallback KEYWORD2 102 | 103 | ####################################### 104 | # Constants (LITERAL1) 105 | ####################################### 106 | BLUEFRUIT_MODE_COMMAND LITERAL1 107 | BLUEFRUIT_MODE_DATA LITERAL1 108 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit BluefruitLE nRF51 2 | version=1.10.0 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Arduino library for nRF51822-based Adafruit Bluefruit LE modules 6 | paragraph=Arduino library for nRF51822-based Adafruit Bluefruit LE modules 7 | category=Communication 8 | url=https://github.com/adafruit/Adafruit_BluefruitLE_nRF51 9 | architectures=* 10 | -------------------------------------------------------------------------------- /utility/Adafruit_FIFO.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_FIFO.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2015, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #ifndef _Adafruit_FIFO_H_ 38 | #define _Adafruit_FIFO_H_ 39 | 40 | #include 41 | #include 42 | 43 | class Adafruit_FIFO 44 | { 45 | private: 46 | uint8_t* m_buffer ; ///< buffer pointer 47 | uint16_t m_depth ; ///< max items 48 | uint8_t m_item_size ; ///< size of each item 49 | bool m_overwritable ; 50 | volatile uint16_t m_count ; ///< number of items in queue 51 | volatile uint16_t m_wr_idx ; ///< write pointer 52 | volatile uint16_t m_rd_idx ; ///< read pointer 53 | 54 | public: 55 | // Constructor 56 | Adafruit_FIFO(void* buffer, uint16_t depth, uint8_t item_size, bool overwrite); 57 | 58 | void clear(void); 59 | bool peek(void* buffer); 60 | bool peekAt(uint16_t position, void * p_buffer); 61 | 62 | bool write(void const* item); 63 | uint16_t write_n(void const * data, uint16_t n); 64 | 65 | bool read(void* buffer); 66 | uint16_t read_n (void * buffer, uint16_t n); 67 | 68 | inline bool empty(void) { return m_count == 0; } 69 | inline bool full(void) { return m_count == m_depth; } 70 | inline uint16_t count(void) { return m_count; } 71 | inline uint16_t remaining(void) { return m_depth - m_count; } 72 | }; 73 | 74 | #endif /* _Adafruit_FIFO_H_ */ 75 | -------------------------------------------------------------------------------- /utility/TimeoutTimer.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file TimeoutTimer.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2014, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | /** 38 | * @{ 39 | */ 40 | 41 | #ifndef _TIMEOUT_TIMER_H_ 42 | #define _TIMEOUT_TIMER_H_ 43 | 44 | class TimeoutTimer 45 | { 46 | private: 47 | uint32_t start; 48 | uint32_t interval; 49 | 50 | public: 51 | TimeoutTimer() { start = millis(); interval = 0; } 52 | TimeoutTimer(uint32_t msec) { set(msec); } 53 | 54 | void set(uint32_t msec) { start = millis(); interval = msec; } 55 | bool expired(void) const { return (millis() - start) >= interval; } 56 | void restart(void) { start = millis(); } 57 | void reset(void) { start += interval; } // used for periodic invoke to prevent drift 58 | }; 59 | 60 | #endif /* _TIMEOUT_TIMER_H_ */ 61 | 62 | /** @} */ 63 | -------------------------------------------------------------------------------- /utility/common_header.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file common_header.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #ifndef _COMMON_HEADER_H_ 38 | #define _COMMON_HEADER_H_ 39 | 40 | #include 41 | #include 42 | 43 | //--------------------------------------------------------------------+ 44 | // COMPILER 45 | //--------------------------------------------------------------------+ 46 | #define STRING_(x) #x // stringify without expand 47 | #define XSTRING_(x) STRING_(x) // expand then stringify 48 | #define STRING_CONCAT_(a, b) a##b // concat without expand 49 | #define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat 50 | 51 | #define ATTR_PACKED __attribute__ ((packed)) 52 | 53 | //--------------------------------------------------------------------+ 54 | // ASSERT & VERIFY 55 | //--------------------------------------------------------------------+ 56 | //#define ASSERT(condition, err) if ( !(condition) ) return err; 57 | 58 | //------------- Compile-time Assert -------------// 59 | #if defined __COUNTER__ && __COUNTER__ != __COUNTER__ 60 | #define _ASSERT_COUNTER __COUNTER__ 61 | #else 62 | #define _ASSERT_COUNTER __LINE__ 63 | #endif 64 | 65 | #define ASSERT_STATIC_(const_expr) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) } 66 | 67 | 68 | #define VERIFY_(condition) if ( !(condition) ) return false; 69 | #define VERIFY_RETURN_(condition, error) if ( !(condition) ) return error; 70 | 71 | //--------------------------------------------------------------------+ 72 | // INLINE FUNCTION 73 | //--------------------------------------------------------------------+ 74 | static inline bool is_within(uint32_t lower, uint32_t value, uint32_t upper) 75 | { 76 | return (lower <= value) && (value <= upper); 77 | } 78 | 79 | #endif /* _COMMON_HEADER_H_ */ 80 | -------------------------------------------------------------------------------- /utility/sdep.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /*! 3 | @file sdep.h 4 | @author hathach 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2013, K. Townsend (microBuilder.eu) 11 | Copyright (c) 2014, Adafruit Industries (adafruit.com) 12 | 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 1. Redistributions of source code must retain the above copyright 18 | notice, this list of conditions and the following disclaimer. 19 | 2. Redistributions in binary form must reproduce the above copyright 20 | notice, this list of conditions and the following disclaimer in the 21 | documentation and/or other materials provided with the distribution. 22 | 3. Neither the name of the copyright holders nor the 23 | names of its contributors may be used to endorse or promote products 24 | derived from this software without specific prior written permission. 25 | 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 27 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 29 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 30 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 31 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 32 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 33 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | */ 37 | /******************************************************************************/ 38 | #ifndef _SDEP_H_ 39 | #define _SDEP_H_ 40 | 41 | #include "common_header.h" 42 | 43 | #define SDEP_MAX_PACKETSIZE 16 // Maximum payload per packet 44 | 45 | /******************************************************************************/ 46 | /*! 47 | This enumeration is used to make sure that each command has a unique 48 | ID, and is used to create the command lookup table enum further down 49 | */ 50 | /******************************************************************************/ 51 | typedef enum 52 | { 53 | SDEP_CMDTYPE_INITIALIZE = 0xBEEF, /**< Controls the on board LED(s) */ 54 | SDEP_CMDTYPE_AT_WRAPPER = 0x0A00, 55 | SDEP_CMDTYPE_BLE_UARTTX = 0x0A01, 56 | SDEP_CMDTYPE_BLE_UARTRX = 0x0A02, 57 | } sdepCmdType_t; 58 | 59 | /******************************************************************************/ 60 | /*! 61 | The first byte of every transfer defines the message type 62 | */ 63 | /******************************************************************************/ 64 | typedef enum 65 | { 66 | SDEP_MSGTYPE_COMMAND = 0x10, 67 | SDEP_MSGTYPE_RESPONSE = 0x20, 68 | SDEP_MSGTYPE_ALERT = 0x40, 69 | SDEP_MSGTYPE_ERROR = 0x80 70 | } sdepMsgType_t; 71 | 72 | /******************************************************************************/ 73 | /*! 74 | 4-byte header for SDEP messages 75 | */ 76 | /******************************************************************************/ 77 | typedef struct ATTR_PACKED { 78 | uint8_t msg_type; // 8-bit message type indicator (sdepMsgType_t) 79 | 80 | union 81 | { 82 | uint16_t cmd_id; // 16-bit command ID 83 | struct 84 | { 85 | uint8_t cmd_id_low; 86 | uint8_t cmd_id_high; 87 | }; 88 | }; 89 | 90 | struct ATTR_PACKED 91 | { 92 | uint8_t length : 7; // Payload length (for this packet) 93 | uint8_t more_data : 1; // 'more' bit for multiple packet transfers 94 | }; 95 | } sdepMsgHeader_t; 96 | 97 | /******************************************************************************/ 98 | /*! 99 | SDEP command message 100 | */ 101 | /******************************************************************************/ 102 | typedef struct ATTR_PACKED 103 | { 104 | sdepMsgHeader_t header; 105 | uint8_t payload[SDEP_MAX_PACKETSIZE]; 106 | } sdepMsgCommand_t; 107 | 108 | /******************************************************************************/ 109 | /*! 110 | Response message struct (same as sdepMsgCommand_t) 111 | */ 112 | /******************************************************************************/ 113 | typedef sdepMsgCommand_t sdepMsgResponse_t; 114 | 115 | /******************************************************************************/ 116 | /*! 117 | Alert message struct 118 | */ 119 | /******************************************************************************/ 120 | typedef sdepMsgCommand_t sdepMsgAlert_t; 121 | 122 | #endif 123 | --------------------------------------------------------------------------------