Skip to content

Commit

Permalink
Merge branch 'low-power-eink' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
melkati authored Apr 8, 2024
2 parents 6ad6717 + 216da48 commit 654c6e7
Show file tree
Hide file tree
Showing 22 changed files with 11,068 additions and 461 deletions.
365 changes: 310 additions & 55 deletions CO2_Gadget.ino

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions CO2_Gadget_BLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ void initBLE() {
#ifdef SUPPORT_BLE
if (activeBLE) {
if (bleInitialized) {
Serial.print(
"-->[SBLE] Sensirion Gadget BLE Lib already initialized with deviceId = ");
Serial.println(provider.getDeviceIdString());
return; // If BLE is already initialized do nothing and return
Serial.print("-->[SBLE] Sensirion Gadget BLE Lib already initialized with deviceId: " + provider.getDeviceIdString() + " - Skipping BLE init.");
return;
} else {
// provider.setSampleIntervalMs(60000); // Set interval for MyAmbiance dataloging at 60 seconds. See https://github.com/melkati/CO2-Gadget/projects/2#card-91517604
provider.begin();
Serial.print("-->[SBLE] Sensirion Gadget BLE Lib initialized with deviceId = ");
Serial.println(provider.getDeviceIdString());
Serial.println("-->[SBLE] Sensirion Gadget BLE Lib initialized with deviceId: " + provider.getDeviceIdString());
bleInitialized = true;
}
// if (activeWIFI) {
Expand All @@ -43,6 +40,7 @@ void publishBLE() {
provider.writeValueToCurrentSample(temp, SignalType::TEMPERATURE_DEGREES_CELSIUS);
provider.writeValueToCurrentSample(hum, SignalType::RELATIVE_HUMIDITY_PERCENTAGE);
provider.commitSample();
Serial.println("-->[SBLE] Published CO2: " + String(co2) + " ppm, Temp: " + String(temp) + " C, Hum: " + String(hum) + " %");
}
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions CO2_Gadget_Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/*****************************************************************************************************/
// clang-format on

// Another option: https://github.com/G6EJD/LiPo_Battery_Capacity_Estimator

float lastBatteryVoltage = 0;
uint16_t timeBetweenBatteryRead = 1;
uint64_t lastTimeBatteryRead = 0; // Time of last battery reading
Expand Down
122 changes: 81 additions & 41 deletions CO2_Gadget_Buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,65 +30,105 @@ void IRAM_ATTR buttonDownISR() {

void initButtons() {
// Interrupt Service Routine to turn on the display on button UP press
attachInterrupt(BTN_UP, buttonUpISR, RISING);
attachInterrupt(BTN_DWN, buttonUpISR, RISING);

btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});

btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});

// btnDwn.setDoubleClickHandler(doubleClick);
}

void reverseButtons(bool reversed) {
if (reversed) {
// Interrupt Service Routine to turn on the display on button UP press
if (BTN_UP >= 0) {
attachInterrupt(BTN_UP, buttonUpISR, RISING);
btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});

btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});
} else {
attachInterrupt(BTN_DWN, buttonUpISR, RISING);
btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});

}
if (BTN_DWN >= 0) {
attachInterrupt(BTN_DWN, buttonUpISR, RISING);
btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});
}

// btnDwn.setDoubleClickHandler(doubleClick);
}

void reverseButtons(bool reversed) {
if (reversed) {
// Interrupt Service Routine to turn on the display on button UP press
if ((BTN_UP >= 0) && (BTN_DWN >= 0)) {
attachInterrupt(BTN_UP, buttonUpISR, RISING);
btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});

btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});
}
} else {
if ((BTN_UP >= 0) && (BTN_DWN >= 0)) {
attachInterrupt(BTN_DWN, buttonUpISR, RISING);
btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});

btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});
}
}
}

void buttonsLoop() {
// Check for button presses
btnUp.loop();
btnDwn.loop();
if (BTN_UP >= 0) {
btnUp.loop();
}
if (BTN_DWN >= 0) {
btnDwn.loop();
}
}

bool isButtonPressedOnWakeUp() {
unsigned long buttonPressStartTime = millis();
bool buttonState = digitalRead(BTN_DWN); // Initial button state

// If the button is not pressed, return false immediately
if (buttonState == HIGH) {
return false;
}

// Wait for the button to be released or for LONGCLICK_TIME_MS
while (buttonState == LOW && (millis() - buttonPressStartTime < LONGCLICK_TIME_MS)) {
delay(10); // Debouncing delay
buttonState = digitalRead(BTN_DWN);

// Check if the button is released
if (buttonState == HIGH) {
// Button was released before LONGCLICK_TIME_MS
return false;
}
}

// Check if the button is still pressed after waiting
if (buttonState == LOW && (millis() - buttonPressStartTime >= LONGCLICK_TIME_MS)) {
// Button was pressed and held for LONGCLICK_TIME_MS
return true;
}

return false;
}

#endif // CO2_Gadget_Buttons_h
Loading

0 comments on commit 654c6e7

Please sign in to comment.