diff --git a/keywords.txt b/keywords.txt index a193389..0f775fa 100644 --- a/keywords.txt +++ b/keywords.txt @@ -45,6 +45,8 @@ getIMEI KEYWORD2 isConnected KEYWORD2 passThruI2Cread KEYWORD2 passThruI2Cwrite KEYWORD2 +configureSleepPin KEYWORD2 +setSleepPin KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/library.properties b/library.properties index 352130b..57f59f7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=IridiumSBDi2c -version=3.0.2 +version=3.0.3 author=Mikal Hart and Paul Clark (PaulZC) maintainer=SparkFun Electronics sentence=This library supports satellite data transmissions from anywhere on earth using the RockBLOCK family of Iridium 9602 and 9603 modems. diff --git a/src/IridiumSBD.cpp b/src/IridiumSBD.cpp index f8370b9..508e143 100644 --- a/src/IridiumSBD.cpp +++ b/src/IridiumSBD.cpp @@ -741,7 +741,7 @@ int IridiumSBD::internalSendReceiveSBD(const char *txTxtMessage, const uint8_t * diagprint(moCode); diagprint(F("\r\n")); - if (moCode >= 0 && moCode <= 4) // this range indicates successful return! + if (moCode <= 4) // this range indicates successful return! { diagprint(F("SBDIX success!\r\n")); @@ -1101,7 +1101,11 @@ void IridiumSBD::power(bool on) } else { - pinMode(this->sleepPin, OUTPUT); // Make the sleep pin an output + if (this->sleepPinConfigured == false) + { + configureSleepPin(); + this->sleepPinConfigured = true; + } } } @@ -1110,7 +1114,7 @@ void IridiumSBD::power(bool on) diagprint(F("Powering on modem...\r\n")); if (this->useSerial) { - digitalWrite(this->sleepPin, HIGH); // HIGH = awake + setSleepPin(HIGH); // HIGH = awake } else { @@ -1118,7 +1122,6 @@ void IridiumSBD::power(bool on) } lastPowerOnTime = millis(); } - else { // Best Practices Guide suggests waiting at least 2 seconds @@ -1130,7 +1133,7 @@ void IridiumSBD::power(bool on) diagprint(F("Powering off modem...\r\n")); if (this->useSerial) { - digitalWrite(this->sleepPin, LOW); // LOW = asleep + setSleepPin(LOW); // LOW = asleep } else { @@ -1139,6 +1142,22 @@ void IridiumSBD::power(bool on) } } +void IridiumSBD::configureSleepPin() +{ + pinMode(this->sleepPin, OUTPUT); // Make the sleep pin an output + diagprint(F("configureSleepPin: sleepPin configured\r\n")); +} + +void IridiumSBD::setSleepPin(uint8_t enable) +{ + digitalWrite(this->sleepPin, enable); // HIGH = awake, LOW = asleep + diagprint(F("setSleepPin: sleepPin set ")); + if (enable == HIGH) + diagprint(F("HIGH\r\n")); + else + diagprint(F("LOW\r\n")); +} + void IridiumSBD::send(FlashString str, bool beginLine, bool endLine) { if (beginLine) @@ -1393,17 +1412,12 @@ void IridiumSBD::check9603data() wireport->beginTransmission((uint8_t)deviceaddress); // Talk to the I2C device wireport->write(LEN_REG); // Point to the serial buffer length wireport->endTransmission(); // Send data and release the bus (the 841 (WireS) doesn't like it if the Master holds the bus!) - wireport->requestFrom((uint8_t)deviceaddress, 2); // Request two bytes - if (wireport->available() >= 2) + if (wireport->requestFrom((uint8_t)deviceaddress, (uint8_t)2) == 2) // Request two bytes { uint8_t msb = wireport->read(); uint8_t lsb = wireport->read(); bytesAvailable = (((uint16_t)msb) << 8) | lsb; } - while (wireport->available()) - { - wireport->read(); // Mop up any unexpected bytes - } //Now read the serial bytes (if any) if (bytesAvailable > 0) @@ -1416,14 +1430,14 @@ void IridiumSBD::check9603data() wireport->endTransmission(); // Send data and release the bus (the 841 (WireS) doesn't like it if the Master holds the bus!) while (bytesAvailable > SER_PACKET_SIZE) // If there are _more_ than SER_PACKET_SIZE bytes to be read { - wireport->requestFrom((uint8_t)deviceaddress, SER_PACKET_SIZE, false); // Request SER_PACKET_SIZE bytes, don't release the bus + wireport->requestFrom((uint8_t)deviceaddress, (uint8_t)SER_PACKET_SIZE, (uint8_t)false); // Request SER_PACKET_SIZE bytes, don't release the bus while (wireport->available()) { i2cSerPoke(wireport->read()); // Read and store each byte } bytesAvailable -= SER_PACKET_SIZE; // Decrease the number of bytes available by SER_PACKET_SIZE } - wireport->requestFrom((uint8_t)deviceaddress, bytesAvailable); // Request remaining bytes, release the bus + wireport->requestFrom((uint8_t)deviceaddress, (uint8_t)bytesAvailable); // Request remaining bytes, release the bus while (wireport->available()) { i2cSerPoke(wireport->read()); // Read and store each byte @@ -1441,15 +1455,10 @@ void IridiumSBD::check9603pins() wireport->beginTransmission((uint8_t)deviceaddress); // Talk to the I2C device wireport->write(IO_REG); // Point to the 'IO register' wireport->endTransmission(); // Send data and release the bus (the 841 (WireS) doesn't like it if the Master holds the bus!) - wireport->requestFrom((uint8_t)deviceaddress, 1); // Request one byte from the IO register - if (wireport->available()) + if (wireport->requestFrom((uint8_t)deviceaddress, (uint8_t)1) == 1) // Request one byte from the IO register { IO_REGISTER = wireport->read(); // Read the IO register } - while (wireport->available()) - { - wireport->read(); // Mop up any unexpected bytes (hopefully redundant!?) - } } //Set the IO pins @@ -1476,17 +1485,12 @@ int IridiumSBD::internalPassThruI2Cread(uint8_t *rxBuffer, size_t &rxBufferSize, wireport->beginTransmission((uint8_t)deviceaddress); // Talk to the I2C device wireport->write(LEN_REG); // Point to the serial buffer length wireport->endTransmission(); // Send data and release the bus (the 841 (WireS) doesn't like it if the Master holds the bus!) - wireport->requestFrom((uint8_t)deviceaddress, 2); // Request two bytes - if (wireport->available() >= 2) + if (wireport->requestFrom((uint8_t)deviceaddress, (uint8_t)2) == 2) // Request two bytes { uint8_t msb = wireport->read(); uint8_t lsb = wireport->read(); bytesAvailable = (((uint16_t)msb) << 8) | lsb; } - while (wireport->available()) - { - wireport->read(); // Mop up any unexpected bytes - } numBytes = (size_t)bytesAvailable; //Store bytesAvailable in numBytes @@ -1503,7 +1507,7 @@ int IridiumSBD::internalPassThruI2Cread(uint8_t *rxBuffer, size_t &rxBufferSize, wireport->endTransmission(); // Send data and release the bus (the 841 (WireS) doesn't like it if the Master holds the bus!) while (bytesAvailable > SER_PACKET_SIZE) // If there are _more_ than SER_PACKET_SIZE bytes to be read { - wireport->requestFrom((uint8_t)deviceaddress, SER_PACKET_SIZE, false); // Request SER_PACKET_SIZE bytes, don't release the bus + wireport->requestFrom((uint8_t)deviceaddress, (uint8_t)SER_PACKET_SIZE, (uint8_t)false); // Request SER_PACKET_SIZE bytes, don't release the bus while (wireport->available()) { uint8_t dbyte = wireport->read(); // Read a byte @@ -1515,7 +1519,7 @@ int IridiumSBD::internalPassThruI2Cread(uint8_t *rxBuffer, size_t &rxBufferSize, } bytesAvailable -= SER_PACKET_SIZE; // Decrease the number of bytes available by SER_PACKET_SIZE } - wireport->requestFrom((uint8_t)deviceaddress, bytesAvailable); // Request remaining bytes, release the bus + wireport->requestFrom((uint8_t)deviceaddress, (uint8_t)bytesAvailable); // Request remaining bytes, release the bus while (wireport->available()) { uint8_t dbyte = wireport->read(); // Read a byte diff --git a/src/IridiumSBD.h b/src/IridiumSBD.h index 68bf393..b5d57ff 100644 --- a/src/IridiumSBD.h +++ b/src/IridiumSBD.h @@ -134,6 +134,10 @@ class IridiumSBD int passThruI2Cread(uint8_t *rxBuffer, size_t &rxBufferSize, size_t &numBytes); int passThruI2Cwrite(uint8_t *txBuffer, size_t &txBufferSize); + // Weak functions to configure and set the sleep pin - user can overwrite with a custom functions if required + void configureSleepPin() __attribute__((weak)); + void setSleepPin(uint8_t enable) __attribute__((weak)); + IridiumSBD(Stream &str, int sleepPinNo = -1, int ringPinNo = -1) { useSerial = true; @@ -146,6 +150,7 @@ class IridiumSBD asleep = true; reentrant = false; sleepPin = sleepPinNo; + sleepPinConfigured = false; ringPin = ringPinNo; msstmWorkaroundRequested = true; ringAlertsEnabled = {ringPinNo != -1}; @@ -175,6 +180,7 @@ class IridiumSBD asleep = true; reentrant = false; sleepPin = -1; + sleepPinConfigured = false; ringPin = -1; msstmWorkaroundRequested = false; ringAlertsEnabled = true; @@ -218,6 +224,7 @@ class IridiumSBD bool asleep; bool reentrant; int sleepPin; + bool sleepPinConfigured; int ringPin; bool msstmWorkaroundRequested; bool ringAlertsEnabled;