Skip to content

Commit d34d29a

Browse files
committed
Added support for generic Stream class
1 parent f5030a2 commit d34d29a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Adafruit_GPS.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ Adafruit_GPS::Adafruit_GPS(HardwareSerial *ser) {
202202
gpsHwSerial = ser; // ...override gpsHwSerial with value passed.
203203
}
204204

205+
/**************************************************************************/
206+
/*!
207+
@brief Constructor when using Stream
208+
@param data Pointer to a Stream object
209+
*/
210+
/**************************************************************************/
211+
Adafruit_GPS::Adafruit_GPS(Stream *data) {
212+
common_init(); // Set everything to common state, then...
213+
gpsStream = data; // ...override gpsStream with value passed.
214+
}
215+
205216
/**************************************************************************/
206217
/*!
207218
@brief Constructor when using I2C
@@ -246,6 +257,7 @@ void Adafruit_GPS::common_init(void) {
246257
gpsSwSerial = NULL; // Set both to NULL, then override correct
247258
#endif
248259
gpsHwSerial = NULL; // port pointer in corresponding constructor
260+
gpsStream = NULL; // port pointer in corresponding constructor
249261
gpsI2C = NULL;
250262
gpsSPI = NULL;
251263
recvdflag = false;
@@ -298,6 +310,9 @@ size_t Adafruit_GPS::available(void) {
298310
if (gpsHwSerial) {
299311
return gpsHwSerial->available();
300312
}
313+
if (gpsStream) {
314+
return gpsStream->available();
315+
}
301316
if (gpsI2C || gpsSPI) {
302317
return 1; // I2C/SPI doesnt have 'availability' so always has a byte at
303318
// least to read!
@@ -322,6 +337,9 @@ size_t Adafruit_GPS::write(uint8_t c) {
322337
if (gpsHwSerial) {
323338
return gpsHwSerial->write(c);
324339
}
340+
if (gpsStream) {
341+
return gpsStream->write(c);
342+
}
325343
if (gpsI2C) {
326344
gpsI2C->beginTransmission(_i2caddr);
327345
if (gpsI2C->write(c) != 1) {
@@ -379,6 +397,11 @@ char Adafruit_GPS::read(void) {
379397
return c;
380398
c = gpsHwSerial->read();
381399
}
400+
if (gpsStream) {
401+
if (!gpsStream->available())
402+
return c;
403+
c = gpsStream->read();
404+
}
382405
if (gpsI2C) {
383406
if (_buff_idx <= _buff_max) {
384407
c = _i2cbuffer[_buff_idx];

src/Adafruit_GPS.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Adafruit_GPS : public Print {
8787
Adafruit_GPS(SoftwareSerial *ser); // Constructor when using SoftwareSerial
8888
#endif
8989
Adafruit_GPS(HardwareSerial *ser); // Constructor when using HardwareSerial
90+
Adafruit_GPS(Stream *data); // Constructor when using Stream
9091
Adafruit_GPS(TwoWire *theWire); // Constructor when using I2C
9192
Adafruit_GPS(SPIClass *theSPI, int8_t cspin); // Constructor when using SPI
9293
Adafruit_GPS(); // Constructor for no communications, just data storage
@@ -286,6 +287,7 @@ class Adafruit_GPS : public Print {
286287
#endif
287288
bool noComms = false;
288289
HardwareSerial *gpsHwSerial;
290+
Stream *gpsStream;
289291
TwoWire *gpsI2C;
290292
SPIClass *gpsSPI;
291293
int8_t gpsSPI_cs = -1;

0 commit comments

Comments
 (0)