refactor
This commit is contained in:
parent
0c5c673e5e
commit
85b0f6db06
|
|
@ -699,8 +699,6 @@ void StartNewIteration() {
|
||||||
{
|
{
|
||||||
os_runloop_once();
|
os_runloop_once();
|
||||||
delay(10);
|
delay(10);
|
||||||
// gCatena.poll();
|
|
||||||
// yield();
|
|
||||||
}
|
}
|
||||||
// handle timeout...
|
// handle timeout...
|
||||||
if (send_in_progress) {
|
if (send_in_progress) {
|
||||||
|
|
@ -733,6 +731,28 @@ void StartNewIteration() {
|
||||||
sleep_time_sec = 10;
|
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) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("now going to sleep for %d seconds...\n", sleep_time_sec);
|
gCatena.SafePrintf("now going to sleep for %d seconds...\n", sleep_time_sec);
|
||||||
if (fUsbPower) {
|
if (fUsbPower) {
|
||||||
|
|
@ -746,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) {
|
if (!fUsbPower) {
|
||||||
DoDeepSleep(sleep_time_sec);
|
DoDeepSleep(sleep_time_sec);
|
||||||
if (! stop_iterations) {
|
if (! stop_iterations) {
|
||||||
StartNewIteration();
|
StartNewIteration();
|
||||||
}
|
}
|
||||||
|
|
||||||
//os_setTimedCallback(
|
|
||||||
// &iterationJob,
|
|
||||||
// os_getTime() + sec2osticks(2),
|
|
||||||
// startNewIterationCb);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
|
|
@ -819,6 +846,8 @@ static void sendBufferDoneCb(
|
||||||
{
|
{
|
||||||
osjobcb_t pFn;
|
osjobcb_t pFn;
|
||||||
|
|
||||||
|
send_in_progress = false;
|
||||||
|
|
||||||
if (config_data.debug_level > 1) {
|
if (config_data.debug_level > 1) {
|
||||||
gLed.Set(LedPattern::Settling);
|
gLed.Set(LedPattern::Settling);
|
||||||
gCatena.SafePrintf("LMIC.opmode in sendBufferDoneCb: %#x\n", LMIC.opmode);
|
gCatena.SafePrintf("LMIC.opmode in sendBufferDoneCb: %#x\n", LMIC.opmode);
|
||||||
|
|
@ -827,7 +856,6 @@ static void sendBufferDoneCb(
|
||||||
pFn = settleDoneCb;
|
pFn = settleDoneCb;
|
||||||
if (! fStatus)
|
if (! fStatus)
|
||||||
{
|
{
|
||||||
send_in_progress = false;
|
|
||||||
if (!gLoRaWAN.IsProvisioned())
|
if (!gLoRaWAN.IsProvisioned())
|
||||||
{
|
{
|
||||||
// we'll talk about it at the callback.
|
// we'll talk about it at the callback.
|
||||||
|
|
@ -865,178 +893,9 @@ static void txNotProvisionedCb(
|
||||||
static void settleDoneCb(
|
static void settleDoneCb(
|
||||||
osjob_t* pSendJob)
|
osjob_t* pSendJob)
|
||||||
{
|
{
|
||||||
const bool fDeepSleep = checkDeepSleep();
|
|
||||||
|
|
||||||
if (config_data.debug_level > 0) {
|
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)
|
|
||||||
{
|
|
||||||
os_runloop_once();
|
|
||||||
delay(10);
|
|
||||||
//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)
|
|
||||||
{
|
|
||||||
os_runloop_once();
|
|
||||||
delay(10);
|
|
||||||
//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)
|
void deepSleepPrepare(void)
|
||||||
|
|
@ -1058,53 +917,6 @@ void deepSleepRecovery(void)
|
||||||
gSPI2.begin();
|
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)
|
static void startNewIterationCb(osjob_t* pJob)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue