disable led, refactor

This commit is contained in:
Joerg Lehmann 2019-11-22 07:39:39 +01:00
parent 54c2985774
commit 54460724db
2 changed files with 65 additions and 23 deletions

View File

@ -93,6 +93,7 @@ LORA_data_first lora_data_first;
CONFIG_data config_data; CONFIG_data config_data;
SENSOR_data last_sensor_reading; SENSOR_data last_sensor_reading;
long iteration = 0; // what iteration number do we have, starts with 0 long iteration = 0; // what iteration number do we have, starts with 0
bool send_in_progress = false;
// generic timer // generic timer
long t_cur; long t_cur;
@ -226,7 +227,11 @@ void setup_platform(void)
// set up the LED // set up the LED
gLed.begin(); gLed.begin();
gCatena.registerObject(&gLed); gCatena.registerObject(&gLed);
gLed.Set(LedPattern::FastFlash); gLed.Set(LedPattern::Off);
if (config_data.debug_level > 1) {
gLed.Set(LedPattern::FastFlash);
}
// set up LoRaWAN // set up LoRaWAN
gCatena.SafePrintf("LoRaWAN init: "); gCatena.SafePrintf("LoRaWAN init: ");
@ -354,7 +359,9 @@ void setup_uplink(void)
if (!gLoRaWAN.IsProvisioned()) if (!gLoRaWAN.IsProvisioned())
gCatena.SafePrintf("%010d - LoRaWAN not provisioned yet. Use the commands to set it up.\n"); gCatena.SafePrintf("%010d - LoRaWAN not provisioned yet. Use the commands to set it up.\n");
else { else {
gLed.Set(LedPattern::Joining); if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Joining);
}
/* trigger a join by sending the first packet */ /* trigger a join by sending the first packet */
StartNewIteration(); StartNewIteration();
@ -481,14 +488,16 @@ uint8_t GetVBatValue(int millivolts)
return res; return res;
} }
void DoDeepSleep() void DoDeepSleep(uint32_t sleep_time)
{ {
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("%010d - now going to deep sleep\n", millis()); gCatena.SafePrintf("%010d - now going to deep sleep\n", millis());
} }
// Prepare Deep Sleep // Prepare Deep Sleep
gLed.Set(LedPattern::Off); if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Off);
}
Serial.end(); Serial.end();
Wire.end(); Wire.end();
@ -497,7 +506,7 @@ void DoDeepSleep()
gSPI2.end(); gSPI2.end();
// Now sleeping... // Now sleeping...
gCatena.Sleep(CATCFG_T_INTERVAL); gCatena.Sleep(sleep_time);
// Recover from wakeup... // Recover from wakeup...
Serial.begin(); Serial.begin();
@ -589,6 +598,8 @@ void ReadSensors(SENSOR_data &sensor_data) {
} }
void StartNewIteration() { void StartNewIteration() {
uint32_t wait_time;
// we increment the iteration counter // we increment the iteration counter
iteration++; iteration++;
@ -649,16 +660,24 @@ void StartNewIteration() {
gCatena.SafePrintf("%010d - startSendingUplink(), my_position: %d, iteration: %d\n", millis(), my_position, iteration); gCatena.SafePrintf("%010d - startSendingUplink(), my_position: %d, iteration: %d\n", millis(), my_position, iteration);
} }
startSendingUplink(iteration == 1); startSendingUplink(iteration == 1);
gLed.Set(LedPattern::TwoShort); if (config_data.debug_level > 1) {
gLed.Set(LedPattern::TwoShort);
}
// If we have any critical jobs to complete in the next 8 seconds, do not sleep but process them // Loop while sending is in progress, timeout just in case after 300 seconds
long loopCount = 0; long start_time = millis();
while (os_queryTimeCriticalJobs(ms2osticks(8000)) != 0) if (config_data.debug_level > 0) {
gCatena.SafePrintf("%010d - waiting while send is in progress\n", millis());
}
while (send_in_progress && ((millis() - start_time) < 300000))
{ {
loopCount++;
gCatena.poll(); gCatena.poll();
yield(); yield();
} }
wait_time = (uint32_t)((millis() - start_time) / 1000);
if (config_data.debug_level > 0) {
gCatena.SafePrintf("%010d - end waiting, wait time was %d seconds\n", millis(), wait_time);
}
} }
if (iteration > 1) { if (iteration > 1) {
@ -674,10 +693,18 @@ void StartNewIteration() {
gCatena.SafePrintf("USB Power is off\n"); gCatena.SafePrintf("USB Power is off\n");
} }
//Serial.flush(); //Serial.flush();
if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Sleeping);
}
}
uint32_t sleep_time_sec;
if ((wait_time + 2) < CATCFG_T_INTERVAL) {
sleep_time_sec = CATCFG_T_INTERVAL - wait_time - 2;
} else {
sleep_time_sec = 0;
} }
gLed.Set(LedPattern::Sleeping);
if (!fUsbPower) { if (!fUsbPower) {
DoDeepSleep(); DoDeepSleep(sleep_time_sec);
os_setTimedCallback( os_setTimedCallback(
&iterationJob, &iterationJob,
os_getTime() + sec2osticks(2), os_getTime() + sec2osticks(2),
@ -687,19 +714,24 @@ void StartNewIteration() {
gCatena.SafePrintf("%010d - light sleep; os_setTimedCallback for startNewIterationCb in %d...\n", millis(), CATCFG_T_INTERVAL); gCatena.SafePrintf("%010d - light sleep; os_setTimedCallback for startNewIterationCb in %d...\n", millis(), CATCFG_T_INTERVAL);
os_setTimedCallback( os_setTimedCallback(
&iterationJob, &iterationJob,
os_getTime() + sec2osticks(CATCFG_T_INTERVAL), os_getTime() + sec2osticks(sleep_time_sec),
startNewIterationCb); startNewIterationCb);
} }
} }
void startSendingUplink(bool firstTime) void startSendingUplink(bool firstTime)
{ {
LedPattern savedLed = gLed.Set(LedPattern::Measuring); LedPattern savedLed;
if (config_data.debug_level > 1) {
savedLed = gLed.Set(LedPattern::Measuring);
}
if (savedLed != LedPattern::Joining) if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Sending); if (savedLed != LedPattern::Joining)
else gLed.Set(LedPattern::Sending);
gLed.Set(LedPattern::Joining); else
gLed.Set(LedPattern::Joining);
}
bool fConfirmed = false; bool fConfirmed = false;
if (gCatena.GetOperatingFlags() & (1 << 16)) { if (gCatena.GetOperatingFlags() & (1 << 16)) {
@ -714,11 +746,14 @@ void startSendingUplink(bool firstTime)
gCatena.SafePrintf("%010d - SendBuffer firstTime\n", millis()); gCatena.SafePrintf("%010d - SendBuffer firstTime\n", millis());
} }
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort); gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
send_in_progress = true;
} else { } else {
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("%010d - SendBuffer not firstTime\n", millis()); gCatena.SafePrintf("%010d - SendBuffer not firstTime\n", millis());
} }
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort); gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
send_in_progress = true;
} }
ClearLoraData(); ClearLoraData();
} }
@ -729,7 +764,9 @@ static void sendBufferDoneCb(
{ {
osjobcb_t pFn; osjobcb_t pFn;
gLed.Set(LedPattern::Settling); if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Settling);
}
pFn = settleDoneCb; pFn = settleDoneCb;
if (! fStatus) if (! fStatus)
@ -762,7 +799,9 @@ static void txNotProvisionedCb(
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); if (config_data.debug_level > 1) {
gLed.Set(LedPattern::NotProvisioned);
}
} }
@ -787,7 +826,9 @@ static void settleDoneCb(
static void sleepDoneCb(osjob_t* pJob) static void sleepDoneCb(osjob_t* pJob)
{ {
gLed.Set(LedPattern::WarmingUp); if (config_data.debug_level > 1) {
gLed.Set(LedPattern::WarmingUp);
}
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("%010d - sleepDoneCb\n", millis()); gCatena.SafePrintf("%010d - sleepDoneCb\n", millis());
@ -804,6 +845,7 @@ static void warmupDoneCb(osjob_t* pJob)
if (config_data.debug_level > 0) { if (config_data.debug_level > 0) {
gCatena.SafePrintf("%010d - warmupDoneCb\n", millis()); gCatena.SafePrintf("%010d - warmupDoneCb\n", millis());
} }
send_in_progress = false;
} }
static void startNewIterationCb(osjob_t* pJob) static void startNewIterationCb(osjob_t* pJob)

View File

@ -55,7 +55,7 @@ enum {
| |
\****************************************************************************/ \****************************************************************************/
static const int32_t fwVersion = 20191120; static const int32_t fwVersion = 20191121;
static const byte MAX_VALUES_TO_SEND = 8; static const byte MAX_VALUES_TO_SEND = 8;
//static const byte MAX_VALUES_TO_SEND = 1; // Testing //static const byte MAX_VALUES_TO_SEND = 1; // Testing
static const uint8_t LORA_DATA_VERSION = 1; static const uint8_t LORA_DATA_VERSION = 1;
@ -69,7 +69,7 @@ typedef struct {
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, no led, 1 => infos, no led, 2 => infos, 3 => error, 4 => highest level
byte fill[122]; byte fill[122];
} __attribute__((packed)) CONFIG_data; } __attribute__((packed)) CONFIG_data;