Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
85b0f6db06 | ||
|
|
0c5c673e5e | ||
|
|
e30a8293ac | ||
|
|
174f8b360f |
+50
-239
@@ -697,8 +697,15 @@ void StartNewIteration() {
|
||||
}
|
||||
while (send_in_progress && ((millis() - start_time) < 300000))
|
||||
{
|
||||
gCatena.poll();
|
||||
yield();
|
||||
os_runloop_once();
|
||||
delay(10);
|
||||
}
|
||||
// handle timeout...
|
||||
if (send_in_progress) {
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("looks like we timed out waiting for sending to finish...\n", wait_time);
|
||||
}
|
||||
send_in_progress = false;
|
||||
}
|
||||
wait_time = (uint32_t)((millis() - start_time) / 1000);
|
||||
if (config_data.debug_level > 0) {
|
||||
@@ -724,6 +731,28 @@ void StartNewIteration() {
|
||||
sleep_time_sec = 10;
|
||||
}
|
||||
|
||||
// Before we go to sleep, we'd like to be sure that this is safe!
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("We wait until is is safe to go to sleep...\n");
|
||||
}
|
||||
long loopCount = 0;
|
||||
long prevPrint;
|
||||
while(os_queryTimeCriticalJobs(ms2osticks(8000)) != 0)
|
||||
{
|
||||
loopCount++;
|
||||
os_runloop_once();
|
||||
if(millis() - prevPrint > 1000) {
|
||||
prevPrint = millis();
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("LMIC.opmode: %#x\n", LMIC.opmode);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("Now it is safe to go to sleep\n");
|
||||
}
|
||||
delay(10);
|
||||
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("now going to sleep for %d seconds...\n", sleep_time_sec);
|
||||
if (fUsbPower) {
|
||||
@@ -737,16 +766,23 @@ void StartNewIteration() {
|
||||
}
|
||||
}
|
||||
|
||||
// if we need to periodically reboot, we can do it now...
|
||||
if (uint32_t(millis()) > gRebootMs) {
|
||||
// time to reboot
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("Reached threshold to reboot...\n");
|
||||
Serial.flush();
|
||||
}
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
|
||||
if (!fUsbPower) {
|
||||
DoDeepSleep(sleep_time_sec);
|
||||
if (! stop_iterations) {
|
||||
StartNewIteration();
|
||||
}
|
||||
|
||||
//os_setTimedCallback(
|
||||
// &iterationJob,
|
||||
// os_getTime() + sec2osticks(2),
|
||||
// startNewIterationCb);
|
||||
}
|
||||
else {
|
||||
if (config_data.debug_level > 0) {
|
||||
@@ -783,37 +819,21 @@ void startSendingUplink(bool firstTime)
|
||||
fConfirmed = true;
|
||||
}
|
||||
|
||||
gCatena.poll();
|
||||
os_runloop_once();
|
||||
//gCatena.poll();
|
||||
|
||||
if (firstTime) {
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("SendBuffer firstTime\n");
|
||||
}
|
||||
bool success = gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
|
||||
// we try a second time if not successful...
|
||||
if (! success) {
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("SendBuffer was not successful, we try a second time...\n");
|
||||
}
|
||||
gCatena.poll();
|
||||
delay(500);
|
||||
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
|
||||
}
|
||||
package_counter++;
|
||||
} else {
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("LMIC.opmode just before SendBuffer: %#x\n", LMIC.opmode);
|
||||
gCatena.SafePrintf("SendBuffer not firstTime\n");
|
||||
}
|
||||
bool success = gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
|
||||
// we try a second time if not successful...
|
||||
if (! success) {
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("SendBuffer was not successful, we try a second time...\n");
|
||||
}
|
||||
gCatena.poll();
|
||||
delay(500);
|
||||
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
|
||||
}
|
||||
package_counter++;
|
||||
}
|
||||
|
||||
@@ -826,8 +846,11 @@ static void sendBufferDoneCb(
|
||||
{
|
||||
osjobcb_t pFn;
|
||||
|
||||
send_in_progress = false;
|
||||
|
||||
if (config_data.debug_level > 1) {
|
||||
gLed.Set(LedPattern::Settling);
|
||||
gCatena.SafePrintf("LMIC.opmode in sendBufferDoneCb: %#x\n", LMIC.opmode);
|
||||
}
|
||||
|
||||
pFn = settleDoneCb;
|
||||
@@ -842,7 +865,7 @@ static void sendBufferDoneCb(
|
||||
gLoRaWAN.Shutdown();
|
||||
}
|
||||
else if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("send buffer failed\n");
|
||||
gCatena.SafePrintf("send buffer failed, LMIC.opmode: %#x\n", LMIC.opmode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -870,174 +893,9 @@ static void txNotProvisionedCb(
|
||||
static void settleDoneCb(
|
||||
osjob_t* pSendJob)
|
||||
{
|
||||
const bool fDeepSleep = checkDeepSleep();
|
||||
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("settleDoneCb\n");
|
||||
gCatena.SafePrintf("settleDoneCb - we are at the end of the callback chain!\n");
|
||||
}
|
||||
|
||||
if (config_data.debug_level > 2) {
|
||||
// Terry vv
|
||||
gCatena.SafePrintf("LMIC.rxDelay: %i\n", LMIC.rxDelay);
|
||||
gCatena.SafePrintf("LMIC.dn2Dr: %i\n", LMIC.dn2Dr);
|
||||
gCatena.SafePrintf("LMIC.dn2Freq: %i\n", LMIC.dn2Freq);
|
||||
gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n", LMIC.rx1DrOffset);
|
||||
gCatena.SafePrintf("LMIC.adrAckReq: %i\n", LMIC.adrAckReq);
|
||||
gCatena.SafePrintf("LMIC.adrEnabled: %i\n", LMIC.adrEnabled);
|
||||
// Terry ^^
|
||||
}
|
||||
|
||||
if (uint32_t(millis()) > gRebootMs) {
|
||||
// time to reboot
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
if (! g_fPrintedSleeping)
|
||||
doSleepAlert(fDeepSleep);
|
||||
|
||||
/* count what we're up to */
|
||||
updateSleepCounters();
|
||||
|
||||
if (fDeepSleep)
|
||||
doDeepSleep(pSendJob);
|
||||
else
|
||||
doLightSleep(pSendJob);
|
||||
}
|
||||
|
||||
bool checkDeepSleep(void)
|
||||
{
|
||||
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
|
||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
|
||||
bool fDeepSleep;
|
||||
|
||||
if (fDeepSleepTest)
|
||||
{
|
||||
fDeepSleep = true;
|
||||
}
|
||||
#ifdef USBCON
|
||||
else if (Serial.dtr())
|
||||
{
|
||||
fDeepSleep = false;
|
||||
}
|
||||
#endif
|
||||
else if (gCatena.GetOperatingFlags() &
|
||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDisableDeepSleep))
|
||||
{
|
||||
fDeepSleep = false;
|
||||
}
|
||||
else if ((gCatena.GetOperatingFlags() &
|
||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fUnattended)) != 0)
|
||||
{
|
||||
fDeepSleep = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
fDeepSleep = false;
|
||||
}
|
||||
|
||||
return fDeepSleep;
|
||||
}
|
||||
|
||||
void doSleepAlert(const bool fDeepSleep)
|
||||
{
|
||||
g_fPrintedSleeping = true;
|
||||
|
||||
if (fDeepSleep)
|
||||
{
|
||||
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
|
||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
|
||||
const uint32_t deepSleepDelay = fDeepSleepTest ? 10 : 30;
|
||||
if (config_data.debug_level > 2) {
|
||||
gCatena.SafePrintf("using deep sleep in %u secs"
|
||||
#ifdef USBCON
|
||||
" (USB will disconnect while asleep)"
|
||||
#endif
|
||||
": ",
|
||||
deepSleepDelay
|
||||
);
|
||||
|
||||
}
|
||||
// sleep and print
|
||||
if (config_data.debug_level > 1) {
|
||||
gLed.Set(LedPattern::TwoShort);
|
||||
}
|
||||
|
||||
for (auto n = deepSleepDelay; n > 0; --n)
|
||||
{
|
||||
uint32_t tNow = millis();
|
||||
|
||||
while (uint32_t(millis() - tNow) < 1000)
|
||||
{
|
||||
gCatena.poll();
|
||||
yield();
|
||||
}
|
||||
if (config_data.debug_level > 2) {
|
||||
gCatena.SafePrintf(".");
|
||||
}
|
||||
}
|
||||
if (config_data.debug_level > 2) {
|
||||
gCatena.SafePrintf("\nStarting deep sleep.\n");
|
||||
}
|
||||
uint32_t tNow = millis();
|
||||
while (uint32_t(millis() - tNow) < 100)
|
||||
{
|
||||
gCatena.poll();
|
||||
yield();
|
||||
}
|
||||
}
|
||||
else if (config_data.debug_level > 2) {
|
||||
gCatena.SafePrintf("using light sleep\n");
|
||||
}
|
||||
}
|
||||
|
||||
void updateSleepCounters(void)
|
||||
{
|
||||
// update the sleep parameters
|
||||
if (gTxCycleCount > 1)
|
||||
{
|
||||
// values greater than one are decremented and ultimately reset to default.
|
||||
--gTxCycleCount;
|
||||
}
|
||||
else if (gTxCycleCount == 1)
|
||||
{
|
||||
// it's now one (otherwise we couldn't be here.)
|
||||
if (config_data.debug_level > 2) {
|
||||
gCatena.SafePrintf("resetting tx cycle to default: %u\n", CATCFG_T_CYCLE);
|
||||
}
|
||||
|
||||
gTxCycleCount = 0;
|
||||
gTxCycle = CATCFG_T_CYCLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
// it's zero. Leave it alone.
|
||||
}
|
||||
}
|
||||
|
||||
void doDeepSleep(osjob_t *pJob)
|
||||
{
|
||||
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
|
||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
|
||||
uint32_t const sleepInterval = CATCFG_GetInterval(
|
||||
fDeepSleepTest ? CATCFG_T_CYCLE_TEST : gTxCycle
|
||||
);
|
||||
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("doDeepSleep, sleepInterval: %d...\n", sleepInterval);
|
||||
}
|
||||
|
||||
/* ok... now it's time for a deep sleep */
|
||||
gLed.Set(LedPattern::Off);
|
||||
deepSleepPrepare();
|
||||
|
||||
/* sleep */
|
||||
gCatena.Sleep(sleepInterval);
|
||||
|
||||
/* recover from sleep */
|
||||
deepSleepRecovery();
|
||||
|
||||
/* and now... we're awake again. trigger another measurement */
|
||||
sleepDoneCb(pJob);
|
||||
}
|
||||
|
||||
void deepSleepPrepare(void)
|
||||
@@ -1059,53 +917,6 @@ void deepSleepRecovery(void)
|
||||
gSPI2.begin();
|
||||
}
|
||||
|
||||
void doLightSleep(osjob_t *pJob)
|
||||
{
|
||||
uint32_t interval = sec2osticks(CATCFG_GetInterval(gTxCycle));
|
||||
|
||||
|
||||
if (config_data.debug_level > 1) {
|
||||
gLed.Set(LedPattern::Sleeping);
|
||||
gCatena.SafePrintf("doLightSleep\n");
|
||||
}
|
||||
|
||||
if (gCatena.GetOperatingFlags() &
|
||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fQuickLightSleep))
|
||||
{
|
||||
interval = 1;
|
||||
}
|
||||
|
||||
os_setTimedCallback(
|
||||
&iterationJob,
|
||||
os_getTime() + interval,
|
||||
sleepDoneCb
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
static void sleepDoneCb(osjob_t* pJob)
|
||||
{
|
||||
if (config_data.debug_level > 1) {
|
||||
gLed.Set(LedPattern::WarmingUp);
|
||||
}
|
||||
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("sleepDoneCb\n");
|
||||
}
|
||||
|
||||
os_setTimedCallback(
|
||||
pJob,
|
||||
os_getTime() + sec2osticks(CATCFG_T_WARMUP),
|
||||
warmupDoneCb);
|
||||
}
|
||||
|
||||
static void warmupDoneCb(osjob_t* pJob)
|
||||
{
|
||||
if (config_data.debug_level > 0) {
|
||||
gCatena.SafePrintf("warmupDoneCb\n");
|
||||
}
|
||||
send_in_progress = false;
|
||||
}
|
||||
|
||||
static void startNewIterationCb(osjob_t* pJob)
|
||||
{
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ enum {
|
||||
|
|
||||
\****************************************************************************/
|
||||
|
||||
static const int32_t fwVersion = 20200528;
|
||||
static const int32_t fwVersion = 20200530;
|
||||
|
||||
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
|
||||
static const byte MAX_VALUES_TO_SEND = 8;
|
||||
|
||||
@@ -14,7 +14,7 @@ NAU7802 myScale; //Create instance of the NAU7802 class
|
||||
|
||||
byte debug_level;
|
||||
|
||||
byte interruptPin = A0;
|
||||
//byte interruptPin = A0;
|
||||
|
||||
void SetScalesDebugLevel(byte dbg_level)
|
||||
{
|
||||
@@ -47,7 +47,7 @@ bool SetupScales(byte dbg_level)
|
||||
if (debug_level > 0) {
|
||||
gCatena.SafePrintf("SetupScales start\n");
|
||||
}
|
||||
pinMode(interruptPin, INPUT);
|
||||
// pinMode(interruptPin, INPUT);
|
||||
|
||||
if (!myScale.begin(Wire, false))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user