Skip to content

Commit

Permalink
Merge pull request #1 from mohake-keshavarzi/main
Browse files Browse the repository at this point in the history
SoftwareSerial dependency removed. Upgraded to support C++11
  • Loading branch information
nthnn authored May 7, 2024
2 parents 7437ef8 + b5e7e12 commit 429d6a0
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
.vscode
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...
}
Expand Down
3 changes: 2 additions & 1 deletion examples/apn_example/apn_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
3 changes: 2 additions & 1 deletion examples/board_info/board_info.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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("-----------------------------------------"));
Expand Down
3 changes: 2 additions & 1 deletion examples/card_info/card_info.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion examples/dial_up/dial_up.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion examples/handshake/handshake.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
3 changes: 2 additions & 1 deletion examples/network_op/network_op.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
3 changes: 2 additions & 1 deletion examples/phonebook_capacity/phonebook_capacity.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
3 changes: 2 additions & 1 deletion examples/phonebook_example/phonebook_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion examples/rtc_example/rtc_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion examples/signal_strength/signal_strength.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
3 changes: 2 additions & 1 deletion examples/sms_send_example/sms_send_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
40 changes: 18 additions & 22 deletions src/sim900.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
* THE SOFTWARE.
*/

#include <sim900.h>
#include <SoftwareSerial.h>
#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;
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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 + ";");
Expand Down Expand Up @@ -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<SIM900OperatorMode>(0);
simOperator.format = static_cast<SIM900OperatorFormat>(0);
simOperator.name = "";

this->sendCommand(F("AT+COPS?"));
Expand All @@ -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;
Expand Down Expand Up @@ -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<SIM900PhonebookType>(0);

String response = this->queryResult();
response = response.substring(response.indexOf(',') + 1);
Expand All @@ -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<SIM900PhonebookType>(type);
else accountInfo.numberType = static_cast<SIM900PhonebookType>(0);

accountInfo.name = response.substring(delim2 + 2, response.length() - 2);
return accountInfo;
Expand Down Expand Up @@ -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<SIM900PhonebookType>(0);

return account;
}
Expand Down
7 changes: 3 additions & 4 deletions src/sim900.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
#define SIM900_H

#include <Arduino.h>
#include <SoftwareSerial.h>

#include <sim900_defs.h>
#include "sim900_defs.h"

/**
*
Expand All @@ -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;
Expand Down Expand Up @@ -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);

/**
*
Expand Down
51 changes: 51 additions & 0 deletions src/sim900_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<SIM900OperatorFormat>(i);
}

/**
*
* @enum SIM900OperatorMode
Expand Down Expand Up @@ -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<SIM900OperatorMode>(i);
}

/**
*
* @enum SIM900CardService
Expand Down Expand Up @@ -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<SIM900CardService>(i);
}

/**
*
* @enum SIM900PhonebookType
Expand Down

0 comments on commit 429d6a0

Please sign in to comment.