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

@ -33,7 +33,7 @@ using namespace McciCatena;
| |
| MANIFEST CONSTANTS & TYPEDEFS | MANIFEST CONSTANTS & TYPEDEFS
| |
\****************************************************************************/ \****************************************************************************/
constexpr uint8_t kUplinkPort = 2; constexpr uint8_t kUplinkPort = 2;
@ -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;
@ -152,8 +146,8 @@ static const uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 10; // when weight value dr
typedef struct { typedef struct {
long cal_w1_0; // 4 Bytes, Wert Waegezelle 1 ohne Gewicht long cal_w1_0; // 4 Bytes, Wert Waegezelle 1 ohne Gewicht
long cal_w2_0; // 4 Bytes, Wert Waegezelle 2 ohne Gewicht long cal_w2_0; // 4 Bytes, Wert Waegezelle 2 ohne Gewicht
float cal_w1_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1 float cal_w1_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1
float cal_w2_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2 float cal_w2_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2
byte debug_level; // 0 => no debugging, 1 => infos, 2 => error, 3 => highest level byte debug_level; // 0 => no debugging, 1 => infos, 2 => error, 3 => highest level
byte fill[122]; byte fill[122];
} __attribute__((packed)) CONFIG_data; } __attribute__((packed)) CONFIG_data;
@ -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);
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb);
if (!fUsbPower) { if (!fUsbPower) {
DoDeepSleep(); DoDeepSleep();
sleepDoneCb(&sensorJob);
} else {
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb);
} }
} }
} }
@ -821,10 +829,9 @@ 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"); }
}
} }
os_setTimedCallback( os_setTimedCallback(
@ -835,15 +842,15 @@ static void sendBufferDoneCb(
} }
static void txNotProvisionedCb( static void txNotProvisionedCb(
osjob_t *pSendJob osjob_t *pSendJob
) )
{ {
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n"); gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n");
} }
gLoRaWAN.Shutdown(); gLoRaWAN.Shutdown();
gLed.Set(LedPattern::NotProvisioned); gLed.Set(LedPattern::NotProvisioned);
} }
static void settleDoneCb( static void settleDoneCb(
@ -882,13 +889,19 @@ 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");
} }
if (config_data.debug_level > 2) { if (config_data.debug_level > 2) {
// Terry vv // Terry vv
if (port == 0) if (port == 0)
@ -909,63 +922,31 @@ 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) {
{ gCatena.SafePrintf("message received port(%02x)/length(%x)\n",
if (config_data.debug_level > 0) { port, nMessage
gCatena.SafePrintf("invalid message port(%02x)/length(%x)\n", );
port, nMessage // we print out the received message...
);
}
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...
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]);
} }
gCatena.SafePrintf("\n"); gCatena.SafePrintf("\n");
} }
setTxCycleTime(txCycle, txCount);
}
void setTxCycleTime(unsigned txCycle, unsigned txCount) if (port == 1 && nMessage == 1) {
{ if (pMessage[0] == 0) {
if (config_data.debug_level > 2) { gCatena.SafePrintf("reset both scales to zero\n");
if (txCount > 0) }
gCatena.SafePrintf( }
"message cycle time %u seconds for %u messages\n",
txCycle, txCount if (port == 1 && nMessage == 9) {
); if (pMessage[0] == 1) {
else memcpy(bytearray.buf, pMessage, 9);
gCatena.SafePrintf( gCatena.SafePrintf("tare scales, A: %d, B: %d\n", bytearray.value.gram_A, bytearray.value.gram_A);
"message cycle time %u seconds indefinitely\n", }
txCycle
);
} }
gTxCycle = txCycle;
gTxCycleCount = txCount;
} }
/* process "application hello" -- args are ignored */ /* process "application hello" -- args are ignored */
@ -1080,7 +1061,7 @@ cCommandStream::CommandStatus cmdCalibrateScaleB(cCommandStream *pThis, void *pC
cCommandStream::CommandStatus cmdSetDebugLevel(cCommandStream *pThis, void *pContext, int argc, char **argv) cCommandStream::CommandStatus cmdSetDebugLevel(cCommandStream *pThis, void *pContext, int argc, char **argv)
{ {
String s_debug_level(argv[1]); String s_debug_level(argv[1]);
config_data.debug_level = s_debug_level.toInt(); config_data.debug_level = s_debug_level.toInt();
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data)); gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
@ -1093,7 +1074,7 @@ cCommandStream::CommandStatus cmdGetDebugLevel(cCommandStream *pThis, void *pCon
{ {
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data)); gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
pThis->printf("{ \"msg\": \"debug_level is %d\" }\n",config_data.debug_level); pThis->printf("{ \"msg\": \"debug_level is %d\" }\n", config_data.debug_level);
return cCommandStream::CommandStatus::kSuccess; return cCommandStream::CommandStatus::kSuccess;
} }