add receive logic
This commit is contained in:
parent
6884d7f1ac
commit
58de0909b3
|
|
@ -33,7 +33,7 @@ using namespace McciCatena;
|
|||
|
|
||||
| MANIFEST CONSTANTS & TYPEDEFS
|
||||
|
|
||||
\****************************************************************************/
|
||||
\****************************************************************************/
|
||||
|
||||
constexpr uint8_t kUplinkPort = 2;
|
||||
|
||||
|
|
@ -76,11 +76,6 @@ enum {
|
|||
PIN_SHT10_DATA = 12, // XSDA0 == D12
|
||||
};
|
||||
|
||||
// the cycle time to use
|
||||
unsigned gTxCycle;
|
||||
// remaining before we reset to default
|
||||
unsigned gTxCycleCount;
|
||||
|
||||
// forwards
|
||||
static void settleDoneCb(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 Arduino_LoRaWAN::SendBufferCbFn sendBufferDoneCb;
|
||||
static Arduino_LoRaWAN::ReceivePortBufferCbFn receiveMessage;
|
||||
void setTxCycleTime(unsigned txCycle, unsigned txCount);
|
||||
|
||||
// Additional Commands
|
||||
// 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 uint8_t LORA_DATA_VERSION = 1;
|
||||
static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 129;
|
||||
|
|
@ -339,8 +333,6 @@ void setup_platform(void)
|
|||
}
|
||||
|
||||
gLoRaWAN.SetReceiveBufferBufferCb(receiveMessage);
|
||||
setTxCycleTime(CATCFG_T_CYCLE_INITIAL, CATCFG_INTERVAL_COUNT_INITIAL);
|
||||
|
||||
gCatena.registerObject(&gLoRaWAN);
|
||||
|
||||
/* find the platform */
|
||||
|
|
@ -590,7 +582,7 @@ void DoDeepSleep()
|
|||
gSPI2.end();
|
||||
|
||||
// Now sleeping...
|
||||
gCatena.Sleep(CATCFG_T_INTERVAL - 5);
|
||||
gCatena.Sleep(CATCFG_T_INTERVAL);
|
||||
|
||||
// Recover from wakeup...
|
||||
Serial.begin();
|
||||
|
|
@ -626,7 +618,7 @@ void ReadSensors(bool firstTime, bool readOnly)
|
|||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
|
||||
}
|
||||
fUsbPower = (vBus > 3.0) ? true : false;
|
||||
fUsbPower = (vBus > 4.3) ? true : false;
|
||||
|
||||
// Setup Scales
|
||||
|
||||
|
|
@ -740,6 +732,19 @@ void ReadSensors(bool firstTime, bool readOnly)
|
|||
gCatena.SafePrintf("startSendingUplink()\n");
|
||||
}
|
||||
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 {
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("now going to sleep for 6 minutes...\n");
|
||||
|
|
@ -751,12 +756,15 @@ void ReadSensors(bool firstTime, bool readOnly)
|
|||
//Serial.flush();
|
||||
}
|
||||
gLed.Set(LedPattern::Sleeping);
|
||||
if (!fUsbPower) {
|
||||
DoDeepSleep();
|
||||
sleepDoneCb(&sensorJob);
|
||||
} else {
|
||||
os_setTimedCallback(
|
||||
&sensorJob,
|
||||
os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
|
||||
sleepDoneCb);
|
||||
if (!fUsbPower) {
|
||||
DoDeepSleep();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -821,8 +829,7 @@ static void sendBufferDoneCb(
|
|||
// but prevent join attempts now.
|
||||
gLoRaWAN.Shutdown();
|
||||
}
|
||||
else
|
||||
if (config_data.debug_level > 0) {
|
||||
else if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("send buffer failed\n");
|
||||
}
|
||||
}
|
||||
|
|
@ -836,14 +843,14 @@ static void sendBufferDoneCb(
|
|||
|
||||
static void txNotProvisionedCb(
|
||||
osjob_t *pSendJob
|
||||
)
|
||||
{
|
||||
)
|
||||
{
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n");
|
||||
}
|
||||
gLoRaWAN.Shutdown();
|
||||
gLed.Set(LedPattern::NotProvisioned);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void settleDoneCb(
|
||||
|
|
@ -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)
|
||||
{
|
||||
unsigned txCycle;
|
||||
unsigned txCount;
|
||||
union {
|
||||
byte buf[9];
|
||||
struct {
|
||||
byte dummy;
|
||||
int32_t gram_A;
|
||||
int32_t gram_B;
|
||||
} value;
|
||||
} bytearray;
|
||||
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("receiveMessage was called!!!\n");
|
||||
|
|
@ -909,36 +922,11 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
|
|||
// Terry ^^
|
||||
}
|
||||
|
||||
if (! (port == 1 && 2 <= nMessage && nMessage <= 3))
|
||||
{
|
||||
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
|
||||
);
|
||||
}
|
||||
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");
|
||||
for (byte i = 0; i < nMessage; 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");
|
||||
}
|
||||
|
||||
setTxCycleTime(txCycle, txCount);
|
||||
}
|
||||
|
||||
void setTxCycleTime(unsigned txCycle, unsigned txCount)
|
||||
{
|
||||
if (config_data.debug_level > 2) {
|
||||
if (txCount > 0)
|
||||
gCatena.SafePrintf(
|
||||
"message cycle time %u seconds for %u messages\n",
|
||||
txCycle, txCount
|
||||
);
|
||||
else
|
||||
gCatena.SafePrintf(
|
||||
"message cycle time %u seconds indefinitely\n",
|
||||
txCycle
|
||||
);
|
||||
if (port == 1 && nMessage == 1) {
|
||||
if (pMessage[0] == 0) {
|
||||
gCatena.SafePrintf("reset both scales to zero\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (port == 1 && nMessage == 9) {
|
||||
if (pMessage[0] == 1) {
|
||||
memcpy(bytearray.buf, pMessage, 9);
|
||||
gCatena.SafePrintf("tare scales, A: %d, B: %d\n", bytearray.value.gram_A, bytearray.value.gram_A);
|
||||
}
|
||||
}
|
||||
|
||||
gTxCycle = txCycle;
|
||||
gTxCycleCount = txCount;
|
||||
}
|
||||
|
||||
/* process "application hello" -- args are ignored */
|
||||
|
|
@ -1093,7 +1074,7 @@ cCommandStream::CommandStatus cmdGetDebugLevel(cCommandStream *pThis, void *pCon
|
|||
{
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue