refactor code

This commit is contained in:
Joerg Lehmann 2019-08-10 20:06:25 +02:00
parent a97afbf36c
commit 3d03d0b52d
1 changed files with 74 additions and 65 deletions

View File

@ -235,7 +235,7 @@ bool fUsbPower;
// have we printed the sleep info? // have we printed the sleep info?
bool g_fPrintedSleeping = false; bool g_fPrintedSleeping = false;
// the job that's used to synchronize us with the LMIC code // tFhe job that's used to synchronize us with the LMIC code
static osjob_t sensorJob; static osjob_t sensorJob;
void sensorJob_cb(osjob_t* pJob); void sensorJob_cb(osjob_t* pJob);
@ -365,6 +365,9 @@ void setup_platform(void)
gCatena.SafePrintf("**** no platform, check provisioning ****\n"); gCatena.SafePrintf("**** no platform, check provisioning ****\n");
flags = 0; flags = 0;
} }
// Because of Alarms on Swisscom Network
LMIC_setDrTxpow(DR_SF10, 14);
LMIC_setAdrMode(1);
} }
void setup_bme280(void) void setup_bme280(void)
@ -378,8 +381,11 @@ void setup_bme280(void)
} }
} }
void setup_scales(void) bool setup_scales(void)
{ {
bool res;
res = true;
#ifdef DEBUG #ifdef DEBUG
gCatena.SafePrintf("Setup Scales...\n"); gCatena.SafePrintf("Setup Scales...\n");
#endif #endif
@ -398,13 +404,16 @@ void setup_scales(void)
#endif #endif
LoadCell.begin(A1, A0); LoadCell.begin(A1, A0);
if (!(LoadCell.wait_ready_timeout(1000))) { if (!(LoadCell.wait_ready_timeout(2000))) {
gCatena.SafePrintf("Scale not ready.\n"); gCatena.SafePrintf("Scale not ready after Init.\n");
res = false;
} }
#ifdef DEBUG #ifdef DEBUG
gCatena.SafePrintf("Setup Scale is complete\n"); gCatena.SafePrintf("Setup Scale is complete\n");
#endif #endif
return res;
} }
void setup_flash(void) void setup_flash(void)
@ -592,30 +601,6 @@ void ReadSensors(bool firstTime, bool readOnly)
int32_t weight_current32; int32_t weight_current32;
uint16_t weight_current; uint16_t weight_current;
if (fBme) {
/* warm up the BME280 by discarding a measurement */
(void)gBME280.readTemperature();
Adafruit_BME280::Measurements m = gBME280.readTemperaturePressureHumidity();
// temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C
// pressure is 2 bytes, hPa * 10.
// humidity is one byte, where 0 == 0/256 and 0xFF == 255/256.
#ifdef DEBUG
gCatena.SafePrintf(
"BME280: T: %d P: %d RH: %d\n",
(int)m.Temperature,
(int)m.Pressure,
(int)m.Humidity);
#endif
temp_current = (int16_t)((m.Temperature) * 10);
humidity_current = (uint8_t)m.Humidity;
pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET);
#ifdef DEBUG
gCatena.SafePrintf("pressure_current: %d\n", pressure_current);
#endif
}
// vBat // vBat
float vBat = gCatena.ReadVbat(); float vBat = gCatena.ReadVbat();
#ifdef DEBUG #ifdef DEBUG
@ -633,7 +618,8 @@ void ReadSensors(bool firstTime, bool readOnly)
setup_scales(); setup_scales();
// Read Scales // Read Scales
if (LoadCell.is_ready()) { //if (LoadCell.is_ready()) {
if (setup_scales()) {
#ifdef DEBUG #ifdef DEBUG
Serial.println("HX711 LoadCell is ready."); Serial.println("HX711 LoadCell is ready.");
#endif #endif
@ -668,6 +654,29 @@ void ReadSensors(bool firstTime, bool readOnly)
} }
weight_current = (uint16_t)weight_current32; weight_current = (uint16_t)weight_current32;
if (fBme) {
/* warm up the BME280 by discarding a measurement */
(void)gBME280.readTemperature();
Adafruit_BME280::Measurements m = gBME280.readTemperaturePressureHumidity();
// temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C
// pressure is 2 bytes, hPa * 10.
// humidity is one byte, where 0 == 0/256 and 0xFF == 255/256.
#ifdef DEBUG
gCatena.SafePrintf(
"BME280: T: %d P: %d RH: %d\n",
(int)m.Temperature,
(int)m.Pressure,
(int)m.Humidity);
#endif
temp_current = (int16_t)((m.Temperature) * 10);
humidity_current = (uint8_t)m.Humidity;
pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET);
#ifdef DEBUG
gCatena.SafePrintf("pressure_current: %d\n", pressure_current);
#endif
}
if (not(readOnly)) { if (not(readOnly)) {
if (firstTime) { if (firstTime) {