Skip to content

Commit

Permalink
Merge pull request #191 from kike-canaries/m5stickcplus
Browse files Browse the repository at this point in the history
v0.5.1 rev896 M5stickcplus version
  • Loading branch information
hpsaturn authored Feb 8, 2022
2 parents 96a3d7e + 32b9e9f commit 5648496
Show file tree
Hide file tree
Showing 37 changed files with 941 additions and 434 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ The [last release](https://github.com/kike-canaries/canairio_firmware/releases)
| ------------- |:-------------:| :-------------:| :----------------------:|
| **TTGO_TDISPLAY** | TTGO T-Display | eTFT | [CanAirIO Bike](https://canair.io/docs/canairio_bike.html) |
| **TTGO_T7** | TTGO T7, D1Mini, ** | OLED 64x48 | [CanAirIO v2.1](https://www.hackster.io/canairio/build-a-low-cost-air-quality-sensor-with-canairio-bbf647) |
| **M5STICKCPLUS** | M5StickC Plus | eTFT | [CanAirIO in M5StickC Plus](https://www.youtube.com/watch?v=TdX1AZ4PzBA) |
| **M5ATOM** | M5Atom Lite | OLED I2C | |
| **ESP32DevKit** | ESP32DevKit, NodeMCU V3, ** | OLED 128x64 | [HacksterIO](https://www.hackster.io/canairio/build-low-cost-air-quality-sensor-canairio-without-soldering-d87494) |
| **TTGO_TQ** | TTGO TQ | Builtin OLED | [TTGO_TQ board](https://de.aliexpress.com/item/10000291636371.html) |
| **WEMOSOLED** | WemosOLED and similar boards | OLED 128x64 | [ESP32 OLED board](https://de.aliexpress.com/item/33047481007.html) |
Expand Down
1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ else
build TTGO_TQ
build ESP32DEVKIT
build TTGO_TDISPLAY
build M5STICKCPLUS
printOutput
;;

Expand Down
1 change: 1 addition & 0 deletions include/bluetooth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Sensors.hpp>
#include <wifi.hpp>
#include <sniffer.hpp>
#include <Batterylib.hpp>

#define SERVICE_UUID "c8d1d262-861f-4082-947e-f383a259aaf3"
#define CHARAC_DATA_UUID "b0f332a8-a5aa-4f3f-bb43-f99e7791ae01"
Expand Down
1 change: 1 addition & 0 deletions include/wifi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ void wifiLoop();
int getWifiRSSI();
String getDeviceInfo();
String getHostId();
void logMemory(const char *msg);
18 changes: 18 additions & 0 deletions lib/batterylib/Batterylib.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef Batterylib_hpp
#define Batterylib_hpp

#ifdef TTGO_TDISPLAY
#include <battery_tft.hpp>
#endif

#ifdef M5STICKCPLUS
#include <battery_m5stack.hpp>
#endif

#ifndef M5STICKCPLUS
#ifndef TTGO_TDISPLAY
#include <battery_oled.hpp>
#endif
#endif

#endif
64 changes: 64 additions & 0 deletions lib/batterylib/battery.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <Arduino.h>

class BatteryUpdateCallbacks {
public:
virtual ~BatteryUpdateCallbacks () {};
virtual void onBatteryUpdate(float voltage, int charge, bool charging);
};

class Battery {
public:

int vref = 1100;
float curv = 0.0;
bool debug;

virtual void init(bool debug = false) = 0;
virtual void update() = 0;
virtual float getVoltage() = 0;
virtual int getCharge() = 0;
virtual bool isCharging() = 0;
virtual void printValues() = 0;

void setUpdateCallbacks(BatteryUpdateCallbacks *callbacks) {
this->callback = callbacks;
}

void loop() {
static uint32_t pmLoopTimeStamp = 0; // timestamp for sensor loop check data
if ((millis() - pmLoopTimeStamp > 3000)) { // sample time for each capture
pmLoopTimeStamp = millis();
update();
if (this->callback != nullptr && isNewVoltage()) {
pcurv = curv;
this->callback->onBatteryUpdate(curv, getCharge(), isCharging());
printValues();
}
}
}

private:

BatteryUpdateCallbacks *callback;
float pcurv = 0.0;

bool isNewVoltage () {
return (abs(curv - pcurv) > 0.1);
}

protected:

int calcPercentage(float volts, float max, float min) {
float percentage = (volts - min) * 100 / (max - min);
if (percentage > 100) {
percentage = 100;
}
if (percentage < 0) {
percentage = 0;
}
return (int)percentage;
}

};


43 changes: 43 additions & 0 deletions lib/batterylib/battery_m5stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <battery_m5stack.hpp>

#ifdef M5STICKCPLUS

void Battery_M5STACK::init(bool debug) {
this->debug = debug;
}

float Battery_M5STACK::getVoltage() {
return curv;
}

void Battery_M5STACK::update() {
curv = M5.Axp.GetBatVoltage();
vusb = M5.Axp.GetVBusVoltage();
}

bool Battery_M5STACK::isCharging() {
return vusb > curv;
}

int Battery_M5STACK::getCharge() {
if (isCharging()) {
return calcPercentage(curv, BATTCHARG_MAX_V, BATTCHARG_MIN_V);
} else {
return calcPercentage(curv, BATTERY_MAX_V, BATTERY_MIN_V);
}
}

void Battery_M5STACK::printValues() {
if (!debug) return;
Serial.printf("-->[BATT] AXP Temp \t: %.1fC \tC: %03d\n", M5.Axp.GetTempInAXP192(), getCharge()); //Get the temperature of AXP192
Serial.printf("-->[BATT] AXP Bat Volts \t: %.3fv \tI: %.3fma\n", curv, M5.Axp.GetBatCurrent()); //Output voltage and current of Bat
Serial.printf("-->[BATT] AXP USB Volts \t: %.3fv \tI: %.3fma\n", M5.Axp.GetVBusVoltage(), M5.Axp.GetVBusCurrent()); //Output current and voltage of USB
Serial.printf("-->[BATT] AXP 5V Volts \t: %.3fv \tI: %.3fma\n", M5.Axp.GetVinVoltage(), M5.Axp.GetVinCurrent());
Serial.printf("-->[BATT] AXP Bat power \t: %.3fmw\n", M5.Axp.GetBatPower());
}

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_M5STACKBATTERY)
Battery_M5STACK battery;
#endif

#endif
30 changes: 30 additions & 0 deletions lib/batterylib/battery_m5stack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef battery_m5stack_hpp
#define battery_m5stack_hpp

#include <battery.hpp>
#ifdef M5STICKCPLUS
#include <M5StickCPlus.h>
#endif

#define BATTERY_MIN_V 3.4
#define BATTERY_MAX_V 4.04
#define BATTCHARG_MIN_V 3.69
#define BATTCHARG_MAX_V 4.198

class Battery_M5STACK : public Battery {
public:
float vusb = 0.0;
void init(bool debug = false);
float getVoltage();
float getCurrent();
int getCharge();
bool isCharging();
void printValues();
void update();
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_M5STACKBATTERY)
extern Battery_M5STACK battery;
#endif

#endif
35 changes: 35 additions & 0 deletions lib/batterylib/battery_oled.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <battery_oled.hpp>

#ifndef M5STICKCPLUS
#ifndef TTGO_TDISPLAY

void Battery_OLED::init(bool debug) {
}

float Battery_OLED::getVoltage() {
return 0;
}

bool Battery_OLED::isCharging() {
return false;
}

void Battery_OLED::printValues() {
}

void Battery_OLED::update() {
}

int Battery_OLED::getCharge() {
return 0;
}

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_OLEDBATTERY)
Battery_OLED battery;
#endif

#endif
#endif



26 changes: 26 additions & 0 deletions lib/batterylib/battery_oled.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef battery_oled_hpp
#define battery_oled_hpp

#include <battery.hpp>

#define BATTERY_MIN_V 3.4
#define BATTERY_MAX_V 4.1
#define BATTCHARG_MIN_V 4.65
#define BATTCHARG_MAX_V 4.8

class Battery_OLED : public Battery {
public:
void init(bool debug = false);
float getVoltage();
bool isCharging();
int getCharge();
void printValues();
void update();
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_OLEDBATTERY)
extern Battery_OLED battery;
#endif

#endif

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "battery.hpp"
#include <battery_tft.hpp>

int vref = 1100;
float curv = 0;
Expand All @@ -17,7 +17,8 @@ void setupBattADC() {
}
}

void setupBattery() {
void Battery_TFT::init(bool debug) {
this->debug = debug;
/*
ADC_EN is the ADC detection enable port
If the USB port is used for power supply, it is turned on by default.
Expand All @@ -30,39 +31,35 @@ void setupBattery() {
delay(10); // suggested by @ygator user in issue #2
}

float battGetVoltage() {
// setupBattery();
float Battery_TFT::getVoltage () {
return curv;
}

void Battery_TFT::update() {
digitalWrite(ADC_EN, HIGH);
delay(10); // suggested by @ygator user in issue #2
delay(10); // suggested by @ygator user in issue #2
uint16_t v = analogRead(ADC_PIN);
curv = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
digitalWrite(ADC_EN, LOW); // for possible issue: https://github.com/Xinyuan-LilyGO/TTGO-T-Display/issues/6
return curv;
digitalWrite(ADC_EN, LOW); // for possible issue: https://github.com/Xinyuan-LilyGO/TTGO-T-Display/issues/6
}

uint8_t _calcPercentage(float volts, float max, float min) {
float percentage = (volts - min) * 100 / (max - min);
if (percentage > 100) {
percentage = 100;
}
if (percentage < 0) {
percentage = 0;
}
return (uint8_t)percentage;
bool Battery_TFT::isCharging() {
return curv > BATTERY_MAX_V + (BATTCHARG_MIN_V - BATTERY_MAX_V ) / 2;
}

uint8_t battCalcPercentage(float volts) {
if (battIsCharging()){
return _calcPercentage(volts,BATTCHARG_MAX_V,BATTCHARG_MIN_V);
int Battery_TFT::getCharge() {
if (isCharging()) {
return calcPercentage(curv, BATTCHARG_MAX_V, BATTCHARG_MIN_V);
} else {
return _calcPercentage(volts,BATTERY_MAX_V,BATTERY_MIN_V);
return calcPercentage(curv, BATTERY_MAX_V, BATTERY_MIN_V);
}
}

void battUpdateChargeStatus() {
// digitalWrite(LED_PIN, battIsCharging());
void Battery_TFT::printValues() {
if (!debug) return;
Serial.printf("-->[BATT] Battery voltage \t: %.3fv vref: %i Charge:%i\n", curv, vref, getCharge()); //Output voltage and current of Bat
}

bool battIsCharging() {
return curv > BATTERY_MAX_V + (BATTCHARG_MIN_V - BATTERY_MAX_V ) / 2;
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TFTBATTERY)
Battery_TFT battery;
#endif
29 changes: 29 additions & 0 deletions lib/batterylib/battery_tft.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef battery_tft_hpp
#define battery_tft_hpp

#include <battery.hpp>
#include <esp_adc_cal.h>

#define ADC_EN 14
#define ADC_PIN 34

#define BATTERY_MIN_V 3.4
#define BATTERY_MAX_V 4.19
#define BATTCHARG_MIN_V 4.21
#define BATTCHARG_MAX_V 4.8

class Battery_TFT : public Battery {
public:
void init(bool debug = false);
float getVoltage();
bool isCharging();
int getCharge();
void printValues();
void update();
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TFTHANDLER)
extern Battery_TFT battery;
#endif

#endif
6 changes: 3 additions & 3 deletions lib/canairioota/src/OTAHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void OTAHandler::setup(const char* ESP_ID, const char* ESP_PASS) {

fota.checkURL = "http://influxdb.canair.io:8080/releases/" + String(TARGET) + "/firmware_" + String(FLAVOR) + ".json";

Serial.print("-->[INFO] OTA on: ");
Serial.print("-->[INFO] local OTA updates on\t: ");
Serial.print(ESP_ID);
Serial.print(".local with passw: ");
Serial.print(".local passw: ");
Serial.println(ESP_PASS);
}

Expand All @@ -62,7 +62,7 @@ void OTAHandler::checkRemoteOTA(bool notify) {
esp_task_wdt_init(120,0);
fota.execOTA();
} else if (notify)
Serial.println("-->[FOTA] not need update");
Serial.println("-->[FOTA] remote OTA update \t: not need update");
}

void OTAHandler::remoteOTAcheckloop() {
Expand Down
Loading

0 comments on commit 5648496

Please sign in to comment.