From ec72ed5b0afbde43bfac3d6bb088ad4fbfa425c0 Mon Sep 17 00:00:00 2001 From: nmori Date: Mon, 4 Mar 2019 15:57:10 +0900 Subject: [PATCH] integrate to Power class. (#124) *power function integrate to Power class. *fixed M5.begin() : add I2C initialize flag. *Check result after send I2C Request *change method of send to power-ic register. (read->mask->write) --- src/M5Stack.cpp | 76 ++++++-------------- src/M5Stack.h | 33 +++++---- src/utility/Power.cpp | 156 ++++++++++++++++++++++++++++++++++++++++++ src/utility/Power.h | 32 +++++++++ 4 files changed, 231 insertions(+), 66 deletions(-) create mode 100644 src/utility/Power.cpp create mode 100644 src/utility/Power.h diff --git a/src/M5Stack.cpp b/src/M5Stack.cpp index 7ba5d61..b782567 100644 --- a/src/M5Stack.cpp +++ b/src/M5Stack.cpp @@ -7,7 +7,7 @@ M5Stack::M5Stack() : isInited(0) { } -void M5Stack::begin(bool LCDEnable, bool SDEnable, bool SerialEnable) { +void M5Stack::begin(bool LCDEnable, bool SDEnable, bool SerialEnable,bool I2CEnable) { // Correct init once if (isInited) return; @@ -35,12 +35,13 @@ void M5Stack::begin(bool LCDEnable, bool SDEnable, bool SerialEnable) { // Speaker.begin(); // Set wakeup button - setWakeupButton(BUTTON_A_PIN); + Power.setWakeupButton(BUTTON_A_PIN); // I2C init - #ifdef M5STACK_FIRE - Wire.begin(21, 22); - #endif + if(I2CEnable) + { + Wire.begin(21, 22); + } if (SerialEnable) { Serial.println("OK"); @@ -58,59 +59,26 @@ void M5Stack::update() { Speaker.update(); } -#ifdef M5STACK_FIRE -// ================ Power IC IP5306 =================== -#define IP5306_ADDR 117 -#define IP5306_REG_SYS_CTL0 0x00 -#define IP5306_REG_READ1 0x71 -#define CHARGE_FULL_BIT 3 - -void M5Stack::setPowerBoostKeepOn(bool en) -{ - Wire.beginTransmission(IP5306_ADDR); - Wire.write(IP5306_REG_SYS_CTL0); - if (en) Wire.write(0x37); // Set bit1: 1 enable 0 disable boost keep on - else Wire.write(0x35); // 0x37 is default reg value - Wire.endTransmission(); +/** + * Function has been move to Power class.(for compatibility) + * This name will be removed in a future release. + */ +void M5Stack::setPowerBoostKeepOn(bool en){ + M5.Power.setPowerBoostKeepOn(en); } - -uint8_t M5Stack::isChargeFull() -{ - uint8_t data; - Wire.beginTransmission(IP5306_ADDR); - Wire.write(IP5306_REG_READ1); - Wire.endTransmission(false); - Wire.requestFrom(IP5306_ADDR, 1); - data = Wire.read(); - if (data & (1 << CHARGE_FULL_BIT)) return true; - else return false; -} -#endif - -// ================== Low power mode ===================== +/** + * Function has been move to Power class.(for compatibility) + * This name will be removed in a future release. + */ void M5Stack::setWakeupButton(uint8_t button) { - _wakeupPin = button; + M5.Power.setWakeupButton(button); } - +/** + * Function has been move to Power class.(for compatibility) + * This name will be removed in a future release. + */ void M5Stack::powerOFF() { - - #ifdef M5STACK_FIRE - // Keep power keep boost on - setPowerBoostKeepOn(true); - #endif - - // power off the Lcd - Lcd.setBrightness(0); - Lcd.sleep(); - - // ESP32 into deep sleep - esp_sleep_enable_ext0_wakeup((gpio_num_t)_wakeupPin , LOW); - - while(digitalRead(_wakeupPin) == LOW) { - delay(10); - } - esp_deep_sleep_start(); + M5.Power.deepSleep(); } - M5Stack M5; diff --git a/src/M5Stack.h b/src/M5Stack.h index e56d1bb..8d7dcec 100644 --- a/src/M5Stack.h +++ b/src/M5Stack.h @@ -20,6 +20,14 @@ M5.begin(); M5.update(); + Power: + M5.Power.setPowerBoostKeepOn() + M5.Power.setCharge(uint8_t mode); + M5.Power.setPowerBoostKeepOn(bool en); + M5.Power.isChargeFull(); + M5.Power.setWakeupButton(uint8_t button); + M5.Power.powerOFF(); + LCD: M5.lcd.setBrightness(uint8_t brightness); M5.Lcd.drawPixel(int16_t x, int16_t y, uint16_t color); @@ -97,24 +105,15 @@ #include "utility/Config.h" #include "utility/Button.h" #include "utility/Speaker.h" - +#include "utility/Power.h" class M5Stack { public: M5Stack(); - - void begin(bool LCDEnable=true, bool SDEnable=true, bool SerialEnable=true); + void begin(bool LCDEnable=true, bool SDEnable=true, bool SerialEnable=true,bool I2CEnable=false); void update(); - #ifdef M5STACK_FIRE - void setPowerBoostKeepOn(bool en); - uint8_t isChargeFull(); - #endif - - void setWakeupButton(uint8_t button); - void powerOFF(); - // Button API #define DEBOUNCE_MS 10 Button BtnA = Button(BUTTON_A_PIN, true, DEBOUNCE_MS); @@ -127,6 +126,9 @@ class M5Stack { // LCD M5Display Lcd = M5Display(); + //Power + POWER Power; + // UART // HardwareSerial Serial0 = HardwareSerial(0); // HardwareSerial Serial2 = HardwareSerial(2); @@ -136,9 +138,16 @@ class M5Stack { MPU9250 IMU = MPU9250(); #endif + /** + * Function has been move to Power class.(for compatibility) + * This name will be removed in a future release. + */ + void setPowerBoostKeepOn(bool en); + void setWakeupButton(uint8_t button); + void powerOFF(); + private: bool isInited; - uint8_t _wakeupPin; }; extern M5Stack M5; diff --git a/src/utility/Power.cpp b/src/utility/Power.cpp new file mode 100644 index 0000000..e37998f --- /dev/null +++ b/src/utility/Power.cpp @@ -0,0 +1,156 @@ +/*----------------------------------------------------------------------* + * M5Stack Bettery/Power Control Library v1.0 * + * * + * This work is licensed under the GNU Lesser General Public * + * License v2.1 * + * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html * + *----------------------------------------------------------------------*/ +#include "Power.h" +#include "../M5Stack.h" + +// ================ Power IC IP5306 =================== +#define IP5306_ADDR (117) +#define IP5306_REG_SYS_CTL0 (0x00) +#define IP5306_REG_READ0 (0x70) +#define IP5306_REG_READ1 (0x71) +#define CHARGE_FULL_BIT ( 3) +#define BOOST_OUT_BIT (0x02) +#define CHARGE_OUT_BIT (0x10) +#define BOOST_ENABLE_BIT (0x80) + +extern M5Stack M5; + +POWER::POWER() +{ +} + +#ifdef M5STACK_FIRE + +bool POWER::setPowerBoostKeepOn(bool en){ + uint8_t data; + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_SYS_CTL0); + Wire.endTransmission(); + + if(Wire.requestFrom(IP5306_ADDR, 1)) + { + data = Wire.read(); + + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_SYS_CTL0); + if (en) Wire.write(data | BOOST_OUT_BIT); + else Wire.write(data &(~BOOST_OUT_BIT)); + Wire.endTransmission(); + return true; + } + return false; +} +#endif + +bool POWER::setCharge(bool en){ + uint8_t data; + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_SYS_CTL0); + Wire.endTransmission(); + + if(Wire.requestFrom(IP5306_ADDR, 1)) + { + data = Wire.read(); + + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_SYS_CTL0); + if (en) Wire.write(data | CHARGE_OUT_BIT); + else Wire.write(data &(~CHARGE_OUT_BIT)); + Wire.endTransmission(); + return true; + } + return false; +} + +bool POWER::isChargeFull(){ + uint8_t data; + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_READ1); + Wire.endTransmission(false); + if(Wire.requestFrom(IP5306_ADDR, 1)) + { + data = Wire.read(); + if (data & (1 << CHARGE_FULL_BIT)) return true; + else return false; + } + return false; +} + +bool POWER::canControl(){ + uint8_t data; + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_READ0); + return(Wire.endTransmission()==0); +} + +bool POWER::isCharging(){ + uint8_t data; + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_READ0); + Wire.endTransmission(false); + if(Wire.requestFrom(IP5306_ADDR, 1)) + { + data = Wire.read(); + if (data & (1 << CHARGE_FULL_BIT)) return true; + else return false; + } + return false; +} + +void POWER::setWakeupButton(uint8_t button) { + _wakeupPin = button; +} + +void POWER::reset() { + esp_restart(); +} + +/* caution: if use off()function on running battery , + M5stack can't restart from Power switch. + (Switch pull up power has down by hard power down)*/ +bool POWER::batteryMode(bool en){ + + uint8_t data; + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_READ1); + Wire.endTransmission(); + + if(Wire.requestFrom(IP5306_ADDR, 1)) + { + data = Wire.read(); + + Wire.beginTransmission(IP5306_ADDR); + Wire.write(IP5306_REG_READ1); + if (en) Wire.write(data | BOOST_ENABLE_BIT); + else Wire.write(data &(~BOOST_ENABLE_BIT)); + Wire.endTransmission(); + return true; + } + return false; +} + +void POWER::deepSleep(){ + + #ifdef M5STACK_FIRE + // Keep power keep boost on + setPowerBoostKeepOn(true); + #endif + + // power off the Lcd + M5.Lcd.setBrightness(0); + M5.Lcd.sleep(); + + // ESP32 into deep sleep + esp_sleep_enable_ext0_wakeup((gpio_num_t)_wakeupPin , LOW); + + while(digitalRead(_wakeupPin) == LOW) { + delay(10); + } + esp_deep_sleep_start(); +} + diff --git a/src/utility/Power.h b/src/utility/Power.h new file mode 100644 index 0000000..b953e2d --- /dev/null +++ b/src/utility/Power.h @@ -0,0 +1,32 @@ +/*----------------------------------------------------------------------* + * M5Stack Bettery/Power Control Library v1.0 * + * * + * This work is licensed under the GNU Lesser General Public * + * License v2.1 * + * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html * + *----------------------------------------------------------------------*/ +#ifndef Power_h +#define Power_h +#include +#include + +class POWER +{ + public: + POWER(); + bool setCharge(bool enabled); + + bool setPowerBoostKeepOn(bool en); + bool canControl(); + bool isChargeFull(); + bool isCharging(); + void setWakeupButton(uint8_t button); + bool batteryMode(bool en); + void deepSleep(); + void reset(); + + private: + uint8_t _wakeupPin; + +}; +#endif