Optimization

This commit is contained in:
Volodymyr Shymanskyy
2016-12-16 18:25:21 +02:00
parent 87298eacce
commit 7b99fd61dd

View File

@@ -205,6 +205,10 @@ public:
* Basic functions * Basic functions
*/ */
bool begin() { bool begin() {
return init();
}
bool init() {
if (!autoBaud()) { if (!autoBaud()) {
return false; return false;
} }
@@ -218,7 +222,7 @@ public:
bool autoBaud(unsigned long timeout = 10000L) { bool autoBaud(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(""); sendAT(GF(""));
if (waitResponse(200) == 1) { if (waitResponse(200) == 1) {
delay(100); delay(100);
return true; return true;
@@ -254,10 +258,6 @@ public:
*/ */
bool restart() { bool restart() {
return resetSoft();
}
bool resetSoft() {
if (!autoBaud()) { if (!autoBaud()) {
return false; return false;
} }
@@ -270,27 +270,7 @@ public:
return false; return false;
} }
delay(3000); delay(3000);
return begin(); return init();
}
// Reboot the module by setting the specified pin LOW, then HIGH.
// (The pin should be connected to a P-MOSFET)
bool resetHard(int pwrPin) {
powerOff(pwrPin);
delay(100);
return powerOn(pwrPin);
}
void powerOff(int pwrPin) {
pinMode(pwrPin, OUTPUT);
digitalWrite(pwrPin, LOW);
}
bool powerOn(int pwrPin) {
pinMode(pwrPin, OUTPUT);
digitalWrite(pwrPin, HIGH);
delay(3000);
return begin();
} }
/* /*
@@ -334,9 +314,10 @@ public:
RegStatus getRegistrationStatus() { RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?")); sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG: 0,")) != 1) { if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN; return REG_UNKNOWN;
} }
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt(); int status = stream.readStringUntil('\n').toInt();
waitResponse(); waitResponse();
return (RegStatus)status; return (RegStatus)status;
@@ -347,7 +328,7 @@ public:
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return ""; return "";
} }
stream.readStringUntil('"'); // Skip mode and format streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"'); String res = stream.readStringUntil('"');
waitResponse(); waitResponse();
return res; return res;
@@ -493,9 +474,8 @@ private:
if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) { if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) {
return -1; return -1;
} }
stream.readStringUntil(','); streamSkipUntil(','); // Skip mux
String data = stream.readStringUntil('\n'); return stream.readStringUntil('\n').toInt();
return data.toInt();
} }
size_t modemRead(size_t size, uint8_t mux) { size_t modemRead(size_t size, uint8_t mux) {
@@ -510,20 +490,20 @@ private:
return 0; return 0;
} }
#endif #endif
stream.readStringUntil(','); // Skip mode 2/3 streamSkipUntil(','); // Skip mode 2/3
stream.readStringUntil(','); // Skip mux streamSkipUntil(','); // Skip mux
size_t len = stream.readStringUntil(',').toInt(); size_t len = stream.readStringUntil(',').toInt();
sockets[mux]->sock_available = stream.readStringUntil('\n').toInt(); sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();
for (size_t i=0; i<len; i++) { for (size_t i=0; i<len; i++) {
#ifdef GSM_USE_HEX #ifdef GSM_USE_HEX
while (stream.available() < 2) { delay(1); } while (stream.available() < 2) {}
char buf[4] = { 0, }; char buf[4] = { 0, };
buf[0] = stream.read(); buf[0] = stream.read();
buf[1] = stream.read(); buf[1] = stream.read();
char c = strtol(buf, NULL, 16); char c = strtol(buf, NULL, 16);
#else #else
while (stream.available() < 1) { delay(1); } while (!stream.available()) {}
char c = stream.read(); char c = stream.read();
#endif #endif
sockets[mux]->rx.put(c); sockets[mux]->rx.put(c);
@@ -535,16 +515,11 @@ private:
size_t modemGetAvailable(uint8_t mux) { size_t modemGetAvailable(uint8_t mux) {
sendAT(GF("+CIPRXGET=4,"), mux); sendAT(GF("+CIPRXGET=4,"), mux);
size_t result = 0; size_t result = 0;
for (byte i = 0; i < 2; i++) { if (waitResponse(GF("+CIPRXGET:")) == 1) {
int res = waitResponse(GF("+CIPRXGET:"), GFP(GSM_OK), GFP(GSM_ERROR)); streamSkipUntil(','); // Skip mode 4
if (res == 1) { streamSkipUntil(','); // Skip mux
stream.readStringUntil(','); // Skip mode 4
stream.readStringUntil(','); // Skip mux
result = stream.readStringUntil('\n').toInt(); result = stream.readStringUntil('\n').toInt();
} else if (res == 2) { waitResponse();
} else {
return result;
}
} }
if (!result) { if (!result) {
sockets[mux]->sock_connected = modemGetConnected(mux); sockets[mux]->sock_connected = modemGetConnected(mux);
@@ -573,6 +548,15 @@ private:
int streamRead() { return stream.read(); } int streamRead() { return stream.read(); }
bool streamSkipUntil(char c) { //TODO: timeout
while (true) {
while (!stream.available()) {}
if (stream.read() == c)
return true;
}
return false;
}
template<typename... Args> template<typename... Args>
void sendAT(Args... cmd) { void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL); streamWrite("AT", cmd..., GSM_NL);