Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove blocking code and optimize for sleep #66

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 58 additions & 18 deletions src/MKRWAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,15 @@ class LoRaModem : public Stream
pollInterval = secs * 1000;
}

void poll() {
if (millis() - lastPollTime < pollInterval) return;
lastPollTime = millis();
/**
* @brief Poll modem for status of a confirmed packet
*
* @return int >0 indicates packet sent. <0 indicates LORA_ERROR.
*/
int poll() {
// simply trigger a fake write
uint8_t dummy = 0;
modemSend(&dummy, 1, true);
return modemSend(&dummy, 1, true);
}

bool factoryDefault() {
Expand Down Expand Up @@ -704,6 +707,55 @@ class LoRaModem : public Stream
return fcd;
}

/**
* @brief Trigger the modem to send received data
* For a Class C device, the module should not send a response if the host MCU (SAMD21) is sleeping.
* Either the host needs to wake on RX or it needs to poll the module for a response.
* This function is used in you want to poll the module for RX data.
* Note, the module firmware should remove the call to at_Receive() in the LoraRxData() function defined in main.c
*
* @return true if we received data from modem
* @return false if no response or LORA_ERROR
*/
bool sendRXData() {
sendAT(GF("+RECV?"));
if (waitResponse(10) != 99) {
return false;
}
return true;
}

/**
* @brief Get the Join Status from the modem
*
* @return int -1: if timeout or LORA_ERROR,
* 0: if not joined
* 1: if joined
*/
int getJoinStatus() {
sendAT(GF("+NJS?"));
if (waitResponse(2000L) != 1) {
return -1;
}
streamSkipUntil('=');
return stream.readStringUntil('\r').toInt();
}

/**
* @brief Get confirmation status of the last AT+SEND (0-1)
* This can be used to poll the status of a confirmed uplink
* Notes: The McpsConfirmed() in lora.c (line 310) callback is called from LoRaMac.c (line 1378) when the mac state is idle.
* Presently, SX1276OnDio0Irq (sx1276.c line 1477) sends the "+ACK" string (line 1647) to the UART when a downlink is received. This is incorrect.
* It may be advantageous to notify the application using McpsConfirmed() when the uplink is confirmed.
* @return int
*/
int getCFS() {
int cfs = -1;
sendAT(GF("+CFS?"));
cfs = waitResponse("0", "1") - 1;
return cfs;
}


private:

Expand Down Expand Up @@ -777,11 +829,6 @@ class LoRaModem : public Stream
if (waitResponse() != 1) {
return -1;
}
if (confirmed) {
if (waitResponse(10000L, "+ACK") != 1) {
return -1;
}
}
return len;
}

Expand All @@ -797,15 +844,6 @@ class LoRaModem : public Stream
return stream.readStringUntil('\r').toInt();
}

size_t getJoinStatus() {
sendAT(GF("+NJS?"));
if (waitResponse(2000L) != 1) {
return 0;
}
streamSkipUntil('=');
return stream.readStringUntil('\r').toInt();
}

/* Utilities */
template<typename T>
void streamWrite(T last) {
Expand Down Expand Up @@ -879,6 +917,8 @@ class LoRaModem : public Stream
i++;
}
}
index = 99;
goto finish;
}
}
} while (millis() - startMillis < timeout);
Expand Down