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
|
\****************************************************************************/
\****************************************************************************/
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;
@ -152,8 +146,8 @@ static const uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 10; // when weight value dr
typedef struct {
long cal_w1_0; // 4 Bytes, Wert Waegezelle 1 ohne Gewicht
long cal_w2_0; // 4 Bytes, Wert Waegezelle 2 ohne Gewicht
float cal_w1_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1
float cal_w2_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2
float cal_w1_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1
float cal_w2_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2
byte debug_level; // 0 => no debugging, 1 => infos, 2 => error, 3 => highest level
byte fill[122];
} __attribute__((packed)) CONFIG_data;
@ -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);
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb);
if (!fUsbPower) {
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.
gLoRaWAN.Shutdown();
}
else
if (config_data.debug_level > 0) {
gCatena.SafePrintf("send buffer failed\n");
}
else if (config_data.debug_level > 0) {
gCatena.SafePrintf("send buffer failed\n");
}
}
os_setTimedCallback(
@ -835,15 +842,15 @@ 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);
}
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,13 +889,19 @@ 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");
}
if (config_data.debug_level > 2) {
// Terry vv
if (port == 0)
@ -909,63 +922,31 @@ 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",
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) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("message received port(%02x)/length(%x)\n",
port, nMessage
);
// we print out the received message...
gCatena.SafePrintf("Received Data (Payload): \n");
for (byte i = 0; i < nMessage; i++) {
gCatena.SafePrintf("%c", pMessage[i]);
}
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 */
@ -1080,7 +1061,7 @@ cCommandStream::CommandStatus cmdCalibrateScaleB(cCommandStream *pThis, void *pC
cCommandStream::CommandStatus cmdSetDebugLevel(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
String s_debug_level(argv[1]);
config_data.debug_level = s_debug_level.toInt();
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));
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;
}