Rozhrani pro detekci RING a CLIP z vetsi casti funkcni

This commit is contained in:
Pavel Brychta 2023-09-03 19:06:43 +02:00
parent 78bd8a981d
commit c2d0707100
4 changed files with 67 additions and 12 deletions

View File

@ -16,7 +16,7 @@ void DTE::SendCommand(const char * command, unsigned long timeout, const char *
mTimeout = timeout;
flush();
mStream.print(command);
mBuffer = "";
clearBuffer();
mTick = millis();
process();
}
@ -34,7 +34,7 @@ void DTE::SendCommand(const __FlashStringHelper * command, unsigned long timeout
flush();
// send command
mStream.print((const __FlashStringHelper *) command);
mBuffer = "";
clearBuffer();
mTick = millis();
process();
}
@ -120,3 +120,25 @@ void DTE::process()
// time out
mResult = EXPECT_TIMEOUT;
}
bool DTE::buffer()
{
while (mStream.available() > 0 && mBuffer.length() < mBufferSize) {
char c = mStream.read();
if (c >= 0x20) {
mBuffer.concat(c);
} else if (c == '\r')
return true;
}
if (mBuffer.length() == mBufferSize)
clearBuffer();
return false;
}
void DTE::clearBuffer()
{
mBuffer = "";
}

View File

@ -46,6 +46,10 @@ class DTE {
String & getBuffer();
bool buffer();
void clearBuffer();
protected:
void flush();

View File

@ -1,4 +1,5 @@
#include "ThreadedGSM.h"
#include <trace.h>
#ifdef THREADEDGSM_DEBUG
#define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print(x)
@ -203,15 +204,19 @@ void ThreadedGSM::Startup()
case STARTUP_CHK_CENG:
if (mDte.getResult() == DTE::EXPECT_RESULT) {
mRequests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY));
clearReq(REQ_STARTUP);
for (unsigned long & i : tickSync)
i = millis();
if (mConfiguration.ready != nullptr)
mConfiguration.ready(*this);
mDte.SendCommand(F("AT+CLIP=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
mState = STARTUP_CHK_CLIP;
} else
mState = STARTUP_POWER_OFF;
break;
case STARTUP_CHK_CLIP:
if (mDte.getResult() == DTE::EXPECT_RESULT) {
startupDone();
} else {
mState = STARTUP_POWER_OFF;
}
break;
}
if (mState != lastState) {
DEBUG_PRINT(F("STARTUP_STATE: "));
@ -333,6 +338,7 @@ void ThreadedGSM::Battery()
void ThreadedGSM::clearReq(int req)
{
mRequests &= ~(req);
mDte.clearBuffer();
nextJob();
}
@ -370,9 +376,10 @@ void ThreadedGSM::Inbox()
case READ_CHK_CMGL:
if (mDte.getResult() == DTE::EXPECT_RESULT) {
// fetch index
int indexStart = mDte.getBuffer().indexOf(F("+CMGL: "));
String & buffer = mDte.getBuffer();
int indexStart = buffer.indexOf(F("+CMGL: "));
if (indexStart >= 0) {
Message.Index = mDte.getBuffer().substring(indexStart + 7, mDte.getBuffer().indexOf(F(","))).toInt();
Message.Index = buffer.substring(indexStart + 7, buffer.indexOf(F(","))).toInt();
if (Message.Index != 0) {
mDte.Delay(2000);
mState = READ_DELAY_CLEAR_BUFF;
@ -381,7 +388,6 @@ void ThreadedGSM::Inbox()
}
if (mState != READ_DELAY_CLEAR_BUFF)
clearReq(REQ_INBOX);
break;
case READ_DELAY_CLEAR_BUFF:
@ -493,8 +499,18 @@ void ThreadedGSM::Outbox()
void ThreadedGSM::CheckRing()
{
int lastState = mRingState;
switch (mRingState) {
case RING_WAIT:
if (mDte.buffer()) {
String & buffer = mDte.getBuffer();
if (buffer.length() > 0) {
TRACE(TRACE_INFO, F("Gsm: %s"), buffer.c_str());
mDte.clearBuffer();
}
}
break;
case RING_CHK:
@ -510,3 +526,13 @@ void ThreadedGSM::CheckRing()
DEBUG_PRINTLN(mRingState);
}
}
void ThreadedGSM::startupDone()
{
mRequests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY));
clearReq(REQ_STARTUP);
for (unsigned long & i : tickSync)
i = millis();
if (mConfiguration.ready != nullptr)
mConfiguration.ready(*this);
}

View File

@ -103,7 +103,8 @@ class ThreadedGSM : public Executable
STARTUP_CHK_CPIN,
STARTUP_CHK_CREG,
STARTUP_CHK_CLTS,
STARTUP_CHK_CENG
STARTUP_CHK_CENG,
STARTUP_CHK_CLIP
};
enum StatesClock {
@ -200,6 +201,8 @@ class ThreadedGSM : public Executable
void sendSMS(String & Number, const char * Text);
protected:
void startupDone();
// States
void Startup();