-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from AIFanatic/feature/battery
Feature/battery
- Loading branch information
Showing
39 changed files
with
734 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ Temporary Items | |
.apdisk | ||
|
||
### PlatformIO ### | ||
.pio | ||
.pioenvs | ||
.piolibdeps | ||
.clang_complete | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ lib_deps = | |
ArduinoJson-esphomelib | ||
ESP Async WebServer | ||
GxEPD | ||
Pushbutton | ||
Pushbutton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "Battery.h" | ||
|
||
#include "../Manager.h" | ||
|
||
Battery::Battery(Manager *_manager) { | ||
manager = _manager; | ||
|
||
isCharging = getIsCharging(); | ||
chargePercentage = getChargePercentage(); | ||
} | ||
|
||
void Battery::requestBatteryStatus(AsyncWebServerRequest *request) { | ||
DynamicJsonBuffer jsonBuffer; | ||
JsonObject& response = jsonBuffer.createObject(); | ||
|
||
DynamicJsonBuffer batteryStatusBuffer; | ||
JsonObject& batteryStatus = batteryStatusBuffer.createObject(); | ||
batteryStatus["charging"] = isCharging; | ||
batteryStatus["percentage"] = chargePercentage; | ||
|
||
DynamicJsonBuffer buffer; | ||
response["status"] = "ok"; | ||
response["message"] = batteryStatus; | ||
|
||
String str; | ||
response.printTo(str); | ||
request->send(200, "application/json", str); | ||
} | ||
|
||
bool Battery::getIsCharging() { | ||
int charge = analogRead(CHARGE_PIN); | ||
|
||
return charge > 100 ? false : true; | ||
} | ||
|
||
int Battery::getChargePercentage() { | ||
int vbat = analogRead(VBAT_PIN); | ||
|
||
if(isCharging) { | ||
vbat -= BATTERY_CHARGE_DIFFERENCE_ADC; | ||
} | ||
|
||
return constrain(map(vbat, BATTERY_ZERO_CHARGE_ADC, BATTERY_FULL_CHARGE_ADC, 0, 100), 0, 100); | ||
} | ||
|
||
void Battery::update() { | ||
long currentTime = Utils::getCurrentTime(); | ||
|
||
if((currentTime - lastUpdate) > BATTERY_CHECK_FREQUENCY) { | ||
isCharging = getIsCharging(); | ||
chargePercentage = getChargePercentage(); | ||
|
||
if(isCharging && chargePercentage <= BATTERY_CHARGED_PERCENTAGE) { | ||
digitalWrite(RED_LED_PIN, HIGH); | ||
} | ||
else { | ||
digitalWrite(RED_LED_PIN, LOW); | ||
} | ||
|
||
lastUpdate = currentTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef Battery_H_ | ||
#define Battery_H_ | ||
|
||
#include "Arduino.h" | ||
#include <ESPAsyncWebServer.h> | ||
#include <ArduinoJson.h> | ||
|
||
class Manager; | ||
|
||
class Battery { | ||
public: | ||
Battery(Manager *_manager); | ||
~Battery(void); | ||
|
||
void update(); | ||
|
||
void requestBatteryStatus(AsyncWebServerRequest *request); | ||
|
||
bool isCharging; | ||
int chargePercentage; | ||
|
||
private: | ||
bool getIsCharging(); | ||
int getChargePercentage(); | ||
|
||
Manager *manager; | ||
|
||
long lastUpdate = 0; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "DeepSleep.h" | ||
|
||
#include "../Manager.h" | ||
|
||
DeepSleep::DeepSleep(Manager *_manager) { | ||
manager = _manager; | ||
}; | ||
|
||
int DeepSleep::getWakeupCause() { | ||
return esp_sleep_get_wakeup_cause(); | ||
} | ||
|
||
bool DeepSleep::hasBootedFromDeepSleep() { | ||
return (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED); | ||
} | ||
|
||
bool DeepSleep::isGoingToDeepSleep() { | ||
return needDeepSleep; | ||
} | ||
|
||
bool DeepSleep::canEnterDeepSleep() { | ||
if(Utils::getBootCurrentTime() <= minAwakeBootTime) { | ||
return false; | ||
} | ||
|
||
// if(manager->battery->isCharging) { | ||
// return false; | ||
// } | ||
|
||
return true; | ||
} | ||
void DeepSleep::enterDeepSleep() { | ||
if(!canEnterDeepSleep()) { | ||
return; | ||
} | ||
|
||
needDeepSleep = true; | ||
} | ||
|
||
void DeepSleep::setMinAwakeBootTimeOffset(long offsetMillis) { | ||
minAwakeBootTime = Utils::getBootCurrentTime() + offsetMillis; | ||
} | ||
|
||
void DeepSleep::update() { | ||
if(needDeepSleep) { | ||
if(sleepCountdown > 0) { | ||
sleepCountdown--; | ||
return; | ||
} | ||
esp_sleep_enable_ext0_wakeup(LEFT_BUTTON, LOW); | ||
esp_sleep_enable_ext0_wakeup(OK_BUTTON, LOW); | ||
esp_sleep_enable_ext0_wakeup(RIGHT_BUTTON, LOW); | ||
esp_deep_sleep_start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef DeepSleep_h | ||
#define DeepSleep_h | ||
|
||
#include <Arduino.h> | ||
|
||
class Manager; | ||
|
||
class DeepSleep { | ||
public: | ||
DeepSleep(Manager *_manager); | ||
~DeepSleep(void); | ||
|
||
int getWakeupCause(); | ||
|
||
bool hasBootedFromDeepSleep(); | ||
|
||
bool canEnterDeepSleep(); | ||
void enterDeepSleep(); | ||
bool isGoingToDeepSleep(); | ||
|
||
void setMinAwakeBootTimeOffset(long offsetMillis); | ||
|
||
void update(); | ||
|
||
private: | ||
Manager *manager; | ||
|
||
long minAwakeBootTime; | ||
|
||
bool needDeepSleep = false; | ||
|
||
int sleepCountdown = 1000; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.