2
2
BLEStream.cpp
3
3
4
4
Based on BLESerial.cpp by Voita Molda
5
- https://github.com/vojtamolda/ BLEPeripheral/blob/master/examples/uart /BLESerial.cpp
5
+ https://github.com/sandeepmistry/arduino- BLEPeripheral/blob/master/examples/serial /BLESerial.cpp
6
6
*/
7
7
8
8
#include " BLEStream.h"
9
9
10
- // #define BLE_SERIAL_DEBUG
10
+ // #define BLE_SERIAL_DEBUG
11
11
12
12
BLEStream* BLEStream::_instance = NULL ;
13
13
14
- BLEStream::BLEStream (unsigned char ) :
14
+ BLEStream::BLEStream (unsigned char req, unsigned char rdy, unsigned char rst) :
15
+ #if defined(_VARIANT_ARDUINO_101_X_)
15
16
BLEPeripheral ()
17
+ #else
18
+ BLEPeripheral (req, rdy, rst)
19
+ #endif
16
20
{
17
21
this ->_txCount = 0 ;
18
22
this ->_rxHead = this ->_rxTail = 0 ;
@@ -29,100 +33,130 @@ BLEStream::BLEStream(unsigned char) :
29
33
addAttribute (this ->_txNameDescriptor );
30
34
}
31
35
32
- void BLEStream::begin (...) {
36
+ void BLEStream::begin (...)
37
+ {
33
38
BLEPeripheral::begin ();
34
- #ifdef BLE_SERIAL_DEBUG
35
- Serial.println (F (" BLEStream::begin()" ));
36
- #endif
39
+ #ifdef BLE_SERIAL_DEBUG
40
+ Serial.println (F (" BLEStream::begin()" ));
41
+ #endif
37
42
}
38
43
39
- bool BLEStream::poll () {
44
+ bool BLEStream::poll ()
45
+ {
46
+ // BLEPeripheral::poll is called each time connected() is called
40
47
this ->_connected = BLEPeripheral::connected ();
41
- if (millis () > this ->_flushed + BLESTREAM_TXBUFFER_FLUSH_INTERVAL) flush ();
48
+ if (millis () > this ->_flushed + BLESTREAM_TXBUFFER_FLUSH_INTERVAL) {
49
+ flush ();
50
+ }
42
51
return this ->_connected ;
43
52
}
44
53
45
- void BLEStream::end () {
54
+ void BLEStream::end ()
55
+ {
46
56
this ->_rxCharacteristic .setEventHandler (BLEWritten, NULL );
47
57
this ->_rxHead = this ->_rxTail = 0 ;
48
58
flush ();
49
59
BLEPeripheral::disconnect ();
50
60
}
51
61
52
- int BLEStream::available (void ) {
62
+ int BLEStream::available (void )
63
+ {
64
+ // BLEPeripheral::poll only calls delay(1) in CurieBLE so skipping it here to avoid the delay
65
+ #ifndef _VARIANT_ARDUINO_101_X_
66
+ // TODO Need to do more testing to determine if all of these calls to BLEPeripheral::poll are
67
+ // actually necessary. Seems to run fine without them, but only minimal testing so far.
68
+ BLEPeripheral::poll ();
69
+ #endif
53
70
int retval = (this ->_rxHead - this ->_rxTail + sizeof (this ->_rxBuffer )) % sizeof (this ->_rxBuffer );
54
- #ifdef BLE_SERIAL_DEBUG
55
- if (retval > 0 ) {
56
- Serial.print (F (" BLEStream::available() = " ));
57
- Serial.println (retval);
58
- }
59
- #endif
71
+ #ifdef BLE_SERIAL_DEBUG
72
+ if (retval > 0 ) {
73
+ Serial.print (F (" BLEStream::available() = " ));
74
+ Serial.println (retval);
75
+ }
76
+ #endif
60
77
return retval;
61
78
}
62
79
63
- int BLEStream::peek (void ) {
80
+ int BLEStream::peek (void )
81
+ {
82
+ #ifndef _VARIANT_ARDUINO_101_X_
83
+ BLEPeripheral::poll ();
84
+ #endif
64
85
if (this ->_rxTail == this ->_rxHead ) return -1 ;
65
- unsigned char byte = this ->_rxBuffer [this ->_rxTail ];
66
- #ifdef BLE_SERIAL_DEBUG
67
- Serial.print (F (" BLEStream::peek() = 0x" ));
68
- Serial.println (byte, HEX);
69
- #endif
86
+ uint8_t byte = this ->_rxBuffer [this ->_rxTail ];
87
+ #ifdef BLE_SERIAL_DEBUG
88
+ Serial.print (F (" BLEStream::peek() = 0x" ));
89
+ Serial.println (byte, HEX);
90
+ #endif
70
91
return byte;
71
92
}
72
93
73
- int BLEStream::read (void ) {
94
+ int BLEStream::read (void )
95
+ {
96
+ #ifndef _VARIANT_ARDUINO_101_X_
97
+ BLEPeripheral::poll ();
98
+ #endif
74
99
if (this ->_rxTail == this ->_rxHead ) return -1 ;
75
100
this ->_rxTail = (this ->_rxTail + 1 ) % sizeof (this ->_rxBuffer );
76
- unsigned char byte = this ->_rxBuffer [this ->_rxTail ];
77
- #ifdef BLE_SERIAL_DEBUG
78
- Serial.print (F (" BLEStream::read() = 0x" ));
79
- Serial.println (byte, HEX);
80
- #endif
101
+ uint8_t byte = this ->_rxBuffer [this ->_rxTail ];
102
+ #ifdef BLE_SERIAL_DEBUG
103
+ Serial.print (F (" BLEStream::read() = 0x" ));
104
+ Serial.println (byte, HEX);
105
+ #endif
81
106
return byte;
82
107
}
83
108
84
- void BLEStream::flush (void ) {
109
+ void BLEStream::flush (void )
110
+ {
85
111
if (this ->_txCount == 0 ) return ;
86
- if ( this -> _connected ) this ->_txCharacteristic .setValue (this ->_txBuffer , this ->_txCount );
112
+ this ->_txCharacteristic .setValue (this ->_txBuffer , this ->_txCount );
87
113
this ->_flushed = millis ();
88
114
this ->_txCount = 0 ;
89
- #ifdef BLE_SERIAL_DEBUG
90
- Serial.println (F (" BLEStream::flush()" ));
91
- #endif
115
+ #ifdef BLE_SERIAL_DEBUG
116
+ Serial.println (F (" BLEStream::flush()" ));
117
+ #endif
92
118
}
93
119
94
- size_t BLEStream::write (uint8_t byte) {
120
+ size_t BLEStream::write (uint8_t byte)
121
+ {
122
+ #ifndef _VARIANT_ARDUINO_101_X_
123
+ BLEPeripheral::poll ();
124
+ #endif
125
+ if (this ->_txCharacteristic .subscribed () == false ) return 0 ;
95
126
this ->_txBuffer [this ->_txCount ++] = byte;
96
127
if (this ->_txCount == sizeof (this ->_txBuffer )) flush ();
97
- #ifdef BLE_SERIAL_DEBUG
98
- Serial.print (F (" BLEStream::write( 0x" ));
99
- Serial.print (byte, HEX);
100
- Serial.println (F (" ) = 1" ));
101
- #endif
128
+ #ifdef BLE_SERIAL_DEBUG
129
+ Serial.print (F (" BLEStream::write( 0x" ));
130
+ Serial.print (byte, HEX);
131
+ Serial.println (F (" ) = 1" ));
132
+ #endif
102
133
return 1 ;
103
134
}
104
135
105
- BLEStream::operator bool () {
136
+ BLEStream::operator bool ()
137
+ {
106
138
bool retval = this ->_connected = BLEPeripheral::connected ();
107
- #ifdef BLE_SERIAL_DEBUG
108
- Serial.print (F (" BLEStream::operator bool() = " ));
109
- Serial.println (retval);
110
- #endif
139
+ #ifdef BLE_SERIAL_DEBUG
140
+ Serial.print (F (" BLEStream::operator bool() = " ));
141
+ Serial.println (retval);
142
+ #endif
111
143
return retval;
112
144
}
113
145
114
- void BLEStream::_received (const unsigned char * data, size_t size) {
115
- for (int i = 0 ; i < size; i++) {
146
+ void BLEStream::_received (const unsigned char * data, size_t size)
147
+ {
148
+ for (size_t i = 0 ; i < size; i++) {
116
149
this ->_rxHead = (this ->_rxHead + 1 ) % sizeof (this ->_rxBuffer );
117
150
this ->_rxBuffer [this ->_rxHead ] = data[i];
118
151
}
119
- #ifdef BLE_SERIAL_DEBUG
120
- Serial.print (F (" BLEStream::received(" ));
121
- for (int i = 0 ; i < size; i++) Serial.print (data[i], HEX);
122
- Serial.println (F (" )" ));
123
- #endif
152
+ #ifdef BLE_SERIAL_DEBUG
153
+ Serial.print (F (" BLEStream::received(" ));
154
+ for (int i = 0 ; i < size; i++) Serial.print (data[i], HEX);
155
+ Serial.println (F (" )" ));
156
+ #endif
124
157
}
125
158
126
- void BLEStream::_received (BLECentral& /* central*/ , BLECharacteristic& rxCharacteristic) {
159
+ void BLEStream::_received (BLECentral& /* central*/ , BLECharacteristic& rxCharacteristic)
160
+ {
127
161
BLEStream::_instance->_received (rxCharacteristic.value (), rxCharacteristic.valueLength ());
128
162
}
0 commit comments