added debugging
Fork of BLE_nRF8001 by
BLE_nRF8001/BLECentral.cpp@2:7805a5595aab, 2014-11-10 (annotated)
- Committer:
- jn80842
- Date:
- Mon Nov 10 01:24:23 2014 +0000
- Revision:
- 2:7805a5595aab
- Parent:
- 0:075ea2812998
just added debugging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:075ea2812998 | 1 | #include "Arduino.h" |
RedBearLab | 0:075ea2812998 | 2 | |
RedBearLab | 0:075ea2812998 | 3 | #include "BLEPeripheral.h" |
RedBearLab | 0:075ea2812998 | 4 | |
RedBearLab | 0:075ea2812998 | 5 | #include "BLECentral.h" |
RedBearLab | 0:075ea2812998 | 6 | |
RedBearLab | 0:075ea2812998 | 7 | BLECentral::BLECentral(BLEPeripheral* peripheral) : |
RedBearLab | 0:075ea2812998 | 8 | _peripheral(peripheral) |
RedBearLab | 0:075ea2812998 | 9 | { |
RedBearLab | 0:075ea2812998 | 10 | this->clearAddress(); |
RedBearLab | 0:075ea2812998 | 11 | } |
RedBearLab | 0:075ea2812998 | 12 | |
RedBearLab | 0:075ea2812998 | 13 | BLECentral::operator bool() const { |
RedBearLab | 0:075ea2812998 | 14 | unsigned char zero[6] = {0, 0, 0, 0, 0, 0}; |
RedBearLab | 0:075ea2812998 | 15 | |
RedBearLab | 0:075ea2812998 | 16 | return (memcmp(this->_address, zero, sizeof(this->_address)) != 0); |
RedBearLab | 0:075ea2812998 | 17 | } |
RedBearLab | 0:075ea2812998 | 18 | |
RedBearLab | 0:075ea2812998 | 19 | bool BLECentral::operator==(const BLECentral& rhs) const { |
RedBearLab | 0:075ea2812998 | 20 | return (memcmp(this->_address, rhs._address, sizeof(this->_address)) == 0); |
RedBearLab | 0:075ea2812998 | 21 | } |
RedBearLab | 0:075ea2812998 | 22 | |
RedBearLab | 0:075ea2812998 | 23 | bool BLECentral::operator!=(const BLECentral& rhs) const { |
RedBearLab | 0:075ea2812998 | 24 | return !(*this == rhs); |
RedBearLab | 0:075ea2812998 | 25 | } |
RedBearLab | 0:075ea2812998 | 26 | |
RedBearLab | 0:075ea2812998 | 27 | bool BLECentral::connected() { |
RedBearLab | 0:075ea2812998 | 28 | this->poll(); |
RedBearLab | 0:075ea2812998 | 29 | |
RedBearLab | 0:075ea2812998 | 30 | return (*this && *this == this->_peripheral->central()); |
RedBearLab | 0:075ea2812998 | 31 | } |
RedBearLab | 0:075ea2812998 | 32 | |
RedBearLab | 0:075ea2812998 | 33 | const char* BLECentral::address() const { |
RedBearLab | 0:075ea2812998 | 34 | static char address[18]; |
RedBearLab | 0:075ea2812998 | 35 | |
RedBearLab | 0:075ea2812998 | 36 | sprintf(address, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", |
RedBearLab | 0:075ea2812998 | 37 | this->_address[5], |
RedBearLab | 0:075ea2812998 | 38 | this->_address[4], |
RedBearLab | 0:075ea2812998 | 39 | this->_address[3], |
RedBearLab | 0:075ea2812998 | 40 | this->_address[2], |
RedBearLab | 0:075ea2812998 | 41 | this->_address[1], |
RedBearLab | 0:075ea2812998 | 42 | this->_address[0]); |
RedBearLab | 0:075ea2812998 | 43 | |
RedBearLab | 0:075ea2812998 | 44 | return address; |
RedBearLab | 0:075ea2812998 | 45 | } |
RedBearLab | 0:075ea2812998 | 46 | |
RedBearLab | 0:075ea2812998 | 47 | void BLECentral::poll() { |
RedBearLab | 0:075ea2812998 | 48 | this->_peripheral->poll(); |
RedBearLab | 0:075ea2812998 | 49 | } |
RedBearLab | 0:075ea2812998 | 50 | |
RedBearLab | 0:075ea2812998 | 51 | void BLECentral::disconnect() { |
RedBearLab | 0:075ea2812998 | 52 | if (this->connected()) { |
RedBearLab | 0:075ea2812998 | 53 | this->_peripheral->disconnect(); |
RedBearLab | 0:075ea2812998 | 54 | } |
RedBearLab | 0:075ea2812998 | 55 | } |
RedBearLab | 0:075ea2812998 | 56 | |
RedBearLab | 0:075ea2812998 | 57 | void BLECentral::setAddress(const unsigned char* address) { |
RedBearLab | 0:075ea2812998 | 58 | memcpy(this->_address, address, sizeof(this->_address)); |
RedBearLab | 0:075ea2812998 | 59 | } |
RedBearLab | 0:075ea2812998 | 60 | |
RedBearLab | 0:075ea2812998 | 61 | void BLECentral::clearAddress() { |
RedBearLab | 0:075ea2812998 | 62 | memset(this->_address, 0x00, sizeof(this->_address)); |
RedBearLab | 0:075ea2812998 | 63 | } |