cleanup, debug-switch

This commit is contained in:
Joerg Lehmann 2019-06-21 15:42:32 +02:00
parent 441356f0ab
commit bac960c897
1 changed files with 71 additions and 46 deletions

View File

@ -28,6 +28,8 @@
using namespace McciCatena;
#define DEBUG
/****************************************************************************\
|
| MANIFEST CONSTANTS & TYPEDEFS
@ -267,25 +269,18 @@ void setup_platform(void)
);
// read config_data from fram...
#ifdef DEBUG
gCatena.SafePrintf("Reading Calibration Config from FRAM...\n");
#endif
gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data));
#ifdef DEBUG
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_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("Size of config_data: %d\n", sizeof(config_data));
// im Moment statisch...
//config_data.cal_w1_0 = 20000;
//config_data.cal_w2_0 = 20000;
//config_data.cal_w1_factor = 2.5;
//config_data.cal_w2_factor = 2.5;
// config_data speichern...
//gCatena.SafePrintf("Writing Calibration Config to FRAM...\n");
//gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
#endif
#ifdef USBCON
// if running unattended, don't wait for USB connect.
@ -298,7 +293,7 @@ void setup_platform(void)
gCatena.SafePrintf("\n");
gCatena.SafePrintf("-------------------------------------------------------------------------------\n");
gCatena.SafePrintf("BeieliScale Version %s.\n", sVersion);
gCatena.SafePrintf("mini-beieli.ch - BeieliScale Version %s.\n", sVersion);
{
char sRegion[16];
gCatena.SafePrintf("Target network: %s / %s\n",
@ -384,7 +379,9 @@ void setup_bme280(void)
void setup_scales(void)
{
#ifdef DEBUG
gCatena.SafePrintf("Setup Scales...\n");
#endif
// Enable Power
digitalWrite(D10, HIGH);
@ -395,25 +392,27 @@ void setup_scales(void)
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
#ifdef DEBUG
gCatena.SafePrintf("Setup Scale 1...\n");
#endif
LoadCell_1.begin(A1, A0);
if (!(LoadCell_1.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 1 not ready.\n");
}
//LoadCell_1.power_down();
#ifdef DEBUG
gCatena.SafePrintf("Setup Scale 2...\n");
#endif
LoadCell_2.begin(D12, A2);
if (!(LoadCell_2.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 2 not ready.\n");
}
//LoadCell_2.power_down();
#ifdef DEBUG
gCatena.SafePrintf("Setup Scales is complete\n");
#endif
}
void setup_flash(void)
@ -421,7 +420,7 @@ void setup_flash(void)
if (gFlash.begin(&gSPI2, Catena::PIN_SPI2_FLASH_SS)) {
fFlash = true;
gFlash.powerDown();
gCatena.SafePrintf("FLASH found, put power down\n");
gCatena.SafePrintf("FLASH found, but power down\n");
}
else {
fFlash = false;
@ -558,6 +557,33 @@ uint8_t GetVBatValue(int millivolts)
return res;
}
void DoDeepSleep()
{
gCatena.SafePrintf("Now going to deep sleep: %d\n",millis());
// Prepare Deep Sleep
gLed.Set(LedPattern::Off);
Serial.end();
Wire.end();
SPI.end();
if (fFlash)
gSPI2.end();
// Now sleeping...
gCatena.Sleep(CATCFG_T_INTERVAL - 5);
// Recover from wakeup...
Serial.begin();
Wire.begin();
SPI.begin();
if (fFlash)
gSPI2.begin();
gCatena.SafePrintf("Done with deep sleep: %d\n",millis());
}
void ReadSensors(bool firstTime, bool readOnly)
{
int16_t temp_current;
@ -576,81 +602,72 @@ void ReadSensors(bool firstTime, bool readOnly)
// 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
float vBat = gCatena.ReadVbat();
#ifdef DEBUG
gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f));
#endif
// vBus
float vBus = gCatena.ReadVbus();
#ifdef DEBUG
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
#endif
fUsbPower = (vBus > 3.0) ? true : false;
// Setup Scales
setup_scales();
// Read Scales
gCatena.SafePrintf("Before Read Scales\n");
// Power-Up HX711
LoadCell_1.power_up();
if (!(LoadCell_1.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 1 not ready.\n");
}
gCatena.SafePrintf("Before Read Scales\n");
if (LoadCell_1.is_ready()) {
#ifdef DEBUG
Serial.println("HX711 LoadCell_1 is ready.");
#endif
long w1 = LoadCell_1.read_average(5);
weight1_current = (int32_t)w1;
#ifdef DEBUG
gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1);
gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current);
#endif
}
else {
Serial.println("HX711 LoadCell_1 not ready.");
}
// Power-Down HX711
//LoadCell_1.power_down();
// Power-Up HX711
LoadCell_2.power_up();
if (!(LoadCell_2.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 2 not ready.\n");
}
if (LoadCell_2.is_ready()) {
#ifdef DEBUG
Serial.println("HX711 LoadCell_2 is ready.");
#endif
long w2 = LoadCell_2.read_average(5);
weight2_current = (int32_t)w2;
#ifdef DEBUG
gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2);
gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current);
#endif
}
else {
Serial.println("HX711 LoadCell_2 not ready.");
}
// Power-Down HX711
//LoadCell_2.power_down();
// Disable Power
digitalWrite(D10, LOW);
gCatena.SafePrintf("After Read Scales\n");
// Gewicht berechnen
weight_current32 = (int32_t)((((weight1_current - config_data.cal_w1_0) / config_data.cal_w1_factor) + ((weight2_current - config_data.cal_w2_0) / config_data.cal_w2_factor)) / 5.0);
if (weight_current32 < 0) {
@ -725,7 +742,7 @@ void ReadSensors(bool firstTime, bool readOnly)
os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb);
if (!fUsbPower) {
gCatena.Sleep(CATCFG_T_INTERVAL - 5);
DoDeepSleep();
}
}
}
@ -751,15 +768,21 @@ void startSendingUplink(bool firstTime)
bool fConfirmed = false;
if (gCatena.GetOperatingFlags() & (1 << 16)) {
#ifdef DEBUG
gCatena.SafePrintf("requesting confirmed tx\n");
#endif
fConfirmed = true;
}
if (firstTime) {
#ifdef DEBUG
gCatena.SafePrintf("SendBuffer firstTime\n");
#endif
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed);
} else {
#ifdef DEBUG
gCatena.SafePrintf("SendBuffer not firstTime\n");
#endif
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed);
}
ClearLoraData();
@ -779,12 +802,10 @@ static void sendBufferDoneCb(
else {
pFn = settleDoneCb;
}
gCatena.SafePrintf("sendBufferDoneCB before TimedCallback\n");
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_SETTLE),
pFn);
gCatena.SafePrintf("sendBufferDoneCB after TimedCallback\n");
}
static void txFailedDoneCb(
@ -800,7 +821,9 @@ static void txFailedDoneCb(
static void settleDoneCb(
osjob_t* pSendJob)
{
#ifdef DEBUG
gCatena.SafePrintf("settleDoneCb\n");
#endif
sleepDoneCb(pSendJob);
}
@ -824,8 +847,10 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
unsigned txCycle;
unsigned txCount;
#ifdef DEBUG
gCatena.SafePrintf("receiveMessage was called!!!\n");
#endif
if (! (port == 1 && 2 <= nMessage && nMessage <= 3))
{
gCatena.SafePrintf("invalid message port(%02x)/length(%zx)\n",