refactor code
This commit is contained in:
parent
a97afbf36c
commit
3d03d0b52d
|
|
@ -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);
|
||||||
|
|
||||||
|
|
@ -270,18 +270,18 @@ void setup_platform(void)
|
||||||
);
|
);
|
||||||
|
|
||||||
// read config_data from fram...
|
// read config_data from fram...
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("Reading Calibration Config from FRAM...\n");
|
gCatena.SafePrintf("Reading Calibration Config from FRAM...\n");
|
||||||
#endif
|
#endif
|
||||||
gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data));
|
gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data));
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("cal_w1_0: %d\n", config_data.cal_w1_0);
|
gCatena.SafePrintf("cal_w1_0: %d\n", config_data.cal_w1_0);
|
||||||
gCatena.SafePrintf("cal_w2_0: %d\n", config_data.cal_w2_0);
|
gCatena.SafePrintf("cal_w2_0: %d\n", config_data.cal_w2_0);
|
||||||
gCatena.SafePrintf("cal_w1_factor: %d.%03d\n", (int)config_data.cal_w1_factor, (int)(config_data.cal_w1_factor * 1000) % 1000);
|
gCatena.SafePrintf("cal_w1_factor: %d.%03d\n", (int)config_data.cal_w1_factor, (int)(config_data.cal_w1_factor * 1000) % 1000);
|
||||||
gCatena.SafePrintf("cal_w2_factor: %d.%03d\n", (int)config_data.cal_w2_factor, (int)(config_data.cal_w2_factor * 1000) % 1000);
|
gCatena.SafePrintf("cal_w2_factor: %d.%03d\n", (int)config_data.cal_w2_factor, (int)(config_data.cal_w2_factor * 1000) % 1000);
|
||||||
gCatena.SafePrintf("Size of config_data: %d\n", sizeof(config_data));
|
gCatena.SafePrintf("Size of config_data: %d\n", sizeof(config_data));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USBCON
|
#ifdef USBCON
|
||||||
// if running unattended, don't wait for USB connect.
|
// if running unattended, don't wait for USB connect.
|
||||||
|
|
@ -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,11 +381,14 @@ void setup_bme280(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_scales(void)
|
bool setup_scales(void)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
bool res;
|
||||||
|
res = true;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("Setup Scales...\n");
|
gCatena.SafePrintf("Setup Scales...\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Enable Power
|
// Enable Power
|
||||||
digitalWrite(D10, HIGH);
|
digitalWrite(D10, HIGH);
|
||||||
|
|
@ -393,18 +399,21 @@ void setup_scales(void)
|
||||||
// - With a gain factor of 32, channel B is selected
|
// - With a gain factor of 32, channel B is selected
|
||||||
// By omitting the gain factor parameter, the library
|
// By omitting the gain factor parameter, the library
|
||||||
// default "128" (Channel A) is used here.
|
// default "128" (Channel A) is used here.
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("Setup Scale 1...\n");
|
gCatena.SafePrintf("Setup Scale 1...\n");
|
||||||
#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)
|
||||||
|
|
@ -557,7 +566,7 @@ uint8_t GetVBatValue(int millivolts)
|
||||||
|
|
||||||
void DoDeepSleep()
|
void DoDeepSleep()
|
||||||
{
|
{
|
||||||
gCatena.SafePrintf("Now going to deep sleep: %d\n",millis());
|
gCatena.SafePrintf("Now going to deep sleep: %d\n", millis());
|
||||||
|
|
||||||
// Prepare Deep Sleep
|
// Prepare Deep Sleep
|
||||||
gLed.Set(LedPattern::Off);
|
gLed.Set(LedPattern::Off);
|
||||||
|
|
@ -577,7 +586,7 @@ void DoDeepSleep()
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
if (fFlash)
|
if (fFlash)
|
||||||
gSPI2.begin();
|
gSPI2.begin();
|
||||||
gCatena.SafePrintf("Done with deep sleep: %d\n",millis());
|
gCatena.SafePrintf("Done with deep sleep: %d\n", millis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -592,65 +601,42 @@ 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
|
||||||
gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f));
|
gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// vBus
|
// vBus
|
||||||
float vBus = gCatena.ReadVbus();
|
float vBus = gCatena.ReadVbus();
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
|
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
|
||||||
#endif
|
#endif
|
||||||
fUsbPower = (vBus > 3.0) ? true : false;
|
fUsbPower = (vBus > 3.0) ? true : false;
|
||||||
|
|
||||||
// Setup Scales
|
// Setup Scales
|
||||||
setup_scales();
|
setup_scales();
|
||||||
|
|
||||||
// Read Scales
|
// Read Scales
|
||||||
if (LoadCell.is_ready()) {
|
//if (LoadCell.is_ready()) {
|
||||||
#ifdef DEBUG
|
if (setup_scales()) {
|
||||||
|
#ifdef DEBUG
|
||||||
Serial.println("HX711 LoadCell is ready.");
|
Serial.println("HX711 LoadCell is ready.");
|
||||||
#endif
|
#endif
|
||||||
LoadCell.set_gain(128);
|
LoadCell.set_gain(128);
|
||||||
long w1 = LoadCell.read_average(5);
|
long w1 = LoadCell.read_average(5);
|
||||||
weight1_current = (int32_t)w1;
|
weight1_current = (int32_t)w1;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1);
|
gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1);
|
||||||
gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current);
|
gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current);
|
||||||
#endif
|
#endif
|
||||||
LoadCell.set_gain(32);
|
LoadCell.set_gain(32);
|
||||||
long w2 = LoadCell.read_average(5);
|
long w2 = LoadCell.read_average(5);
|
||||||
weight2_current = (int32_t)w2;
|
weight2_current = (int32_t)w2;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2);
|
gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2);
|
||||||
gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current);
|
gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.println("HX711 LoadCell not ready.");
|
Serial.println("HX711 LoadCell not ready.");
|
||||||
|
|
@ -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) {
|
||||||
|
|
@ -748,21 +757,21 @@ void startSendingUplink(bool firstTime)
|
||||||
|
|
||||||
bool fConfirmed = false;
|
bool fConfirmed = false;
|
||||||
if (gCatena.GetOperatingFlags() & (1 << 16)) {
|
if (gCatena.GetOperatingFlags() & (1 << 16)) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("requesting confirmed tx\n");
|
gCatena.SafePrintf("requesting confirmed tx\n");
|
||||||
#endif
|
#endif
|
||||||
fConfirmed = true;
|
fConfirmed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (firstTime) {
|
if (firstTime) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("SendBuffer firstTime\n");
|
gCatena.SafePrintf("SendBuffer firstTime\n");
|
||||||
#endif
|
#endif
|
||||||
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed);
|
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed);
|
||||||
} else {
|
} else {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("SendBuffer not firstTime\n");
|
gCatena.SafePrintf("SendBuffer not firstTime\n");
|
||||||
#endif
|
#endif
|
||||||
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed);
|
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed);
|
||||||
}
|
}
|
||||||
ClearLoraData();
|
ClearLoraData();
|
||||||
|
|
@ -801,9 +810,9 @@ static void txFailedDoneCb(
|
||||||
static void settleDoneCb(
|
static void settleDoneCb(
|
||||||
osjob_t* pSendJob)
|
osjob_t* pSendJob)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("settleDoneCb\n");
|
gCatena.SafePrintf("settleDoneCb\n");
|
||||||
#endif
|
#endif
|
||||||
sleepDoneCb(pSendJob);
|
sleepDoneCb(pSendJob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -827,9 +836,9 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
|
||||||
unsigned txCycle;
|
unsigned txCycle;
|
||||||
unsigned txCount;
|
unsigned txCount;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
gCatena.SafePrintf("receiveMessage was called!!!\n");
|
gCatena.SafePrintf("receiveMessage was called!!!\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (! (port == 1 && 2 <= nMessage && nMessage <= 3))
|
if (! (port == 1 && 2 <= nMessage && nMessage <= 3))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue