Skip to content

Commit 6ce3fbe

Browse files
fix previousMillis val. minor BLEStream improvements
1 parent 92eed41 commit 6ce3fbe

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

examples/StandardFirmataBLE/StandardFirmataBLE.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved.
1212
Copyright (C) 2010-2011 Paul Stoffregen. All rights reserved.
1313
Copyright (C) 2009 Shigeru Kobayashi. All rights reserved.
14-
Copyright (C) 2009-2015 Jeff Hoefs. All rights reserved.
14+
Copyright (C) 2009-2016 Jeff Hoefs. All rights reserved.
1515
1616
This library is free software; you can redistribute it and/or
1717
modify it under the terms of the GNU Lesser General Public
@@ -20,7 +20,7 @@
2020
2121
See file LICENSE.txt for further informations on licensing terms.
2222
23-
Last updated by Jeff Hoefs: December 26th, 2015
23+
Last updated by Jeff Hoefs: January 3rd, 2015
2424
*/
2525

2626
#include <Servo.h>
@@ -30,6 +30,9 @@
3030
#include <CurieBle.h>
3131
#include "utility/BLEStream.h"
3232

33+
//#define SERIAL_DEBUG
34+
#include "utility/firmataDebug.h"
35+
3336
#define I2C_WRITE B00000000
3437
#define I2C_READ B00001000
3538
#define I2C_READ_CONTINUOUSLY B00010000
@@ -718,6 +721,8 @@ void systemResetCallback()
718721

719722
void setup()
720723
{
724+
DEBUG_BEGIN(9600);
725+
721726
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
722727

723728
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
@@ -745,12 +750,12 @@ void loop()
745750
byte pin, analogPin;
746751

747752
// do not process data if no BLE connection is established
753+
// poll will send the TX buffer every 30ms while it contains data
748754
if (!stream.poll()) return;
749755

750756
/* DIGITALREAD - as fast as possible, check for changes and output them to the
751757
* Stream buffer using Stream.write() */
752758
checkDigitalInputs();
753-
//stream.flush(); // send digital input values immediately
754759

755760
/* STREAMREAD - processing incoming messagse as soon as possible, while still
756761
* checking digital inputs. */
@@ -759,7 +764,7 @@ void loop()
759764

760765
currentMillis = millis();
761766
if (currentMillis - previousMillis > samplingInterval) {
762-
previousMillis += samplingInterval;
767+
previousMillis = currentMillis;
763768
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
764769
for (pin = 0; pin < TOTAL_PINS; pin++) {
765770
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == PIN_MODE_ANALOG) {
@@ -775,6 +780,5 @@ void loop()
775780
readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX);
776781
}
777782
}
778-
//stream.flush();
779783
}
780784
}

utility/BLEStream.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "BLEStream.h"
99

10-
// #define BLE_SERIAL_DEBUG
10+
//#define BLE_SERIAL_DEBUG
1111

1212
BLEStream* BLEStream::_instance = NULL;
1313

@@ -38,7 +38,7 @@ void BLEStream::begin(...) {
3838

3939
bool BLEStream::poll() {
4040
this->_connected = BLEPeripheral::connected();
41-
if (millis() > this->_flushed + 100) flush();
41+
if (millis() > this->_flushed + BLESTREAM_TXBUFFER_FLUSH_INTERVAL) flush();
4242
return this->_connected;
4343
}
4444

@@ -52,8 +52,10 @@ void BLEStream::end() {
5252
int BLEStream::available(void) {
5353
int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer);
5454
#ifdef BLE_SERIAL_DEBUG
55-
Serial.print(F("BLEStream::available() = "));
56-
Serial.println(retval);
55+
if (retval > 0) {
56+
Serial.print(F("BLEStream::available() = "));
57+
Serial.println(retval);
58+
}
5759
#endif
5860
return retval;
5961
}
@@ -62,9 +64,7 @@ int BLEStream::peek(void) {
6264
if (this->_rxTail == this->_rxHead) return -1;
6365
unsigned char byte = this->_rxBuffer[this->_rxTail];
6466
#ifdef BLE_SERIAL_DEBUG
65-
Serial.print(F("BLEStream::peek() = "));
66-
Serial.print((char) byte);
67-
Serial.print(F(" 0x"));
67+
Serial.print(F("BLEStream::peek() = 0x"));
6868
Serial.println(byte, HEX);
6969
#endif
7070
return byte;
@@ -75,9 +75,7 @@ int BLEStream::read(void) {
7575
this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer);
7676
unsigned char byte = this->_rxBuffer[this->_rxTail];
7777
#ifdef BLE_SERIAL_DEBUG
78-
Serial.print(F("BLEStream::read() = "));
79-
Serial.print((char) byte);
80-
Serial.print(F(" 0x"));
78+
Serial.print(F("BLEStream::read() = 0x"));
8179
Serial.println(byte, HEX);
8280
#endif
8381
return byte;
@@ -97,9 +95,7 @@ size_t BLEStream::write(uint8_t byte) {
9795
this->_txBuffer[this->_txCount++] = byte;
9896
if (this->_txCount == sizeof(this->_txBuffer)) flush();
9997
#ifdef BLE_SERIAL_DEBUG
100-
Serial.print(F("BLEStream::write("));
101-
Serial.print((char) byte);
102-
Serial.print(F(" 0x"));
98+
Serial.print(F("BLEStream::write( 0x"));
10399
Serial.print(byte, HEX);
104100
Serial.println(F(") = 1"));
105101
#endif
@@ -122,7 +118,7 @@ void BLEStream::_received(const unsigned char* data, size_t size) {
122118
}
123119
#ifdef BLE_SERIAL_DEBUG
124120
Serial.print(F("BLEStream::received("));
125-
for (int i = 0; i < size; i++) Serial.print((char)data[i]);
121+
for (int i = 0; i < size; i++) Serial.print(data[i], HEX);
126122
Serial.println(F(")"));
127123
#endif
128124
}

utility/BLEStream.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <Arduino.h>
1212
#include <CurieBle.h>
1313

14+
#define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 30
15+
1416
class BLEStream : public BLEPeripheral, public Stream
1517
{
1618
public:

0 commit comments

Comments
 (0)