@@ -665,9 +665,12 @@ uint16_t DevUBLOXGNSS::available()
665665}
666666// For I2C, ping the _address
667667// Not Applicable for SPI and Serial
668- uint16_t DevUBLOXGNSS::ping()
668+ bool DevUBLOXGNSS::ping()
669669{
670- return _sfeBus->ping();
670+ if (!lock()) return false;
671+ bool ok = _sfeBus->ping();
672+ unlock();
673+ return ok;
671674}
672675// For Serial, do Serial.write
673676// For I2C, push data to register 0xFF. Chunkify if necessary. Prevent single byte writes as these are illegal
@@ -915,7 +918,7 @@ bool DevUBLOXGNSS::isConnected(uint16_t maxWait)
915918
916919// Enable or disable the printing of sent/response HEX values.
917920// Use this in conjunction with 'Transport Logging' from the Universal Reader Assistant to see what they're doing that we're not
918- void DevUBLOXGNSS::enableDebugging(Stream &debugPort, bool printLimitedDebug)
921+ void DevUBLOXGNSS::enableDebugging(Print &debugPort, bool printLimitedDebug)
919922{
920923 _debugSerial.init(debugPort); // Grab which port the user wants us to use for debugging
921924 if (printLimitedDebug == false)
@@ -1014,13 +1017,16 @@ bool DevUBLOXGNSS::checkUblox(uint8_t requestedClass, uint8_t requestedID)
10141017// PRIVATE: Called regularly to check for available bytes on the user' specified port
10151018bool DevUBLOXGNSS::checkUbloxInternal(ubxPacket *incomingUBX, uint8_t requestedClass, uint8_t requestedID)
10161019{
1020+ if (!lock()) return false;
1021+ bool ok = false;
10171022 if (_commType == COMM_TYPE_I2C)
1018- return (checkUbloxI2C(incomingUBX, requestedClass, requestedID));
1023+ ok = (checkUbloxI2C(incomingUBX, requestedClass, requestedID));
10191024 else if (_commType == COMM_TYPE_SERIAL)
1020- return (checkUbloxSerial(incomingUBX, requestedClass, requestedID));
1025+ ok = (checkUbloxSerial(incomingUBX, requestedClass, requestedID));
10211026 else if (_commType == COMM_TYPE_SPI)
1022- return (checkUbloxSpi(incomingUBX, requestedClass, requestedID));
1023- return false;
1027+ ok = (checkUbloxSpi(incomingUBX, requestedClass, requestedID));
1028+ unlock();
1029+ return ok;
10241030}
10251031
10261032// Polls I2C for data, passing any new bytes to process()
@@ -1510,7 +1516,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
15101516
15111517 // If user has assigned an output port then pipe the characters there,
15121518 // but only if the port is different (otherwise we'll output each character twice!)
1513- if (_outputPort._serialPort != _ubxOutputPort._serialPort )
1519+ if (_outputPort != _ubxOutputPort)
15141520 _ubxOutputPort.write(incoming); // Echo this byte to the serial port
15151521
15161522 // Finally, increment the frame counter
@@ -1571,7 +1577,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
15711577 processNMEA(nmeaAddressField[i]); // Process the start character and address field
15721578 // If user has assigned an output port then pipe the characters there,
15731579 // but only if the port is different (otherwise we'll output each character twice!)
1574- if (_outputPort._serialPort != _nmeaOutputPort._serialPort )
1580+ if (_outputPort != _nmeaOutputPort)
15751581 _nmeaOutputPort.write(nmeaAddressField[i]); // Echo this byte to the serial port
15761582 }
15771583 }
@@ -1606,7 +1612,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
16061612 processNMEA(incoming); // Pass incoming to processNMEA
16071613 // If user has assigned an output port then pipe the characters there,
16081614 // but only if the port is different (otherwise we'll output each character twice!)
1609- if (_outputPort._serialPort != _nmeaOutputPort._serialPort )
1615+ if (_outputPort != _nmeaOutputPort)
16101616 _nmeaOutputPort.write(incoming); // Echo this byte to the serial port
16111617 }
16121618 }
@@ -1706,7 +1712,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
17061712
17071713 // If user has assigned an output port then pipe the characters there,
17081714 // but only if the port is different (otherwise we'll output each character twice!)
1709- if (_outputPort._serialPort != _rtcmOutputPort._serialPort )
1715+ if (_outputPort != _rtcmOutputPort)
17101716 _rtcmOutputPort.write(incoming); // Echo this byte to the serial port
17111717 }
17121718}
@@ -4173,6 +4179,7 @@ sfe_ublox_status_e DevUBLOXGNSS::sendCommand(ubxPacket *outgoingUBX, uint16_t ma
41734179 }
41744180#endif
41754181
4182+ if (!lock()) return SFE_UBLOX_STATUS_FAIL;
41764183 if (_commType == COMM_TYPE_I2C)
41774184 {
41784185 retVal = sendI2cCommand(outgoingUBX);
@@ -4195,7 +4202,8 @@ sfe_ublox_status_e DevUBLOXGNSS::sendCommand(ubxPacket *outgoingUBX, uint16_t ma
41954202 {
41964203 sendSpiCommand(outgoingUBX);
41974204 }
4198-
4205+ unlock();
4206+
41994207 if (maxWait > 0)
42004208 {
42014209 // Depending on what we just sent, either we need to look for an ACK or not
@@ -5341,12 +5349,14 @@ bool DevUBLOXGNSS::pushRawData(uint8_t *dataBytes, size_t numDataBytes, bool cal
53415349{
53425350 // Return now if numDataBytes is zero
53435351 if (numDataBytes == 0)
5344- return ( false) ; // Indicate to the user that there was no data to push
5352+ return false; // Indicate to the user that there was no data to push
53455353
5354+ if (!lock()) return false;
5355+ bool ok = false;
53465356 if (_commType == COMM_TYPE_SERIAL)
53475357 {
53485358 // Serial: write all the bytes in one go
5349- return writeBytes(dataBytes, numDataBytes) == numDataBytes;
5359+ ok = writeBytes(dataBytes, numDataBytes) == numDataBytes;
53505360 }
53515361 else if (_commType == COMM_TYPE_I2C)
53525362 {
@@ -5362,55 +5372,56 @@ bool DevUBLOXGNSS::pushRawData(uint8_t *dataBytes, size_t numDataBytes, bool cal
53625372 {
53635373 _pushThisSingleByte = *dataBytes;
53645374 _pushSingleByte = true;
5365- return ( false) ; // Indicate to the user that their data has not been pushed yet
5366- }
5375+ ok = false; // Indicate to the user that their data has not been pushed yet
5376+ } else {
53675377
5368- // I2C: split the data up into packets of i2cTransactionSize
5369- size_t bytesLeftToWrite = numDataBytes;
5370- size_t bytesWrittenTotal = 0;
5378+ // I2C: split the data up into packets of i2cTransactionSize
5379+ size_t bytesLeftToWrite = numDataBytes;
5380+ size_t bytesWrittenTotal = 0;
53715381
5372- if (_pushSingleByte == true) // Increment bytesLeftToWrite if we have a single byte waiting to be pushed
5373- bytesLeftToWrite++;
5382+ if (_pushSingleByte == true) // Increment bytesLeftToWrite if we have a single byte waiting to be pushed
5383+ bytesLeftToWrite++;
53745384
5375- while (bytesLeftToWrite > 0)
5376- {
5377- size_t bytesToWrite; // Limit bytesToWrite to i2cTransactionSize
5385+ while (bytesLeftToWrite > 0)
5386+ {
5387+ size_t bytesToWrite; // Limit bytesToWrite to i2cTransactionSize
53785388
5379- if (bytesLeftToWrite > i2cTransactionSize)
5380- bytesToWrite = i2cTransactionSize;
5381- else
5382- bytesToWrite = bytesLeftToWrite;
5389+ if (bytesLeftToWrite > i2cTransactionSize)
5390+ bytesToWrite = i2cTransactionSize;
5391+ else
5392+ bytesToWrite = bytesLeftToWrite;
53835393
5384- // If there would be one byte left to be written next time, send one byte less now
5385- if ((bytesLeftToWrite - bytesToWrite) == 1)
5386- bytesToWrite--;
5394+ // If there would be one byte left to be written next time, send one byte less now
5395+ if ((bytesLeftToWrite - bytesToWrite) == 1)
5396+ bytesToWrite--;
53875397
5388- size_t bytesWritten = 0;
5398+ size_t bytesWritten = 0;
53895399
5390- if (_pushSingleByte == true)
5391- {
5392- uint8_t buf[i2cTransactionSize];
5400+ if (_pushSingleByte == true)
5401+ {
5402+ uint8_t buf[i2cTransactionSize];
53935403
5394- buf[0] = _pushThisSingleByte;
5404+ buf[0] = _pushThisSingleByte;
53955405
5396- for (uint16_t x = 1; x < bytesToWrite; x++)
5397- buf[x] = dataBytes[x - 1];
5406+ for (uint16_t x = 1; x < bytesToWrite; x++)
5407+ buf[x] = dataBytes[x - 1];
53985408
5399- bytesWritten += writeBytes(buf, bytesToWrite); // Write the bytes
5400- dataBytes += bytesToWrite - 1; // Point to fresh data
5401- _pushSingleByte = false; // Clear the flag
5402- }
5403- else
5404- {
5405- bytesWritten += writeBytes(dataBytes, bytesToWrite); // Write the bytes
5406- dataBytes += bytesToWrite; // Point to fresh data
5409+ bytesWritten += writeBytes(buf, bytesToWrite); // Write the bytes
5410+ dataBytes += bytesToWrite - 1; // Point to fresh data
5411+ _pushSingleByte = false; // Clear the flag
5412+ }
5413+ else
5414+ {
5415+ bytesWritten += writeBytes(dataBytes, bytesToWrite); // Write the bytes
5416+ dataBytes += bytesToWrite; // Point to fresh data
5417+ }
5418+
5419+ bytesWrittenTotal += bytesWritten; // Update the totals
5420+ bytesLeftToWrite -= bytesToWrite;
54075421 }
54085422
5409- bytesWrittenTotal += bytesWritten; // Update the totals
5410- bytesLeftToWrite -= bytesToWrite;
5423+ ok = (bytesWrittenTotal == numDataBytes); // Return true if the correct number of bytes were written
54115424 }
5412-
5413- return (bytesWrittenTotal == numDataBytes); // Return true if the correct number of bytes were written
54145425 }
54155426 else if (_commType == COMM_TYPE_SPI)
54165427 {
@@ -5446,9 +5457,10 @@ bool DevUBLOXGNSS::pushRawData(uint8_t *dataBytes, size_t numDataBytes, bool cal
54465457 processSpiBuffer(&packetCfg, 0, 0); // This will hopefully prevent any lost data?
54475458 }
54485459
5449- return ( true) ;
5460+ ok = true;
54505461 }
5451- return false;
5462+ unlock();
5463+ return ok;
54525464}
54535465
54545466// Push MGA AssistNow data to the module.
@@ -6610,24 +6622,24 @@ bool DevUBLOXGNSS::setSPIInput(uint8_t comSettings, uint8_t layer, uint16_t maxW
66106622}
66116623
66126624// Want to see the NMEA messages on the Serial port? Here's how
6613- void DevUBLOXGNSS::setNMEAOutputPort(Stream &outputPort)
6625+ void DevUBLOXGNSS::setNMEAOutputPort(Print &outputPort)
66146626{
66156627 _nmeaOutputPort.init(outputPort); // Store the port from user
66166628}
66176629
66186630// Want to see the RTCM messages on the Serial port? Here's how
6619- void DevUBLOXGNSS::setRTCMOutputPort(Stream &outputPort)
6631+ void DevUBLOXGNSS::setRTCMOutputPort(Print &outputPort)
66206632{
66216633 _rtcmOutputPort.init(outputPort); // Store the port from user
66226634}
66236635
66246636// Want to see the UBX messages on the Serial port? Here's how
6625- void DevUBLOXGNSS::setUBXOutputPort(Stream &outputPort)
6637+ void DevUBLOXGNSS::setUBXOutputPort(Print &outputPort)
66266638{
66276639 _ubxOutputPort.init(outputPort); // Store the port from user
66286640}
66296641
6630- void DevUBLOXGNSS::setOutputPort(Stream &outputPort)
6642+ void DevUBLOXGNSS::setOutputPort(Print &outputPort)
66316643{
66326644 _outputPort.init(outputPort); // Store the port from user
66336645}
0 commit comments