Skip to content

Commit 38de26f

Browse files
committed
use Print instead of Stream
1 parent a7eed4d commit 38de26f

File tree

3 files changed

+55
-49
lines changed

3 files changed

+55
-49
lines changed

src/sfe_bus.h

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -187,66 +187,70 @@ namespace SparkFun_UBLOX_GNSS
187187
Stream *_serialPort;
188188
};
189189

190-
// The sfeStream device defines behavior for Serial diagnostic prints based around the Stream class.
190+
// The sfePrint device defines behavior for Serial diagnostic prints based around the Stream class.
191191
// This is Arduino specific.
192-
class SfeStream
192+
class SfePrint
193193
{
194194
public:
195-
SfeStream(void) { _serialPort = nullptr; }
195+
SfePrint(void) { _outputPort = nullptr; }
196196

197-
void init(Stream &serialPort) { _serialPort = &serialPort; }
197+
void init(Print &outputPort) { _outputPort = &outputPort; }
198+
inline bool operator==(SfePrint const &other) const { return _outputPort == other._outputPort; }
199+
inline bool operator!=(SfePrint const &other) const { return !(*this == other); }
200+
198201
void write(uint8_t c)
199202
{
200-
if (_serialPort != nullptr)
201-
_serialPort->write(c);
203+
if (_outputPort != nullptr)
204+
_outputPort->write(c);
202205
}
203206
void print(const char *c)
204207
{
205-
if (_serialPort != nullptr)
206-
_serialPort->print(c);
208+
if (_outputPort != nullptr)
209+
_outputPort->print(c);
207210
}
208211
void print(const __FlashStringHelper *c)
209212
{
210-
if (_serialPort != nullptr)
211-
_serialPort->print(c);
213+
if (_outputPort != nullptr)
214+
_outputPort->print(c);
212215
}
213216
void print(unsigned int c, int f)
214217
{
215-
if (_serialPort != nullptr)
216-
_serialPort->print(c, f);
218+
if (_outputPort != nullptr)
219+
_outputPort->print(c, f);
217220
}
218221
void print(uint16_t c)
219222
{
220-
if (_serialPort != nullptr)
221-
_serialPort->print(c);
223+
if (_outputPort != nullptr)
224+
_outputPort->print(c);
222225
}
223226
void println()
224227
{
225-
if (_serialPort != nullptr)
226-
_serialPort->println();
228+
if (_outputPort != nullptr)
229+
_outputPort->println();
227230
}
228231
void println(const char *c)
229232
{
230-
if (_serialPort != nullptr)
231-
_serialPort->println(c);
233+
if (_outputPort != nullptr)
234+
_outputPort->println(c);
232235
}
233236
void println(const __FlashStringHelper *c)
234237
{
235-
if (_serialPort != nullptr)
236-
_serialPort->println(c);
238+
if (_outputPort != nullptr)
239+
_outputPort->println(c);
237240
}
238241
void println(size_t c)
239242
{
240-
if (_serialPort != nullptr)
241-
_serialPort->println(c);
243+
if (_outputPort != nullptr)
244+
_outputPort->println(c);
242245
}
243246
void println(uint8_t c, int f)
244247
{
245-
if (_serialPort != nullptr)
246-
_serialPort->println(c, f);
248+
if (_outputPort != nullptr)
249+
_outputPort->println(c, f);
247250
}
248-
249-
Stream *_serialPort; // friend doesn't seem to work... Leave this public
251+
252+
private:
253+
Print *_outputPort;
250254
};
251255

252256
};

src/u-blox_GNSS.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ bool DevUBLOXGNSS::isConnected(uint16_t maxWait)
918918

919919
// Enable or disable the printing of sent/response HEX values.
920920
// Use this in conjunction with 'Transport Logging' from the Universal Reader Assistant to see what they're doing that we're not
921-
void DevUBLOXGNSS::enableDebugging(Stream &debugPort, bool printLimitedDebug)
921+
void DevUBLOXGNSS::enableDebugging(Print &debugPort, bool printLimitedDebug)
922922
{
923923
_debugSerial.init(debugPort); // Grab which port the user wants us to use for debugging
924924
if (printLimitedDebug == false)
@@ -1516,7 +1516,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
15161516

15171517
// If user has assigned an output port then pipe the characters there,
15181518
// but only if the port is different (otherwise we'll output each character twice!)
1519-
if (_outputPort._serialPort != _ubxOutputPort._serialPort)
1519+
if (_outputPort != _ubxOutputPort)
15201520
_ubxOutputPort.write(incoming); // Echo this byte to the serial port
15211521

15221522
// Finally, increment the frame counter
@@ -1577,7 +1577,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
15771577
processNMEA(nmeaAddressField[i]); // Process the start character and address field
15781578
// If user has assigned an output port then pipe the characters there,
15791579
// but only if the port is different (otherwise we'll output each character twice!)
1580-
if (_outputPort._serialPort != _nmeaOutputPort._serialPort)
1580+
if (_outputPort != _nmeaOutputPort)
15811581
_nmeaOutputPort.write(nmeaAddressField[i]); // Echo this byte to the serial port
15821582
}
15831583
}
@@ -1612,7 +1612,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
16121612
processNMEA(incoming); // Pass incoming to processNMEA
16131613
// If user has assigned an output port then pipe the characters there,
16141614
// but only if the port is different (otherwise we'll output each character twice!)
1615-
if (_outputPort._serialPort != _nmeaOutputPort._serialPort)
1615+
if (_outputPort != _nmeaOutputPort)
16161616
_nmeaOutputPort.write(incoming); // Echo this byte to the serial port
16171617
}
16181618
}
@@ -1712,7 +1712,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
17121712

17131713
// If user has assigned an output port then pipe the characters there,
17141714
// but only if the port is different (otherwise we'll output each character twice!)
1715-
if (_outputPort._serialPort != _rtcmOutputPort._serialPort)
1715+
if (_outputPort != _rtcmOutputPort)
17161716
_rtcmOutputPort.write(incoming); // Echo this byte to the serial port
17171717
}
17181718
}
@@ -6622,24 +6622,24 @@ bool DevUBLOXGNSS::setSPIInput(uint8_t comSettings, uint8_t layer, uint16_t maxW
66226622
}
66236623

66246624
// Want to see the NMEA messages on the Serial port? Here's how
6625-
void DevUBLOXGNSS::setNMEAOutputPort(Stream &outputPort)
6625+
void DevUBLOXGNSS::setNMEAOutputPort(Print &outputPort)
66266626
{
66276627
_nmeaOutputPort.init(outputPort); // Store the port from user
66286628
}
66296629

66306630
// Want to see the RTCM messages on the Serial port? Here's how
6631-
void DevUBLOXGNSS::setRTCMOutputPort(Stream &outputPort)
6631+
void DevUBLOXGNSS::setRTCMOutputPort(Print &outputPort)
66326632
{
66336633
_rtcmOutputPort.init(outputPort); // Store the port from user
66346634
}
66356635

66366636
// Want to see the UBX messages on the Serial port? Here's how
6637-
void DevUBLOXGNSS::setUBXOutputPort(Stream &outputPort)
6637+
void DevUBLOXGNSS::setUBXOutputPort(Print &outputPort)
66386638
{
66396639
_ubxOutputPort.init(outputPort); // Store the port from user
66406640
}
66416641

6642-
void DevUBLOXGNSS::setOutputPort(Stream &outputPort)
6642+
void DevUBLOXGNSS::setOutputPort(Print &outputPort)
66436643
{
66446644
_outputPort.init(outputPort); // Store the port from user
66456645
}

src/u-blox_GNSS.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class DevUBLOXGNSS
130130
// Flag to indicate if we are connected to UART1 or UART2
131131
// Needed to select the correct config items when enabling a periodic message
132132
bool _UART2 = false; // Default to UART1
133+
// the lock / unlock functions can be used if you have multiple tasks writing to the bus.
134+
// the idea is that in a RTOS you override this class and the two functions in which you take and give a mutex.
133135
virtual bool lock(void) { return true; }
134136
virtual void unlock(void) { }
135137
public:
@@ -181,18 +183,18 @@ class DevUBLOXGNSS
181183
#if defined(USB_VID) // Is the USB Vendor ID defined?
182184
#if (USB_VID == 0x1B4F) // Is this a SparkFun board?
183185
#if !defined(ARDUINO_SAMD51_THING_PLUS) & !defined(ARDUINO_SAMD51_MICROMOD) // If it is not a SAMD51 Thing Plus or SAMD51 MicroMod
184-
void enableDebugging(Stream &debugPort = SerialUSB, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
186+
void enableDebugging(Print &debugPort = SerialUSB, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
185187
#else
186-
void enableDebugging(Stream &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
188+
void enableDebugging(Print &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
187189
#endif
188190
#else
189-
void enableDebugging(Stream &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
191+
void enableDebugging(Print &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
190192
#endif
191193
#else
192-
void enableDebugging(Stream &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
194+
void enableDebugging(Print &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
193195
#endif
194196
#else
195-
void enableDebugging(Stream &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
197+
void enableDebugging(Print &debugPort = Serial, bool printLimitedDebug = false); // Given a port to print to, enable debug messages. Default to all, not limited.
196198
#endif
197199

198200
void disableDebugging(void); // Turn off debug statements
@@ -319,10 +321,10 @@ class DevUBLOXGNSS
319321
bool setUSBInput(uint8_t comSettings, uint8_t layer = VAL_LAYER_RAM_BBR, uint16_t maxWait = kUBLOXGNSSDefaultMaxWait); // Configure USB port to output UBX, NMEA, RTCM3, SPARTN or a combination thereof
320322
bool setSPIInput(uint8_t comSettings, uint8_t layer = VAL_LAYER_RAM_BBR, uint16_t maxWait = kUBLOXGNSSDefaultMaxWait); // Configure SPI port to output UBX, NMEA, RTCM3, SPARTN or a combination thereof
321323

322-
void setNMEAOutputPort(Stream &outputPort); // Sets the internal variable for the port to direct only NMEA characters to
323-
void setRTCMOutputPort(Stream &outputPort); // Sets the internal variable for the port to direct only RTCM characters to
324-
void setUBXOutputPort(Stream &outputPort); // Sets the internal variable for the port to direct only UBX characters to
325-
void setOutputPort(Stream &outputPort); // Sets the internal variable for the port to direct ALL characters to
324+
void setNMEAOutputPort(Print &outputPort); // Sets the internal variable for the port to direct only NMEA characters to
325+
void setRTCMOutputPort(Print &outputPort); // Sets the internal variable for the port to direct only RTCM characters to
326+
void setUBXOutputPort(Print &outputPort); // Sets the internal variable for the port to direct only UBX characters to
327+
void setOutputPort(Print &outputPort); // Sets the internal variable for the port to direct ALL characters to
326328

327329
// Reset to defaults
328330

@@ -1352,11 +1354,11 @@ class DevUBLOXGNSS
13521354
// Variables
13531355
SparkFun_UBLOX_GNSS::GNSSDeviceBus *_sfeBus;
13541356

1355-
SparkFun_UBLOX_GNSS::SfeStream _nmeaOutputPort; // The user can assign an output port to print NMEA sentences if they wish
1356-
SparkFun_UBLOX_GNSS::SfeStream _rtcmOutputPort; // The user can assign an output port to print RTCM sentences if they wish
1357-
SparkFun_UBLOX_GNSS::SfeStream _ubxOutputPort; // The user can assign an output port to print UBX sentences if they wish
1358-
SparkFun_UBLOX_GNSS::SfeStream _outputPort; // The user can assign an output port to print ALL characters to if they wish
1359-
SparkFun_UBLOX_GNSS::SfeStream _debugSerial; // The stream to send debug messages to if enabled
1357+
SparkFun_UBLOX_GNSS::SfePrint _nmeaOutputPort; // The user can assign an output port to print NMEA sentences if they wish
1358+
SparkFun_UBLOX_GNSS::SfePrint _rtcmOutputPort; // The user can assign an output port to print RTCM sentences if they wish
1359+
SparkFun_UBLOX_GNSS::SfePrint _ubxOutputPort; // The user can assign an output port to print UBX sentences if they wish
1360+
SparkFun_UBLOX_GNSS::SfePrint _outputPort; // The user can assign an output port to print ALL characters to if they wish
1361+
SparkFun_UBLOX_GNSS::SfePrint _debugSerial; // The stream to send debug messages to if enabled
13601362
bool _printDebug = false; // Flag to print the serial commands we are sending to the Serial port for debug
13611363
bool _printLimitedDebug = false; // Flag to print limited debug messages. Useful for I2C debugging or high navigation rates
13621364

0 commit comments

Comments
 (0)