diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml new file mode 100644 index 0000000..e5c8337 --- /dev/null +++ b/.github/workflows/githubci.yml @@ -0,0 +1,26 @@ +name: Arduino Library CI + +on: [pull_request, push, repository_dispatch] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/setup-python@v1 + with: + python-version: '3.x' + - uses: actions/checkout@v2 + - uses: actions/checkout@v2 + with: + repository: adafruit/ci-arduino + path: ci + + - name: pre-install + run: bash ci/actions_install.sh + + - name: test platforms + run: python3 ci/build_platform.py cpx_ada metro_m0 + + - name: clang + run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . diff --git a/Adafruit_ATParser.cpp b/Adafruit_ATParser.cpp index c23276b..cd5ff83 100644 --- a/Adafruit_ATParser.cpp +++ b/Adafruit_ATParser.cpp @@ -29,16 +29,15 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include "Adafruit_ATParser.h" -static inline char digit2ascii(uint8_t digit) -{ - return ( digit + ((digit) < 10 ? '0' : ('A'-10)) ); +static inline char digit2ascii(uint8_t digit) { + return (digit + ((digit) < 10 ? '0' : ('A' - 10))); } /******************************************************************************/ @@ -46,9 +45,8 @@ static inline char digit2ascii(uint8_t digit) @brief Constructor */ /******************************************************************************/ -Adafruit_ATParser::Adafruit_ATParser(void) -{ - _mode = BLUEFRUIT_MODE_COMMAND; +Adafruit_ATParser::Adafruit_ATParser(void) { + _mode = BLUEFRUIT_MODE_COMMAND; _verbose = false; } @@ -58,16 +56,18 @@ Adafruit_ATParser::Adafruit_ATParser(void) @return true if response is ended with "OK". Otherwise it could be "ERROR" */ /******************************************************************************/ -bool Adafruit_ATParser::waitForOK(void) -{ - if (_verbose) SerialDebug.print( F("\n<- ") ); +bool Adafruit_ATParser::waitForOK(void) { + if (_verbose) + SerialDebug.print(F("\n<- ")); // Use temp buffer to avoid overwrite returned result if any - char tempbuf[BLE_BUFSIZE+1]; + char tempbuf[BLE_BUFSIZE + 1]; - while ( readline(tempbuf, BLE_BUFSIZE) ) { - if ( strcmp(tempbuf, "OK") == 0 ) return true; - if ( strcmp(tempbuf, "ERROR") == 0 ) return false; + while (readline(tempbuf, BLE_BUFSIZE)) { + if (strcmp(tempbuf, "OK") == 0) + return true; + if (strcmp(tempbuf, "ERROR") == 0) + return false; // Copy to internal buffer if not OK or ERROR strcpy(this->buffer, tempbuf); @@ -81,62 +81,61 @@ bool Adafruit_ATParser::waitForOK(void) @param */ /******************************************************************************/ -bool Adafruit_ATParser::send_arg_get_resp(int32_t* reply, uint8_t argcount, uint16_t argtype[], uint32_t args[]) -{ +bool Adafruit_ATParser::send_arg_get_resp(int32_t *reply, uint8_t argcount, + uint16_t argtype[], uint32_t args[]) { // Command arguments according to its type - for(uint8_t i=0; isend_arg_get_resp(reply, argcount, argtype, args); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_DATA); return result; } @@ -174,20 +176,23 @@ bool Adafruit_ATParser::atcommand_full(const char cmd[], int32_t* reply, uint8_t @param */ /******************************************************************************/ -bool Adafruit_ATParser::atcommand_full(const __FlashStringHelper *cmd, int32_t* reply, uint8_t argcount, uint16_t argtype[], uint32_t args[]) -{ +bool Adafruit_ATParser::atcommand_full(const __FlashStringHelper *cmd, + int32_t *reply, uint8_t argcount, + uint16_t argtype[], uint32_t args[]) { bool result; uint8_t current_mode = _mode; // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_COMMAND); // Execute command with parameter and get response print(cmd); result = this->send_arg_get_resp(reply, argcount, argtype, args); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_DATA); return result; } @@ -200,10 +205,10 @@ bool Adafruit_ATParser::atcommand_full(const __FlashStringHelper *cmd, int32_t* data to the end of the line. */ /******************************************************************************/ -int32_t Adafruit_ATParser::readline_parseInt(void) -{ +int32_t Adafruit_ATParser::readline_parseInt(void) { uint16_t len = readline(); - if (len == 0) return 0; + if (len == 0) + return 0; // also parsed hex number e.g 0xADAF int32_t val = strtol(buffer, NULL, 0); @@ -223,20 +228,22 @@ int32_t Adafruit_ATParser::readline_parseInt(void) @note '\r' and '\n' are not included in returned buffer. */ /******************************************************************************/ -uint16_t Adafruit_ATParser::readline(char * buf, uint16_t bufsize, uint16_t timeout, boolean multiline) -{ +uint16_t Adafruit_ATParser::readline(char *buf, uint16_t bufsize, + uint16_t timeout, boolean multiline) { uint16_t replyidx = 0; while (timeout--) { - while(available()) { + while (available()) { char c = read(); - //SerialDebug.println(c); + // SerialDebug.println(c); - if (c == '\r') continue; + if (c == '\r') + continue; if (c == '\n') { // the first '\n' is ignored - if (replyidx == 0) continue; + if (replyidx == 0) + continue; if (!multiline) { timeout = 0; @@ -248,23 +255,25 @@ uint16_t Adafruit_ATParser::readline(char * buf, uint16_t bufsize, uint16_t time // Buffer is full if (replyidx >= bufsize) { - //if (_verbose) { SerialDebug.println("*overflow*"); } // for my debuggin' only! + // if (_verbose) { SerialDebug.println("*overflow*"); } // for my + // debuggin' only! timeout = 0; break; } } // delay if needed - if (timeout) delay(1); + if (timeout) + delay(1); } - buf[replyidx] = 0; // null term + buf[replyidx] = 0; // null term // Print out if is verbose - if (_verbose && replyidx > 0) - { + if (_verbose && replyidx > 0) { SerialDebug.print(buf); - if (replyidx < bufsize) SerialDebug.println(); + if (replyidx < bufsize) + SerialDebug.println(); } return replyidx; @@ -283,26 +292,24 @@ uint16_t Adafruit_ATParser::readline(char * buf, uint16_t bufsize, uint16_t time 0 usually means error */ /******************************************************************************/ -uint16_t Adafruit_ATParser::readraw(uint16_t timeout) -{ +uint16_t Adafruit_ATParser::readraw(uint16_t timeout) { uint16_t replyidx = 0; while (timeout--) { - while(available()) { - char c = read(); + while (available()) { + char c = read(); - if (c == '\n') - { + if (c == '\n') { // done if ends with "OK\r\n" - if ( (replyidx >= 3) && !strncmp(this->buffer + replyidx-3, "OK\r", 3) ) - { + if ((replyidx >= 3) && + !strncmp(this->buffer + replyidx - 3, "OK\r", 3)) { replyidx -= 3; // chop OK\r timeout = 0; break; } // done if ends with "ERROR\r\n" - else if ((replyidx >= 6) && !strncmp(this->buffer + replyidx-6, "ERROR\r", 6)) - { + else if ((replyidx >= 6) && + !strncmp(this->buffer + replyidx - 6, "ERROR\r", 6)) { replyidx -= 6; // chop ERROR\r timeout = 0; break; @@ -314,23 +321,25 @@ uint16_t Adafruit_ATParser::readraw(uint16_t timeout) // Buffer is full if (replyidx >= BLE_BUFSIZE) { - //if (_verbose) { SerialDebug.println("*overflow*"); } // for my debuggin' only! + // if (_verbose) { SerialDebug.println("*overflow*"); } // for my + // debuggin' only! timeout = 0; break; } } - if (timeout == 0) break; + if (timeout == 0) + break; delay(1); } - this->buffer[replyidx] = 0; // null term + this->buffer[replyidx] = 0; // null term // Print out if is verbose -// if (_verbose && replyidx > 0) -// { -// SerialDebug.print(buffer); -// if (replyidx < BLE_BUFSIZE) SerialDebug.println(); -// } + // if (_verbose && replyidx > 0) + // { + // SerialDebug.print(buffer); + // if (replyidx < BLE_BUFSIZE) SerialDebug.println(); + // } return replyidx; } @@ -343,15 +352,14 @@ uint16_t Adafruit_ATParser::readraw(uint16_t timeout) @return number of printed characters */ /******************************************************************************/ -int Adafruit_ATParser::printByteArray(uint8_t const bytearray[], int size) -{ - while(size--) - { +int Adafruit_ATParser::printByteArray(uint8_t const bytearray[], int size) { + while (size--) { uint8_t byte = *bytearray++; - write( digit2ascii((byte & 0xF0) >> 4) ); - write( digit2ascii(byte & 0x0F) ); - if ( size!=0 ) write('-'); + write(digit2ascii((byte & 0xF0) >> 4)); + write(digit2ascii(byte & 0x0F)); + if (size != 0) + write('-'); } - return (size*3) - 1; + return (size * 3) - 1; } diff --git a/Adafruit_ATParser.h b/Adafruit_ATParser.h index d8a5ddf..816157e 100644 --- a/Adafruit_ATParser.h +++ b/Adafruit_ATParser.h @@ -29,23 +29,22 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _ADAFRUIT_ATPARSER_H_ #define _ADAFRUIT_ATPARSER_H_ -#include #include "utility/sdep.h" +#include // Class to facilitate sending AT Command and check response -#define BLUEFRUIT_MODE_COMMAND HIGH -#define BLUEFRUIT_MODE_DATA LOW -#define BLE_BUFSIZE 4*SDEP_MAX_PACKETSIZE - +#define BLUEFRUIT_MODE_COMMAND HIGH +#define BLUEFRUIT_MODE_DATA LOW +#define BLE_BUFSIZE 4 * SDEP_MAX_PACKETSIZE #if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL) #define SerialDebug SERIAL_PORT_USBVIRTUAL @@ -55,170 +54,170 @@ // High byte is type, Low byte is datacount // datacount is needed when passing ByteArray argument -enum -{ - AT_ARGTYPE_STRING = 0x0100, +enum { + AT_ARGTYPE_STRING = 0x0100, AT_ARGTYPE_BYTEARRAY = 0x0200, - AT_ARGTYPE_INT32 = 0x0300, - AT_ARGTYPE_UINT32 = 0x0400, - AT_ARGTYPE_INT16 = 0x0500, - AT_ARGTYPE_UINT16 = 0x0600, - AT_ARGTYPE_INT8 = 0x0700, - AT_ARGTYPE_UINT8 = 0x0800, + AT_ARGTYPE_INT32 = 0x0300, + AT_ARGTYPE_UINT32 = 0x0400, + AT_ARGTYPE_INT16 = 0x0500, + AT_ARGTYPE_UINT16 = 0x0600, + AT_ARGTYPE_INT8 = 0x0700, + AT_ARGTYPE_UINT8 = 0x0800, }; -class Adafruit_ATParser : public Stream -{ +class Adafruit_ATParser : public Stream { protected: uint8_t _mode; - bool _verbose; + bool _verbose; // internal function - bool send_arg_get_resp(int32_t* reply, uint8_t argcount, uint16_t argtype[], uint32_t args[]); + bool send_arg_get_resp(int32_t *reply, uint8_t argcount, uint16_t argtype[], + uint32_t args[]); public: Adafruit_ATParser(void); - char buffer[BLE_BUFSIZE+1]; + char buffer[BLE_BUFSIZE + 1]; - uint8_t getMode(void) { return _mode; } + uint8_t getMode(void) { return _mode; } virtual bool setMode(uint8_t mode) = 0; // Auto print out TX & RX data to normal Serial void verbose(bool enable) { _verbose = enable; } - bool atcommand_full(const char cmd[] , int32_t* reply, uint8_t argcount, uint16_t argtype[], uint32_t args[]); - bool atcommand_full(const __FlashStringHelper *cmd , int32_t* reply, uint8_t argcount, uint16_t argtype[], uint32_t args[]); + bool atcommand_full(const char cmd[], int32_t *reply, uint8_t argcount, + uint16_t argtype[], uint32_t args[]); + bool atcommand_full(const __FlashStringHelper *cmd, int32_t *reply, + uint8_t argcount, uint16_t argtype[], uint32_t args[]); //--------------------------------------------------------------------+ // Without Reply //--------------------------------------------------------------------+ - bool atcommand(const char cmd[] ) { return this->atcommand_full(cmd, NULL, 0, NULL, NULL); } - bool atcommand(const __FlashStringHelper *cmd ) { return this->atcommand_full(cmd, NULL, 0, NULL, NULL); } + bool atcommand(const char cmd[]) { + return this->atcommand_full(cmd, NULL, 0, NULL, NULL); + } + bool atcommand(const __FlashStringHelper *cmd) { + return this->atcommand_full(cmd, NULL, 0, NULL, NULL); + } //------------- One integer argument -------------// - bool atcommand(const char cmd[] , int32_t para1) - { - uint16_t type[] = { AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1 }; + bool atcommand(const char cmd[], int32_t para1) { + uint16_t type[] = {AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1}; return this->atcommand_full(cmd, NULL, 1, type, args); } - bool atcommand(const __FlashStringHelper *cmd, int32_t para1) - { - uint16_t type[] = { AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1 }; + bool atcommand(const __FlashStringHelper *cmd, int32_t para1) { + uint16_t type[] = {AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1}; return this->atcommand_full(cmd, NULL, 1, type, args); } //------------- Two integer arguments -------------// - bool atcommand(const char cmd[] , int32_t para1, int32_t para2) - { - uint16_t type[] = { AT_ARGTYPE_INT32, AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1, (uint32_t) para2 }; + bool atcommand(const char cmd[], int32_t para1, int32_t para2) { + uint16_t type[] = {AT_ARGTYPE_INT32, AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1, (uint32_t)para2}; return this->atcommand_full(cmd, NULL, 2, type, args); } - bool atcommand(const __FlashStringHelper *cmd, int32_t para1, int32_t para2) - { - uint16_t type[] = { AT_ARGTYPE_INT32, AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1, (uint32_t) para2 }; + bool atcommand(const __FlashStringHelper *cmd, int32_t para1, int32_t para2) { + uint16_t type[] = {AT_ARGTYPE_INT32, AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1, (uint32_t)para2}; return this->atcommand_full(cmd, NULL, 2, type, args); } //------------- One ByteArray arguments -------------// - bool atcommand(const char cmd[] , const uint8_t bytearray[], uint16_t count) - { - uint16_t type[] = { (uint16_t) (AT_ARGTYPE_BYTEARRAY+count) }; - uint32_t args[] = { (uint32_t) bytearray }; + bool atcommand(const char cmd[], const uint8_t bytearray[], uint16_t count) { + uint16_t type[] = {(uint16_t)(AT_ARGTYPE_BYTEARRAY + count)}; + uint32_t args[] = {(uint32_t)bytearray}; return this->atcommand_full(cmd, NULL, 1, type, args); } - bool atcommand(const __FlashStringHelper *cmd, const uint8_t bytearray[], uint16_t count) - { - uint16_t type[] = { (uint16_t) (AT_ARGTYPE_BYTEARRAY+count) }; - uint32_t args[] = { (uint32_t) bytearray }; + bool atcommand(const __FlashStringHelper *cmd, const uint8_t bytearray[], + uint16_t count) { + uint16_t type[] = {(uint16_t)(AT_ARGTYPE_BYTEARRAY + count)}; + uint32_t args[] = {(uint32_t)bytearray}; return this->atcommand_full(cmd, NULL, 1, type, args); } //------------- One String argument -------------// - bool atcommand(const char cmd[] , const char* str) - { - uint16_t type[] = { AT_ARGTYPE_STRING }; - uint32_t args[] = { (uint32_t) str }; + bool atcommand(const char cmd[], const char *str) { + uint16_t type[] = {AT_ARGTYPE_STRING}; + uint32_t args[] = {(uint32_t)str}; return this->atcommand_full(cmd, NULL, 1, type, args); } - bool atcommand(const __FlashStringHelper *cmd, const char* str) - { - uint16_t type[] = { AT_ARGTYPE_STRING }; - uint32_t args[] = { (uint32_t) str }; + bool atcommand(const __FlashStringHelper *cmd, const char *str) { + uint16_t type[] = {AT_ARGTYPE_STRING}; + uint32_t args[] = {(uint32_t)str}; return this->atcommand_full(cmd, NULL, 1, type, args); } //--------------------------------------------------------------------+ // With Reply //--------------------------------------------------------------------+ - bool atcommandIntReply(const char cmd[], int32_t* reply) { return this->atcommand_full(cmd, reply, 0, NULL, NULL); } - bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t* reply) { return this->atcommand_full(cmd, reply, 0, NULL, NULL); } + bool atcommandIntReply(const char cmd[], int32_t *reply) { + return this->atcommand_full(cmd, reply, 0, NULL, NULL); + } + bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t *reply) { + return this->atcommand_full(cmd, reply, 0, NULL, NULL); + } //------------- One integer argument -------------// - bool atcommandIntReply(const char cmd[] , int32_t* reply, int32_t para1) - { - uint16_t type[] = { AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1 }; + bool atcommandIntReply(const char cmd[], int32_t *reply, int32_t para1) { + uint16_t type[] = {AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1}; return this->atcommand_full(cmd, reply, 1, type, args); } - bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t* reply, int32_t para1) - { - uint16_t type[] = { AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1 }; + bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t *reply, + int32_t para1) { + uint16_t type[] = {AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1}; return this->atcommand_full(cmd, reply, 1, type, args); } //------------- Two integer arguments -------------// - bool atcommandIntReply(const char cmd[] , int32_t* reply, int32_t para1, int32_t para2) - { - uint16_t type[] = { AT_ARGTYPE_INT32, AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1, (uint32_t) para2 }; + bool atcommandIntReply(const char cmd[], int32_t *reply, int32_t para1, + int32_t para2) { + uint16_t type[] = {AT_ARGTYPE_INT32, AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1, (uint32_t)para2}; return this->atcommand_full(cmd, reply, 2, type, args); } - bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t* reply, int32_t para1, int32_t para2) - { - uint16_t type[] = { AT_ARGTYPE_INT32, AT_ARGTYPE_INT32 }; - uint32_t args[] = { (uint32_t) para1, (uint32_t) para2 }; + bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t *reply, + int32_t para1, int32_t para2) { + uint16_t type[] = {AT_ARGTYPE_INT32, AT_ARGTYPE_INT32}; + uint32_t args[] = {(uint32_t)para1, (uint32_t)para2}; return this->atcommand_full(cmd, reply, 2, type, args); } //------------- One ByteArray arguments -------------// - bool atcommandIntReply(const char cmd[] , int32_t* reply, const uint8_t bytearray[], uint16_t count) - { - uint16_t type[] = { (uint16_t) (AT_ARGTYPE_BYTEARRAY+count) }; - uint32_t args[] = { (uint32_t) bytearray }; + bool atcommandIntReply(const char cmd[], int32_t *reply, + const uint8_t bytearray[], uint16_t count) { + uint16_t type[] = {(uint16_t)(AT_ARGTYPE_BYTEARRAY + count)}; + uint32_t args[] = {(uint32_t)bytearray}; return this->atcommand_full(cmd, reply, 1, type, args); } - bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t* reply, const uint8_t bytearray[], uint16_t count) - { - uint16_t type[] = { (uint16_t) (AT_ARGTYPE_BYTEARRAY+count) }; - uint32_t args[] = { (uint32_t) bytearray }; + bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t *reply, + const uint8_t bytearray[], uint16_t count) { + uint16_t type[] = {(uint16_t)(AT_ARGTYPE_BYTEARRAY + count)}; + uint32_t args[] = {(uint32_t)bytearray}; return this->atcommand_full(cmd, reply, 1, type, args); } //------------- One String argument -------------// - bool atcommandIntReply(const char cmd[] , int32_t* reply, const char* str) - { - uint16_t type[] = { AT_ARGTYPE_STRING }; - uint32_t args[] = { (uint32_t) str }; + bool atcommandIntReply(const char cmd[], int32_t *reply, const char *str) { + uint16_t type[] = {AT_ARGTYPE_STRING}; + uint32_t args[] = {(uint32_t)str}; return this->atcommand_full(cmd, reply, 1, type, args); } - bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t* reply, const char* str) - { - uint16_t type[] = { AT_ARGTYPE_STRING }; - uint32_t args[] = { (uint32_t) str }; + bool atcommandIntReply(const __FlashStringHelper *cmd, int32_t *reply, + const char *str) { + uint16_t type[] = {AT_ARGTYPE_STRING}; + uint32_t args[] = {(uint32_t)str}; return this->atcommand_full(cmd, reply, 1, type, args); } @@ -230,34 +229,33 @@ class Adafruit_ATParser : public Stream // Read one line of response into internal buffer TODO use below API // Read one line of response into provided buffer - uint16_t readline(char * buf, uint16_t bufsize, uint16_t timeout, boolean multiline = false); - uint16_t readline(uint8_t * buf, uint16_t bufsize, uint16_t timeout, boolean multiline = false) - { - return readline( (char*) buf, bufsize, timeout, multiline ); + uint16_t readline(char *buf, uint16_t bufsize, uint16_t timeout, + boolean multiline = false); + uint16_t readline(uint8_t *buf, uint16_t bufsize, uint16_t timeout, + boolean multiline = false) { + return readline((char *)buf, bufsize, timeout, multiline); } - uint16_t readline(char * buf, uint16_t bufsize) { return readline(buf, bufsize, _timeout, false); } - uint16_t readline(uint8_t * buf, uint16_t bufsize) { return readline(buf, bufsize, _timeout, false); } + uint16_t readline(char *buf, uint16_t bufsize) { + return readline(buf, bufsize, _timeout, false); + } + uint16_t readline(uint8_t *buf, uint16_t bufsize) { + return readline(buf, bufsize, _timeout, false); + } - uint16_t readline(uint16_t timeout, boolean multiline = false) - { + uint16_t readline(uint16_t timeout, boolean multiline = false) { return readline(this->buffer, BLE_BUFSIZE, timeout, multiline); } - uint16_t readline(void) - { + uint16_t readline(void) { return this->readline(this->buffer, BLE_BUFSIZE, _timeout, false); } - // read one line and convert the string to integer number int32_t readline_parseInt(void); uint16_t readraw(uint16_t timeout); - uint16_t readraw(void) - { - return readraw(_timeout); - } + uint16_t readraw(void) { return readraw(_timeout); } //--------------------------------------------------------------------+ // HELPER diff --git a/Adafruit_BLE.cpp b/Adafruit_BLE.cpp index 3383715..d1c45d8 100644 --- a/Adafruit_BLE.cpp +++ b/Adafruit_BLE.cpp @@ -29,20 +29,20 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include "Adafruit_BLE.h" #include "Adafruit_BLEMIDI.h" #ifndef min - #define min(a,b) ((a) < (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) #endif enum { - EVENT_SYSTEM_CONNECT = 0, - EVENT_SYSTEM_DISCONNECT = 1, + EVENT_SYSTEM_CONNECT = 0, + EVENT_SYSTEM_DISCONNECT = 1, EVENT_SYSTEM_BLE_UART_RX = 8, // 9 reserved @@ -51,23 +51,20 @@ enum { // 11 reserved }; -enum { - NVM_USERDATA_SIZE = 256 -}; +enum { NVM_USERDATA_SIZE = 256 }; /******************************************************************************/ /*! @brief Constructor */ /******************************************************************************/ -Adafruit_BLE::Adafruit_BLE(void) -{ +Adafruit_BLE::Adafruit_BLE(void) { _timeout = BLE_DEFAULT_TIMEOUT; _reset_started_timestamp = 0; - _disconnect_callback = NULL; - _connect_callback = NULL; + _disconnect_callback = NULL; + _connect_callback = NULL; _ble_uart_rx_callback = NULL; _ble_midi_rx_callback = NULL; _ble_gatt_rx_callback = NULL; @@ -79,20 +76,20 @@ Adafruit_BLE::Adafruit_BLE(void) @param */ /******************************************************************************/ -void Adafruit_BLE::install_callback(bool enable, int8_t system_id, int8_t gatts_id) -{ +void Adafruit_BLE::install_callback(bool enable, int8_t system_id, + int8_t gatts_id) { uint8_t current_mode = _mode; // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_COMMAND); - print( enable ? F("AT+EVENTENABLE=0x") : F("AT+EVENTDISABLE=0x") ); - print( (system_id < 0) ? 0 : bit(system_id), HEX ); + print(enable ? F("AT+EVENTENABLE=0x") : F("AT+EVENTDISABLE=0x")); + print((system_id < 0) ? 0 : bit(system_id), HEX); - if ( gatts_id >= 0 ) - { - print( F(",0x") ); - println( bit(gatts_id), HEX ); + if (gatts_id >= 0) { + print(F(",0x")); + println(bit(gatts_id), HEX); } println(); @@ -100,7 +97,8 @@ void Adafruit_BLE::install_callback(bool enable, int8_t system_id, int8_t gatts_ waitForOK(); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_DATA); } /******************************************************************************/ @@ -109,36 +107,37 @@ void Adafruit_BLE::install_callback(bool enable, int8_t system_id, int8_t gatts_ @param blocking blocking until bluefruit is ready, will take 1 second mostly */ /******************************************************************************/ -bool Adafruit_BLE::reset(boolean blocking) -{ +bool Adafruit_BLE::reset(boolean blocking) { bool isOK; // println(); - for (uint8_t t=0; t < 5; t++) { + for (uint8_t t = 0; t < 5; t++) { isOK = atcommand(F("ATZ")); - if (isOK) break; + if (isOK) + break; } - if (! isOK) { + if (!isOK) { // ok we're going to get desperate delay(50); setMode(BLUEFRUIT_MODE_COMMAND); delay(50); - - for (uint8_t t=0; t < 5; t++) { + + for (uint8_t t = 0; t < 5; t++) { isOK = atcommand(F("ATZ")); - - if (isOK) break; + + if (isOK) + break; } - if (!isOK) return false; + if (!isOK) + return false; } _reset_started_timestamp = millis(); // Bluefruit need 1 second to reboot - if (blocking) - { + if (blocking) { delay(1000); } @@ -153,16 +152,14 @@ bool Adafruit_BLE::reset(boolean blocking) @brief Performs a factory reset */ /******************************************************************************/ -bool Adafruit_BLE::factoryReset(boolean blocking) -{ - println( F("AT+FACTORYRESET") ); +bool Adafruit_BLE::factoryReset(boolean blocking) { + println(F("AT+FACTORYRESET")); bool isOK = waitForOK(); _reset_started_timestamp = millis(); // Bluefruit need 1 second to reboot - if (blocking) - { + if (blocking) { delay(1000); } @@ -178,8 +175,7 @@ bool Adafruit_BLE::factoryReset(boolean blocking) reset Bluefruit with non-blocking aka reset(false) */ /******************************************************************************/ -bool Adafruit_BLE::resetCompleted(void) -{ +bool Adafruit_BLE::resetCompleted(void) { return millis() > (_reset_started_timestamp + 1000); } @@ -191,9 +187,8 @@ bool Adafruit_BLE::resetCompleted(void) true to enable (default), false to disable */ /******************************************************************************/ -bool Adafruit_BLE::echo(bool enable) -{ - return atcommand(F("ATE"), (int32_t) enable); +bool Adafruit_BLE::echo(bool enable) { + return atcommand(F("ATE"), (int32_t)enable); } /******************************************************************************/ @@ -201,8 +196,7 @@ bool Adafruit_BLE::echo(bool enable) @brief Check connection state, returns true is connected! */ /******************************************************************************/ -bool Adafruit_BLE::isConnected(void) -{ +bool Adafruit_BLE::isConnected(void) { int32_t connected = 0; atcommandIntReply(F("AT+GAPGETCONN"), &connected); return connected; @@ -213,18 +207,14 @@ bool Adafruit_BLE::isConnected(void) @brief Disconnect if currently connected */ /******************************************************************************/ -void Adafruit_BLE::disconnect(void) -{ - atcommand( F("AT+GAPDISCONNECT") ); -} +void Adafruit_BLE::disconnect(void) { atcommand(F("AT+GAPDISCONNECT")); } /******************************************************************************/ /*! @brief Print Bluefruit's information retrieved by ATI command */ /******************************************************************************/ -void Adafruit_BLE::info(void) -{ +void Adafruit_BLE::info(void) { uint8_t current_mode = _mode; bool v = _verbose; @@ -233,17 +223,20 @@ void Adafruit_BLE::info(void) SerialDebug.println(F("----------------")); // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_COMMAND); println(F("ATI")); - while ( readline() ) { - if ( !strcmp(buffer, "OK") || !strcmp(buffer, "ERROR") ) break; + while (readline()) { + if (!strcmp(buffer, "OK") || !strcmp(buffer, "ERROR")) + break; SerialDebug.println(buffer); } // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_DATA); SerialDebug.println(F("----------------")); @@ -255,22 +248,23 @@ void Adafruit_BLE::info(void) @brief Checks if firmware is equal or later than specified version */ /**************************************************************************/ -bool Adafruit_BLE::isVersionAtLeast(const char * versionString) -{ +bool Adafruit_BLE::isVersionAtLeast(const char *versionString) { uint8_t current_mode = _mode; // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_COMMAND); // requesting version number println(F("ATI=4")); readline(); - bool result = ( strcmp(buffer, versionString) >= 0 ); + bool result = (strcmp(buffer, versionString) >= 0); waitForOK(); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_DATA); return result; } @@ -284,12 +278,10 @@ bool Adafruit_BLE::isVersionAtLeast(const char * versionString) @return None */ /******************************************************************************/ -void Adafruit_BLE::update(uint32_t period_ms) -{ +void Adafruit_BLE::update(uint32_t period_ms) { static TimeoutTimer tt; - if ( tt.expired() ) - { + if (tt.expired()) { tt.set(period_ms); bool v = _verbose; @@ -298,68 +290,70 @@ void Adafruit_BLE::update(uint32_t period_ms) uint8_t current_mode = _mode; // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_COMMAND); - println( F("AT+EVENTSTATUS") ); + println(F("AT+EVENTSTATUS")); readline(); waitForOK(); // parse event status system_event, gatts_event - uint8_t tempbuf[BLE_BUFSIZE+1]; + uint8_t tempbuf[BLE_BUFSIZE + 1]; uint32_t system_event, gatts_event; - char * p_comma = NULL; + char *p_comma = NULL; system_event = strtoul(this->buffer, &p_comma, 16); - gatts_event = strtoul(p_comma+1, NULL, 16); + gatts_event = strtoul(p_comma + 1, NULL, 16); //--------------------------------------------------------------------+ // System Event //--------------------------------------------------------------------+ - if ( this->_connect_callback && bitRead(system_event, EVENT_SYSTEM_CONNECT ) ) this->_connect_callback(); - if ( this->_disconnect_callback && bitRead(system_event, EVENT_SYSTEM_DISCONNECT) ) this->_disconnect_callback(); - - if ( this->_ble_uart_rx_callback && bitRead(system_event, EVENT_SYSTEM_BLE_UART_RX) ) - { + if (this->_connect_callback && bitRead(system_event, EVENT_SYSTEM_CONNECT)) + this->_connect_callback(); + if (this->_disconnect_callback && + bitRead(system_event, EVENT_SYSTEM_DISCONNECT)) + this->_disconnect_callback(); + + if (this->_ble_uart_rx_callback && + bitRead(system_event, EVENT_SYSTEM_BLE_UART_RX)) { // _verbose = true; - println( F("AT+BLEUARTRX") ); + println(F("AT+BLEUARTRX")); uint16_t len = readline(tempbuf, BLE_BUFSIZE); waitForOK(); - this->_ble_uart_rx_callback( (char*) tempbuf, len); + this->_ble_uart_rx_callback((char *)tempbuf, len); } - if ( this->_ble_midi_rx_callback && bitRead(system_event, EVENT_SYSTEM_BLE_MIDI_RX) ) - { -// _verbose = true; - while(1) - { + if (this->_ble_midi_rx_callback && + bitRead(system_event, EVENT_SYSTEM_BLE_MIDI_RX)) { + // _verbose = true; + while (1) { // use RAW command version - println( F("AT+BLEMIDIRXRAW") ); + println(F("AT+BLEMIDIRXRAW")); // readraw swallow OK/ERROR already uint16_t len = readraw(); // break if there is no more MIDI event - if ( len == 0 ) break; + if (len == 0) + break; // copy to internal buffer for other usage ! memcpy(tempbuf, this->buffer, len); - Adafruit_BLEMIDI::processRxCallback(tempbuf, len, this->_ble_midi_rx_callback); + Adafruit_BLEMIDI::processRxCallback(tempbuf, len, + this->_ble_midi_rx_callback); } } //--------------------------------------------------------------------+ // Gatt Event //--------------------------------------------------------------------+ - if ( this->_ble_gatt_rx_callback && gatts_event ) - { -// _verbose = true; - for(uint8_t charid=1; charid < 30; charid++) - { - if ( bitRead(gatts_event, charid-1) ) - { - print( F("AT+GATTCHARRAW=") ); // use RAW command version + if (this->_ble_gatt_rx_callback && gatts_event) { + // _verbose = true; + for (uint8_t charid = 1; charid < 30; charid++) { + if (bitRead(gatts_event, charid - 1)) { + print(F("AT+GATTCHARRAW=")); // use RAW command version println(charid); uint16_t len = readraw(); // readraw swallow OK/ERROR already @@ -371,7 +365,8 @@ void Adafruit_BLE::update(uint32_t period_ms) } // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_DATA); _verbose = v; } @@ -383,8 +378,7 @@ void Adafruit_BLE::update(uint32_t period_ms) @param */ /******************************************************************************/ -bool Adafruit_BLE::setAdvData(uint8_t advdata[], uint8_t size) -{ +bool Adafruit_BLE::setAdvData(uint8_t advdata[], uint8_t size) { return this->atcommand(F("AT+GAPSETADVDATA"), advdata, size); } @@ -396,12 +390,13 @@ bool Adafruit_BLE::setAdvData(uint8_t advdata[], uint8_t size) @param offset relative offset in the NVM section */ /******************************************************************************/ -bool Adafruit_BLE::writeNVM(uint16_t offset, uint8_t const data[], uint16_t size) -{ - VERIFY_(offset + size <= NVM_USERDATA_SIZE ); +bool Adafruit_BLE::writeNVM(uint16_t offset, uint8_t const data[], + uint16_t size) { + VERIFY_(offset + size <= NVM_USERDATA_SIZE); - uint16_t type[] = { AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8, (uint16_t) (AT_ARGTYPE_BYTEARRAY + size) }; - uint32_t args[] = { offset, BLE_DATATYPE_BYTEARRAY, (uint32_t) data }; + uint16_t type[] = {AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8, + (uint16_t)(AT_ARGTYPE_BYTEARRAY + size)}; + uint32_t args[] = {offset, BLE_DATATYPE_BYTEARRAY, (uint32_t)data}; return this->atcommand_full(F("AT+NVMWRITE"), NULL, 3, type, args); } @@ -414,12 +409,11 @@ bool Adafruit_BLE::writeNVM(uint16_t offset, uint8_t const data[], uint16_t size @param offset relative offset in the NVM section */ /******************************************************************************/ -bool Adafruit_BLE::writeNVM(uint16_t offset, char const* str) -{ - VERIFY_(offset + strlen(str) <= NVM_USERDATA_SIZE ); +bool Adafruit_BLE::writeNVM(uint16_t offset, char const *str) { + VERIFY_(offset + strlen(str) <= NVM_USERDATA_SIZE); - uint16_t type[] = { AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8, AT_ARGTYPE_STRING }; - uint32_t args[] = { offset, BLE_DATATYPE_STRING, (uint32_t) str }; + uint16_t type[] = {AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8, AT_ARGTYPE_STRING}; + uint32_t args[] = {offset, BLE_DATATYPE_STRING, (uint32_t)str}; return this->atcommand_full(F("AT+NVMWRITE"), NULL, 3, type, args); } @@ -431,12 +425,11 @@ bool Adafruit_BLE::writeNVM(uint16_t offset, char const* str) @param offset relative offset in the NVM section */ /******************************************************************************/ -bool Adafruit_BLE::writeNVM(uint16_t offset, int32_t number) -{ - VERIFY_(offset + 4 <= NVM_USERDATA_SIZE ); +bool Adafruit_BLE::writeNVM(uint16_t offset, int32_t number) { + VERIFY_(offset + 4 <= NVM_USERDATA_SIZE); - uint16_t type[] = { AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8, AT_ARGTYPE_INT32 }; - uint32_t args[] = { offset, BLE_DATATYPE_INTEGER, (uint32_t) number }; + uint16_t type[] = {AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8, AT_ARGTYPE_INT32}; + uint32_t args[] = {offset, BLE_DATATYPE_INTEGER, (uint32_t)number}; return this->atcommand_full(F("AT+NVMWRITE"), NULL, 3, type, args); } @@ -447,17 +440,17 @@ bool Adafruit_BLE::writeNVM(uint16_t offset, int32_t number) @param */ /******************************************************************************/ -bool Adafruit_BLE::readNVM(uint16_t offset, uint8_t data[], uint16_t size) -{ +bool Adafruit_BLE::readNVM(uint16_t offset, uint8_t data[], uint16_t size) { VERIFY_(offset < NVM_USERDATA_SIZE); uint8_t current_mode = _mode; // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_COMMAND); // use RAW command version - print( F("AT+NVMREADRAW=") ); + print(F("AT+NVMREADRAW=")); print(offset); print(','); @@ -466,13 +459,16 @@ bool Adafruit_BLE::readNVM(uint16_t offset, uint8_t data[], uint16_t size) uint16_t len = readraw(); // readraw swallow OK/ERROR already // Check for an error reading - if ( len != size ) return false; + if (len != size) + return false; // skip if NULL is entered - if (data) memcpy(data, this->buffer, min(size, BLE_BUFSIZE)); + if (data) + memcpy(data, this->buffer, min(size, BLE_BUFSIZE)); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + setMode(BLUEFRUIT_MODE_DATA); return true; } @@ -483,17 +479,17 @@ bool Adafruit_BLE::readNVM(uint16_t offset, uint8_t data[], uint16_t size) @param */ /******************************************************************************/ -bool Adafruit_BLE::readNVM(uint16_t offset, char* str, uint16_t size) -{ +bool Adafruit_BLE::readNVM(uint16_t offset, char *str, uint16_t size) { VERIFY_(offset < NVM_USERDATA_SIZE); - uint16_t type[] = { AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8 }; - uint32_t args[] = { offset, size, BLE_DATATYPE_STRING}; + uint16_t type[] = {AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT16, AT_ARGTYPE_UINT8}; + uint32_t args[] = {offset, size, BLE_DATATYPE_STRING}; - bool isOK = this->atcommand_full(F("AT+NVMREAD"), NULL, 3, type, args); + bool isOK = this->atcommand_full(F("AT+NVMREAD"), NULL, 3, type, args); // skip if NULL is entered - if ( isOK && str ) strncpy(str, this->buffer, min(size, BLE_BUFSIZE)); + if (isOK && str) + strncpy(str, this->buffer, min(size, BLE_BUFSIZE)); return isOK; } @@ -504,9 +500,8 @@ bool Adafruit_BLE::readNVM(uint16_t offset, char* str, uint16_t size) @param */ /******************************************************************************/ -bool Adafruit_BLE::readNVM(uint16_t offset, int32_t* number) -{ - return this->readNVM(offset, (uint8_t*)number, 4); +bool Adafruit_BLE::readNVM(uint16_t offset, int32_t *number) { + return this->readNVM(offset, (uint8_t *)number, 4); } /** @@ -515,17 +510,18 @@ bool Adafruit_BLE::readNVM(uint16_t offset, int32_t* number) * @param size * @return */ -int Adafruit_BLE::writeBLEUart(uint8_t const * buffer, int size) -{ +int Adafruit_BLE::writeBLEUart(uint8_t const *buffer, int size) { uint8_t current_mode = _mode; // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_COMMAND ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_COMMAND) + setMode(BLUEFRUIT_MODE_DATA); size_t n = write(buffer, size); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_COMMAND ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_COMMAND) + setMode(BLUEFRUIT_MODE_COMMAND); return n; } @@ -536,17 +532,18 @@ int Adafruit_BLE::writeBLEUart(uint8_t const * buffer, int size) * @param size * @return */ -int Adafruit_BLE::readBLEUart(uint8_t* buffer, int size) -{ +int Adafruit_BLE::readBLEUart(uint8_t *buffer, int size) { uint8_t current_mode = _mode; // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_COMMAND ) setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_COMMAND) + setMode(BLUEFRUIT_MODE_DATA); size_t n = readBytes(buffer, size); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_COMMAND ) setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_COMMAND) + setMode(BLUEFRUIT_MODE_COMMAND); return n; } @@ -558,8 +555,7 @@ int Adafruit_BLE::readBLEUart(uint8_t* buffer, int size) @param[in] fp function pointer, NULL will discard callback */ /******************************************************************************/ -void Adafruit_BLE::setConnectCallback( void (*fp) (void) ) -{ +void Adafruit_BLE::setConnectCallback(void (*fp)(void)) { this->_connect_callback = fp; install_callback(fp != NULL, EVENT_SYSTEM_CONNECT, -1); } @@ -571,8 +567,7 @@ void Adafruit_BLE::setConnectCallback( void (*fp) (void) ) @param[in] fp function pointer, NULL will discard callback */ /******************************************************************************/ -void Adafruit_BLE::setDisconnectCallback( void (*fp) (void) ) -{ +void Adafruit_BLE::setDisconnectCallback(void (*fp)(void)) { this->_disconnect_callback = fp; install_callback(fp != NULL, EVENT_SYSTEM_DISCONNECT, -1); } @@ -584,8 +579,7 @@ void Adafruit_BLE::setDisconnectCallback( void (*fp) (void) ) @param[in] fp function pointer, NULL will discard callback */ /******************************************************************************/ -void Adafruit_BLE::setBleUartRxCallback( void (*fp) (char data[], uint16_t len) ) -{ +void Adafruit_BLE::setBleUartRxCallback(void (*fp)(char data[], uint16_t len)) { this->_ble_uart_rx_callback = fp; install_callback(fp != NULL, EVENT_SYSTEM_BLE_UART_RX, -1); } @@ -597,8 +591,7 @@ void Adafruit_BLE::setBleUartRxCallback( void (*fp) (char data[], uint16_t len) @param[in] fp function pointer, NULL will discard callback */ /******************************************************************************/ -void Adafruit_BLE::setBleMidiRxCallback( midiRxCallback_t fp ) -{ +void Adafruit_BLE::setBleMidiRxCallback(midiRxCallback_t fp) { this->_ble_midi_rx_callback = fp; install_callback(fp != NULL, EVENT_SYSTEM_BLE_MIDI_RX, -1); } @@ -610,11 +603,12 @@ void Adafruit_BLE::setBleMidiRxCallback( midiRxCallback_t fp ) @param[in] fp function pointer, NULL will discard callback */ /******************************************************************************/ -void Adafruit_BLE::setBleGattRxCallback(int32_t chars_idx, void (*fp) (int32_t, uint8_t[], uint16_t) ) -{ - if ( chars_idx == 0) return; +void Adafruit_BLE::setBleGattRxCallback(int32_t chars_idx, + void (*fp)(int32_t, uint8_t[], + uint16_t)) { + if (chars_idx == 0) + return; this->_ble_gatt_rx_callback = fp; - install_callback(fp != NULL, -1, chars_idx-1); + install_callback(fp != NULL, -1, chars_idx - 1); } - diff --git a/Adafruit_BLE.h b/Adafruit_BLE.h index c5af975..617eceb 100644 --- a/Adafruit_BLE.h +++ b/Adafruit_BLE.h @@ -29,148 +29,170 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _Adafruit_BLE_H_ #define _Adafruit_BLE_H_ -#include -#include +#include "Adafruit_ATParser.h" +#include "utility/TimeoutTimer.h" #include "utility/common_header.h" #include "utility/errors.h" -#include "utility/TimeoutTimer.h" -#include "Adafruit_ATParser.h" +#include +#include -#define BLE_DEFAULT_TIMEOUT 250 +#define BLE_DEFAULT_TIMEOUT 250 -enum BLEDataType_t -{ +enum BLEDataType_t { BLE_DATATYPE_AUTO = 0, BLE_DATATYPE_STRING, BLE_DATATYPE_BYTEARRAY, BLE_DATATYPE_INTEGER, }; - -class Adafruit_BLE : public Adafruit_ATParser -{ - protected: - enum - { - BLUEFRUIT_TRANSPORT_INVALID, - BLUEFRUIT_TRANSPORT_HWUART, - BLUEFRUIT_TRANSPORT_SWUART, - BLUEFRUIT_TRANSPORT_HWSPI, - BLUEFRUIT_TRANSPORT_SWSPI, - }; - -// uint8_t _mode; -// uint16_t _timeout; - uint8_t _physical_transport; - uint32_t _reset_started_timestamp; - - public: - typedef void (*midiRxCallback_t) (uint16_t timestamp, uint8_t status, uint8_t byte1, uint8_t byte2); - - // Constructor - Adafruit_BLE(void); - - // Functions implemented in this base class - bool reset(boolean blocking = true); - bool factoryReset(boolean blocking = true); - bool resetCompleted(void); - - void info(void); - bool echo(bool enable); - - bool isConnected(void); - bool isVersionAtLeast(const char * versionString); - void disconnect(void); - - bool setAdvData(uint8_t advdata[], uint8_t size); - - bool writeNVM(uint16_t offset, uint8_t const data[], uint16_t size); - bool writeNVM(uint16_t offset, char const* str); - bool writeNVM(uint16_t offset, int32_t number); - - bool readNVM(uint16_t offset, uint8_t data[], uint16_t size); - bool readNVM(uint16_t offset, char * str , uint16_t size); - bool readNVM(uint16_t offset, int32_t* number); - - // helper with bleuart - int writeBLEUart(uint8_t const * buffer, int size); - int writeBLEUart(char const * str) { return writeBLEUart( (uint8_t const*) str, strlen(str)); } - - int readBLEUart(uint8_t* buffer, int size); - - - // No parameters - bool sendCommandCheckOK(const __FlashStringHelper *cmd) { return this->atcommand(cmd); } - bool sendCommandCheckOK(const char cmd[]) { return this->atcommand(cmd); } - - bool sendCommandWithIntReply(const __FlashStringHelper *cmd, int32_t *reply) { return this->atcommandIntReply(cmd, reply); } - bool sendCommandWithIntReply(const char cmd[] , int32_t *reply) { return this->atcommandIntReply(cmd, reply); } - - // Physical transportation checking - bool isTransportHwUart (void) { return _physical_transport == BLUEFRUIT_TRANSPORT_HWUART; } - bool isTransportSwUart (void) { return _physical_transport == BLUEFRUIT_TRANSPORT_SWUART; } - bool isTransportUart (void) { return isTransportHwUart() || isTransportSwUart(); } - - bool isTransportHwSpi (void) { return _physical_transport == BLUEFRUIT_TRANSPORT_HWSPI; } - bool isTransportSwSpi (void) { return _physical_transport == BLUEFRUIT_TRANSPORT_SWSPI; } - bool isTransportSpi (void) { return isTransportHwSpi() || isTransportSwSpi(); } - - ///////////////////// - // callback functions - ///////////////////// - void update(uint32_t period_ms = 200); - void handleDfuIrq(void) - { - this->update(0); - } - - void setDisconnectCallback( void (*fp) (void) ); - void setConnectCallback ( void (*fp) (void) ); - - void setBleUartRxCallback( void (*fp) (char data[], uint16_t len) ); - void setBleMidiRxCallback( midiRxCallback_t fp ); - void setBleGattRxCallback( int32_t chars_idx, void (*fp) (int32_t, uint8_t[], uint16_t) ); - - protected: - // helper - void install_callback(bool enable, int8_t system_id, int8_t gatts_id); - - void (*_disconnect_callback) (void); - void (*_connect_callback) (void); - - void (*_ble_uart_rx_callback) (char data[], uint16_t len); - midiRxCallback_t _ble_midi_rx_callback; - - void (*_ble_gatt_rx_callback) (int32_t chars_id, uint8_t data[], uint16_t len); +class Adafruit_BLE : public Adafruit_ATParser { +protected: + enum { + BLUEFRUIT_TRANSPORT_INVALID, + BLUEFRUIT_TRANSPORT_HWUART, + BLUEFRUIT_TRANSPORT_SWUART, + BLUEFRUIT_TRANSPORT_HWSPI, + BLUEFRUIT_TRANSPORT_SWSPI, + }; + + // uint8_t _mode; + // uint16_t _timeout; + uint8_t _physical_transport; + uint32_t _reset_started_timestamp; + +public: + typedef void (*midiRxCallback_t)(uint16_t timestamp, uint8_t status, + uint8_t byte1, uint8_t byte2); + + // Constructor + Adafruit_BLE(void); + + // Functions implemented in this base class + bool reset(boolean blocking = true); + bool factoryReset(boolean blocking = true); + bool resetCompleted(void); + + void info(void); + bool echo(bool enable); + + bool isConnected(void); + bool isVersionAtLeast(const char *versionString); + void disconnect(void); + + bool setAdvData(uint8_t advdata[], uint8_t size); + + bool writeNVM(uint16_t offset, uint8_t const data[], uint16_t size); + bool writeNVM(uint16_t offset, char const *str); + bool writeNVM(uint16_t offset, int32_t number); + + bool readNVM(uint16_t offset, uint8_t data[], uint16_t size); + bool readNVM(uint16_t offset, char *str, uint16_t size); + bool readNVM(uint16_t offset, int32_t *number); + + // helper with bleuart + int writeBLEUart(uint8_t const *buffer, int size); + int writeBLEUart(char const *str) { + return writeBLEUart((uint8_t const *)str, strlen(str)); + } + + int readBLEUart(uint8_t *buffer, int size); + + // No parameters + bool sendCommandCheckOK(const __FlashStringHelper *cmd) { + return this->atcommand(cmd); + } + bool sendCommandCheckOK(const char cmd[]) { return this->atcommand(cmd); } + + bool sendCommandWithIntReply(const __FlashStringHelper *cmd, int32_t *reply) { + return this->atcommandIntReply(cmd, reply); + } + bool sendCommandWithIntReply(const char cmd[], int32_t *reply) { + return this->atcommandIntReply(cmd, reply); + } + + // Physical transportation checking + bool isTransportHwUart(void) { + return _physical_transport == BLUEFRUIT_TRANSPORT_HWUART; + } + bool isTransportSwUart(void) { + return _physical_transport == BLUEFRUIT_TRANSPORT_SWUART; + } + bool isTransportUart(void) { + return isTransportHwUart() || isTransportSwUart(); + } + + bool isTransportHwSpi(void) { + return _physical_transport == BLUEFRUIT_TRANSPORT_HWSPI; + } + bool isTransportSwSpi(void) { + return _physical_transport == BLUEFRUIT_TRANSPORT_SWSPI; + } + bool isTransportSpi(void) { return isTransportHwSpi() || isTransportSwSpi(); } + + ///////////////////// + // callback functions + ///////////////////// + void update(uint32_t period_ms = 200); + void handleDfuIrq(void) { this->update(0); } + + void setDisconnectCallback(void (*fp)(void)); + void setConnectCallback(void (*fp)(void)); + + void setBleUartRxCallback(void (*fp)(char data[], uint16_t len)); + void setBleMidiRxCallback(midiRxCallback_t fp); + void setBleGattRxCallback(int32_t chars_idx, + void (*fp)(int32_t, uint8_t[], uint16_t)); + +protected: + // helper + void install_callback(bool enable, int8_t system_id, int8_t gatts_id); + + void (*_disconnect_callback)(void); + void (*_connect_callback)(void); + + void (*_ble_uart_rx_callback)(char data[], uint16_t len); + midiRxCallback_t _ble_midi_rx_callback; + + void (*_ble_gatt_rx_callback)(int32_t chars_id, uint8_t data[], uint16_t len); }; //--------------------------------------------------------------------+ // DEBUG HELPER //--------------------------------------------------------------------+ #ifndef DBG_ENABLE -#define DBG_ENABLE 0 +#define DBG_ENABLE 0 #endif #if DBG_ENABLE - #define DBG_LOCATION() Serial.printf("%s: %d: \r\n", __PRETTY_FUNCTION__, __LINE__) - #define DBG_INT(x) do { Serial.print(#x " = "); Serial.println(x); } while(0) - #define DBG_HEX(x) do { Serial.print(#x " = "); Serial.println(x, HEX); } while(0) - #define DBG_STR(x) Serial.printf(#x " = %s\r\n", (char*)(x) ) - #define DBG_BUFFER(buf, n) \ - do {\ - uint8_t* p8 = (uint8_t*) (buf);\ - Serial.print(#buf ": ");\ - for(uint32_t i=0; i<(n); i++) Serial.printf("%02x ", p8[i]);\ - Serial.print("\r\n");\ - }while(0) +#define DBG_LOCATION() \ + Serial.printf("%s: %d: \r\n", __PRETTY_FUNCTION__, __LINE__) +#define DBG_INT(x) \ + do { \ + Serial.print(#x " = "); \ + Serial.println(x); \ + } while (0) +#define DBG_HEX(x) \ + do { \ + Serial.print(#x " = "); \ + Serial.println(x, HEX); \ + } while (0) +#define DBG_STR(x) Serial.printf(#x " = %s\r\n", (char *)(x)) +#define DBG_BUFFER(buf, n) \ + do { \ + uint8_t *p8 = (uint8_t *)(buf); \ + Serial.print(#buf ": "); \ + for (uint32_t i = 0; i < (n); i++) \ + Serial.printf("%02x ", p8[i]); \ + Serial.print("\r\n"); \ + } while (0) #endif #endif /* _Adafruit_BLE_H_ */ diff --git a/Adafruit_BLEBattery.cpp b/Adafruit_BLEBattery.cpp index bb9ffa4..3b0ef91 100644 --- a/Adafruit_BLEBattery.cpp +++ b/Adafruit_BLEBattery.cpp @@ -29,8 +29,8 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ @@ -41,11 +41,7 @@ @brief Constructor */ /******************************************************************************/ -Adafruit_BLEBattery::Adafruit_BLEBattery(Adafruit_BLE& ble) : - _ble(ble) -{ - -} +Adafruit_BLEBattery::Adafruit_BLEBattery(Adafruit_BLE &ble) : _ble(ble) {} /******************************************************************************/ /*! @@ -53,16 +49,17 @@ Adafruit_BLEBattery::Adafruit_BLEBattery(Adafruit_BLE& ble) : @param reset true will reset Bluefruit */ /******************************************************************************/ -bool Adafruit_BLEBattery::begin(bool reset) -{ +bool Adafruit_BLEBattery::begin(bool reset) { int32_t enabled = 0; - VERIFY_( _ble.atcommandIntReply( F("AT+BLEBATTEN"), &enabled) ); - if ( enabled ) return true; + VERIFY_(_ble.atcommandIntReply(F("AT+BLEBATTEN"), &enabled)); + if (enabled) + return true; - VERIFY_( _ble.atcommand( F("AT+BLEBATTEN=1") ) ); + VERIFY_(_ble.atcommand(F("AT+BLEBATTEN=1"))); // Perform Bluefruit reset if needed - if (reset) _ble.reset(); + if (reset) + _ble.reset(); return true; } @@ -73,16 +70,17 @@ bool Adafruit_BLEBattery::begin(bool reset) @param reset true will reset Bluefruit */ /******************************************************************************/ -bool Adafruit_BLEBattery::stop(bool reset) -{ +bool Adafruit_BLEBattery::stop(bool reset) { int32_t enabled = 0; - VERIFY_( _ble.atcommandIntReply( F("AT+BLEBATTEN"), &enabled) ); - if ( !enabled ) return true; + VERIFY_(_ble.atcommandIntReply(F("AT+BLEBATTEN"), &enabled)); + if (!enabled) + return true; - VERIFY_( _ble.atcommand( F("AT+BLEBATTEN=0") ) ); + VERIFY_(_ble.atcommand(F("AT+BLEBATTEN=0"))); // Perform Bluefruit reset if needed - if (reset) _ble.reset(); + if (reset) + _ble.reset(); return true; } @@ -93,9 +91,7 @@ bool Adafruit_BLEBattery::stop(bool reset) @param percent Battery value in percentage 0-100 */ /******************************************************************************/ -bool Adafruit_BLEBattery::update(uint8_t percent) -{ - VERIFY_( is_within(0, percent, 100) ); - return _ble.atcommand( F("AT+BLEBATTVAL"), percent ) ; +bool Adafruit_BLEBattery::update(uint8_t percent) { + VERIFY_(is_within(0, percent, 100)); + return _ble.atcommand(F("AT+BLEBATTVAL"), percent); } - diff --git a/Adafruit_BLEBattery.h b/Adafruit_BLEBattery.h index a4da3c9..257333c 100644 --- a/Adafruit_BLEBattery.h +++ b/Adafruit_BLEBattery.h @@ -29,27 +29,26 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _ADAFRUIT_BLEBATTERY_H_ #define _ADAFRUIT_BLEBATTERY_H_ -#include #include "Adafruit_BLE.h" +#include -class Adafruit_BLEBattery -{ +class Adafruit_BLEBattery { private: - Adafruit_BLE& _ble; + Adafruit_BLE &_ble; public: - Adafruit_BLEBattery(Adafruit_BLE& ble); + Adafruit_BLEBattery(Adafruit_BLE &ble); bool begin(bool reset = true); - bool stop (bool reset = true); + bool stop(bool reset = true); bool update(uint8_t percent); }; diff --git a/Adafruit_BLEEddystone.cpp b/Adafruit_BLEEddystone.cpp index 4d555cc..03aaebe 100644 --- a/Adafruit_BLEEddystone.cpp +++ b/Adafruit_BLEEddystone.cpp @@ -29,25 +29,21 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include "Adafruit_BLEEddystone.h" -#define EDDYSTONE_MINIMUM_FIRMWARE_VERSION "0.7.0" - +#define EDDYSTONE_MINIMUM_FIRMWARE_VERSION "0.7.0" /******************************************************************************/ /*! @brief Constructor */ /******************************************************************************/ -Adafruit_BLEEddystone::Adafruit_BLEEddystone(Adafruit_BLE& ble) : - _ble(ble) -{ -} +Adafruit_BLEEddystone::Adafruit_BLEEddystone(Adafruit_BLE &ble) : _ble(ble) {} /******************************************************************************/ /*! @@ -55,18 +51,19 @@ Adafruit_BLEEddystone::Adafruit_BLEEddystone(Adafruit_BLE& ble) : @param reset true will reset Bluefruit */ /******************************************************************************/ -bool Adafruit_BLEEddystone::begin(bool reset) -{ - VERIFY_( _ble.isVersionAtLeast(EDDYSTONE_MINIMUM_FIRMWARE_VERSION) ); +bool Adafruit_BLEEddystone::begin(bool reset) { + VERIFY_(_ble.isVersionAtLeast(EDDYSTONE_MINIMUM_FIRMWARE_VERSION)); int32_t enabled = 0; - VERIFY_( _ble.atcommandIntReply( F("AT+EDDYSTONESERVICEEN"), &enabled) ); + VERIFY_(_ble.atcommandIntReply(F("AT+EDDYSTONESERVICEEN"), &enabled)); - if ( enabled ) return true; - VERIFY_( _ble.atcommand( F("AT+EDDYSTONESERVICEEN=1") ) ); + if (enabled) + return true; + VERIFY_(_ble.atcommand(F("AT+EDDYSTONESERVICEEN=1"))); // Perform Bluefruit reset if needed - if (reset) _ble.reset(); + if (reset) + _ble.reset(); return true; } @@ -77,16 +74,17 @@ bool Adafruit_BLEEddystone::begin(bool reset) @param reset true will reset Bluefruit */ /******************************************************************************/ -bool Adafruit_BLEEddystone::stop(bool reset) -{ +bool Adafruit_BLEEddystone::stop(bool reset) { int32_t enabled = 0; - VERIFY_( _ble.atcommandIntReply( F("AT+EDDYSTONESERVICEEN"), &enabled) ); - if ( !enabled ) return true; + VERIFY_(_ble.atcommandIntReply(F("AT+EDDYSTONESERVICEEN"), &enabled)); + if (!enabled) + return true; - VERIFY_( _ble.atcommand( F("AT+EDDYSTONESERVICEEN=0") ) ); + VERIFY_(_ble.atcommand(F("AT+EDDYSTONESERVICEEN=0"))); // Perform Bluefruit reset if needed - if (reset) _ble.reset(); + if (reset) + _ble.reset(); return true; } @@ -99,27 +97,31 @@ bool Adafruit_BLEEddystone::stop(bool reset) @param rssi_at_0m RSSI value at 0m (check out EddyStone specs) */ /******************************************************************************/ -bool Adafruit_BLEEddystone::setURL(const char* url, bool broadcastEvenConnect, int8_t rssi_at_0m) -{ +bool Adafruit_BLEEddystone::setURL(const char *url, bool broadcastEvenConnect, + int8_t rssi_at_0m) { bool result; uint8_t current_mode = _ble.getMode(); // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + _ble.setMode(BLUEFRUIT_MODE_COMMAND); // send command and integer parameters separated by comma - _ble.print( F("AT+EDDYSTONEURL=") ); + _ble.print(F("AT+EDDYSTONEURL=")); _ble.print(url); - _ble.print(','); _ble.print(broadcastEvenConnect, DEC); - _ble.print(','); _ble.print(rssi_at_0m); + _ble.print(','); + _ble.print(broadcastEvenConnect, DEC); + _ble.print(','); + _ble.print(rssi_at_0m); _ble.println(); // execute command result = _ble.waitForOK(); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + _ble.setMode(BLUEFRUIT_MODE_DATA); return result; } @@ -129,9 +131,8 @@ bool Adafruit_BLEEddystone::setURL(const char* url, bool broadcastEvenConnect, i @brief Start Broadcasting (advertising) specified URL */ /******************************************************************************/ -bool Adafruit_BLEEddystone::startBroadcast(void) -{ - return _ble.atcommand( F("AT+EDDYSTONEBROADCAST=1") ); +bool Adafruit_BLEEddystone::startBroadcast(void) { + return _ble.atcommand(F("AT+EDDYSTONEBROADCAST=1")); } /******************************************************************************/ @@ -139,9 +140,8 @@ bool Adafruit_BLEEddystone::startBroadcast(void) @brief Stop Broadcasting (advertising) specified URL */ /******************************************************************************/ -bool Adafruit_BLEEddystone::stopBroadcast(void) -{ - return _ble.atcommand( F("AT+EDDYSTONEBROADCAST=0") ); +bool Adafruit_BLEEddystone::stopBroadcast(void) { + return _ble.atcommand(F("AT+EDDYSTONEBROADCAST=0")); } /******************************************************************************/ @@ -149,7 +149,6 @@ bool Adafruit_BLEEddystone::stopBroadcast(void) @brief Broadcast (advertising) specified URL */ /******************************************************************************/ -bool Adafruit_BLEEddystone::startConfigMode(uint32_t seconds) -{ - return _ble.atcommand( F("AT+EDDYSTONECONFIGEN"), (int32_t) seconds ); +bool Adafruit_BLEEddystone::startConfigMode(uint32_t seconds) { + return _ble.atcommand(F("AT+EDDYSTONECONFIGEN"), (int32_t)seconds); } diff --git a/Adafruit_BLEEddystone.h b/Adafruit_BLEEddystone.h index dabd41e..52c74da 100644 --- a/Adafruit_BLEEddystone.h +++ b/Adafruit_BLEEddystone.h @@ -29,37 +29,36 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _ADAFRUIT_BLEEDDYSTONE_H_ #define _ADAFRUIT_BLEEDDYSTONE_H_ -#include #include "Adafruit_BLE.h" +#include -#define EDDYSTONE_DEFAULT_RSSI0M (-18) +#define EDDYSTONE_DEFAULT_RSSI0M (-18) -class Adafruit_BLEEddystone -{ +class Adafruit_BLEEddystone { private: - Adafruit_BLE& _ble; + Adafruit_BLE &_ble; public: - Adafruit_BLEEddystone(Adafruit_BLE& ble); + Adafruit_BLEEddystone(Adafruit_BLE &ble); bool begin(bool reset = true); - bool stop (bool reset = true); + bool stop(bool reset = true); - bool setURL(const char* url, bool broadcastEvenConnect = false, int8_t rssi_at_0m = EDDYSTONE_DEFAULT_RSSI0M); + bool setURL(const char *url, bool broadcastEvenConnect = false, + int8_t rssi_at_0m = EDDYSTONE_DEFAULT_RSSI0M); bool startBroadcast(void); bool stopBroadcast(void); bool startConfigMode(uint32_t seconds); - }; #endif /* _ADAFRUIT_BLEEDDYSTONE_H_ */ diff --git a/Adafruit_BLEGatt.cpp b/Adafruit_BLEGatt.cpp index 75a9955..37297de 100644 --- a/Adafruit_BLEGatt.cpp +++ b/Adafruit_BLEGatt.cpp @@ -29,22 +29,19 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include "Adafruit_BLEGatt.h" - /******************************************************************************/ /*! @brief Constructor */ /******************************************************************************/ -Adafruit_BLEGatt::Adafruit_BLEGatt(Adafruit_BLE& ble) : - _ble(ble) -{ +Adafruit_BLEGatt::Adafruit_BLEGatt(Adafruit_BLE &ble) : _ble(ble) { this->buffer = _ble.buffer; } @@ -53,10 +50,7 @@ Adafruit_BLEGatt::Adafruit_BLEGatt(Adafruit_BLE& ble) : @brief Clear all GATT data */ /******************************************************************************/ -bool Adafruit_BLEGatt::clear(void) -{ - return _ble.atcommand( F("AT+GATTCLEAR") ); -} +bool Adafruit_BLEGatt::clear(void) { return _ble.atcommand(F("AT+GATTCLEAR")); } /******************************************************************************/ /*! @@ -64,11 +58,12 @@ bool Adafruit_BLEGatt::clear(void) @return Service ID (starting from 1). If failed 0 is returned */ /******************************************************************************/ -uint8_t Adafruit_BLEGatt::addService(uint16_t uuid16) -{ +uint8_t Adafruit_BLEGatt::addService(uint16_t uuid16) { int32_t service_id; - VERIFY_RETURN_( _ble.atcommandIntReply( F("AT+GATTADDSERVICE=UUID"), &service_id, uuid16), 0 ); - return (uint8_t) service_id; + VERIFY_RETURN_( + _ble.atcommandIntReply(F("AT+GATTADDSERVICE=UUID"), &service_id, uuid16), + 0); + return (uint8_t)service_id; } /******************************************************************************/ @@ -77,11 +72,12 @@ uint8_t Adafruit_BLEGatt::addService(uint16_t uuid16) @return Service ID (starting from 1). If failed 0 is returned */ /******************************************************************************/ -uint8_t Adafruit_BLEGatt::addService(uint8_t uuid128[]) -{ +uint8_t Adafruit_BLEGatt::addService(uint8_t uuid128[]) { int32_t service_id; - VERIFY_RETURN_( _ble.atcommandIntReply( F("AT+GATTADDSERVICE=UUID128"), &service_id, uuid128, 16), 0 ); - return (uint8_t) service_id; + VERIFY_RETURN_(_ble.atcommandIntReply(F("AT+GATTADDSERVICE=UUID128"), + &service_id, uuid128, 16), + 0); + return (uint8_t)service_id; } /******************************************************************************/ @@ -90,67 +86,68 @@ uint8_t Adafruit_BLEGatt::addService(uint8_t uuid128[]) @return Chars ID (starting from 1). If failed 0 is returned */ /******************************************************************************/ -uint8_t Adafruit_BLEGatt::addChar_internal(uint8_t uuid[], uint8_t uuid_len, uint8_t properties, uint8_t min_len, uint8_t max_len, BLEDataType_t datatype, const char* description, const GattPresentationFormat* presentFormat) -{ +uint8_t Adafruit_BLEGatt::addChar_internal( + uint8_t uuid[], uint8_t uuid_len, uint8_t properties, uint8_t min_len, + uint8_t max_len, BLEDataType_t datatype, const char *description, + const GattPresentationFormat *presentFormat) { bool isOK; int32_t chars_id; uint8_t current_mode = _ble.getMode(); // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + _ble.setMode(BLUEFRUIT_MODE_COMMAND); // Standard UUID or UUID128 - if ( uuid_len == 2 ) - { + if (uuid_len == 2) { uint16_t uuid16; memcpy(&uuid16, uuid, 2); - _ble.print( F("AT+GATTADDCHAR=UUID=") ); + _ble.print(F("AT+GATTADDCHAR=UUID=")); _ble.print(uuid16); - }else - { - _ble.print( F("AT+GATTADDCHAR=UUID128=") ); + } else { + _ble.print(F("AT+GATTADDCHAR=UUID128=")); _ble.printByteArray(uuid, 16); } - _ble.print( F(",PROPERTIES=") ); + _ble.print(F(",PROPERTIES=")); _ble.print(properties); - _ble.print( F(",MIN_LEN=") ); + _ble.print(F(",MIN_LEN=")); _ble.print(min_len); - _ble.print( F(",MAX_LEN=") ); + _ble.print(F(",MAX_LEN=")); _ble.print(max_len); - _ble.print( F(",DATATYPE=") ); - _ble.print((uint8_t) datatype); + _ble.print(F(",DATATYPE=")); + _ble.print((uint8_t)datatype); - if (description) - { - _ble.print( F(",DESCRIPTION=") ); + if (description) { + _ble.print(F(",DESCRIPTION=")); _ble.print(description); } - if (presentFormat && presentFormat->format) - { - // presentation format is packed 7 bytes, use tempbuf to avoid mis-aligned memory + if (presentFormat && presentFormat->format) { + // presentation format is packed 7 bytes, use tempbuf to avoid mis-aligned + // memory uint8_t tempbuf[7]; memcpy(tempbuf, presentFormat, 5); - memcpy(tempbuf+5, &presentFormat->desc, 2); + memcpy(tempbuf + 5, &presentFormat->desc, 2); - _ble.print( F(",PRESENTATION=") ); + _ble.print(F(",PRESENTATION=")); _ble.printByteArray(tempbuf, 7); } _ble.println(); // execute command -// if (_verbose) SerialDebug.print( F("\n<- ") ); + // if (_verbose) SerialDebug.print( F("\n<- ") ); chars_id = _ble.readline_parseInt(); isOK = _ble.waitForOK(); // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + _ble.setMode(BLUEFRUIT_MODE_DATA); return isOK ? chars_id : 0; } @@ -162,9 +159,12 @@ uint8_t Adafruit_BLEGatt::addChar_internal(uint8_t uuid[], uint8_t uuid_len, uin @return Chars ID (starting from 1). If failed 0 is returned */ /******************************************************************************/ -uint8_t Adafruit_BLEGatt::addCharacteristic(uint16_t uuid16, uint8_t properties, uint8_t min_len, uint8_t max_len, BLEDataType_t datatype, const char* description, const GattPresentationFormat* presentFormat) -{ - return addChar_internal((uint8_t*) &uuid16, 2, properties, min_len, max_len, datatype, description, presentFormat); +uint8_t Adafruit_BLEGatt::addCharacteristic( + uint16_t uuid16, uint8_t properties, uint8_t min_len, uint8_t max_len, + BLEDataType_t datatype, const char *description, + const GattPresentationFormat *presentFormat) { + return addChar_internal((uint8_t *)&uuid16, 2, properties, min_len, max_len, + datatype, description, presentFormat); } /******************************************************************************/ @@ -174,9 +174,12 @@ uint8_t Adafruit_BLEGatt::addCharacteristic(uint16_t uuid16, uint8_t properties, @return Chars ID (starting from 1). If failed 0 is returned */ /******************************************************************************/ -uint8_t Adafruit_BLEGatt::addCharacteristic(uint8_t uuid128[], uint8_t properties, uint8_t min_len, uint8_t max_len, BLEDataType_t datatype, const char* description, const GattPresentationFormat* presentFormat) -{ - return addChar_internal(uuid128, 16, properties, min_len, max_len, datatype, description, presentFormat); +uint8_t Adafruit_BLEGatt::addCharacteristic( + uint8_t uuid128[], uint8_t properties, uint8_t min_len, uint8_t max_len, + BLEDataType_t datatype, const char *description, + const GattPresentationFormat *presentFormat) { + return addChar_internal(uuid128, 16, properties, min_len, max_len, datatype, + description, presentFormat); } /******************************************************************************/ @@ -185,10 +188,11 @@ uint8_t Adafruit_BLEGatt::addCharacteristic(uint8_t uuid128[], uint8_t propertie @param */ /******************************************************************************/ -bool Adafruit_BLEGatt::setChar(uint8_t charID, uint8_t const data[], uint8_t size) -{ - uint16_t argtype[] = { AT_ARGTYPE_UINT8, (uint16_t) (AT_ARGTYPE_BYTEARRAY+ size) }; - uint32_t args[] = { charID, (uint32_t) data }; +bool Adafruit_BLEGatt::setChar(uint8_t charID, uint8_t const data[], + uint8_t size) { + uint16_t argtype[] = {AT_ARGTYPE_UINT8, + (uint16_t)(AT_ARGTYPE_BYTEARRAY + size)}; + uint32_t args[] = {charID, (uint32_t)data}; return _ble.atcommand_full(F("AT+GATTCHAR"), NULL, 2, argtype, args); } @@ -199,10 +203,9 @@ bool Adafruit_BLEGatt::setChar(uint8_t charID, uint8_t const data[], uint8_t siz @param */ /******************************************************************************/ -bool Adafruit_BLEGatt::setChar(uint8_t charID, char const* str) -{ - uint16_t argtype[] = { AT_ARGTYPE_UINT8, AT_ARGTYPE_STRING }; - uint32_t args[] = { charID, (uint32_t) str }; +bool Adafruit_BLEGatt::setChar(uint8_t charID, char const *str) { + uint16_t argtype[] = {AT_ARGTYPE_UINT8, AT_ARGTYPE_STRING}; + uint32_t args[] = {charID, (uint32_t)str}; return _ble.atcommand_full(F("AT+GATTCHAR"), NULL, 2, argtype, args); } @@ -216,20 +219,21 @@ bool Adafruit_BLEGatt::setChar(uint8_t charID, char const* str) */ /******************************************************************************/ -uint8_t Adafruit_BLEGatt::getChar(uint8_t charID) -{ +uint8_t Adafruit_BLEGatt::getChar(uint8_t charID) { uint8_t current_mode = _ble.getMode(); // switch mode if necessary to execute command - if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_COMMAND); + if (current_mode == BLUEFRUIT_MODE_DATA) + _ble.setMode(BLUEFRUIT_MODE_COMMAND); // use RAW command version - _ble.print( F("AT+GATTCHARRAW=") ); + _ble.print(F("AT+GATTCHARRAW=")); _ble.println(charID); uint16_t len = _ble.readraw(); // readraw swallow OK/ERROR already // switch back if necessary - if ( current_mode == BLUEFRUIT_MODE_DATA ) _ble.setMode(BLUEFRUIT_MODE_DATA); + if (current_mode == BLUEFRUIT_MODE_DATA) + _ble.setMode(BLUEFRUIT_MODE_DATA); return len; } @@ -244,8 +248,8 @@ uint8_t Adafruit_BLEGatt::getChar(uint8_t charID) 0 usually means error. */ /******************************************************************************/ -uint8_t Adafruit_BLEGatt::getChar(uint8_t charID, uint8_t* buf, uint8_t bufsize) -{ +uint8_t Adafruit_BLEGatt::getChar(uint8_t charID, uint8_t *buf, + uint8_t bufsize) { uint8_t len = this->getChar(charID); len = min(len, bufsize); memcpy(buf, _ble.buffer, len); diff --git a/Adafruit_BLEGatt.h b/Adafruit_BLEGatt.h index 265ea74..eaffa8b 100644 --- a/Adafruit_BLEGatt.h +++ b/Adafruit_BLEGatt.h @@ -29,244 +29,264 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _ADAFRUIT_BLEGATT_H_ #define _ADAFRUIT_BLEGATT_H_ -#include #include "Adafruit_BLE.h" +#include -#define GATT_CHARS_PROPERTIES_BROADCAST bit(0) -#define GATT_CHARS_PROPERTIES_READ bit(1) -#define GATT_CHARS_PROPERTIES_WRITE_WO_RESP bit(2) -#define GATT_CHARS_PROPERTIES_WRITE bit(3) -#define GATT_CHARS_PROPERTIES_NOTIFY bit(4) -#define GATT_CHARS_PROPERTIES_INDICATE bit(5) +#define GATT_CHARS_PROPERTIES_BROADCAST bit(0) +#define GATT_CHARS_PROPERTIES_READ bit(1) +#define GATT_CHARS_PROPERTIES_WRITE_WO_RESP bit(2) +#define GATT_CHARS_PROPERTIES_WRITE bit(3) +#define GATT_CHARS_PROPERTIES_NOTIFY bit(4) +#define GATT_CHARS_PROPERTIES_INDICATE bit(5) -struct GattPresentationFormat -{ - uint8_t format; - int8_t exponent; +struct GattPresentationFormat { + uint8_t format; + int8_t exponent; uint16_t unit; - uint8_t name_space; + uint8_t name_space; uint16_t desc; }; -class Adafruit_BLEGatt -{ +class Adafruit_BLEGatt { private: - Adafruit_BLE& _ble; + Adafruit_BLE &_ble; - uint8_t addChar_internal(uint8_t uuid[], uint8_t uuid_len, uint8_t properties, uint8_t min_len, uint8_t max_len, BLEDataType_t datatype, const char* description, const GattPresentationFormat* presentFormat); + uint8_t addChar_internal(uint8_t uuid[], uint8_t uuid_len, uint8_t properties, + uint8_t min_len, uint8_t max_len, + BLEDataType_t datatype, const char *description, + const GattPresentationFormat *presentFormat); public: - char* buffer; // alias to ble's buffer + char *buffer; // alias to ble's buffer - Adafruit_BLEGatt(Adafruit_BLE& ble); + Adafruit_BLEGatt(Adafruit_BLE &ble); - bool clear(void); + bool clear(void); uint8_t addService(uint16_t uuid16); uint8_t addService(uint8_t uuid128[]); - uint8_t addCharacteristic(uint16_t uuid16 , uint8_t properties, uint8_t min_len, uint8_t max_len, BLEDataType_t datatype, const char* description = NULL, const GattPresentationFormat* presentFormat = NULL); - uint8_t addCharacteristic(uint8_t uuid128[], uint8_t properties, uint8_t min_len, uint8_t max_len, BLEDataType_t datatype, const char* description = NULL, const GattPresentationFormat* presentFormat = NULL); + uint8_t addCharacteristic(uint16_t uuid16, uint8_t properties, + uint8_t min_len, uint8_t max_len, + BLEDataType_t datatype, + const char *description = NULL, + const GattPresentationFormat *presentFormat = NULL); + uint8_t addCharacteristic(uint8_t uuid128[], uint8_t properties, + uint8_t min_len, uint8_t max_len, + BLEDataType_t datatype, + const char *description = NULL, + const GattPresentationFormat *presentFormat = NULL); //------------- Get Characteristic -------------// - uint8_t getChar(uint8_t charID); - uint8_t getChar(uint8_t charID, uint8_t* buf, uint8_t bufsize); + uint8_t getChar(uint8_t charID); + uint8_t getChar(uint8_t charID, uint8_t *buf, uint8_t bufsize); - uint8_t getCharInt8(uint8_t charID) - { - if ( this->getChar(charID) < sizeof(uint8_t) ) return 0; + uint8_t getCharInt8(uint8_t charID) { + if (this->getChar(charID) < sizeof(uint8_t)) + return 0; uint8_t result; memcpy(&result, this->buffer, sizeof(result)); return result; } - uint16_t getCharInt16(uint8_t charID) - { - if ( this->getChar(charID) < sizeof(uint16_t) ) return 0; + uint16_t getCharInt16(uint8_t charID) { + if (this->getChar(charID) < sizeof(uint16_t)) + return 0; uint16_t result; memcpy(&result, this->buffer, sizeof(result)); return result; } - uint32_t getCharInt32(uint8_t charID) - { - if ( this->getChar(charID) < sizeof(uint32_t) ) return 0; + uint32_t getCharInt32(uint8_t charID) { + if (this->getChar(charID) < sizeof(uint32_t)) + return 0; uint32_t result; memcpy(&result, this->buffer, sizeof(result)); return result; } - char* getCharStr(uint8_t charID) - { - if ( this->getChar(charID) == 0 ) return NULL; + char *getCharStr(uint8_t charID) { + if (this->getChar(charID) == 0) + return NULL; return this->buffer; } //------------- Set Characteristic -------------// - bool setChar(uint8_t charID, uint8_t const data[], uint8_t size); - bool setChar(uint8_t charID, char const * str); + bool setChar(uint8_t charID, uint8_t const data[], uint8_t size); + bool setChar(uint8_t charID, char const *str); - bool setChar(uint8_t charID, uint8_t data8 ) { return this->setChar(charID, (uint8_t*) &data8, 1); } - bool setChar(uint8_t charID, int8_t data8 ) { return this->setChar(charID, (uint8_t*) &data8, 1); } + bool setChar(uint8_t charID, uint8_t data8) { + return this->setChar(charID, (uint8_t *)&data8, 1); + } + bool setChar(uint8_t charID, int8_t data8) { + return this->setChar(charID, (uint8_t *)&data8, 1); + } - bool setChar(uint8_t charID, uint16_t data16) { return this->setChar(charID, (uint8_t*) &data16, 2); } - bool setChar(uint8_t charID, int16_t data16) { return this->setChar(charID, (uint8_t*) &data16, 2); } + bool setChar(uint8_t charID, uint16_t data16) { + return this->setChar(charID, (uint8_t *)&data16, 2); + } + bool setChar(uint8_t charID, int16_t data16) { + return this->setChar(charID, (uint8_t *)&data16, 2); + } - bool setChar(uint8_t charID, uint32_t data32) { return this->setChar(charID, (uint8_t*) &data32, 4); } - bool setChar(uint8_t charID, int32_t data32) { return this->setChar(charID, (uint8_t*) &data32, 4); } + bool setChar(uint8_t charID, uint32_t data32) { + return this->setChar(charID, (uint8_t *)&data32, 4); + } + bool setChar(uint8_t charID, int32_t data32) { + return this->setChar(charID, (uint8_t *)&data32, 4); + } }; -enum -{ +enum { GATT_PRESENT_FORMAT_BOOLEAN = 0x01, - GATT_PRESENT_FORMAT_2BIT = 0x02, - GATT_PRESENT_FORMAT_4BIT = 0x03, - GATT_PRESENT_FORMAT_UINT8 = 0x04, - GATT_PRESENT_FORMAT_UINT12 = 0x05, - GATT_PRESENT_FORMAT_UINT16 = 0x06, - GATT_PRESENT_FORMAT_UINT24 = 0x07, - GATT_PRESENT_FORMAT_UINT32 = 0x08, - GATT_PRESENT_FORMAT_UINT48 = 0x09, - GATT_PRESENT_FORMAT_UINT64 = 0x0A, + GATT_PRESENT_FORMAT_2BIT = 0x02, + GATT_PRESENT_FORMAT_4BIT = 0x03, + GATT_PRESENT_FORMAT_UINT8 = 0x04, + GATT_PRESENT_FORMAT_UINT12 = 0x05, + GATT_PRESENT_FORMAT_UINT16 = 0x06, + GATT_PRESENT_FORMAT_UINT24 = 0x07, + GATT_PRESENT_FORMAT_UINT32 = 0x08, + GATT_PRESENT_FORMAT_UINT48 = 0x09, + GATT_PRESENT_FORMAT_UINT64 = 0x0A, GATT_PRESENT_FORMAT_UINT128 = 0x0B, - GATT_PRESENT_FORMAT_SINT8 = 0x0C, - GATT_PRESENT_FORMAT_SINT12 = 0x0D, - GATT_PRESENT_FORMAT_SINT16 = 0x0E, - GATT_PRESENT_FORMAT_SINT24 = 0x0F, - GATT_PRESENT_FORMAT_SINT32 = 0x10, - GATT_PRESENT_FORMAT_SINT48 = 0x11, - GATT_PRESENT_FORMAT_SINT64 = 0x12, + GATT_PRESENT_FORMAT_SINT8 = 0x0C, + GATT_PRESENT_FORMAT_SINT12 = 0x0D, + GATT_PRESENT_FORMAT_SINT16 = 0x0E, + GATT_PRESENT_FORMAT_SINT24 = 0x0F, + GATT_PRESENT_FORMAT_SINT32 = 0x10, + GATT_PRESENT_FORMAT_SINT48 = 0x11, + GATT_PRESENT_FORMAT_SINT64 = 0x12, GATT_PRESENT_FORMAT_SINT128 = 0x13, GATT_PRESENT_FORMAT_FLOAT32 = 0x14, GATT_PRESENT_FORMAT_FLOAT64 = 0x15, - GATT_PRESENT_FORMAT_SFLOAT = 0x16, - GATT_PRESENT_FORMAT_FLOAT = 0x17, + GATT_PRESENT_FORMAT_SFLOAT = 0x16, + GATT_PRESENT_FORMAT_FLOAT = 0x17, GATT_PRESENT_FORMAT_DUINT16 = 0x18, - GATT_PRESENT_FORMAT_UTF8S = 0x19, - GATT_PRESENT_FORMAT_UTF16S = 0x1A, - GATT_PRESENT_FORMAT_STRUCT = 0x1B, + GATT_PRESENT_FORMAT_UTF8S = 0x19, + GATT_PRESENT_FORMAT_UTF16S = 0x1A, + GATT_PRESENT_FORMAT_STRUCT = 0x1B, }; /* See https://developer.bluetooth.org/gatt/units/Pages/default.aspx */ -enum -{ - GATT_PRESENT_UNIT_NONE = 0x2700, - GATT_PRESENT_UNIT_LENGTH_METRE = 0x2701, - GATT_PRESENT_UNIT_MASS_KILOGRAM = 0x2702, - GATT_PRESENT_UNIT_TIME_SECOND = 0x2703, - GATT_PRESENT_UNIT_ELECTRIC_CURRENT_AMPERE = 0x2704, - GATT_PRESENT_UNIT_THERMODYNAMIC_TEMPERATURE_KELVIN = 0x2705, - GATT_PRESENT_UNIT_AMOUNT_OF_SUBSTANCE_MOLE = 0x2706, - GATT_PRESENT_UNIT_LUMINOUS_INTENSITY_CANDELA = 0x2707, - GATT_PRESENT_UNIT_AREA_SQUARE_METRES = 0x2710, - GATT_PRESENT_UNIT_VOLUME_CUBIC_METRES = 0x2711, - GATT_PRESENT_UNIT_VELOCITY_METRES_PER_SECOND = 0x2712, - GATT_PRESENT_UNIT_ACCELERATION_METRES_PER_SECOND_SQUARED = 0x2713, - GATT_PRESENT_UNIT_WAVENUMBER_RECIPROCAL_METRE = 0x2714, - GATT_PRESENT_UNIT_DENSITY_KILOGRAM_PER_CUBIC_METRE = 0x2715, - GATT_PRESENT_UNIT_SURFACE_DENSITY_KILOGRAM_PER_SQUARE_METRE = 0x2716, - GATT_PRESENT_UNIT_SPECIFIC_VOLUME_CUBIC_METRE_PER_KILOGRAM = 0x2717, - GATT_PRESENT_UNIT_CURRENT_DENSITY_AMPERE_PER_SQUARE_METRE = 0x2718, - GATT_PRESENT_UNIT_MAGNETIC_FIELD_STRENGTH_AMPERE_PER_METRE = 0x2719, - GATT_PRESENT_UNIT_AMOUNT_CONCENTRATION_MOLE_PER_CUBIC_METRE = 0x271A, - GATT_PRESENT_UNIT_MASS_CONCENTRATION_KILOGRAM_PER_CUBIC_METRE = 0x271B, - GATT_PRESENT_UNIT_LUMINANCE_CANDELA_PER_SQUARE_METRE = 0x271C, - GATT_PRESENT_UNIT_REFRACTIVE_INDEX = 0x271D, - GATT_PRESENT_UNIT_RELATIVE_PERMEABILITY = 0x271E, - GATT_PRESENT_UNIT_PLANE_ANGLE_RADIAN = 0x2720, - GATT_PRESENT_UNIT_SOLID_ANGLE_STERADIAN = 0x2721, - GATT_PRESENT_UNIT_FREQUENCY_HERTZ = 0x2722, - GATT_PRESENT_UNIT_FORCE_NEWTON = 0x2723, - GATT_PRESENT_UNIT_PRESSURE_PASCAL = 0x2724, - GATT_PRESENT_UNIT_ENERGY_JOULE = 0x2725, - GATT_PRESENT_UNIT_POWER_WATT = 0x2726, - GATT_PRESENT_UNIT_ELECTRIC_CHARGE_COULOMB = 0x2727, - GATT_PRESENT_UNIT_ELECTRIC_POTENTIAL_DIFFERENCE_VOLT = 0x2728, - GATT_PRESENT_UNIT_CAPACITANCE_FARAD = 0x2729, - GATT_PRESENT_UNIT_ELECTRIC_RESISTANCE_OHM = 0x272A, - GATT_PRESENT_UNIT_ELECTRIC_CONDUCTANCE_SIEMENS = 0x272B, - GATT_PRESENT_UNIT_MAGNETIC_FLEX_WEBER = 0x272C, - GATT_PRESENT_UNIT_MAGNETIC_FLEX_DENSITY_TESLA = 0x272D, - GATT_PRESENT_UNIT_INDUCTANCE_HENRY = 0x272E, - GATT_PRESENT_UNIT_THERMODYNAMIC_TEMPERATURE_DEGREE_CELSIUS = 0x272F, - GATT_PRESENT_UNIT_LUMINOUS_FLUX_LUMEN = 0x2730, - GATT_PRESENT_UNIT_ILLUMINANCE_LUX = 0x2731, - GATT_PRESENT_UNIT_ACTIVITY_REFERRED_TO_A_RADIONUCLIDE_BECQUEREL = 0x2732, - GATT_PRESENT_UNIT_ABSORBED_DOSE_GRAY = 0x2733, - GATT_PRESENT_UNIT_DOSE_EQUIVALENT_SIEVERT = 0x2734, - GATT_PRESENT_UNIT_CATALYTIC_ACTIVITY_KATAL = 0x2735, - GATT_PRESENT_UNIT_DYNAMIC_VISCOSITY_PASCAL_SECOND = 0x2740, - GATT_PRESENT_UNIT_MOMENT_OF_FORCE_NEWTON_METRE = 0x2741, - GATT_PRESENT_UNIT_SURFACE_TENSION_NEWTON_PER_METRE = 0x2742, - GATT_PRESENT_UNIT_ANGULAR_VELOCITY_RADIAN_PER_SECOND = 0x2743, - GATT_PRESENT_UNIT_ANGULAR_ACCELERATION_RADIAN_PER_SECOND_SQUARED = 0x2744, - GATT_PRESENT_UNIT_HEAT_FLUX_DENSITY_WATT_PER_SQUARE_METRE = 0x2745, - GATT_PRESENT_UNIT_HEAT_CAPACITY_JOULE_PER_KELVIN = 0x2746, - GATT_PRESENT_UNIT_SPECIFIC_HEAT_CAPACITY_JOULE_PER_KILOGRAM_KELVIN = 0x2747, - GATT_PRESENT_UNIT_SPECIFIC_ENERGY_JOULE_PER_KILOGRAM = 0x2748, - GATT_PRESENT_UNIT_THERMAL_CONDUCTIVITY_WATT_PER_METRE_KELVIN = 0x2749, - GATT_PRESENT_UNIT_ENERGY_DENSITY_JOULE_PER_CUBIC_METRE = 0x274A, - GATT_PRESENT_UNIT_ELECTRIC_FIELD_STRENGTH_VOLT_PER_METRE = 0x274B, - GATT_PRESENT_UNIT_ELECTRIC_CHARGE_DENSITY_COULOMB_PER_CUBIC_METRE = 0x274C, - GATT_PRESENT_UNIT_SURFACE_CHARGE_DENSITY_COULOMB_PER_SQUARE_METRE = 0x274D, - GATT_PRESENT_UNIT_ELECTRIC_FLUX_DENSITY_COULOMB_PER_SQUARE_METRE = 0x274E, - GATT_PRESENT_UNIT_PERMITTIVITY_FARAD_PER_METRE = 0x274F, - GATT_PRESENT_UNIT_PERMEABILITY_HENRY_PER_METRE = 0x2750, - GATT_PRESENT_UNIT_MOLAR_ENERGY_JOULE_PER_MOLE = 0x2751, - GATT_PRESENT_UNIT_MOLAR_ENTROPY_JOULE_PER_MOLE_KELVIN = 0x2752, - GATT_PRESENT_UNIT_EXPOSURE_COULOMB_PER_KILOGRAM = 0x2753, - GATT_PRESENT_UNIT_ABSORBED_DOSE_RATE_GRAY_PER_SECOND = 0x2754, - GATT_PRESENT_UNIT_RADIANT_INTENSITY_WATT_PER_STERADIAN = 0x2755, - GATT_PRESENT_UNIT_RADIANCE_WATT_PER_SQUARE_METRE_STERADIAN = 0x2756, - GATT_PRESENT_UNIT_CATALYTIC_ACTIVITY_CONCENTRATION_KATAL_PER_CUBIC_METRE = 0x2757, - GATT_PRESENT_UNIT_TIME_MINUTE = 0x2760, - GATT_PRESENT_UNIT_TIME_HOUR = 0x2761, - GATT_PRESENT_UNIT_TIME_DAY = 0x2762, - GATT_PRESENT_UNIT_PLANE_ANGLE_DEGREE = 0x2763, - GATT_PRESENT_UNIT_PLANE_ANGLE_MINUTE = 0x2764, - GATT_PRESENT_UNIT_PLANE_ANGLE_SECOND = 0x2765, - GATT_PRESENT_UNIT_AREA_HECTARE = 0x2766, - GATT_PRESENT_UNIT_VOLUME_LITRE = 0x2767, - GATT_PRESENT_UNIT_MASS_TONNE = 0x2768, - GATT_PRESENT_UNIT_PRESSURE_BAR = 0x2780, - GATT_PRESENT_UNIT_PRESSURE_MILLIMETRE_OF_MERCURY = 0x2781, - GATT_PRESENT_UNIT_LENGTH_ANGSTROM = 0x2782, - GATT_PRESENT_UNIT_LENGTH_NAUTICAL_MILE = 0x2783, - GATT_PRESENT_UNIT_AREA_BARN = 0x2784, - GATT_PRESENT_UNIT_VELOCITY_KNOT = 0x2785, - GATT_PRESENT_UNIT_LOGARITHMIC_RADIO_QUANTITY_NEPER = 0x2786, - GATT_PRESENT_UNIT_LOGARITHMIC_RADIO_QUANTITY_BEL = 0x2787, - GATT_PRESENT_UNIT_LENGTH_YARD = 0x27A0, - GATT_PRESENT_UNIT_LENGTH_PARSEC = 0x27A1, - GATT_PRESENT_UNIT_LENGTH_INCH = 0x27A2, - GATT_PRESENT_UNIT_LENGTH_FOOT = 0x27A3, - GATT_PRESENT_UNIT_LENGTH_MILE = 0x27A4, - GATT_PRESENT_UNIT_PRESSURE_POUND_FORCE_PER_SQUARE_INCH = 0x27A5, - GATT_PRESENT_UNIT_VELOCITY_KILOMETRE_PER_HOUR = 0x27A6, - GATT_PRESENT_UNIT_VELOCITY_MILE_PER_HOUR = 0x27A7, - GATT_PRESENT_UNIT_ANGULAR_VELOCITY_REVOLUTION_PER_MINUTE = 0x27A8, - GATT_PRESENT_UNIT_ENERGY_GRAM_CALORIE = 0x27A9, - GATT_PRESENT_UNIT_ENERGY_KILOGRAM_CALORIE = 0x27AA, - GATT_PRESENT_UNIT_ENERGY_KILOWATT_HOUR = 0x27AB, - GATT_PRESENT_UNIT_THERMODYNAMIC_TEMPERATURE_DEGREE_FAHRENHEIT = 0x27AC, - GATT_PRESENT_UNIT_PERCENTAGE = 0x27AD, - GATT_PRESENT_UNIT_PER_MILLE = 0x27AE, - GATT_PRESENT_UNIT_PERIOD_BEATS_PER_MINUTE = 0x27AF, - GATT_PRESENT_UNIT_ELECTRIC_CHARGE_AMPERE_HOURS = 0x27B0, - GATT_PRESENT_UNIT_MASS_DENSITY_MILLIGRAM_PER_DECILITRE = 0x27B1, - GATT_PRESENT_UNIT_MASS_DENSITY_MILLIMOLE_PER_LITRE = 0x27B2, - GATT_PRESENT_UNIT_TIME_YEAR = 0x27B3, - GATT_PRESENT_UNIT_TIME_MONTH = 0x27B4, - GATT_PRESENT_UNIT_CONCENTRATION_COUNT_PER_CUBIC_METRE = 0x27B5, - GATT_PRESENT_UNIT_IRRADIANCE_WATT_PER_SQUARE_METRE = 0x27B6 +enum { + GATT_PRESENT_UNIT_NONE = 0x2700, + GATT_PRESENT_UNIT_LENGTH_METRE = 0x2701, + GATT_PRESENT_UNIT_MASS_KILOGRAM = 0x2702, + GATT_PRESENT_UNIT_TIME_SECOND = 0x2703, + GATT_PRESENT_UNIT_ELECTRIC_CURRENT_AMPERE = 0x2704, + GATT_PRESENT_UNIT_THERMODYNAMIC_TEMPERATURE_KELVIN = 0x2705, + GATT_PRESENT_UNIT_AMOUNT_OF_SUBSTANCE_MOLE = 0x2706, + GATT_PRESENT_UNIT_LUMINOUS_INTENSITY_CANDELA = 0x2707, + GATT_PRESENT_UNIT_AREA_SQUARE_METRES = 0x2710, + GATT_PRESENT_UNIT_VOLUME_CUBIC_METRES = 0x2711, + GATT_PRESENT_UNIT_VELOCITY_METRES_PER_SECOND = 0x2712, + GATT_PRESENT_UNIT_ACCELERATION_METRES_PER_SECOND_SQUARED = 0x2713, + GATT_PRESENT_UNIT_WAVENUMBER_RECIPROCAL_METRE = 0x2714, + GATT_PRESENT_UNIT_DENSITY_KILOGRAM_PER_CUBIC_METRE = 0x2715, + GATT_PRESENT_UNIT_SURFACE_DENSITY_KILOGRAM_PER_SQUARE_METRE = 0x2716, + GATT_PRESENT_UNIT_SPECIFIC_VOLUME_CUBIC_METRE_PER_KILOGRAM = 0x2717, + GATT_PRESENT_UNIT_CURRENT_DENSITY_AMPERE_PER_SQUARE_METRE = 0x2718, + GATT_PRESENT_UNIT_MAGNETIC_FIELD_STRENGTH_AMPERE_PER_METRE = 0x2719, + GATT_PRESENT_UNIT_AMOUNT_CONCENTRATION_MOLE_PER_CUBIC_METRE = 0x271A, + GATT_PRESENT_UNIT_MASS_CONCENTRATION_KILOGRAM_PER_CUBIC_METRE = 0x271B, + GATT_PRESENT_UNIT_LUMINANCE_CANDELA_PER_SQUARE_METRE = 0x271C, + GATT_PRESENT_UNIT_REFRACTIVE_INDEX = 0x271D, + GATT_PRESENT_UNIT_RELATIVE_PERMEABILITY = 0x271E, + GATT_PRESENT_UNIT_PLANE_ANGLE_RADIAN = 0x2720, + GATT_PRESENT_UNIT_SOLID_ANGLE_STERADIAN = 0x2721, + GATT_PRESENT_UNIT_FREQUENCY_HERTZ = 0x2722, + GATT_PRESENT_UNIT_FORCE_NEWTON = 0x2723, + GATT_PRESENT_UNIT_PRESSURE_PASCAL = 0x2724, + GATT_PRESENT_UNIT_ENERGY_JOULE = 0x2725, + GATT_PRESENT_UNIT_POWER_WATT = 0x2726, + GATT_PRESENT_UNIT_ELECTRIC_CHARGE_COULOMB = 0x2727, + GATT_PRESENT_UNIT_ELECTRIC_POTENTIAL_DIFFERENCE_VOLT = 0x2728, + GATT_PRESENT_UNIT_CAPACITANCE_FARAD = 0x2729, + GATT_PRESENT_UNIT_ELECTRIC_RESISTANCE_OHM = 0x272A, + GATT_PRESENT_UNIT_ELECTRIC_CONDUCTANCE_SIEMENS = 0x272B, + GATT_PRESENT_UNIT_MAGNETIC_FLEX_WEBER = 0x272C, + GATT_PRESENT_UNIT_MAGNETIC_FLEX_DENSITY_TESLA = 0x272D, + GATT_PRESENT_UNIT_INDUCTANCE_HENRY = 0x272E, + GATT_PRESENT_UNIT_THERMODYNAMIC_TEMPERATURE_DEGREE_CELSIUS = 0x272F, + GATT_PRESENT_UNIT_LUMINOUS_FLUX_LUMEN = 0x2730, + GATT_PRESENT_UNIT_ILLUMINANCE_LUX = 0x2731, + GATT_PRESENT_UNIT_ACTIVITY_REFERRED_TO_A_RADIONUCLIDE_BECQUEREL = 0x2732, + GATT_PRESENT_UNIT_ABSORBED_DOSE_GRAY = 0x2733, + GATT_PRESENT_UNIT_DOSE_EQUIVALENT_SIEVERT = 0x2734, + GATT_PRESENT_UNIT_CATALYTIC_ACTIVITY_KATAL = 0x2735, + GATT_PRESENT_UNIT_DYNAMIC_VISCOSITY_PASCAL_SECOND = 0x2740, + GATT_PRESENT_UNIT_MOMENT_OF_FORCE_NEWTON_METRE = 0x2741, + GATT_PRESENT_UNIT_SURFACE_TENSION_NEWTON_PER_METRE = 0x2742, + GATT_PRESENT_UNIT_ANGULAR_VELOCITY_RADIAN_PER_SECOND = 0x2743, + GATT_PRESENT_UNIT_ANGULAR_ACCELERATION_RADIAN_PER_SECOND_SQUARED = 0x2744, + GATT_PRESENT_UNIT_HEAT_FLUX_DENSITY_WATT_PER_SQUARE_METRE = 0x2745, + GATT_PRESENT_UNIT_HEAT_CAPACITY_JOULE_PER_KELVIN = 0x2746, + GATT_PRESENT_UNIT_SPECIFIC_HEAT_CAPACITY_JOULE_PER_KILOGRAM_KELVIN = 0x2747, + GATT_PRESENT_UNIT_SPECIFIC_ENERGY_JOULE_PER_KILOGRAM = 0x2748, + GATT_PRESENT_UNIT_THERMAL_CONDUCTIVITY_WATT_PER_METRE_KELVIN = 0x2749, + GATT_PRESENT_UNIT_ENERGY_DENSITY_JOULE_PER_CUBIC_METRE = 0x274A, + GATT_PRESENT_UNIT_ELECTRIC_FIELD_STRENGTH_VOLT_PER_METRE = 0x274B, + GATT_PRESENT_UNIT_ELECTRIC_CHARGE_DENSITY_COULOMB_PER_CUBIC_METRE = 0x274C, + GATT_PRESENT_UNIT_SURFACE_CHARGE_DENSITY_COULOMB_PER_SQUARE_METRE = 0x274D, + GATT_PRESENT_UNIT_ELECTRIC_FLUX_DENSITY_COULOMB_PER_SQUARE_METRE = 0x274E, + GATT_PRESENT_UNIT_PERMITTIVITY_FARAD_PER_METRE = 0x274F, + GATT_PRESENT_UNIT_PERMEABILITY_HENRY_PER_METRE = 0x2750, + GATT_PRESENT_UNIT_MOLAR_ENERGY_JOULE_PER_MOLE = 0x2751, + GATT_PRESENT_UNIT_MOLAR_ENTROPY_JOULE_PER_MOLE_KELVIN = 0x2752, + GATT_PRESENT_UNIT_EXPOSURE_COULOMB_PER_KILOGRAM = 0x2753, + GATT_PRESENT_UNIT_ABSORBED_DOSE_RATE_GRAY_PER_SECOND = 0x2754, + GATT_PRESENT_UNIT_RADIANT_INTENSITY_WATT_PER_STERADIAN = 0x2755, + GATT_PRESENT_UNIT_RADIANCE_WATT_PER_SQUARE_METRE_STERADIAN = 0x2756, + GATT_PRESENT_UNIT_CATALYTIC_ACTIVITY_CONCENTRATION_KATAL_PER_CUBIC_METRE = + 0x2757, + GATT_PRESENT_UNIT_TIME_MINUTE = 0x2760, + GATT_PRESENT_UNIT_TIME_HOUR = 0x2761, + GATT_PRESENT_UNIT_TIME_DAY = 0x2762, + GATT_PRESENT_UNIT_PLANE_ANGLE_DEGREE = 0x2763, + GATT_PRESENT_UNIT_PLANE_ANGLE_MINUTE = 0x2764, + GATT_PRESENT_UNIT_PLANE_ANGLE_SECOND = 0x2765, + GATT_PRESENT_UNIT_AREA_HECTARE = 0x2766, + GATT_PRESENT_UNIT_VOLUME_LITRE = 0x2767, + GATT_PRESENT_UNIT_MASS_TONNE = 0x2768, + GATT_PRESENT_UNIT_PRESSURE_BAR = 0x2780, + GATT_PRESENT_UNIT_PRESSURE_MILLIMETRE_OF_MERCURY = 0x2781, + GATT_PRESENT_UNIT_LENGTH_ANGSTROM = 0x2782, + GATT_PRESENT_UNIT_LENGTH_NAUTICAL_MILE = 0x2783, + GATT_PRESENT_UNIT_AREA_BARN = 0x2784, + GATT_PRESENT_UNIT_VELOCITY_KNOT = 0x2785, + GATT_PRESENT_UNIT_LOGARITHMIC_RADIO_QUANTITY_NEPER = 0x2786, + GATT_PRESENT_UNIT_LOGARITHMIC_RADIO_QUANTITY_BEL = 0x2787, + GATT_PRESENT_UNIT_LENGTH_YARD = 0x27A0, + GATT_PRESENT_UNIT_LENGTH_PARSEC = 0x27A1, + GATT_PRESENT_UNIT_LENGTH_INCH = 0x27A2, + GATT_PRESENT_UNIT_LENGTH_FOOT = 0x27A3, + GATT_PRESENT_UNIT_LENGTH_MILE = 0x27A4, + GATT_PRESENT_UNIT_PRESSURE_POUND_FORCE_PER_SQUARE_INCH = 0x27A5, + GATT_PRESENT_UNIT_VELOCITY_KILOMETRE_PER_HOUR = 0x27A6, + GATT_PRESENT_UNIT_VELOCITY_MILE_PER_HOUR = 0x27A7, + GATT_PRESENT_UNIT_ANGULAR_VELOCITY_REVOLUTION_PER_MINUTE = 0x27A8, + GATT_PRESENT_UNIT_ENERGY_GRAM_CALORIE = 0x27A9, + GATT_PRESENT_UNIT_ENERGY_KILOGRAM_CALORIE = 0x27AA, + GATT_PRESENT_UNIT_ENERGY_KILOWATT_HOUR = 0x27AB, + GATT_PRESENT_UNIT_THERMODYNAMIC_TEMPERATURE_DEGREE_FAHRENHEIT = 0x27AC, + GATT_PRESENT_UNIT_PERCENTAGE = 0x27AD, + GATT_PRESENT_UNIT_PER_MILLE = 0x27AE, + GATT_PRESENT_UNIT_PERIOD_BEATS_PER_MINUTE = 0x27AF, + GATT_PRESENT_UNIT_ELECTRIC_CHARGE_AMPERE_HOURS = 0x27B0, + GATT_PRESENT_UNIT_MASS_DENSITY_MILLIGRAM_PER_DECILITRE = 0x27B1, + GATT_PRESENT_UNIT_MASS_DENSITY_MILLIMOLE_PER_LITRE = 0x27B2, + GATT_PRESENT_UNIT_TIME_YEAR = 0x27B3, + GATT_PRESENT_UNIT_TIME_MONTH = 0x27B4, + GATT_PRESENT_UNIT_CONCENTRATION_COUNT_PER_CUBIC_METRE = 0x27B5, + GATT_PRESENT_UNIT_IRRADIANCE_WATT_PER_SQUARE_METRE = 0x27B6 }; #endif /* _ADAFRUIT_BLEGATT_H_ */ diff --git a/Adafruit_BLEMIDI.cpp b/Adafruit_BLEMIDI.cpp index 65471ca..324a85b 100644 --- a/Adafruit_BLEMIDI.cpp +++ b/Adafruit_BLEMIDI.cpp @@ -29,54 +29,50 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include "Adafruit_BLEMIDI.h" -#define MIDI_MINIMUM_FIRMWARE_VERSION "0.7.0" +#define MIDI_MINIMUM_FIRMWARE_VERSION "0.7.0" /******************************************************************************/ /*! @brief Constructor */ /******************************************************************************/ -Adafruit_BLEMIDI::Adafruit_BLEMIDI(Adafruit_BLE& ble) : - _ble(ble) -{ -} +Adafruit_BLEMIDI::Adafruit_BLEMIDI(Adafruit_BLE &ble) : _ble(ble) {} /******************************************************************************/ /*! @brief Set callback */ /******************************************************************************/ -void Adafruit_BLEMIDI::setRxCallback(midiRxCallback_t fp) -{ +void Adafruit_BLEMIDI::setRxCallback(midiRxCallback_t fp) { _ble.setBleMidiRxCallback(fp); } - /******************************************************************************/ /*! @brief Enable MIDI service if not already enabled @param reset true will reset Bluefruit */ /******************************************************************************/ -bool Adafruit_BLEMIDI::begin(bool reset) -{ - VERIFY_( _ble.isVersionAtLeast(MIDI_MINIMUM_FIRMWARE_VERSION) ); +bool Adafruit_BLEMIDI::begin(bool reset) { + VERIFY_(_ble.isVersionAtLeast(MIDI_MINIMUM_FIRMWARE_VERSION)); int32_t enabled = 0; - VERIFY_( _ble.atcommandIntReply( F("AT+BLEMIDIEN"), &enabled) ); + VERIFY_(_ble.atcommandIntReply(F("AT+BLEMIDIEN"), &enabled)); - if ( enabled ) return true; - VERIFY_( _ble.atcommand( F("AT+BLEMIDIEN=1") ) ); + if (enabled) + return true; + VERIFY_(_ble.atcommand(F("AT+BLEMIDIEN=1"))); // Perform Bluefruit reset if needed - if (reset) _ble.reset(); + if (reset) + _ble.reset(); return true; } @@ -87,16 +83,17 @@ bool Adafruit_BLEMIDI::begin(bool reset) @param reset true will reset Bluefruit */ /******************************************************************************/ -bool Adafruit_BLEMIDI::stop(bool reset) -{ +bool Adafruit_BLEMIDI::stop(bool reset) { int32_t enabled = 0; - VERIFY_( _ble.atcommandIntReply( F("AT+BLEMIDIEN"), &enabled) ); - if ( !enabled ) return true; + VERIFY_(_ble.atcommandIntReply(F("AT+BLEMIDIEN"), &enabled)); + if (!enabled) + return true; - VERIFY_( _ble.atcommand( F("AT+BLEMIDIEN=0") ) ); + VERIFY_(_ble.atcommand(F("AT+BLEMIDIEN=0"))); // Perform Bluefruit reset if needed - if (reset) _ble.reset(); + if (reset) + _ble.reset(); return true; } @@ -107,9 +104,8 @@ bool Adafruit_BLEMIDI::stop(bool reset) @param bytes MIDI event data */ /******************************************************************************/ -bool Adafruit_BLEMIDI::send(const uint8_t bytes[3]) -{ - return _ble.atcommand( F("AT+BLEMIDITX"), bytes, 3); +bool Adafruit_BLEMIDI::send(const uint8_t bytes[3]) { + return _ble.atcommand(F("AT+BLEMIDITX"), bytes, 3); } /******************************************************************************/ @@ -122,14 +118,14 @@ bool Adafruit_BLEMIDI::send(const uint8_t bytes[3]) @note count + 1 must less than (20-3) --> count <= 16 */ /******************************************************************************/ -bool Adafruit_BLEMIDI::send_n(uint8_t status, const uint8_t bytes[], uint8_t count) -{ +bool Adafruit_BLEMIDI::send_n(uint8_t status, const uint8_t bytes[], + uint8_t count) { VERIFY_(count <= 16); - uint8_t data[17] = { status }; - memcpy(data+1, bytes, count); + uint8_t data[17] = {status}; + memcpy(data + 1, bytes, count); - return _ble.atcommand( F("AT+BLEMIDITX"), data, count+1); + return _ble.atcommand(F("AT+BLEMIDITX"), data, count + 1); } /******************************************************************************/ @@ -138,9 +134,11 @@ bool Adafruit_BLEMIDI::send_n(uint8_t status, const uint8_t bytes[], uint8_t cou @param */ /******************************************************************************/ -void Adafruit_BLEMIDI::processRxCallback(uint8_t data[], uint16_t len, Adafruit_BLE::midiRxCallback_t callback_func) -{ - if ( len < 3 ) return; +void Adafruit_BLEMIDI::processRxCallback( + uint8_t data[], uint16_t len, + Adafruit_BLE::midiRxCallback_t callback_func) { + if (len < 3) + return; // First 3 bytes is always : Header + Timestamp + Status midi_header_t header; @@ -150,15 +148,13 @@ void Adafruit_BLEMIDI::processRxCallback(uint8_t data[], uint16_t len, Adafruit_ header.byte = *data++; len--; - while (len) - { + while (len) { /* event : 0x00 - 0x7F status: 0x80 - 0xEF sysex : 0xF0 - 0xFF */ - if ( bitRead(data[0], 7) ) - { + if (bitRead(data[0], 7)) { // Start of new full event midi_timestamp_t timestamp; timestamp.byte = *data++; @@ -167,19 +163,18 @@ void Adafruit_BLEMIDI::processRxCallback(uint8_t data[], uint16_t len, Adafruit_ status = *data++; // Status must have 7th-bit set, must have something wrong - if ( !bitRead(status, 7) ) return; + if (!bitRead(status, 7)) + return; - callback_func( tstamp, status, data[0], data[1]); + callback_func(tstamp, status, data[0], data[1]); - len -= 4; + len -= 4; data += 2; - } - else - { + } else { // Running event - callback_func( tstamp, status, data[0], data[1]); + callback_func(tstamp, status, data[0], data[1]); - len -= 2; + len -= 2; data += 2; } } diff --git a/Adafruit_BLEMIDI.h b/Adafruit_BLEMIDI.h index 957ea51..b1be577 100644 --- a/Adafruit_BLEMIDI.h +++ b/Adafruit_BLEMIDI.h @@ -29,34 +29,32 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _ADAFRUIT_BLEMIDI_H_ #define _ADAFRUIT_BLEMIDI_H_ -#include #include "Adafruit_BLE.h" +#include -typedef struct ATTR_PACKED -{ +typedef struct ATTR_PACKED { union { struct { uint8_t timestamp_hi : 6; - uint8_t reserved : 1; - uint8_t start_bit : 1; + uint8_t reserved : 1; + uint8_t start_bit : 1; }; uint8_t byte; }; } midi_header_t; -ASSERT_STATIC_ ( sizeof(midi_header_t) == 1 ); +ASSERT_STATIC_(sizeof(midi_header_t) == 1); -typedef struct ATTR_PACKED -{ +typedef struct ATTR_PACKED { union { struct { uint8_t timestamp_low : 7; @@ -67,31 +65,28 @@ typedef struct ATTR_PACKED }; } midi_timestamp_t; -ASSERT_STATIC_ ( sizeof(midi_timestamp_t) == 1 ); +ASSERT_STATIC_(sizeof(midi_timestamp_t) == 1); -class Adafruit_BLEMIDI -{ +class Adafruit_BLEMIDI { private: - Adafruit_BLE& _ble; + Adafruit_BLE &_ble; public: typedef Adafruit_BLE::midiRxCallback_t midiRxCallback_t; - Adafruit_BLEMIDI(Adafruit_BLE& ble); + Adafruit_BLEMIDI(Adafruit_BLE &ble); bool begin(bool reset = true); - bool stop (bool reset = true); + bool stop(bool reset = true); bool send(const uint8_t bytes[3]); - bool send(uint8_t status, const uint8_t bytes[2]) - { - uint8_t buffer[3] = { status, bytes[0], bytes[1] }; + bool send(uint8_t status, const uint8_t bytes[2]) { + uint8_t buffer[3] = {status, bytes[0], bytes[1]}; return send(buffer); } - bool send(uint8_t status, uint8_t byte1, uint8_t byte2) - { - uint8_t buffer[3] = { status, byte1, byte2 }; + bool send(uint8_t status, uint8_t byte1, uint8_t byte2) { + uint8_t buffer[3] = {status, byte1, byte2}; return send(buffer); } @@ -99,7 +94,8 @@ class Adafruit_BLEMIDI void setRxCallback(midiRxCallback_t fp); - static void processRxCallback(uint8_t data[], uint16_t len, Adafruit_BLE::midiRxCallback_t callback_func); + static void processRxCallback(uint8_t data[], uint16_t len, + Adafruit_BLE::midiRxCallback_t callback_func); }; #endif /* _ADAFRUIT_BLEMIDI_H_ */ diff --git a/Adafruit_BluefruitLE_SPI.cpp b/Adafruit_BluefruitLE_SPI.cpp index 39f261d..a9b6889 100644 --- a/Adafruit_BluefruitLE_SPI.cpp +++ b/Adafruit_BluefruitLE_SPI.cpp @@ -29,8 +29,8 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include "Adafruit_BluefruitLE_SPI.h" @@ -38,13 +38,11 @@ #include #ifndef min - #define min(a,b) ((a) < (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) #endif - SPISettings bluefruitSPI(4000000, MSBFIRST, SPI_MODE0); - /******************************************************************************/ /*! @brief Instantiates a new instance of the Adafruit_BluefruitLE_SPI class @@ -57,12 +55,12 @@ SPISettings bluefruitSPI(4000000, MSBFIRST, SPI_MODE0); @param[in] rstPin */ /******************************************************************************/ -Adafruit_BluefruitLE_SPI::Adafruit_BluefruitLE_SPI(int8_t csPin, int8_t irqPin, int8_t rstPin) : - m_rx_fifo(m_rx_buffer, sizeof(m_rx_buffer), 1, true) -{ +Adafruit_BluefruitLE_SPI::Adafruit_BluefruitLE_SPI(int8_t csPin, int8_t irqPin, + int8_t rstPin) + : m_rx_fifo(m_rx_buffer, sizeof(m_rx_buffer), 1, true) { _physical_transport = BLUEFRUIT_TRANSPORT_HWSPI; - m_cs_pin = csPin; + m_cs_pin = csPin; m_irq_pin = irqPin; m_rst_pin = rstPin; @@ -92,25 +90,25 @@ Adafruit_BluefruitLE_SPI::Adafruit_BluefruitLE_SPI(int8_t csPin, int8_t irqPin, @param[in] rstPin */ /******************************************************************************/ -Adafruit_BluefruitLE_SPI::Adafruit_BluefruitLE_SPI(int8_t clkPin, int8_t misoPin, - int8_t mosiPin, int8_t csPin, int8_t irqPin, int8_t rstPin) : - m_rx_fifo(m_rx_buffer, sizeof(m_rx_buffer), 1, true) -{ +Adafruit_BluefruitLE_SPI::Adafruit_BluefruitLE_SPI(int8_t clkPin, + int8_t misoPin, + int8_t mosiPin, int8_t csPin, + int8_t irqPin, int8_t rstPin) + : m_rx_fifo(m_rx_buffer, sizeof(m_rx_buffer), 1, true) { _physical_transport = BLUEFRUIT_TRANSPORT_SWSPI; - m_sck_pin = clkPin; + m_sck_pin = clkPin; m_miso_pin = misoPin; m_mosi_pin = mosiPin; - m_cs_pin = csPin; - m_irq_pin = irqPin; - m_rst_pin = rstPin; + m_cs_pin = csPin; + m_irq_pin = irqPin; + m_rst_pin = rstPin; m_tx_count = 0; m_mode_switch_command_enabled = true; } - /******************************************************************************/ /*! @brief Initialize the HW to enable communication with the BLE module @@ -120,8 +118,7 @@ Adafruit_BluefruitLE_SPI::Adafruit_BluefruitLE_SPI(int8_t clkPin, int8_t misoPin 'irqPin' is not a HW interrupt pin false will be returned. */ /******************************************************************************/ -bool Adafruit_BluefruitLE_SPI::begin(boolean v, boolean blocking) -{ +bool Adafruit_BluefruitLE_SPI::begin(boolean v, boolean blocking) { _verbose = v; pinMode(m_irq_pin, INPUT); @@ -147,8 +144,7 @@ bool Adafruit_BluefruitLE_SPI::begin(boolean v, boolean blocking) isOK = sendInitializePattern(); // use hardware reset if available - if (m_rst_pin >= 0) - { + if (m_rst_pin >= 0) { // pull the RST to GND for 10 ms pinMode(m_rst_pin, OUTPUT); digitalWrite(m_rst_pin, HIGH); @@ -156,14 +152,13 @@ bool Adafruit_BluefruitLE_SPI::begin(boolean v, boolean blocking) delay(10); digitalWrite(m_rst_pin, HIGH); - isOK= true; + isOK = true; } _reset_started_timestamp = millis(); // Bluefruit takes 1 second to reboot - if (blocking) - { + if (blocking) { delay(1000); } @@ -175,8 +170,7 @@ bool Adafruit_BluefruitLE_SPI::begin(boolean v, boolean blocking) @brief Uninitializes the SPI interface */ /******************************************************************************/ -void Adafruit_BluefruitLE_SPI::end(void) -{ +void Adafruit_BluefruitLE_SPI::end(void) { if (m_sck_pin == -1) { SPI.end(); } @@ -188,8 +182,7 @@ void Adafruit_BluefruitLE_SPI::end(void) User should use setMode instead */ /******************************************************************************/ -void Adafruit_BluefruitLE_SPI::simulateSwitchMode(void) -{ +void Adafruit_BluefruitLE_SPI::simulateSwitchMode(void) { _mode = 1 - _mode; char ch = '0' + _mode; @@ -202,13 +195,14 @@ void Adafruit_BluefruitLE_SPI::simulateSwitchMode(void) @brief Simulate "+++" switch mode command */ /******************************************************************************/ -bool Adafruit_BluefruitLE_SPI::setMode(uint8_t new_mode) -{ +bool Adafruit_BluefruitLE_SPI::setMode(uint8_t new_mode) { // invalid mode - if ( !(new_mode == BLUEFRUIT_MODE_COMMAND || new_mode == BLUEFRUIT_MODE_DATA) ) return false; + if (!(new_mode == BLUEFRUIT_MODE_COMMAND || new_mode == BLUEFRUIT_MODE_DATA)) + return false; // Already in the wanted mode - if ( _mode == new_mode ) return true; + if (_mode == new_mode) + return true; // SPI use different SDEP command when in DATA/COMMAND mode. // --> does not switch using +++ command @@ -216,7 +210,8 @@ bool Adafruit_BluefruitLE_SPI::setMode(uint8_t new_mode) // If we're entering DATA mode, flush any old response, so that it isn't // interpreted as incoming UART data - if (_mode == BLUEFRUIT_MODE_DATA) flush(); + if (_mode == BLUEFRUIT_MODE_DATA) + flush(); return true; } @@ -227,20 +222,19 @@ bool Adafruit_BluefruitLE_SPI::setMode(uint8_t new_mode) Usage of setMode is not affected. */ /******************************************************************************/ -void Adafruit_BluefruitLE_SPI::enableModeSwitchCommand(bool enabled) -{ +void Adafruit_BluefruitLE_SPI::enableModeSwitchCommand(bool enabled) { m_mode_switch_command_enabled = enabled; } /******************************************************************************/ /*! - @brief Send initialize pattern to Bluefruit LE to force a reset. This pattern - follow the SDEP command syntax with command_id = SDEP_CMDTYPE_INITIALIZE. - The command has NO response, and is expected to complete within 1 second + @brief Send initialize pattern to Bluefruit LE to force a reset. This + pattern follow the SDEP command syntax with command_id = + SDEP_CMDTYPE_INITIALIZE. The command has NO response, and is expected to + complete within 1 second */ /******************************************************************************/ -bool Adafruit_BluefruitLE_SPI::sendInitializePattern(void) -{ +bool Adafruit_BluefruitLE_SPI::sendInitializePattern(void) { return sendPacket(SDEP_CMDTYPE_INITIALIZE, NULL, 0, 0); } @@ -252,23 +246,25 @@ bool Adafruit_BluefruitLE_SPI::sendInitializePattern(void) More Data bitfield, 0 indicates this is not end of transfer yet */ /******************************************************************************/ -bool Adafruit_BluefruitLE_SPI::sendPacket(uint16_t command, const uint8_t* buf, uint8_t count, uint8_t more_data) -{ +bool Adafruit_BluefruitLE_SPI::sendPacket(uint16_t command, const uint8_t *buf, + uint8_t count, uint8_t more_data) { // flush old response before sending the new command, but only if we're *not* // in DATA mode, as the RX FIFO may containg incoming UART data that hasn't // been read yet - if (more_data == 0 && _mode != BLUEFRUIT_MODE_DATA) flush(); + if (more_data == 0 && _mode != BLUEFRUIT_MODE_DATA) + flush(); sdepMsgCommand_t msgCmd; - msgCmd.header.msg_type = SDEP_MSGTYPE_COMMAND; + msgCmd.header.msg_type = SDEP_MSGTYPE_COMMAND; msgCmd.header.cmd_id_high = highByte(command); - msgCmd.header.cmd_id_low = lowByte(command); - msgCmd.header.length = count; - msgCmd.header.more_data = (count == SDEP_MAX_PACKETSIZE) ? more_data : 0; + msgCmd.header.cmd_id_low = lowByte(command); + msgCmd.header.length = count; + msgCmd.header.more_data = (count == SDEP_MAX_PACKETSIZE) ? more_data : 0; // Copy payload - if ( buf != NULL && count > 0) memcpy(msgCmd.payload, buf, count); + if (buf != NULL && count > 0) + memcpy(msgCmd.payload, buf, count); // Starting SPI transaction if (m_sck_pin == -1) @@ -279,8 +275,8 @@ bool Adafruit_BluefruitLE_SPI::sendPacket(uint16_t command, const uint8_t* buf, TimeoutTimer tt(_timeout); // Bluefruit may not be ready - while ( ( spixfer(msgCmd.header.msg_type) == SPI_IGNORED_BYTE ) && !tt.expired() ) - { + while ((spixfer(msgCmd.header.msg_type) == SPI_IGNORED_BYTE) && + !tt.expired()) { // Disable & Re-enable CS with a bit of delay for Bluefruit to ready itself SPI_CS_DISABLE(); delayMicroseconds(SPI_DEFAULT_DELAY_US); @@ -288,10 +284,10 @@ bool Adafruit_BluefruitLE_SPI::sendPacket(uint16_t command, const uint8_t* buf, } bool result = !tt.expired(); - if ( result ) - { + if (result) { // transfer the rest of the data - spixfer((void*) (((uint8_t*)&msgCmd) +1), sizeof(sdepMsgHeader_t)+count-1); + spixfer((void *)(((uint8_t *)&msgCmd) + 1), + sizeof(sdepMsgHeader_t) + count - 1); } SPI_CS_DISABLE(); @@ -311,10 +307,8 @@ bool Adafruit_BluefruitLE_SPI::sendPacket(uint16_t command, const uint8_t* buf, Character to send */ /******************************************************************************/ -size_t Adafruit_BluefruitLE_SPI::write(uint8_t c) -{ - if (_mode == BLUEFRUIT_MODE_DATA) - { +size_t Adafruit_BluefruitLE_SPI::write(uint8_t c) { + if (_mode == BLUEFRUIT_MODE_DATA) { sendPacket(SDEP_CMDTYPE_BLE_UARTTX, &c, 1, 0); getResponse(); return 1; @@ -323,36 +317,31 @@ size_t Adafruit_BluefruitLE_SPI::write(uint8_t c) // Following code handle BLUEFRUIT_MODE_COMMAND // Final packet due to \r or \n terminator - if (c == '\r' || c == '\n') - { - if (m_tx_count > 0) - { + if (c == '\r' || c == '\n') { + if (m_tx_count > 0) { // +++ command to switch mode - if (m_mode_switch_command_enabled && memcmp(m_tx_buffer, "+++", 3) == 0) - { + if (m_mode_switch_command_enabled && memcmp(m_tx_buffer, "+++", 3) == 0) { simulateSwitchMode(); - }else - { + } else { sendPacket(SDEP_CMDTYPE_AT_WRAPPER, m_tx_buffer, m_tx_count, 0); } m_tx_count = 0; } } // More than max packet buffered --> send with more_data = 1 - else if (m_tx_count == SDEP_MAX_PACKETSIZE) - { + else if (m_tx_count == SDEP_MAX_PACKETSIZE) { sendPacket(SDEP_CMDTYPE_AT_WRAPPER, m_tx_buffer, m_tx_count, 1); m_tx_buffer[0] = c; m_tx_count = 1; } // Not enough data, continue to buffer - else - { + else { m_tx_buffer[m_tx_count++] = c; } - if (_verbose) SerialDebug.print((char) c); + if (_verbose) + SerialDebug.print((char)c); return 1; } @@ -362,25 +351,19 @@ size_t Adafruit_BluefruitLE_SPI::write(uint8_t c) */ /******************************************************************************/ -size_t Adafruit_BluefruitLE_SPI::write(const uint8_t *buf, size_t size) -{ - if ( _mode == BLUEFRUIT_MODE_DATA ) - { - if (m_mode_switch_command_enabled && - (size >= 3) && +size_t Adafruit_BluefruitLE_SPI::write(const uint8_t *buf, size_t size) { + if (_mode == BLUEFRUIT_MODE_DATA) { + if (m_mode_switch_command_enabled && (size >= 3) && !memcmp(buf, "+++", 3) && - !(size > 3 && buf[3] != '\r' && buf[3] != '\n') ) - { + !(size > 3 && buf[3] != '\r' && buf[3] != '\n')) { simulateSwitchMode(); - }else - { + } else { size_t remain = size; - while(remain) - { + while (remain) { size_t len = min(remain, SDEP_MAX_PACKETSIZE); remain -= len; - sendPacket(SDEP_CMDTYPE_BLE_UARTTX, buf, (uint8_t) len, remain ? 1 : 0); + sendPacket(SDEP_CMDTYPE_BLE_UARTTX, buf, (uint8_t)len, remain ? 1 : 0); buf += len; } @@ -390,8 +373,7 @@ size_t Adafruit_BluefruitLE_SPI::write(const uint8_t *buf, size_t size) return size; } // Command mode - else - { + else { size_t n = 0; while (size--) { n += write(*buf++); @@ -407,14 +389,12 @@ size_t Adafruit_BluefruitLE_SPI::write(const uint8_t *buf, size_t size) @return 'true' if a response is ready, otherwise 'false' */ /******************************************************************************/ -int Adafruit_BluefruitLE_SPI::available(void) -{ - if (! m_rx_fifo.empty() ) { +int Adafruit_BluefruitLE_SPI::available(void) { + if (!m_rx_fifo.empty()) { return m_rx_fifo.count(); } - if ( _mode == BLUEFRUIT_MODE_DATA ) - { + if (_mode == BLUEFRUIT_MODE_DATA) { // DATA Mode: query for BLE UART data sendPacket(SDEP_CMDTYPE_BLE_UARTRX, NULL, 0, 0); @@ -422,8 +402,7 @@ int Adafruit_BluefruitLE_SPI::available(void) getResponse(); return m_rx_fifo.count(); - }else - { + } else { return (digitalRead(m_irq_pin)); } } @@ -435,8 +414,7 @@ int Adafruit_BluefruitLE_SPI::available(void) @return -1 if no data is available */ /******************************************************************************/ -int Adafruit_BluefruitLE_SPI::read(void) -{ +int Adafruit_BluefruitLE_SPI::read(void) { uint8_t ch; // try to grab from buffer first... @@ -445,21 +423,19 @@ int Adafruit_BluefruitLE_SPI::read(void) return (int)ch; } - if ( _mode == BLUEFRUIT_MODE_DATA ) - { + if (_mode == BLUEFRUIT_MODE_DATA) { // DATA Mode: query for BLE UART data sendPacket(SDEP_CMDTYPE_BLE_UARTRX, NULL, 0, 0); // Waiting to get response from Bluefruit getResponse(); - }else - { + } else { // COMMAND Mode: Only read data from Bluefruit if IRQ is raised - if ( digitalRead(m_irq_pin) ) getResponse(); + if (digitalRead(m_irq_pin)) + getResponse(); } - return m_rx_fifo.read(&ch) ? ((int) ch) : EOF; - + return m_rx_fifo.read(&ch) ? ((int)ch) : EOF; } /******************************************************************************/ @@ -470,26 +446,24 @@ int Adafruit_BluefruitLE_SPI::read(void) @return -1 if no data is available */ /******************************************************************************/ -int Adafruit_BluefruitLE_SPI::peek(void) -{ +int Adafruit_BluefruitLE_SPI::peek(void) { uint8_t ch; // try to grab from buffer first... - if ( m_rx_fifo.peek(&ch) ) { + if (m_rx_fifo.peek(&ch)) { return (int)ch; } - if ( _mode == BLUEFRUIT_MODE_DATA ) - { + if (_mode == BLUEFRUIT_MODE_DATA) { // DATA Mode: query for BLE UART data sendPacket(SDEP_CMDTYPE_BLE_UARTRX, NULL, 0, 0); // Waiting to get response from Bluefruit getResponse(); - }else - { + } else { // Read data from Bluefruit if possible - if ( digitalRead(m_irq_pin) ) getResponse(); + if (digitalRead(m_irq_pin)) + getResponse(); } return m_rx_fifo.peek(&ch) ? ch : EOF; @@ -502,15 +476,12 @@ int Adafruit_BluefruitLE_SPI::peek(void) @return -1 if no data is available */ /******************************************************************************/ -void Adafruit_BluefruitLE_SPI::flush(void) -{ - m_rx_fifo.clear(); -} +void Adafruit_BluefruitLE_SPI::flush(void) { m_rx_fifo.clear(); } /******************************************************************************/ /*! - @brief Try to perform an full AT response transfer from Bluefruit, or execute - as many SPI transaction as internal FIFO can hold up. + @brief Try to perform an full AT response transfer from Bluefruit, or + execute as many SPI transaction as internal FIFO can hold up. @note If verbose is enabled, all the received data will be print to Serial @@ -519,25 +490,24 @@ void Adafruit_BluefruitLE_SPI::flush(void) - false : if failed */ /******************************************************************************/ -bool Adafruit_BluefruitLE_SPI::getResponse(void) -{ +bool Adafruit_BluefruitLE_SPI::getResponse(void) { // Try to read data from Bluefruit if there is enough room in the fifo - while ( m_rx_fifo.remaining() >= SDEP_MAX_PACKETSIZE ) - { + while (m_rx_fifo.remaining() >= SDEP_MAX_PACKETSIZE) { // Get a SDEP packet sdepMsgResponse_t msg_response; memclr(&msg_response, sizeof(sdepMsgResponse_t)); - if ( !getPacket(&msg_response) ) return false; + if (!getPacket(&msg_response)) + return false; // Write to fifo - if ( msg_response.header.length > 0) - { + if (msg_response.header.length > 0) { m_rx_fifo.write_n(msg_response.payload, msg_response.header.length); } // No more packet data - if ( !msg_response.header.more_data ) break; + if (!msg_response.header.more_data) + break; // It takes a bit since all Data received to IRQ to get LOW // May need to delay a bit for it to be stable before the next try @@ -549,8 +519,8 @@ bool Adafruit_BluefruitLE_SPI::getResponse(void) /******************************************************************************/ /*! - @brief Perform a single SPI SDEP transaction and is used by getReponse to - get a full response composed of multiple packets. + @brief Perform a single SPI SDEP transaction and is used by getReponse + to get a full response composed of multiple packets. @param[in] buf Memory location where payload is copied to @@ -558,16 +528,17 @@ bool Adafruit_BluefruitLE_SPI::getResponse(void) @return number of bytes in SDEP payload */ /******************************************************************************/ -bool Adafruit_BluefruitLE_SPI::getPacket(sdepMsgResponse_t* p_response) -{ - // Wait until IRQ is asserted, double timeout since some commands take long time to start responding - TimeoutTimer tt(2*_timeout); - - while ( !digitalRead(m_irq_pin) ) { - if (tt.expired()) return false; +bool Adafruit_BluefruitLE_SPI::getPacket(sdepMsgResponse_t *p_response) { + // Wait until IRQ is asserted, double timeout since some commands take long + // time to start responding + TimeoutTimer tt(2 * _timeout); + + while (!digitalRead(m_irq_pin)) { + if (tt.expired()) + return false; } - - sdepMsgHeader_t* p_header = &p_response->header; + + sdepMsgHeader_t *p_header = &p_response->header; if (m_sck_pin == -1) SPI.beginTransaction(bluefruitSPI); @@ -576,78 +547,83 @@ bool Adafruit_BluefruitLE_SPI::getPacket(sdepMsgResponse_t* p_response) tt.set(_timeout); do { - if ( tt.expired() ) break; + if (tt.expired()) + break; p_header->msg_type = spixfer(0xff); - if (p_header->msg_type == SPI_IGNORED_BYTE) - { + if (p_header->msg_type == SPI_IGNORED_BYTE) { // Bluefruit may not be ready - // Disable & Re-enable CS with a bit of delay for Bluefruit to ready itself + // Disable & Re-enable CS with a bit of delay for Bluefruit to ready + // itself SPI_CS_DISABLE(); delayMicroseconds(SPI_DEFAULT_DELAY_US); SPI_CS_ENABLE(); - } - else if (p_header->msg_type == SPI_OVERREAD_BYTE) - { - // IRQ may not be pulled down by Bluefruit when returning all data in previous transfer. - // This could happen when Arduino MCU is running at fast rate comparing to Bluefruit's MCU, - // causing an SPI_OVERREAD_BYTE to be returned at stage. + } else if (p_header->msg_type == SPI_OVERREAD_BYTE) { + // IRQ may not be pulled down by Bluefruit when returning all data in + // previous transfer. This could happen when Arduino MCU is running at + // fast rate comparing to Bluefruit's MCU, causing an SPI_OVERREAD_BYTE to + // be returned at stage. // // Walkaround: Disable & Re-enable CS with a bit of delay and keep waiting - // TODO IRQ is supposed to be OFF then ON, it is better to use GPIO trigger interrupt. + // TODO IRQ is supposed to be OFF then ON, it is better to use GPIO + // trigger interrupt. SPI_CS_DISABLE(); // wait for the clock to be enabled.. -// while (!digitalRead(m_irq_pin)) { -// if ( tt.expired() ) break; -// } -// if (!digitalRead(m_irq_pin)) break; + // while (!digitalRead(m_irq_pin)) { + // if ( tt.expired() ) break; + // } + // if (!digitalRead(m_irq_pin)) break; delayMicroseconds(SPI_DEFAULT_DELAY_US); SPI_CS_ENABLE(); } - } while (p_header->msg_type == SPI_IGNORED_BYTE || p_header->msg_type == SPI_OVERREAD_BYTE); + } while (p_header->msg_type == SPI_IGNORED_BYTE || + p_header->msg_type == SPI_OVERREAD_BYTE); - bool result=false; + bool result = false; // Not a loop, just a way to avoid goto with error handling - do - { + do { // Look for the header - // note that we should always get the right header at this point, and not doing so will really mess up things. - while ( p_header->msg_type != SDEP_MSGTYPE_RESPONSE && p_header->msg_type != SDEP_MSGTYPE_ERROR && !tt.expired() ) - { + // note that we should always get the right header at this point, and not + // doing so will really mess up things. + while (p_header->msg_type != SDEP_MSGTYPE_RESPONSE && + p_header->msg_type != SDEP_MSGTYPE_ERROR && !tt.expired()) { p_header->msg_type = spixfer(0xff); } - - if ( tt.expired() ) break; - - memset( (&p_header->msg_type)+1, 0xff, sizeof(sdepMsgHeader_t) - 1); - spixfer((&p_header->msg_type)+1, sizeof(sdepMsgHeader_t) - 1); - // Command is 16-bit at odd address, may have alignment issue with 32-bit chip + if (tt.expired()) + break; + + memset((&p_header->msg_type) + 1, 0xff, sizeof(sdepMsgHeader_t) - 1); + spixfer((&p_header->msg_type) + 1, sizeof(sdepMsgHeader_t) - 1); + + // Command is 16-bit at odd address, may have alignment issue with 32-bit + // chip uint16_t cmd_id = word(p_header->cmd_id_high, p_header->cmd_id_low); // Error Message Response - if ( p_header->msg_type == SDEP_MSGTYPE_ERROR ) break; + if (p_header->msg_type == SDEP_MSGTYPE_ERROR) + break; // Invalid command if (!(cmd_id == SDEP_CMDTYPE_AT_WRAPPER || cmd_id == SDEP_CMDTYPE_BLE_UARTTX || - cmd_id == SDEP_CMDTYPE_BLE_UARTRX) ) - { + cmd_id == SDEP_CMDTYPE_BLE_UARTRX)) { break; } // Invalid length - if(p_header->length > SDEP_MAX_PACKETSIZE) break; + if (p_header->length > SDEP_MAX_PACKETSIZE) + break; // read payload memset(p_response->payload, 0xff, p_header->length); spixfer(p_response->payload, p_header->length); result = true; - }while(0); + } while (0); SPI_CS_DISABLE(); if (m_sck_pin == -1) @@ -678,21 +654,21 @@ void Adafruit_BluefruitLE_SPI::spixfer(void *buff, size_t len) { uint8_t Adafruit_BluefruitLE_SPI::spixfer(uint8_t x) { if (m_sck_pin == -1) { uint8_t reply = SPI.transfer(x); - //SerialDebug.println(reply, HEX); + // SerialDebug.println(reply, HEX); return reply; } // software spi uint8_t reply = 0; - for (int i=7; i>=0; i--) { + for (int i = 7; i >= 0; i--) { reply <<= 1; digitalWrite(m_sck_pin, LOW); - digitalWrite(m_mosi_pin, x & (1< #include -#include "utility/Adafruit_FIFO.h" - -#define SPI_CS_ENABLE() digitalWrite(m_cs_pin, LOW) -#define SPI_CS_DISABLE() digitalWrite(m_cs_pin, HIGH) - -#define SPI_IGNORED_BYTE 0xFEu /**< SPI default character. Character clocked out in case of an ignored transaction. */ -#define SPI_OVERREAD_BYTE 0xFFu /**< SPI over-read character. Character clocked out after an over-read of the transmit buffer. */ -#define SPI_DEFAULT_DELAY_US 50 - -#define memclr(buffer, size) memset(buffer, 0, size) - - -class Adafruit_BluefruitLE_SPI : public Adafruit_BLE -{ - private: - // Hardware Pin - int8_t m_cs_pin; - int8_t m_irq_pin; - int8_t m_rst_pin; - - // software SPI pins - int8_t m_sck_pin; - int8_t m_mosi_pin; - int8_t m_miso_pin; - - // TX - uint8_t m_tx_buffer[SDEP_MAX_PACKETSIZE]; - uint8_t m_tx_count; - - // RX - uint8_t m_rx_buffer[BLE_BUFSIZE]; - Adafruit_FIFO m_rx_fifo; - - bool m_mode_switch_command_enabled; - - // Low level transportation I/O functions - bool sendInitializePattern(void); - bool sendPacket(uint16_t command, const uint8_t* buffer, uint8_t count, uint8_t more_data); - bool getPacket(sdepMsgResponse_t* p_response); - - bool getResponse(void); - void simulateSwitchMode(void); -// bool handleSwitchCmdInDataMode(uint8_t ch); - - uint8_t spixfer(uint8_t x); - void spixfer(void *x, size_t len); - - public: - // Constructor - Adafruit_BluefruitLE_SPI(int8_t csPin, int8_t irqPin, int8_t rstPin = -1); - Adafruit_BluefruitLE_SPI(int8_t clkPin, int8_t misoPin, int8_t mosiPin, int8_t csPin, int8_t irqPin, int8_t rstPin); - - // HW initialisation - bool begin(boolean v = false, boolean blocking = true); - void end(void); - - bool setMode(uint8_t new_mode); - void enableModeSwitchCommand(bool enabled); - - // Class Print virtual function Interface - virtual size_t write(uint8_t c); - virtual size_t write(const uint8_t *buffer, size_t size); - - // pull in write(str) and write(buf, size) from Print - using Print::write; - // Class Stream interface - virtual int available(void); - virtual int read(void); - virtual void flush(void); - virtual int peek(void); +#define SPI_CS_ENABLE() digitalWrite(m_cs_pin, LOW) +#define SPI_CS_DISABLE() digitalWrite(m_cs_pin, HIGH) + +#define SPI_IGNORED_BYTE \ + 0xFEu /**< SPI default character. Character clocked out in case of an \ + ignored transaction. */ +#define SPI_OVERREAD_BYTE \ + 0xFFu /**< SPI over-read character. Character clocked out after an over-read \ + of the transmit buffer. */ +#define SPI_DEFAULT_DELAY_US 50 + +#define memclr(buffer, size) memset(buffer, 0, size) + +class Adafruit_BluefruitLE_SPI : public Adafruit_BLE { +private: + // Hardware Pin + int8_t m_cs_pin; + int8_t m_irq_pin; + int8_t m_rst_pin; + + // software SPI pins + int8_t m_sck_pin; + int8_t m_mosi_pin; + int8_t m_miso_pin; + + // TX + uint8_t m_tx_buffer[SDEP_MAX_PACKETSIZE]; + uint8_t m_tx_count; + + // RX + uint8_t m_rx_buffer[BLE_BUFSIZE]; + Adafruit_FIFO m_rx_fifo; + + bool m_mode_switch_command_enabled; + + // Low level transportation I/O functions + bool sendInitializePattern(void); + bool sendPacket(uint16_t command, const uint8_t *buffer, uint8_t count, + uint8_t more_data); + bool getPacket(sdepMsgResponse_t *p_response); + + bool getResponse(void); + void simulateSwitchMode(void); + // bool handleSwitchCmdInDataMode(uint8_t ch); + + uint8_t spixfer(uint8_t x); + void spixfer(void *x, size_t len); + +public: + // Constructor + Adafruit_BluefruitLE_SPI(int8_t csPin, int8_t irqPin, int8_t rstPin = -1); + Adafruit_BluefruitLE_SPI(int8_t clkPin, int8_t misoPin, int8_t mosiPin, + int8_t csPin, int8_t irqPin, int8_t rstPin); + + // HW initialisation + bool begin(boolean v = false, boolean blocking = true); + void end(void); + + bool setMode(uint8_t new_mode); + void enableModeSwitchCommand(bool enabled); + + // Class Print virtual function Interface + virtual size_t write(uint8_t c); + virtual size_t write(const uint8_t *buffer, size_t size); + + // pull in write(str) and write(buf, size) from Print + using Print::write; + + // Class Stream interface + virtual int available(void); + virtual int read(void); + virtual void flush(void); + virtual int peek(void); }; #endif diff --git a/Adafruit_BluefruitLE_UART.cpp b/Adafruit_BluefruitLE_UART.cpp index 53e82c5..6c99c5a 100644 --- a/Adafruit_BluefruitLE_UART.cpp +++ b/Adafruit_BluefruitLE_UART.cpp @@ -29,8 +29,8 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include "Adafruit_BluefruitLE_UART.h" @@ -40,9 +40,11 @@ @brief Instantiates a new instance of the Adafruit_BluefruitLE_UART class */ /******************************************************************************/ -Adafruit_BluefruitLE_UART::Adafruit_BluefruitLE_UART(HardwareSerial &port, int8_t mode_pin, int8_t cts_pin, int8_t rts_pin) : - _mode_pin(mode_pin), _cts_pin(cts_pin), _rts_pin(rts_pin) -{ +Adafruit_BluefruitLE_UART::Adafruit_BluefruitLE_UART(HardwareSerial &port, + int8_t mode_pin, + int8_t cts_pin, + int8_t rts_pin) + : _mode_pin(mode_pin), _cts_pin(cts_pin), _rts_pin(rts_pin) { _physical_transport = BLUEFRUIT_TRANSPORT_HWUART; #if SOFTWARE_SERIAL_AVAILABLE @@ -60,9 +62,11 @@ Adafruit_BluefruitLE_UART::Adafruit_BluefruitLE_UART(HardwareSerial &port, int8_ using software serial */ /******************************************************************************/ -Adafruit_BluefruitLE_UART::Adafruit_BluefruitLE_UART(SoftwareSerial &port, int8_t mode_pin, int8_t cts_pin, int8_t rts_pin) : - _mode_pin(mode_pin), _cts_pin(cts_pin), _rts_pin(rts_pin) -{ +Adafruit_BluefruitLE_UART::Adafruit_BluefruitLE_UART(SoftwareSerial &port, + int8_t mode_pin, + int8_t cts_pin, + int8_t rts_pin) + : _mode_pin(mode_pin), _cts_pin(cts_pin), _rts_pin(rts_pin) { _physical_transport = BLUEFRUIT_TRANSPORT_SWUART; hs = 0; @@ -71,16 +75,12 @@ Adafruit_BluefruitLE_UART::Adafruit_BluefruitLE_UART(SoftwareSerial &port, int8_ } #endif - /******************************************************************************/ /*! @brief Class's Destructor */ /******************************************************************************/ -Adafruit_BluefruitLE_UART::~Adafruit_BluefruitLE_UART() -{ - end(); -} +Adafruit_BluefruitLE_UART::~Adafruit_BluefruitLE_UART() { end(); } /******************************************************************************/ /*! @@ -91,14 +91,12 @@ Adafruit_BluefruitLE_UART::~Adafruit_BluefruitLE_UART() 'irqPin' is not a HW interrupt pin false will be returned. */ /******************************************************************************/ -bool Adafruit_BluefruitLE_UART::begin(boolean debug, boolean blocking) -{ +bool Adafruit_BluefruitLE_UART::begin(boolean debug, boolean blocking) { _verbose = debug; _intercharwritedelay = 0; // If hardware mode pin is enabled, set it to CMD first - if ( _mode_pin >= 0) - { + if (_mode_pin >= 0) { pinMode(_mode_pin, OUTPUT); digitalWrite(_mode_pin, BLUEFRUIT_MODE_COMMAND); @@ -110,9 +108,9 @@ bool Adafruit_BluefruitLE_UART::begin(boolean debug, boolean blocking) if (hs) { hs->begin(9600); - #ifdef ARDUINO_STM32_FEATHER +#ifdef ARDUINO_STM32_FEATHER hs->enableFlowControl(); - #endif +#endif } else { #if SOFTWARE_SERIAL_AVAILABLE ss->begin(9600); @@ -121,9 +119,9 @@ bool Adafruit_BluefruitLE_UART::begin(boolean debug, boolean blocking) if (_cts_pin > 0) { pinMode(_cts_pin, OUTPUT); - digitalWrite(_cts_pin, HIGH); // turn off txo + digitalWrite(_cts_pin, HIGH); // turn off txo } - + if (_rts_pin > 0) { pinMode(_rts_pin, INPUT); } @@ -138,8 +136,7 @@ bool Adafruit_BluefruitLE_UART::begin(boolean debug, boolean blocking) @brief Uninitializes the SPI interface */ /******************************************************************************/ -void Adafruit_BluefruitLE_UART::end(void) -{ +void Adafruit_BluefruitLE_UART::end(void) { if (hs) { hs->end(); } else { @@ -161,35 +158,32 @@ void Adafruit_BluefruitLE_UART::end(void) @return true if the mode switch was successful, otherwise false */ /******************************************************************************/ -bool Adafruit_BluefruitLE_UART::setMode(uint8_t new_mode) -{ +bool Adafruit_BluefruitLE_UART::setMode(uint8_t new_mode) { // invalid mode - if ( !(new_mode == BLUEFRUIT_MODE_COMMAND || new_mode == BLUEFRUIT_MODE_DATA) ) return false; + if (!(new_mode == BLUEFRUIT_MODE_COMMAND || new_mode == BLUEFRUIT_MODE_DATA)) + return false; bool isOK; - if ( _mode_pin >= 0 ) - { + if (_mode_pin >= 0) { // Switch mode using hardware pin digitalWrite(_mode_pin, new_mode); delay(1); isOK = true; - } else - { + } else { // Switch mode using +++ command, at worst switch 2 times int32_t updated_mode; isOK = atcommandIntReply(F("+++"), &updated_mode); - if ( isOK ) - { + if (isOK) { // Ahhh, we are already in the wanted mode before sending +++ // Switch again. This is required to make sure it is always correct - if ( updated_mode != new_mode ) - { + if (updated_mode != new_mode) { isOK = atcommandIntReply(F("+++"), &updated_mode); // Still does not match -> give up - if ( updated_mode != new_mode ) return false; + if (updated_mode != new_mode) + return false; } } } @@ -209,12 +203,12 @@ bool Adafruit_BluefruitLE_UART::setMode(uint8_t new_mode) Character to send */ /******************************************************************************/ -size_t Adafruit_BluefruitLE_UART::write(uint8_t c) -{ +size_t Adafruit_BluefruitLE_UART::write(uint8_t c) { // flush left-over before a new command -// if (c == '\r') flush(); + // if (c == '\r') flush(); - if (_verbose) SerialDebug.print((char) c); + if (_verbose) + SerialDebug.print((char)c); if (_rts_pin >= 0) { while (digitalRead(_rts_pin)) { @@ -235,9 +229,8 @@ size_t Adafruit_BluefruitLE_UART::write(uint8_t c) @return 'true' if a response is ready, otherwise 'false' */ /******************************************************************************/ -int Adafruit_BluefruitLE_UART::available(void) -{ - if (! mySerial->available() & (_cts_pin > 0)) { +int Adafruit_BluefruitLE_UART::available(void) { + if (!mySerial->available() & (_cts_pin > 0)) { // toggle flow control to get more byteses digitalWrite(_cts_pin, LOW); delay(1); @@ -253,8 +246,7 @@ int Adafruit_BluefruitLE_UART::available(void) @return -1 if no data is available */ /******************************************************************************/ -int Adafruit_BluefruitLE_UART::read(void) -{ +int Adafruit_BluefruitLE_UART::read(void) { int c = mySerial->read(); return c; } @@ -267,10 +259,7 @@ int Adafruit_BluefruitLE_UART::read(void) @return -1 if no data is available */ /******************************************************************************/ -int Adafruit_BluefruitLE_UART::peek(void) -{ - return mySerial->peek(); -} +int Adafruit_BluefruitLE_UART::peek(void) { return mySerial->peek(); } /******************************************************************************/ /*! @@ -279,7 +268,4 @@ int Adafruit_BluefruitLE_UART::peek(void) @return -1 if no data is available */ /******************************************************************************/ -void Adafruit_BluefruitLE_UART::flush(void) -{ - mySerial->flush(); -} +void Adafruit_BluefruitLE_UART::flush(void) { mySerial->flush(); } diff --git a/Adafruit_BluefruitLE_UART.h b/Adafruit_BluefruitLE_UART.h index 80e0146..dbad9f9 100644 --- a/Adafruit_BluefruitLE_UART.h +++ b/Adafruit_BluefruitLE_UART.h @@ -29,8 +29,8 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ @@ -40,59 +40,56 @@ #include "Arduino.h" #include -#define SOFTWARE_SERIAL_AVAILABLE ( ! (defined (_VARIANT_ARDUINO_DUE_X_) || defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_STM32_FEATHER)) ) +#define SOFTWARE_SERIAL_AVAILABLE \ + (!(defined(_VARIANT_ARDUINO_DUE_X_) || defined(ARDUINO_ARCH_SAMD) || \ + defined(ARDUINO_STM32_FEATHER))) #if SOFTWARE_SERIAL_AVAILABLE - #include +#include #endif -class Adafruit_BluefruitLE_UART : public Adafruit_BLE -{ - private: - // Hardware Pins - int8_t _mode_pin, _cts_pin, _rts_pin; - Stream *mySerial; +class Adafruit_BluefruitLE_UART : public Adafruit_BLE { +private: + // Hardware Pins + int8_t _mode_pin, _cts_pin, _rts_pin; + Stream *mySerial; #if SOFTWARE_SERIAL_AVAILABLE - SoftwareSerial *ss; + SoftwareSerial *ss; #endif - HardwareSerial *hs; - boolean _debug; - uint8_t _intercharwritedelay; - - public: - // Software Serial Constructor (0, 1, 2, or 3 pins) - Adafruit_BluefruitLE_UART(HardwareSerial &port, - int8_t mode_pin = -1, - int8_t cts_pin = -1, - int8_t rts_pin = -1); + HardwareSerial *hs; + boolean _debug; + uint8_t _intercharwritedelay; + +public: + // Software Serial Constructor (0, 1, 2, or 3 pins) + Adafruit_BluefruitLE_UART(HardwareSerial &port, int8_t mode_pin = -1, + int8_t cts_pin = -1, int8_t rts_pin = -1); #if SOFTWARE_SERIAL_AVAILABLE - Adafruit_BluefruitLE_UART(SoftwareSerial &port, - int8_t mode_pin = -1, - int8_t cts_pin = -1, - int8_t rts_pin = -1); + Adafruit_BluefruitLE_UART(SoftwareSerial &port, int8_t mode_pin = -1, + int8_t cts_pin = -1, int8_t rts_pin = -1); #endif - void setInterCharWriteDelay(uint8_t x) { _intercharwritedelay = x; }; + void setInterCharWriteDelay(uint8_t x) { _intercharwritedelay = x; }; - virtual ~Adafruit_BluefruitLE_UART(); + virtual ~Adafruit_BluefruitLE_UART(); - // HW initialisation - bool begin(boolean debug = false, boolean blocking = true); - void end(void); + // HW initialisation + bool begin(boolean debug = false, boolean blocking = true); + void end(void); - bool setMode(uint8_t new_mode); + bool setMode(uint8_t new_mode); - // Class Print virtual function Interface - virtual size_t write(uint8_t c); + // Class Print virtual function Interface + virtual size_t write(uint8_t c); - // pull in write(str) and write(buf, size) from Print - using Print::write; + // pull in write(str) and write(buf, size) from Print + using Print::write; - // Class Stream interface - virtual int available(void); - virtual int read(void); - virtual void flush(void); - virtual int peek(void); + // Class Stream interface + virtual int available(void); + virtual int read(void); + virtual void flush(void); + virtual int peek(void); }; #endif diff --git a/README.md b/README.md index 0d99e3d..473a707 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# Adafruit BluefruitLE nRF51 Library [![Build Status](https://github.com/adafruit/Adafruit_BluefruitLE_nRF51/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_BluefruitLE_nRF51/actions) + This library is for all nRF51 based Adafruit Bluefruit LE modules that use SPI or UART. Current nRF51 based Bluefruit LE products include: diff --git a/examples/atcommand/BluefruitConfig.h b/examples/atcommand/BluefruitConfig.h index f52b19e..2fe13ab 100644 --- a/examples/atcommand/BluefruitConfig.h +++ b/examples/atcommand/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 160 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 160 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/battery/BluefruitConfig.h b/examples/battery/BluefruitConfig.h index f69e6a4..327990f 100644 --- a/examples/battery/BluefruitConfig.h +++ b/examples/battery/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/beacon/BluefruitConfig.h b/examples/beacon/BluefruitConfig.h index f69e6a4..327990f 100644 --- a/examples/beacon/BluefruitConfig.h +++ b/examples/beacon/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/bleuart_cmdmode/BluefruitConfig.h b/examples/bleuart_cmdmode/BluefruitConfig.h index f69e6a4..327990f 100644 --- a/examples/bleuart_cmdmode/BluefruitConfig.h +++ b/examples/bleuart_cmdmode/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/bleuart_datamode/BluefruitConfig.h b/examples/bleuart_datamode/BluefruitConfig.h index f69e6a4..327990f 100644 --- a/examples/bleuart_datamode/BluefruitConfig.h +++ b/examples/bleuart_datamode/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/callbacks/BluefruitConfig.h b/examples/callbacks/BluefruitConfig.h index a7d99be..55e3691 100644 --- a/examples/callbacks/BluefruitConfig.h +++ b/examples/callbacks/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/callbacks_dfuirq/BluefruitConfig.h b/examples/callbacks_dfuirq/BluefruitConfig.h index a7d99be..55e3691 100644 --- a/examples/callbacks_dfuirq/BluefruitConfig.h +++ b/examples/callbacks_dfuirq/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/controller/.cpc.test.only b/examples/controller/.cpc.test.only new file mode 100644 index 0000000..e69de29 diff --git a/examples/controller/BluefruitConfig.h b/examples/controller/BluefruitConfig.h index 86260e3..e1092d7 100644 --- a/examples/controller/BluefruitConfig.h +++ b/examples/controller/BluefruitConfig.h @@ -2,38 +2,35 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output -#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output +#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -42,9 +39,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -52,6 +49,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/controller/packetParser.cpp b/examples/controller/packetParser.cpp index 29942be..e371cd0 100644 --- a/examples/controller/packetParser.cpp +++ b/examples/controller/packetParser.cpp @@ -1,68 +1,60 @@ -#include #include #include -#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) && not defined(__SAMD51__) - #include +#include +#if not defined(_VARIANT_ARDUINO_DUE_X_) && \ + not defined(_VARIANT_ARDUINO_ZERO_) && not defined(__SAMD51__) +#include #endif #include "Adafruit_BLE.h" #include "Adafruit_BluefruitLE_SPI.h" #include "Adafruit_BluefruitLE_UART.h" - -#define PACKET_ACC_LEN (15) -#define PACKET_GYRO_LEN (15) -#define PACKET_MAG_LEN (15) -#define PACKET_QUAT_LEN (19) -#define PACKET_BUTTON_LEN (5) -#define PACKET_COLOR_LEN (6) -#define PACKET_LOCATION_LEN (15) +#define PACKET_ACC_LEN (15) +#define PACKET_GYRO_LEN (15) +#define PACKET_MAG_LEN (15) +#define PACKET_QUAT_LEN (19) +#define PACKET_BUTTON_LEN (5) +#define PACKET_COLOR_LEN (6) +#define PACKET_LOCATION_LEN (15) // READ_BUFSIZE Size of the read buffer for incoming packets -#define READ_BUFSIZE (20) - +#define READ_BUFSIZE (20) /* Buffer to hold incoming characters */ -uint8_t packetbuffer[READ_BUFSIZE+1]; +uint8_t packetbuffer[READ_BUFSIZE + 1]; /**************************************************************************/ /*! @brief Casts the four bytes at the specified address to a float */ /**************************************************************************/ -float parsefloat(uint8_t *buffer) -{ +float parsefloat(uint8_t *buffer) { float f; memcpy(&f, buffer, 4); return f; } /**************************************************************************/ -/*! +/*! @brief Prints a hexadecimal value in plain characters @param data Pointer to the byte data @param numBytes Data length in bytes */ /**************************************************************************/ -void printHex(const uint8_t * data, const uint32_t numBytes) -{ +void printHex(const uint8_t *data, const uint32_t numBytes) { uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++) - { + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values - if (data[szPos] <= 0xF) - { + if (data[szPos] <= 0xF) { Serial.print(F("0")); Serial.print(data[szPos] & 0xf, HEX); - } - else - { + } else { Serial.print(data[szPos] & 0xff, HEX); } // Add a trailing space if appropriate - if ((numBytes > 1) && (szPos != numBytes - 1)) - { + if ((numBytes > 1) && (szPos != numBytes - 1)) { Serial.print(F(" ")); } } @@ -74,14 +66,14 @@ void printHex(const uint8_t * data, const uint32_t numBytes) @brief Waits for incoming data and parses it */ /**************************************************************************/ -uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) -{ +uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) { uint16_t origtimeout = timeout, replyidx = 0; memset(packetbuffer, 0, READ_BUFSIZE); while (timeout--) { - if (replyidx >= 20) break; + if (replyidx >= 20) + break; if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) break; if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) @@ -98,7 +90,7 @@ uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) break; while (ble->available()) { - char c = ble->read(); + char c = ble->read(); if (c == '!') { replyidx = 0; } @@ -106,36 +98,35 @@ uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) replyidx++; timeout = origtimeout; } - - if (timeout == 0) break; + + if (timeout == 0) + break; delay(1); } - packetbuffer[replyidx] = 0; // null term + packetbuffer[replyidx] = 0; // null term - if (!replyidx) // no data or timeout + if (!replyidx) // no data or timeout return 0; - if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning + if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning return 0; - + // check checksum! uint8_t xsum = 0; - uint8_t checksum = packetbuffer[replyidx-1]; - - for (uint8_t i=0; i #include #include -#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) - #include +#include +#if not defined(_VARIANT_ARDUINO_DUE_X_) && not defined(_VARIANT_ARDUINO_ZERO_) +#include #endif #include "Adafruit_BLE.h" #include "Adafruit_BluefruitLE_SPI.h" #include "Adafruit_BluefruitLE_UART.h" - -#define PACKET_ACC_LEN (15) -#define PACKET_GYRO_LEN (15) -#define PACKET_MAG_LEN (15) -#define PACKET_QUAT_LEN (19) -#define PACKET_BUTTON_LEN (5) -#define PACKET_COLOR_LEN (6) -#define PACKET_LOCATION_LEN (15) +#define PACKET_ACC_LEN (15) +#define PACKET_GYRO_LEN (15) +#define PACKET_MAG_LEN (15) +#define PACKET_QUAT_LEN (19) +#define PACKET_BUTTON_LEN (5) +#define PACKET_COLOR_LEN (6) +#define PACKET_LOCATION_LEN (15) // READ_BUFSIZE Size of the read buffer for incoming packets -#define READ_BUFSIZE (20) - +#define READ_BUFSIZE (20) /* Buffer to hold incoming characters */ -uint8_t packetbuffer[READ_BUFSIZE+1]; +uint8_t packetbuffer[READ_BUFSIZE + 1]; /**************************************************************************/ /*! @brief Casts the four bytes at the specified address to a float */ /**************************************************************************/ -float parsefloat(uint8_t *buffer) -{ +float parsefloat(uint8_t *buffer) { float f = ((float *)buffer)[0]; return f; } /**************************************************************************/ -/*! +/*! @brief Prints a hexadecimal value in plain characters @param data Pointer to the byte data @param numBytes Data length in bytes */ /**************************************************************************/ -void printHex(const uint8_t * data, const uint32_t numBytes) -{ +void printHex(const uint8_t *data, const uint32_t numBytes) { uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++) - { + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values - if (data[szPos] <= 0xF) - { + if (data[szPos] <= 0xF) { Serial.print(F("0")); Serial.print(data[szPos] & 0xf, HEX); - } - else - { + } else { Serial.print(data[szPos] & 0xff, HEX); } // Add a trailing space if appropriate - if ((numBytes > 1) && (szPos != numBytes - 1)) - { + if ((numBytes > 1) && (szPos != numBytes - 1)) { Serial.print(F(" ")); } } @@ -73,14 +64,14 @@ void printHex(const uint8_t * data, const uint32_t numBytes) @brief Waits for incoming data and parses it */ /**************************************************************************/ -uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) -{ +uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) { uint16_t origtimeout = timeout, replyidx = 0; memset(packetbuffer, 0, READ_BUFSIZE); while (timeout--) { - if (replyidx >= 20) break; + if (replyidx >= 20) + break; if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) break; if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) @@ -97,7 +88,7 @@ uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) break; while (ble->available()) { - char c = ble->read(); + char c = ble->read(); if (c == '!') { replyidx = 0; } @@ -105,36 +96,35 @@ uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) replyidx++; timeout = origtimeout; } - - if (timeout == 0) break; + + if (timeout == 0) + break; delay(1); } - packetbuffer[replyidx] = 0; // null term + packetbuffer[replyidx] = 0; // null term - if (!replyidx) // no data or timeout + if (!replyidx) // no data or timeout return 0; - if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning + if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning return 0; - + // check checksum! uint8_t xsum = 0; - uint8_t checksum = packetbuffer[replyidx-1]; - - for (uint8_t i=0; i #include "IEEE11073float.h" +#include -uint32_t float2IEEE11073(double data, uint8_t output[4]) -{ +uint32_t float2IEEE11073(double data, uint8_t output[4]) { uint32_t result = MDER_NaN; - if (isnan(data)) { goto finally; - }/* else if (data > MDER_FLOAT_MAX) { - result = MDER_POSITIVE_INFINITY; - goto finally; - } else if (data < MDER_FLOAT_MIN) { - result = MDER_NEGATIVE_INFINITY; - goto finally; - } else if (data >= -MDER_FLOAT_EPSILON && - data <= MDER_FLOAT_EPSILON) { - result = 0; - goto finally; - }*/ + } /* else if (data > MDER_FLOAT_MAX) { + result = MDER_POSITIVE_INFINITY; + goto finally; + } else if (data < MDER_FLOAT_MIN) { + result = MDER_NEGATIVE_INFINITY; + goto finally; + } else if (data >= -MDER_FLOAT_EPSILON && + data <= MDER_FLOAT_EPSILON) { + result = 0; + goto finally; + }*/ - double sgn; sgn = data > 0 ? +1 : -1; - double mantissa; mantissa = fabs(data); - int32_t exponent; exponent = 0; // Note: 10**x exponent, not 2**x + double sgn; + sgn = data > 0 ? +1 : -1; + double mantissa; + mantissa = fabs(data); + int32_t exponent; + exponent = 0; // Note: 10**x exponent, not 2**x // scale up if number is too big while (mantissa > MDER_FLOAT_MANTISSA_MAX) { @@ -85,11 +86,14 @@ uint32_t float2IEEE11073(double data, uint8_t output[4]) } // scale down if number needs more precision - double smantissa; smantissa = round(mantissa * MDER_FLOAT_PRECISION); - double rmantissa; rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; - double mdiff; mdiff = abs(smantissa - rmantissa); + double smantissa; + smantissa = round(mantissa * MDER_FLOAT_PRECISION); + double rmantissa; + rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; + double mdiff; + mdiff = abs(smantissa - rmantissa); while (mdiff > 0.5 && exponent > MDER_FLOAT_EXPONENT_MIN && - (mantissa * 10) <= MDER_FLOAT_MANTISSA_MAX) { + (mantissa * 10) <= MDER_FLOAT_MANTISSA_MAX) { mantissa *= 10; --exponent; smantissa = round(mantissa * MDER_FLOAT_PRECISION); @@ -97,10 +101,12 @@ uint32_t float2IEEE11073(double data, uint8_t output[4]) mdiff = abs(smantissa - rmantissa); } - uint32_t int_mantissa; int_mantissa = (int) round(sgn * mantissa); + uint32_t int_mantissa; + int_mantissa = (int)round(sgn * mantissa); result = (exponent << 24) | (int_mantissa & 0xFFFFFF); finally: - if ( output ) memcpy(output, &result, 4); + if (output) + memcpy(output, &result, 4); return result; -} +} diff --git a/examples/healththermometer/IEEE11073float.h b/examples/healththermometer/IEEE11073float.h index b5dec8f..73d86ed 100644 --- a/examples/healththermometer/IEEE11073float.h +++ b/examples/healththermometer/IEEE11073float.h @@ -86,4 +86,4 @@ static const uint32_t FIRST_S_RESERVED_VALUE = MDER_S_POSITIVE_INFINITY; uint32_t float2IEEE11073(double data, uint8_t output[4]); -#endif /* _IEEE11073FLOAT_H_ */ +#endif /* _IEEE11073FLOAT_H_ */ diff --git a/examples/heartratemonitor/BluefruitConfig.h b/examples/heartratemonitor/BluefruitConfig.h index 9caf61a..1a249a7 100644 --- a/examples/heartratemonitor/BluefruitConfig.h +++ b/examples/heartratemonitor/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN 8 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN 8 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/hidcontrolkey/BluefruitConfig.h b/examples/hidcontrolkey/BluefruitConfig.h index 2c19279..88855ca 100644 --- a/examples/hidcontrolkey/BluefruitConfig.h +++ b/examples/hidcontrolkey/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // HARDWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -40,6 +37,6 @@ // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. // This should be used with nRF51822 based Bluefruit LE modules that use SPI. // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused diff --git a/examples/hidkeyboard/BluefruitConfig.h b/examples/hidkeyboard/BluefruitConfig.h index 2c19279..88855ca 100644 --- a/examples/hidkeyboard/BluefruitConfig.h +++ b/examples/hidkeyboard/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // HARDWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -40,6 +37,6 @@ // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. // This should be used with nRF51822 based Bluefruit LE modules that use SPI. // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused diff --git a/examples/hidmouse/BluefruitConfig.h b/examples/hidmouse/BluefruitConfig.h index 2c19279..88855ca 100644 --- a/examples/hidmouse/BluefruitConfig.h +++ b/examples/hidmouse/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // HARDWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -40,6 +37,6 @@ // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. // This should be used with nRF51822 based Bluefruit LE modules that use SPI. // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused diff --git a/examples/midi/BluefruitConfig.h b/examples/midi/BluefruitConfig.h index 193fedf..8b0427e 100644 --- a/examples/midi/BluefruitConfig.h +++ b/examples/midi/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN 12 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN 12 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN -1 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN -1 // Set to -1 if unused // HARDWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -40,6 +37,6 @@ // SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno, etc. // This should be used with nRF51822 based Bluefruit LE modules that use SPI. // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 6 // Optional but recommended, set to -1 if unused diff --git a/examples/ndof_bno055/BluefruitConfig.h b/examples/ndof_bno055/BluefruitConfig.h index f69e6a4..327990f 100644 --- a/examples/ndof_bno055/BluefruitConfig.h +++ b/examples/ndof_bno055/BluefruitConfig.h @@ -2,37 +2,34 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -41,9 +38,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -51,6 +48,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/neopixel/BluefruitConfig.h b/examples/neopixel/BluefruitConfig.h index 86260e3..e1092d7 100644 --- a/examples/neopixel/BluefruitConfig.h +++ b/examples/neopixel/BluefruitConfig.h @@ -2,38 +2,35 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output -#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output +#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -42,9 +39,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -52,6 +49,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/neopixel_picker/.cpc.test.only b/examples/neopixel_picker/.cpc.test.only new file mode 100644 index 0000000..e69de29 diff --git a/examples/neopixel_picker/BluefruitConfig.h b/examples/neopixel_picker/BluefruitConfig.h index 86260e3..e1092d7 100644 --- a/examples/neopixel_picker/BluefruitConfig.h +++ b/examples/neopixel_picker/BluefruitConfig.h @@ -2,38 +2,35 @@ // ---------------------------------------------------------------------------------------------- // These settings are used in both SW UART, HW UART and SPI mode // ---------------------------------------------------------------------------------------------- -#define BUFSIZE 128 // Size of the read buffer for incoming data -#define VERBOSE_MODE true // If set to 'true' enables debug output -#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response - +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output +#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response // SOFTWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused - +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following macros declare the HW serial port you are using. Uncomment // this line if you are connecting the BLE to Leonardo/Micro or Flora // ---------------------------------------------------------------------------------------------- -#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 - #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#ifdef Serial1 // this makes it not complain on compilation if there's no + // Serial1 +#define BLUEFRUIT_HWSERIAL_NAME Serial1 #endif - // SHARED UART SETTINGS // ---------------------------------------------------------------------------------------------- // The following sets the optional Mode pin, its recommended but not required // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused - +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused // SHARED SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -42,9 +39,9 @@ // using HW SPI. This should be used with nRF51822 based Bluefruit LE modules // that use SPI (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_CS 8 -#define BLUEFRUIT_SPI_IRQ 7 -#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused // SOFTWARE SPI SETTINGS // ---------------------------------------------------------------------------------------------- @@ -52,6 +49,6 @@ // This should be used with nRF51822 based Bluefruit LE modules that use SPI // (Bluefruit LE SPI Friend). // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SPI_SCK 13 -#define BLUEFRUIT_SPI_MISO 12 -#define BLUEFRUIT_SPI_MOSI 11 +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/examples/neopixel_picker/packetParser.cpp b/examples/neopixel_picker/packetParser.cpp index 3cfdc85..dba06f2 100644 --- a/examples/neopixel_picker/packetParser.cpp +++ b/examples/neopixel_picker/packetParser.cpp @@ -1,67 +1,58 @@ -#include #include #include -#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) - #include +#include +#if not defined(_VARIANT_ARDUINO_DUE_X_) && not defined(_VARIANT_ARDUINO_ZERO_) +#include #endif #include "Adafruit_BLE.h" #include "Adafruit_BluefruitLE_SPI.h" #include "Adafruit_BluefruitLE_UART.h" - -#define PACKET_ACC_LEN (15) -#define PACKET_GYRO_LEN (15) -#define PACKET_MAG_LEN (15) -#define PACKET_QUAT_LEN (19) -#define PACKET_BUTTON_LEN (5) -#define PACKET_COLOR_LEN (6) -#define PACKET_LOCATION_LEN (15) +#define PACKET_ACC_LEN (15) +#define PACKET_GYRO_LEN (15) +#define PACKET_MAG_LEN (15) +#define PACKET_QUAT_LEN (19) +#define PACKET_BUTTON_LEN (5) +#define PACKET_COLOR_LEN (6) +#define PACKET_LOCATION_LEN (15) // READ_BUFSIZE Size of the read buffer for incoming packets -#define READ_BUFSIZE (20) - +#define READ_BUFSIZE (20) /* Buffer to hold incoming characters */ -uint8_t packetbuffer[READ_BUFSIZE+1]; +uint8_t packetbuffer[READ_BUFSIZE + 1]; /**************************************************************************/ /*! @brief Casts the four bytes at the specified address to a float */ /**************************************************************************/ -float parsefloat(uint8_t *buffer) -{ +float parsefloat(uint8_t *buffer) { float f = ((float *)buffer)[0]; return f; } /**************************************************************************/ -/*! +/*! @brief Prints a hexadecimal value in plain characters @param data Pointer to the byte data @param numBytes Data length in bytes */ /**************************************************************************/ -void printHex(const uint8_t * data, const uint32_t numBytes) -{ +void printHex(const uint8_t *data, const uint32_t numBytes) { uint32_t szPos; - for (szPos=0; szPos < numBytes; szPos++) - { + for (szPos = 0; szPos < numBytes; szPos++) { Serial.print(F("0x")); // Append leading 0 for small values - if (data[szPos] <= 0xF) - { + if (data[szPos] <= 0xF) { Serial.print(F("0")); Serial.print(data[szPos] & 0xf, HEX); - } - else - { + } else { Serial.print(data[szPos] & 0xff, HEX); } // Add a trailing space if appropriate - if ((numBytes > 1) && (szPos != numBytes - 1)) - { + if ((numBytes > 1) && (szPos != numBytes - 1)) { Serial.print(F(" ")); } } @@ -73,14 +64,14 @@ void printHex(const uint8_t * data, const uint32_t numBytes) @brief Waits for incoming data and parses it */ /**************************************************************************/ -uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) -{ +uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) { uint16_t origtimeout = timeout, replyidx = 0; memset(packetbuffer, 0, READ_BUFSIZE); while (timeout--) { - if (replyidx >= 20) break; + if (replyidx >= 20) + break; if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) break; if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) @@ -97,7 +88,7 @@ uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) break; while (ble->available()) { - char c = ble->read(); + char c = ble->read(); if (c == '!') { replyidx = 0; } @@ -105,36 +96,35 @@ uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout) replyidx++; timeout = origtimeout; } - - if (timeout == 0) break; + + if (timeout == 0) + break; delay(1); } - packetbuffer[replyidx] = 0; // null term + packetbuffer[replyidx] = 0; // null term - if (!replyidx) // no data or timeout + if (!replyidx) // no data or timeout return 0; - if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning + if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning return 0; - + // check checksum! uint8_t xsum = 0; - uint8_t checksum = packetbuffer[replyidx-1]; - - for (uint8_t i=0; i sentence=Arduino library for nRF51822-based Adafruit Bluefruit LE modules @@ -7,3 +7,4 @@ paragraph=Arduino library for nRF51822-based Adafruit Bluefruit LE modules category=Communication url=https://github.com/adafruit/Adafruit_BluefruitLE_nRF51 architectures=* +depends=Adafruit Circuit Playground, Adafruit Unified Sensor, Adafruit NeoPixel, Adafruit BNO055, Adafruit Zero PDM Library diff --git a/utility/Adafruit_FIFO.cpp b/utility/Adafruit_FIFO.cpp index 6a7f16a..f67ab2d 100644 --- a/utility/Adafruit_FIFO.cpp +++ b/utility/Adafruit_FIFO.cpp @@ -29,8 +29,8 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ @@ -51,11 +51,11 @@ Should the buffer is overwitten to the first item when it is full */ /******************************************************************************/ -Adafruit_FIFO::Adafruit_FIFO(void* buffer, uint16_t depth, uint8_t item_size, bool overwrite) -{ - m_buffer = (uint8_t*) buffer; - m_depth = depth; - m_item_size = item_size; +Adafruit_FIFO::Adafruit_FIFO(void *buffer, uint16_t depth, uint8_t item_size, + bool overwrite) { + m_buffer = (uint8_t *)buffer; + m_depth = depth; + m_item_size = item_size; m_overwritable = overwrite; m_count = m_wr_idx = m_rd_idx = 0; @@ -66,10 +66,7 @@ Adafruit_FIFO::Adafruit_FIFO(void* buffer, uint16_t depth, uint8_t item_size, bo @brief Clear the FIFO */ /******************************************************************************/ -void Adafruit_FIFO::clear(void) -{ - m_rd_idx = m_wr_idx = m_count = 0; -} +void Adafruit_FIFO::clear(void) { m_rd_idx = m_wr_idx = m_count = 0; } /******************************************************************************/ /*! @@ -79,22 +76,17 @@ void Adafruit_FIFO::clear(void) Memory address of the item */ /******************************************************************************/ -bool Adafruit_FIFO::write(void const* item) -{ - if ( full() && !m_overwritable ) return false; +bool Adafruit_FIFO::write(void const *item) { + if (full() && !m_overwritable) + return false; - memcpy( m_buffer + (m_wr_idx * m_item_size), - item, - m_item_size); + memcpy(m_buffer + (m_wr_idx * m_item_size), item, m_item_size); m_wr_idx = (m_wr_idx + 1) % m_depth; - if ( full() ) - { + if (full()) { m_rd_idx = m_wr_idx; // keep the full state (rd == wr && len = size) - } - else - { + } else { m_count++; } @@ -113,15 +105,14 @@ bool Adafruit_FIFO::write(void const* item) @return Number of written items */ /******************************************************************************/ -uint16_t Adafruit_FIFO::write_n(void const * data, uint16_t n) -{ - if ( n == 0 ) return 0; +uint16_t Adafruit_FIFO::write_n(void const *data, uint16_t n) { + if (n == 0) + return 0; - uint8_t* buf = (uint8_t*) data; + uint8_t *buf = (uint8_t *)data; uint16_t len = 0; - while( (len < n) && write(buf) ) - { + while ((len < n) && write(buf)) { len++; buf += m_item_size; } @@ -137,13 +128,11 @@ uint16_t Adafruit_FIFO::write_n(void const * data, uint16_t n) Memory address to store item */ /******************************************************************************/ -bool Adafruit_FIFO::read(void* buffer) -{ - if( empty() ) return false; +bool Adafruit_FIFO::read(void *buffer) { + if (empty()) + return false; - memcpy(buffer, - m_buffer + (m_rd_idx * m_item_size), - m_item_size); + memcpy(buffer, m_buffer + (m_rd_idx * m_item_size), m_item_size); m_rd_idx = (m_rd_idx + 1) % m_depth; m_count--; @@ -163,15 +152,14 @@ bool Adafruit_FIFO::read(void* buffer) */ /******************************************************************************/ -uint16_t Adafruit_FIFO::read_n (void * buffer, uint16_t n) -{ - if( n == 0 ) return 0; +uint16_t Adafruit_FIFO::read_n(void *buffer, uint16_t n) { + if (n == 0) + return 0; - uint8_t* buf = (uint8_t*) buffer; + uint8_t *buf = (uint8_t *)buffer; uint16_t len = 0; - while( (len < n) && read(buf) ) - { + while ((len < n) && read(buf)) { len++; buf += m_item_size; } @@ -187,18 +175,15 @@ uint16_t Adafruit_FIFO::read_n (void * buffer, uint16_t n) Memory address to store item */ /******************************************************************************/ -bool Adafruit_FIFO::peek(void* buffer) -{ - if( empty() ) return false; +bool Adafruit_FIFO::peek(void *buffer) { + if (empty()) + return false; - memcpy(buffer, - m_buffer + (m_rd_idx * m_item_size), - m_item_size); + memcpy(buffer, m_buffer + (m_rd_idx * m_item_size), m_item_size); return true; } - /******************************************************************************/ /*! @brief Read an item without removing it from the FIFO at the specific index @@ -210,15 +195,12 @@ bool Adafruit_FIFO::peek(void* buffer) Memory address to store item */ /******************************************************************************/ -bool Adafruit_FIFO::peekAt(uint16_t position, void * p_buffer) -{ - if( empty() || (position >= m_count) ) return false; +bool Adafruit_FIFO::peekAt(uint16_t position, void *p_buffer) { + if (empty() || (position >= m_count)) + return false; uint16_t index = (m_rd_idx + position) % m_depth; // rd_idx is position=0 - memcpy(p_buffer, - m_buffer + (index * m_item_size), - m_item_size); + memcpy(p_buffer, m_buffer + (index * m_item_size), m_item_size); return true; } - diff --git a/utility/Adafruit_FIFO.h b/utility/Adafruit_FIFO.h index 8ae5076..93bcf1c 100644 --- a/utility/Adafruit_FIFO.h +++ b/utility/Adafruit_FIFO.h @@ -29,46 +29,46 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _Adafruit_FIFO_H_ #define _Adafruit_FIFO_H_ -#include #include +#include -class Adafruit_FIFO -{ - private: - uint8_t* m_buffer ; ///< buffer pointer - uint16_t m_depth ; ///< max items - uint8_t m_item_size ; ///< size of each item - bool m_overwritable ; - volatile uint16_t m_count ; ///< number of items in queue - volatile uint16_t m_wr_idx ; ///< write pointer - volatile uint16_t m_rd_idx ; ///< read pointer +class Adafruit_FIFO { +private: + uint8_t *m_buffer; ///< buffer pointer + uint16_t m_depth; ///< max items + uint8_t m_item_size; ///< size of each item + bool m_overwritable; + volatile uint16_t m_count; ///< number of items in queue + volatile uint16_t m_wr_idx; ///< write pointer + volatile uint16_t m_rd_idx; ///< read pointer - public: - // Constructor - Adafruit_FIFO(void* buffer, uint16_t depth, uint8_t item_size, bool overwrite); +public: + // Constructor + Adafruit_FIFO(void *buffer, uint16_t depth, uint8_t item_size, + bool overwrite); - void clear(void); - bool peek(void* buffer); - bool peekAt(uint16_t position, void * p_buffer); + void clear(void); + bool peek(void *buffer); + bool peekAt(uint16_t position, void *p_buffer); - bool write(void const* item); - uint16_t write_n(void const * data, uint16_t n); + bool write(void const *item); + uint16_t write_n(void const *data, uint16_t n); - bool read(void* buffer); - uint16_t read_n (void * buffer, uint16_t n); + bool read(void *buffer); + uint16_t read_n(void *buffer, uint16_t n); - inline bool empty(void) { return m_count == 0; } - inline bool full(void) { return m_count == m_depth; } - inline uint16_t count(void) { return m_count; } - inline uint16_t remaining(void) { return m_depth - m_count; } + inline bool empty(void) { return m_count == 0; } + inline bool full(void) { return m_count == m_depth; } + inline uint16_t count(void) { return m_count; } + inline uint16_t remaining(void) { return m_depth - m_count; } }; #endif /* _Adafruit_FIFO_H_ */ diff --git a/utility/TimeoutTimer.h b/utility/TimeoutTimer.h index fc2d0b5..6c457ba 100644 --- a/utility/TimeoutTimer.h +++ b/utility/TimeoutTimer.h @@ -29,32 +29,39 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ -/** +/** * @{ */ #ifndef _TIMEOUT_TIMER_H_ #define _TIMEOUT_TIMER_H_ -class TimeoutTimer -{ - private: - uint32_t start; - uint32_t interval; +class TimeoutTimer { +private: + uint32_t start; + uint32_t interval; - public: - TimeoutTimer() { start = millis(); interval = 0; } - TimeoutTimer(uint32_t msec) { set(msec); } +public: + TimeoutTimer() { + start = millis(); + interval = 0; + } + TimeoutTimer(uint32_t msec) { set(msec); } - void set(uint32_t msec) { start = millis(); interval = msec; } - bool expired(void) const { return (millis() - start) >= interval; } - void restart(void) { start = millis(); } - void reset(void) { start += interval; } // used for periodic invoke to prevent drift + void set(uint32_t msec) { + start = millis(); + interval = msec; + } + bool expired(void) const { return (millis() - start) >= interval; } + void restart(void) { start = millis(); } + void reset(void) { + start += interval; + } // used for periodic invoke to prevent drift }; #endif /* _TIMEOUT_TIMER_H_ */ diff --git a/utility/common_header.h b/utility/common_header.h index 9417d61..43d3d91 100644 --- a/utility/common_header.h +++ b/utility/common_header.h @@ -29,26 +29,26 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #ifndef _COMMON_HEADER_H_ #define _COMMON_HEADER_H_ -#include #include +#include //--------------------------------------------------------------------+ // COMPILER //--------------------------------------------------------------------+ -#define STRING_(x) #x // stringify without expand -#define XSTRING_(x) STRING_(x) // expand then stringify -#define STRING_CONCAT_(a, b) a##b // concat without expand -#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat +#define STRING_(x) #x // stringify without expand +#define XSTRING_(x) STRING_(x) // expand then stringify +#define STRING_CONCAT_(a, b) a##b // concat without expand +#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat -#define ATTR_PACKED __attribute__ ((packed)) +#define ATTR_PACKED __attribute__((packed)) //--------------------------------------------------------------------+ // ASSERT & VERIFY @@ -57,22 +57,27 @@ //------------- Compile-time Assert -------------// #if defined __COUNTER__ && __COUNTER__ != __COUNTER__ - #define _ASSERT_COUNTER __COUNTER__ +#define _ASSERT_COUNTER __COUNTER__ #else - #define _ASSERT_COUNTER __LINE__ +#define _ASSERT_COUNTER __LINE__ #endif -#define ASSERT_STATIC_(const_expr) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) } - +#define ASSERT_STATIC_(const_expr) \ + enum { \ + XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1 / (!!(const_expr)) \ + } -#define VERIFY_(condition) if ( !(condition) ) return false; -#define VERIFY_RETURN_(condition, error) if ( !(condition) ) return error; +#define VERIFY_(condition) \ + if (!(condition)) \ + return false; +#define VERIFY_RETURN_(condition, error) \ + if (!(condition)) \ + return error; //--------------------------------------------------------------------+ // INLINE FUNCTION //--------------------------------------------------------------------+ -static inline bool is_within(uint32_t lower, uint32_t value, uint32_t upper) -{ +static inline bool is_within(uint32_t lower, uint32_t value, uint32_t upper) { return (lower <= value) && (value <= upper); } diff --git a/utility/errors.h b/utility/errors.h index 69ffeec..2fdcb17 100644 --- a/utility/errors.h +++ b/utility/errors.h @@ -28,8 +28,8 @@ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /******************************************************************************/ #ifndef _ERRORS_H_ @@ -44,38 +44,37 @@ extern "C" { @brief Standard error codes used throughout this codebase */ /******************************************************************************/ -typedef enum -{ +typedef enum { /*======================================================================= CUSTOM GLOBAL ERRORS 0x8000 .. 0x801F ----------------------------------------------------------------------- Custom global errors (not defined by Nordic) -----------------------------------------------------------------------*/ - ERROR_INITFAILED = 0x8001, ///< Function or driver initialisation failed - ERROR_NOT_INITIALISED = 0x8002, ///< Attempted to call a function in an unitialised device/driver - ERROR_I2C_XFER_FAILED = 0x8003, ///< I2C transfer failure + ERROR_INITFAILED = 0x8001, ///< Function or driver initialisation failed + ERROR_NOT_INITIALISED = + 0x8002, ///< Attempted to call a function in an unitialised device/driver + ERROR_I2C_XFER_FAILED = 0x8003, ///< I2C transfer failure /*=======================================================================*/ - /*======================================================================= SIMPLE BINARY PROTOCOL ERRORS 0x8020 .. 0x803F ----------------------------------------------------------------------- Errors relating to the simple binary protocol -----------------------------------------------------------------------*/ - ERROR_SDEP_INVALIDMSGTYPE = 0x8021, ///< Unexpected msg type encountered - ERROR_SDEP_INVALIDCOMMANDID = 0x8022, ///< Unknown or out of range command ID - ERROR_SDEP_INVALIDPAYLOAD = 0x8023, ///< Message payload has a problem (invalid len, etc.) + ERROR_SDEP_INVALIDMSGTYPE = 0x8021, ///< Unexpected msg type encountered + ERROR_SDEP_INVALIDCOMMANDID = 0x8022, ///< Unknown or out of range command ID + ERROR_SDEP_INVALIDPAYLOAD = + 0x8023, ///< Message payload has a problem (invalid len, etc.) /*=======================================================================*/ - /*======================================================================= COMMAND LINE INTERFACE ERRORS 0x8040 .. 0x805F ----------------------------------------------------------------------- Errors relating to the simple binary protocol -----------------------------------------------------------------------*/ - ERROR_CLI_UNKNOWN_COMMAND = 0x8041, ///< Unknown Command entered - ERROR_CLI_TOO_FEW_ARGUMENTS = 0x8042, ///< Too Few Arguments - ERROR_CLI_TOO_MANY_ARGUMENTS = 0x8043, ///< Too Few Arguments + ERROR_CLI_UNKNOWN_COMMAND = 0x8041, ///< Unknown Command entered + ERROR_CLI_TOO_FEW_ARGUMENTS = 0x8042, ///< Too Few Arguments + ERROR_CLI_TOO_MANY_ARGUMENTS = 0x8043, ///< Too Few Arguments /*=======================================================================*/ /*======================================================================= @@ -83,9 +82,9 @@ typedef enum ----------------------------------------------------------------------- Errors relating to AT Command -----------------------------------------------------------------------*/ - ERROR_AT_INVALID_INPUT = 0x8060, - ERROR_AT_UNKNOWN_COMMAND = 0x8061, - ERROR_AT_INVALID_PARAMETER = 0x8062, + ERROR_AT_INVALID_INPUT = 0x8060, + ERROR_AT_UNKNOWN_COMMAND = 0x8061, + ERROR_AT_INVALID_PARAMETER = 0x8062, /*=======================================================================*/ /*======================================================================= @@ -93,7 +92,7 @@ typedef enum ----------------------------------------------------------------------- Errors relating to the NVM -----------------------------------------------------------------------*/ - ERROR_NVM_BUSY = 0x8060, + ERROR_NVM_BUSY = 0x8060, /*=======================================================================*/ /*======================================================================= @@ -101,7 +100,7 @@ typedef enum ----------------------------------------------------------------------- Errors relating to the NVM -----------------------------------------------------------------------*/ - ERROR_OSAL_WAITING = 0x80A0, + ERROR_OSAL_WAITING = 0x80A0, /*=======================================================================*/ /*======================================================================= @@ -109,89 +108,99 @@ typedef enum ----------------------------------------------------------------------- Errors mapped from nrf_error.h -----------------------------------------------------------------------*/ - ERROR_NONE = 0x0000, ///< Successful command - ERROR_SVC_HANDLER_MISSING = 0x0001, ///< SVC handler is missing - ERROR_SOFTDEVICE_NOT_ENABLED = 0x0002, ///< SoftDevice has not been enabled - ERROR_INTERNAL = 0x0003, ///< Internal Error - ERROR_NO_MEM = 0x0004, ///< No Memory for operation - ERROR_NOT_FOUND = 0x0005, ///< Not found - ERROR_NOT_SUPPORTED = 0x0006, ///< Not supported - ERROR_INVALID_PARAM = 0x0007, ///< Invalid Parameter - ERROR_INVALID_STATE = 0x0008, ///< Invalid state, operation disallowed in this state - ERROR_INVALID_LENGTH = 0x0009, ///< Invalid Length - ERROR_INVALID_FLAGS = 0x000A, ///< Invalid Flags - ERROR_INVALID_DATA = 0x000B, ///< Invalid Data - ERROR_DATA_SIZE = 0x000C, ///< Data size exceeds limit - ERROR_TIMEOUT = 0x000D, ///< Operation timed out - ERROR_NULL = 0x000E, ///< Null Pointer - ERROR_FORBIDDEN = 0x000F, ///< Forbidden Operation - ERROR_INVALID_ADDR = 0x0010, ///< Bad Memory Address - ERROR_BUSY = 0x0011, ///< Busy + ERROR_NONE = 0x0000, ///< Successful command + ERROR_SVC_HANDLER_MISSING = 0x0001, ///< SVC handler is missing + ERROR_SOFTDEVICE_NOT_ENABLED = 0x0002, ///< SoftDevice has not been enabled + ERROR_INTERNAL = 0x0003, ///< Internal Error + ERROR_NO_MEM = 0x0004, ///< No Memory for operation + ERROR_NOT_FOUND = 0x0005, ///< Not found + ERROR_NOT_SUPPORTED = 0x0006, ///< Not supported + ERROR_INVALID_PARAM = 0x0007, ///< Invalid Parameter + ERROR_INVALID_STATE = + 0x0008, ///< Invalid state, operation disallowed in this state + ERROR_INVALID_LENGTH = 0x0009, ///< Invalid Length + ERROR_INVALID_FLAGS = 0x000A, ///< Invalid Flags + ERROR_INVALID_DATA = 0x000B, ///< Invalid Data + ERROR_DATA_SIZE = 0x000C, ///< Data size exceeds limit + ERROR_TIMEOUT = 0x000D, ///< Operation timed out + ERROR_NULL = 0x000E, ///< Null Pointer + ERROR_FORBIDDEN = 0x000F, ///< Forbidden Operation + ERROR_INVALID_ADDR = 0x0010, ///< Bad Memory Address + ERROR_BUSY = 0x0011, ///< Busy /*=======================================================================*/ - /*======================================================================= NORDIC SDM ERRORS 0x1000 .. 0x1FFF ----------------------------------------------------------------------- Errors based on Nordic's SDM nrf_error_sdm.h -----------------------------------------------------------------------*/ - ERROR_SDM_LFCLK_SOURCE_UNKNOWN = 0x1000, ///< Unknown lfclk source - ERROR_SDM_INCORRECT_INTERRUPT_CONFIGURATION = 0x1001, ///< Incorrect interrupt configuration (can be caused by using illegal priority levels, or having enabled SoftDevice interrupts) - ERROR_SDM_INCORRECT_CLENR0 = 0x1002, ///< Incorrect CLENR0 (can be caused by erronous SoftDevice flashing) + ERROR_SDM_LFCLK_SOURCE_UNKNOWN = 0x1000, ///< Unknown lfclk source + ERROR_SDM_INCORRECT_INTERRUPT_CONFIGURATION = + 0x1001, ///< Incorrect interrupt configuration (can be caused by using + ///< illegal priority levels, or having enabled SoftDevice + ///< interrupts) + ERROR_SDM_INCORRECT_CLENR0 = 0x1002, ///< Incorrect CLENR0 (can be caused by + ///< erronous SoftDevice flashing) /*=======================================================================*/ - /*======================================================================= NORDIC SOC ERRORS 0x2000 .. 0x2FFF ----------------------------------------------------------------------- Errors based on Nordic's nrf_error_soc.h -----------------------------------------------------------------------*/ - /* Mutex Errors */ - ERROR_SOC_MUTEX_ALREADY_TAKEN = 0x2000, ///< Mutex already taken - - /* NVIC errors */ - ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE = 0x2001, ///< NVIC interrupt not available - ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED = 0x2002, ///< NVIC interrupt priority not allowed - ERROR_SOC_NVIC_SHOULD_NOT_RETURN = 0x2003, ///< NVIC should not return - - /* Power errors */ - ERROR_SOC_POWER_MODE_UNKNOWN = 0x2004, ///< Power mode unknown - ERROR_SOC_POWER_POF_THRESHOLD_UNKNOWN = 0x2005, ///< Power POF threshold unknown - ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN = 0x2006, ///< Power off should not return - - /* Rand errors */ - ERROR_SOC_RAND_NOT_ENOUGH_VALUES = 0x2007, ///< RAND not enough values - - /* PPI errors */ - ERROR_SOC_PPI_INVALID_CHANNEL = 0x2008, ///< Invalid PPI Channel - ERROR_SOC_PPI_INVALID_GROUP = 0x2009, ///< Invalid PPI Group + /* Mutex Errors */ + ERROR_SOC_MUTEX_ALREADY_TAKEN = 0x2000, ///< Mutex already taken + + /* NVIC errors */ + ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE = + 0x2001, ///< NVIC interrupt not available + ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED = + 0x2002, ///< NVIC interrupt priority not allowed + ERROR_SOC_NVIC_SHOULD_NOT_RETURN = 0x2003, ///< NVIC should not return + + /* Power errors */ + ERROR_SOC_POWER_MODE_UNKNOWN = 0x2004, ///< Power mode unknown + ERROR_SOC_POWER_POF_THRESHOLD_UNKNOWN = + 0x2005, ///< Power POF threshold unknown + ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN = + 0x2006, ///< Power off should not return + + /* Rand errors */ + ERROR_SOC_RAND_NOT_ENOUGH_VALUES = 0x2007, ///< RAND not enough values + + /* PPI errors */ + ERROR_SOC_PPI_INVALID_CHANNEL = 0x2008, ///< Invalid PPI Channel + ERROR_SOC_PPI_INVALID_GROUP = 0x2009, ///< Invalid PPI Group /*=======================================================================*/ - /*======================================================================= NORDIC BLE ERRORS 0x3000 .. 0x3FFF ----------------------------------------------------------------------- Errors based on Nordic's ble_err.h -----------------------------------------------------------------------*/ - ERROR_BLE_NOT_ENABLED = 0x3001, - ERROR_BLE_INVALID_CONN_HANDLE = 0x3002, ///< Invalid connection handle. - ERROR_BLE_INVALID_ATTR_HANDLE = 0x3003, ///< Invalid attribute handle. - ERROR_BLE_NO_TX_BUFFERS = 0x3004, ///< Buffer capacity exceeded. - - /* L2CAP */ - ERROR_BLE_L2CAP_CID_IN_USE = 0x3100, ///< CID already in use. - - /* GAP */ - ERROR_BLE_GAP_UUID_LIST_MISMATCH = 0x3200, ///< UUID list does not contain an integral number of UUIDs. - ERROR_BLE_GAP_DISCOVERABLE_WITH_WHITELIST = 0x3201, ///< Use of Whitelist not permitted with discoverable advertising. - ERROR_BLE_GAP_INVALID_BLE_ADDR = 0x3202, ///< The upper two bits of the address do not correspond to the specified address type. - - /* GATTC */ - ERROR_BLE_GATTC_PROC_NOT_PERMITTED = 0x3300, - - /* GATTS */ - ERROR_BLEGATTS_INVALID_ATTR_TYPE = 0x3400, ///< Invalid attribute type. - ERROR_BLEGATTS_SYS_ATTR_MISSING = 0x3401, ///< System Attributes missing. + ERROR_BLE_NOT_ENABLED = 0x3001, + ERROR_BLE_INVALID_CONN_HANDLE = 0x3002, ///< Invalid connection handle. + ERROR_BLE_INVALID_ATTR_HANDLE = 0x3003, ///< Invalid attribute handle. + ERROR_BLE_NO_TX_BUFFERS = 0x3004, ///< Buffer capacity exceeded. + + /* L2CAP */ + ERROR_BLE_L2CAP_CID_IN_USE = 0x3100, ///< CID already in use. + + /* GAP */ + ERROR_BLE_GAP_UUID_LIST_MISMATCH = + 0x3200, ///< UUID list does not contain an integral number of UUIDs. + ERROR_BLE_GAP_DISCOVERABLE_WITH_WHITELIST = + 0x3201, ///< Use of Whitelist not permitted with discoverable advertising. + ERROR_BLE_GAP_INVALID_BLE_ADDR = + 0x3202, ///< The upper two bits of the address do not correspond to the + ///< specified address type. + + /* GATTC */ + ERROR_BLE_GATTC_PROC_NOT_PERMITTED = 0x3300, + + /* GATTS */ + ERROR_BLEGATTS_INVALID_ATTR_TYPE = 0x3400, ///< Invalid attribute type. + ERROR_BLEGATTS_SYS_ATTR_MISSING = 0x3401, ///< System Attributes missing. /*=======================================================================*/ } err_t; diff --git a/utility/sdep.h b/utility/sdep.h index fd07c60..4f348a4 100644 --- a/utility/sdep.h +++ b/utility/sdep.h @@ -40,7 +40,7 @@ #include "common_header.h" -#define SDEP_MAX_PACKETSIZE 16 // Maximum payload per packet +#define SDEP_MAX_PACKETSIZE 16 // Maximum payload per packet /******************************************************************************/ /*! @@ -48,12 +48,11 @@ ID, and is used to create the command lookup table enum further down */ /******************************************************************************/ -typedef enum -{ - SDEP_CMDTYPE_INITIALIZE = 0xBEEF, /**< Controls the on board LED(s) */ - SDEP_CMDTYPE_AT_WRAPPER = 0x0A00, - SDEP_CMDTYPE_BLE_UARTTX = 0x0A01, - SDEP_CMDTYPE_BLE_UARTRX = 0x0A02, +typedef enum { + SDEP_CMDTYPE_INITIALIZE = 0xBEEF, /**< Controls the on board LED(s) */ + SDEP_CMDTYPE_AT_WRAPPER = 0x0A00, + SDEP_CMDTYPE_BLE_UARTTX = 0x0A01, + SDEP_CMDTYPE_BLE_UARTRX = 0x0A02, } sdepCmdType_t; /******************************************************************************/ @@ -61,12 +60,11 @@ typedef enum The first byte of every transfer defines the message type */ /******************************************************************************/ -typedef enum -{ - SDEP_MSGTYPE_COMMAND = 0x10, - SDEP_MSGTYPE_RESPONSE = 0x20, - SDEP_MSGTYPE_ALERT = 0x40, - SDEP_MSGTYPE_ERROR = 0x80 +typedef enum { + SDEP_MSGTYPE_COMMAND = 0x10, + SDEP_MSGTYPE_RESPONSE = 0x20, + SDEP_MSGTYPE_ALERT = 0x40, + SDEP_MSGTYPE_ERROR = 0x80 } sdepMsgType_t; /******************************************************************************/ @@ -75,22 +73,19 @@ typedef enum */ /******************************************************************************/ typedef struct ATTR_PACKED { - uint8_t msg_type; // 8-bit message type indicator (sdepMsgType_t) + uint8_t msg_type; // 8-bit message type indicator (sdepMsgType_t) - union - { - uint16_t cmd_id; // 16-bit command ID - struct - { + union { + uint16_t cmd_id; // 16-bit command ID + struct { uint8_t cmd_id_low; uint8_t cmd_id_high; }; }; - struct ATTR_PACKED - { - uint8_t length : 7; // Payload length (for this packet) - uint8_t more_data : 1; // 'more' bit for multiple packet transfers + struct ATTR_PACKED { + uint8_t length : 7; // Payload length (for this packet) + uint8_t more_data : 1; // 'more' bit for multiple packet transfers }; } sdepMsgHeader_t; @@ -99,8 +94,7 @@ typedef struct ATTR_PACKED { SDEP command message */ /******************************************************************************/ -typedef struct ATTR_PACKED -{ +typedef struct ATTR_PACKED { sdepMsgHeader_t header; uint8_t payload[SDEP_MAX_PACKETSIZE]; } sdepMsgCommand_t;