Skip to content

Commit a3b3f65

Browse files
fix issue where BLEPeripheral lib needed to be included even if not needed
1 parent 872e3cc commit a3b3f65

File tree

2 files changed

+178
-177
lines changed

2 files changed

+178
-177
lines changed

utility/BLEStream.cpp

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

utility/BLEStream.h

+177-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Based on BLESerial.cpp by Voita Molda
55
https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/examples/serial/BLESerial.h
66
7-
Last updated March 13th, 2016
7+
Last updated April 04th, 2016
88
*/
99

1010
#ifndef _BLE_STREAM_H_
@@ -22,6 +22,8 @@
2222
#define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80
2323
#define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer
2424

25+
// #define BLE_SERIAL_DEBUG
26+
2527
class BLEStream : public BLEPeripheral, public Stream
2628
{
2729
public:
@@ -64,4 +66,178 @@ class BLEStream : public BLEPeripheral, public Stream
6466
static void _received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic);
6567
};
6668

69+
70+
/*
71+
* BLEStream.cpp
72+
* Copied here as a hack to avoid having to install the BLEPeripheral libarary even if it's
73+
* not needed.
74+
*/
75+
76+
BLEStream* BLEStream::_instance = NULL;
77+
78+
BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) :
79+
#if defined(_VARIANT_ARDUINO_101_X_)
80+
BLEPeripheral()
81+
#else
82+
BLEPeripheral(req, rdy, rst)
83+
#endif
84+
{
85+
this->_txCount = 0;
86+
this->_rxHead = this->_rxTail = 0;
87+
this->_flushed = 0;
88+
this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL;
89+
BLEStream::_instance = this;
90+
91+
addAttribute(this->_uartService);
92+
addAttribute(this->_uartNameDescriptor);
93+
setAdvertisedServiceUuid(this->_uartService.uuid());
94+
addAttribute(this->_rxCharacteristic);
95+
addAttribute(this->_rxNameDescriptor);
96+
this->_rxCharacteristic.setEventHandler(BLEWritten, BLEStream::_received);
97+
addAttribute(this->_txCharacteristic);
98+
addAttribute(this->_txNameDescriptor);
99+
}
100+
101+
void BLEStream::begin(...)
102+
{
103+
BLEPeripheral::begin();
104+
#ifdef BLE_SERIAL_DEBUG
105+
Serial.println(F("BLEStream::begin()"));
106+
#endif
107+
}
108+
109+
bool BLEStream::poll()
110+
{
111+
// BLEPeripheral::poll is called each time connected() is called
112+
this->_connected = BLEPeripheral::connected();
113+
if (millis() > this->_flushed + this->_flushInterval) {
114+
flush();
115+
}
116+
return this->_connected;
117+
}
118+
119+
void BLEStream::end()
120+
{
121+
this->_rxCharacteristic.setEventHandler(BLEWritten, NULL);
122+
this->_rxHead = this->_rxTail = 0;
123+
flush();
124+
BLEPeripheral::disconnect();
125+
}
126+
127+
int BLEStream::available(void)
128+
{
129+
// BLEPeripheral::poll only calls delay(1) in CurieBLE so skipping it here to avoid the delay
130+
#ifndef _VARIANT_ARDUINO_101_X_
131+
// TODO Need to do more testing to determine if all of these calls to BLEPeripheral::poll are
132+
// actually necessary. Seems to run fine without them, but only minimal testing so far.
133+
BLEPeripheral::poll();
134+
#endif
135+
int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer);
136+
#ifdef BLE_SERIAL_DEBUG
137+
if (retval > 0) {
138+
Serial.print(F("BLEStream::available() = "));
139+
Serial.println(retval);
140+
}
141+
#endif
142+
return retval;
143+
}
144+
145+
int BLEStream::peek(void)
146+
{
147+
#ifndef _VARIANT_ARDUINO_101_X_
148+
BLEPeripheral::poll();
149+
#endif
150+
if (this->_rxTail == this->_rxHead) return -1;
151+
uint8_t byte = this->_rxBuffer[this->_rxTail];
152+
#ifdef BLE_SERIAL_DEBUG
153+
Serial.print(F("BLEStream::peek() = 0x"));
154+
Serial.println(byte, HEX);
67155
#endif
156+
return byte;
157+
}
158+
159+
int BLEStream::read(void)
160+
{
161+
#ifndef _VARIANT_ARDUINO_101_X_
162+
BLEPeripheral::poll();
163+
#endif
164+
if (this->_rxTail == this->_rxHead) return -1;
165+
this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer);
166+
uint8_t byte = this->_rxBuffer[this->_rxTail];
167+
#ifdef BLE_SERIAL_DEBUG
168+
Serial.print(F("BLEStream::read() = 0x"));
169+
Serial.println(byte, HEX);
170+
#endif
171+
return byte;
172+
}
173+
174+
void BLEStream::flush(void)
175+
{
176+
if (this->_txCount == 0) return;
177+
#ifndef _VARIANT_ARDUINO_101_X_
178+
// ensure there are available packets before sending
179+
while(!this->_txCharacteristic.canNotify()) {
180+
BLEPeripheral::poll();
181+
}
182+
#endif
183+
this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount);
184+
this->_flushed = millis();
185+
this->_txCount = 0;
186+
#ifdef BLE_SERIAL_DEBUG
187+
Serial.println(F("BLEStream::flush()"));
188+
#endif
189+
}
190+
191+
size_t BLEStream::write(uint8_t byte)
192+
{
193+
#ifndef _VARIANT_ARDUINO_101_X_
194+
BLEPeripheral::poll();
195+
#endif
196+
if (this->_txCharacteristic.subscribed() == false) return 0;
197+
this->_txBuffer[this->_txCount++] = byte;
198+
if (this->_txCount == sizeof(this->_txBuffer)) flush();
199+
#ifdef BLE_SERIAL_DEBUG
200+
Serial.print(F("BLEStream::write( 0x"));
201+
Serial.print(byte, HEX);
202+
Serial.println(F(") = 1"));
203+
#endif
204+
return 1;
205+
}
206+
207+
BLEStream::operator bool()
208+
{
209+
bool retval = this->_connected = BLEPeripheral::connected();
210+
#ifdef BLE_SERIAL_DEBUG
211+
Serial.print(F("BLEStream::operator bool() = "));
212+
Serial.println(retval);
213+
#endif
214+
return retval;
215+
}
216+
217+
void BLEStream::setFlushInterval(int interval)
218+
{
219+
if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) {
220+
this->_flushInterval = interval;
221+
}
222+
}
223+
224+
void BLEStream::_received(const unsigned char* data, size_t size)
225+
{
226+
for (size_t i = 0; i < size; i++) {
227+
this->_rxHead = (this->_rxHead + 1) % sizeof(this->_rxBuffer);
228+
this->_rxBuffer[this->_rxHead] = data[i];
229+
}
230+
#ifdef BLE_SERIAL_DEBUG
231+
Serial.print(F("BLEStream::received("));
232+
for (int i = 0; i < size; i++) Serial.print(data[i], HEX);
233+
Serial.println(F(")"));
234+
#endif
235+
}
236+
237+
void BLEStream::_received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic)
238+
{
239+
BLEStream::_instance->_received(rxCharacteristic.value(), rxCharacteristic.valueLength());
240+
}
241+
242+
243+
#endif // _BLE_STREAM_H_

0 commit comments

Comments
 (0)