diff --git a/.gitignore b/.gitignore index 496ee2c..2608ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 4399790..c72bcba 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,8 @@ SoftwareSerial shieldSerial(7, 8); //RX and TX void setup() { Serial.begin(9600); // Initialize serial communication - SIM900 sim900(&shieldSerial); // Initialize the SIM900 shield + shieldSerial.begin(9600) // Initialize shield communication + SIM900 sim900(shieldSerial); // Initialize the SIM900 shield // Your code goes here... } diff --git a/examples/apn_example/apn_example.ino b/examples/apn_example/apn_example.ino index 2dd1ddd..085bff9 100644 --- a/examples/apn_example/apn_example.ino +++ b/examples/apn_example/apn_example.ino @@ -5,7 +5,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); SIM900APN access; access.apn = F(""); diff --git a/examples/board_info/board_info.ino b/examples/board_info/board_info.ino index 2e857b9..d970a94 100644 --- a/examples/board_info/board_info.ino +++ b/examples/board_info/board_info.ino @@ -5,7 +5,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); Serial.println(F("Dumping board informations...")); Serial.println(F("-----------------------------------------")); diff --git a/examples/card_info/card_info.ino b/examples/card_info/card_info.ino index fd1864c..cadf5cd 100644 --- a/examples/card_info/card_info.ino +++ b/examples/card_info/card_info.ino @@ -5,7 +5,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); uint8_t phonebookIndex = 1; SIM900CardAccount accountInfo; diff --git a/examples/dial_up/dial_up.ino b/examples/dial_up/dial_up.ino index 8ebe749..ca45b90 100644 --- a/examples/dial_up/dial_up.ino +++ b/examples/dial_up/dial_up.ino @@ -5,7 +5,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); sim900.dialUp("+XXxxxxxxxxxx"); delay(20000); diff --git a/examples/handshake/handshake.ino b/examples/handshake/handshake.ino index ff05fa3..3857a90 100644 --- a/examples/handshake/handshake.ino +++ b/examples/handshake/handshake.ino @@ -5,7 +5,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); Serial.println( sim900.handshake() ? "Handshaked!" : "Something went wrong." diff --git a/examples/network_op/network_op.ino b/examples/network_op/network_op.ino index 96b4577..52702be 100644 --- a/examples/network_op/network_op.ino +++ b/examples/network_op/network_op.ino @@ -6,7 +6,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); SIM900Operator network = sim900.networkOperator(); Serial.println(F("SIM900 Current Network Operator")); diff --git a/examples/phonebook_capacity/phonebook_capacity.ino b/examples/phonebook_capacity/phonebook_capacity.ino index b2d028d..4ea3cc2 100644 --- a/examples/phonebook_capacity/phonebook_capacity.ino +++ b/examples/phonebook_capacity/phonebook_capacity.ino @@ -6,7 +6,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); SIM900PhonebookCapacity capacity = sim900.phonebookCapacity(); Serial.println(F("Phonebook Capacity")); diff --git a/examples/phonebook_example/phonebook_example.ino b/examples/phonebook_example/phonebook_example.ino index 0cc52ac..c8db376 100644 --- a/examples/phonebook_example/phonebook_example.ino +++ b/examples/phonebook_example/phonebook_example.ino @@ -5,7 +5,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); uint8_t index = 1; SIM900CardAccount account; diff --git a/examples/rtc_example/rtc_example.ino b/examples/rtc_example/rtc_example.ino index a496590..c0da1a6 100644 --- a/examples/rtc_example/rtc_example.ino +++ b/examples/rtc_example/rtc_example.ino @@ -7,7 +7,8 @@ void printRTC(SIM900RTC rtc); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); SIM900RTC rtc; rtc.year = 2; diff --git a/examples/signal_strength/signal_strength.ino b/examples/signal_strength/signal_strength.ino index 913b30d..94076b9 100644 --- a/examples/signal_strength/signal_strength.ino +++ b/examples/signal_strength/signal_strength.ino @@ -6,7 +6,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); SIM900Signal signal = sim900.signal(); Serial.println(F("Signal Strength")); diff --git a/examples/sms_send_example/sms_send_example.ino b/examples/sms_send_example/sms_send_example.ino index 9322972..63c836a 100644 --- a/examples/sms_send_example/sms_send_example.ino +++ b/examples/sms_send_example/sms_send_example.ino @@ -6,7 +6,8 @@ SoftwareSerial shieldSerial(7, 8); void setup() { Serial.begin(9600); - SIM900 sim900(&shieldSerial); + shieldSerial.begin(9600); + SIM900 sim900(shieldSerial); Serial.println( sim900.sendSMS("+XXxxxxxxxxxx", "Hello, world!!") ? "Sent!" : "Not sent." diff --git a/src/sim900.cpp b/src/sim900.cpp index 0f7511f..f4b4f2c 100644 --- a/src/sim900.cpp +++ b/src/sim900.cpp @@ -21,18 +21,17 @@ * THE SOFTWARE. */ -#include -#include +#include "sim900.h" void SIM900::sendCommand(String message) { - this->sim900->println(message); + this->sim900.println(message); } String SIM900::getResponse() { delay(500); - if(this->sim900->available() > 0) { - String response = this->sim900->readString(); + if(this->sim900.available() > 0) { + String response = this->sim900.readString(); response.trim(); return response; @@ -82,10 +81,7 @@ String SIM900::queryResult() { return result; } -SIM900::SIM900(SoftwareSerial *_sim900): - sim900(_sim900) { - this->sim900->begin(9600); -} +SIM900::SIM900(Stream& _sim900):sim900(_sim900){} bool SIM900::handshake() { this->sendCommand(F("AT")); @@ -122,9 +118,9 @@ SIM900Signal SIM900::signal() { return signal; } -void SIM900::close() { - this->sim900->end(); -} +// void SIM900::close() { +// this->sim900->end(); +// } SIM900DialResult SIM900::dialUp(String number) { this->sendCommand("ATD+ " + number + ";"); @@ -194,15 +190,15 @@ bool SIM900::sendSMS(String number, String message) { delay(500); this->sendCommand(message); delay(500); - this->sim900->write(0x1a); + this->sim900.write(0x1a); return this->getReturnedMode().startsWith(">"); } SIM900Operator SIM900::networkOperator() { SIM900Operator simOperator; - simOperator.mode = 0; - simOperator.format = 0; + simOperator.mode = static_cast(0); + simOperator.format = static_cast(0); simOperator.name = ""; this->sendCommand(F("AT+COPS?")); @@ -211,8 +207,8 @@ SIM900Operator SIM900::networkOperator() { uint8_t delim1 = response.indexOf(','), delim2 = response.indexOf(',', delim1 + 1); - simOperator.mode = (uint8_t) response.substring(0, delim1).toInt(); - simOperator.format = (uint8_t) response.substring(delim1 + 1, delim2).toInt(); + simOperator.mode = intToSIM900OperatorMode((uint8_t) response.substring(0, delim1).toInt()); + simOperator.format = intToSIM900OperatorFormat((uint8_t) response.substring(delim1 + 1, delim2).toInt()); simOperator.name = response.substring(delim2 + 2, response.length() - 2); return simOperator; @@ -347,7 +343,7 @@ SIM900CardAccount SIM900::retrievePhonebook(uint8_t index) { this->sendCommand("AT+CPBR=" + String(index)); SIM900CardAccount accountInfo; - accountInfo.numberType = 0; + accountInfo.numberType = static_cast(0); String response = this->queryResult(); response = response.substring(response.indexOf(',') + 1); @@ -359,8 +355,8 @@ SIM900CardAccount SIM900::retrievePhonebook(uint8_t index) { uint8_t type = (uint8_t) response.substring(delim1 + 1, delim2).toInt(); if(type == 129 || type == 145) - accountInfo.numberType = type; - else accountInfo.numberType = 0; + accountInfo.numberType = static_cast(type); + else accountInfo.numberType = static_cast(0); accountInfo.name = response.substring(delim2 + 2, response.length() - 2); return accountInfo; @@ -408,8 +404,8 @@ SIM900CardAccount SIM900::cardNumber() { account.number = response.substring(delim1 + 2, delim2 - 1); account.type = (uint8_t) response.substring(delim2 + 1, delim3).toInt(); account.speed = (uint8_t) response.substring(delim3 + 1, delim4).toInt(); - account.service = (uint8_t) response.substring(delim4 + 1).toInt(); - account.numberType = 0; + account.service = intToSIM900CardService((uint8_t) response.substring(delim4 + 1).toInt()); + account.numberType = static_cast(0); return account; } diff --git a/src/sim900.h b/src/sim900.h index 0ff7e38..fe3489e 100644 --- a/src/sim900.h +++ b/src/sim900.h @@ -25,9 +25,8 @@ #define SIM900_H #include -#include -#include +#include "sim900_defs.h" /** * @@ -42,7 +41,7 @@ class SIM900 { private: /// The SoftwareSerial object used for communication with the SIM900 module. - SoftwareSerial *sim900; + Stream& sim900; /// A flag indicating whether Access Point Name (APN) configuration is set. bool hasAPN = false; @@ -73,7 +72,7 @@ class SIM900 { * @param _sim900 A pointer to the SoftwareSerial object for communication with the SIM900 module. * */ - SIM900(SoftwareSerial *_sim900); + SIM900(Stream& _sim900); /** * diff --git a/src/sim900_defs.h b/src/sim900_defs.h index 0b66606..e5276f9 100644 --- a/src/sim900_defs.h +++ b/src/sim900_defs.h @@ -87,6 +87,23 @@ typedef enum _SIM900OperatorFormat { SIM900_OPERATOR_FORMAT_MANUAL_AUTO } SIM900OperatorFormat; +/** + * @param i Integer input to be casted. + * @return A valid SIM900OperatorFormat value. + * + * @brief A function to safely cast from integer value to SIM900OperatorFormat. Invalid inputs are casted to default value of SIM900_OPERATOR_FORMAT_AUTO. + * + * From C++11 up to newer versions of C++, enumerators are considered as a specific type rather than integer + * values. The same is true for newer Arduino compilers, too. So impilicit cast of integer value to an enum type is no longer allowed. For the sake of the safety of an explicit + * cast is better to first check the range of the input. +*/ +SIM900OperatorFormat intToSIM900OperatorFormat(int i){ + if (i < SIM900OperatorFormat::SIM900_OPERATOR_FORMAT_AUTO || i > SIM900OperatorFormat::SIM900_OPERATOR_FORMAT_MANUAL_AUTO) + return SIM900OperatorFormat::SIM900_OPERATOR_FORMAT_AUTO; + else + return static_cast(i); +} + /** * * @enum SIM900OperatorMode @@ -122,6 +139,23 @@ typedef enum _SIM900OperatorMode { SIM900_OPERATOR_MODE_E_UTRAN } SIM900OperatorMode; +/** + * @param i Integer input to be casted. + * @return A valid SIM900OperatorMode value. + * + * @brief A function to safely cast from integer value to SIM900OperatorMode. Invalid inputs are casted to default value of SIM900_OPERATOR_MODE_GSM. + * + * From C++11 up to newer versions of C++, enumerators are considered as a specific type rather than integer + * values. The same is true for newer Arduino compilers, too. So impilicit cast of integer value to an enum type is no longer allowed. For the sake of the safety of an explicit + * cast is better to first check the range of the input. +*/ +SIM900OperatorMode intToSIM900OperatorMode(int i){ + if (i < SIM900OperatorMode::SIM900_OPERATOR_MODE_GSM || i > SIM900OperatorMode::SIM900_OPERATOR_MODE_E_UTRAN) + return SIM900OperatorMode::SIM900_OPERATOR_MODE_GSM; + else + return static_cast(i); +} + /** * * @enum SIM900CardService @@ -150,6 +184,23 @@ typedef enum _SIM900CardService { SIM900_CARD_SERVICE_FAX } SIM900CardService; +/** + * @param i Integer input to be casted. + * @return A valid SIM900CardService value. + * + * @brief A function to safely cast from integer value to SIM900CardService. Invalid inputs are casted to default value of SIM900_CARD_SERVICE_ASYNC. + * + * From C++11 up to newer versions of C++, enumerators are considered as a specific type rather than integer + * values. The same is true for newer Arduino compilers, too. So impilicit cast of integer value to an enum type is no longer allowed. For the sake of the safety of an explicit + * cast is better to first check the range of the input. +*/ +SIM900CardService intToSIM900CardService(int i){ + if (i < SIM900CardService::SIM900_CARD_SERVICE_ASYNC || i > SIM900CardService::SIM900_CARD_SERVICE_FAX) + return SIM900CardService::SIM900_CARD_SERVICE_ASYNC; + else + return static_cast(i); +} + /** * * @enum SIM900PhonebookType