add receive logic

This commit is contained in:
Joerg Lehmann 2019-10-03 20:23:11 +02:00
parent 6884d7f1ac
commit 58de0909b3
1 changed files with 65 additions and 84 deletions

View File

@ -76,11 +76,6 @@ enum {
PIN_SHT10_DATA = 12, // XSDA0 == D12 PIN_SHT10_DATA = 12, // XSDA0 == D12
}; };
// the cycle time to use
unsigned gTxCycle;
// remaining before we reset to default
unsigned gTxCycleCount;
// forwards // forwards
static void settleDoneCb(osjob_t* pSendJob); static void settleDoneCb(osjob_t* pSendJob);
static void warmupDoneCb(osjob_t* pSendJob); static void warmupDoneCb(osjob_t* pSendJob);
@ -88,7 +83,6 @@ static void txNotProvisionedCb(osjob_t *pSendJob);
static void sleepDoneCb(osjob_t* pSendJob); static void sleepDoneCb(osjob_t* pSendJob);
static Arduino_LoRaWAN::SendBufferCbFn sendBufferDoneCb; static Arduino_LoRaWAN::SendBufferCbFn sendBufferDoneCb;
static Arduino_LoRaWAN::ReceivePortBufferCbFn receiveMessage; static Arduino_LoRaWAN::ReceivePortBufferCbFn receiveMessage;
void setTxCycleTime(unsigned txCycle, unsigned txCount);
// Additional Commands // Additional Commands
// forward reference to the command function // forward reference to the command function
@ -135,7 +129,7 @@ sMyExtraCommands_top(
| |
\****************************************************************************/ \****************************************************************************/
static const int32_t fwVersion = 20190913; static const int32_t fwVersion = 20190926;
static const byte MAX_VALUES_TO_SEND = 8; static const byte MAX_VALUES_TO_SEND = 8;
static const uint8_t LORA_DATA_VERSION = 1; static const uint8_t LORA_DATA_VERSION = 1;
static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 129; static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 129;
@ -339,8 +333,6 @@ void setup_platform(void)
} }
gLoRaWAN.SetReceiveBufferBufferCb(receiveMessage); gLoRaWAN.SetReceiveBufferBufferCb(receiveMessage);
setTxCycleTime(CATCFG_T_CYCLE_INITIAL, CATCFG_INTERVAL_COUNT_INITIAL);
gCatena.registerObject(&gLoRaWAN); gCatena.registerObject(&gLoRaWAN);
/* find the platform */ /* find the platform */
@ -590,7 +582,7 @@ void DoDeepSleep()
gSPI2.end(); gSPI2.end();
// Now sleeping... // Now sleeping...
gCatena.Sleep(CATCFG_T_INTERVAL - 5); gCatena.Sleep(CATCFG_T_INTERVAL);
// Recover from wakeup... // Recover from wakeup...
Serial.begin(); Serial.begin();
@ -626,7 +618,7 @@ void ReadSensors(bool firstTime, bool readOnly)
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f)); gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
} }
fUsbPower = (vBus > 3.0) ? true : false; fUsbPower = (vBus > 4.3) ? true : false;
// Setup Scales // Setup Scales
@ -740,6 +732,19 @@ void ReadSensors(bool firstTime, bool readOnly)
gCatena.SafePrintf("startSendingUplink()\n"); gCatena.SafePrintf("startSendingUplink()\n");
} }
startSendingUplink(firstTime); startSendingUplink(firstTime);
gLed.Set(LedPattern::TwoShort);
uint32_t deepSleepDelay = 10;
for (auto n = deepSleepDelay; n > 0; --n)
{
uint32_t tNow = millis();
while (uint32_t(millis() - tNow) < 1000)
{
gCatena.poll();
yield();
}
}
} else { } else {
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("now going to sleep for 6 minutes...\n"); gCatena.SafePrintf("now going to sleep for 6 minutes...\n");
@ -751,12 +756,15 @@ void ReadSensors(bool firstTime, bool readOnly)
//Serial.flush(); //Serial.flush();
} }
gLed.Set(LedPattern::Sleeping); gLed.Set(LedPattern::Sleeping);
if (!fUsbPower) {
DoDeepSleep();
sleepDoneCb(&sensorJob);
} else {
os_setTimedCallback( os_setTimedCallback(
&sensorJob, &sensorJob,
os_getTime() + sec2osticks(CATCFG_T_INTERVAL), os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb); sleepDoneCb);
if (!fUsbPower) {
DoDeepSleep();
} }
} }
} }
@ -821,8 +829,7 @@ static void sendBufferDoneCb(
// but prevent join attempts now. // but prevent join attempts now.
gLoRaWAN.Shutdown(); gLoRaWAN.Shutdown();
} }
else else if (config_data.debug_level > 0) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("send buffer failed\n"); gCatena.SafePrintf("send buffer failed\n");
} }
} }
@ -882,8 +889,14 @@ static void warmupDoneCb(osjob_t* pJob)
static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage, size_t nMessage) static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage, size_t nMessage)
{ {
unsigned txCycle; union {
unsigned txCount; byte buf[9];
struct {
byte dummy;
int32_t gram_A;
int32_t gram_B;
} value;
} bytearray;
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("receiveMessage was called!!!\n"); gCatena.SafePrintf("receiveMessage was called!!!\n");
@ -909,36 +922,11 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
// Terry ^^ // Terry ^^
} }
if (! (port == 1 && 2 <= nMessage && nMessage <= 3))
{
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("invalid message port(%02x)/length(%x)\n", gCatena.SafePrintf("message received port(%02x)/length(%x)\n",
port, nMessage port, nMessage
); );
}
return;
}
txCycle = (pMessage[0] << 8) | pMessage[1];
if (txCycle < CATCFG_T_MIN || txCycle > CATCFG_T_MAX)
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("tx cycle time out of range: %u\n", txCycle);
}
return;
}
// byte [2], if present, is the repeat count.
// explicitly sending zero causes it to stick.
txCount = CATCFG_INTERVAL_COUNT;
if (nMessage >= 3)
{
txCount = pMessage[2];
}
// we print out the received message... // we print out the received message...
if (config_data.debug_level > 2) {
gCatena.SafePrintf("Received Data (Payload): \n"); gCatena.SafePrintf("Received Data (Payload): \n");
for (byte i = 0; i < nMessage; i++) { for (byte i = 0; i < nMessage; i++) {
gCatena.SafePrintf("%c", pMessage[i]); gCatena.SafePrintf("%c", pMessage[i]);
@ -946,26 +934,19 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
gCatena.SafePrintf("\n"); gCatena.SafePrintf("\n");
} }
setTxCycleTime(txCycle, txCount); if (port == 1 && nMessage == 1) {
if (pMessage[0] == 0) {
gCatena.SafePrintf("reset both scales to zero\n");
}
} }
void setTxCycleTime(unsigned txCycle, unsigned txCount) if (port == 1 && nMessage == 9) {
{ if (pMessage[0] == 1) {
if (config_data.debug_level > 2) { memcpy(bytearray.buf, pMessage, 9);
if (txCount > 0) gCatena.SafePrintf("tare scales, A: %d, B: %d\n", bytearray.value.gram_A, bytearray.value.gram_A);
gCatena.SafePrintf( }
"message cycle time %u seconds for %u messages\n",
txCycle, txCount
);
else
gCatena.SafePrintf(
"message cycle time %u seconds indefinitely\n",
txCycle
);
} }
gTxCycle = txCycle;
gTxCycleCount = txCount;
} }
/* process "application hello" -- args are ignored */ /* process "application hello" -- args are ignored */