2017-04-28 21:53:40 +08:00
|
|
|
// Copyright (c) M5Stack. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
#include "M5Stack.h"
|
|
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
M5Stack::M5Stack() : isInited(0) {
|
2017-09-25 19:47:37 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
}
|
2017-04-28 21:53:40 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
void M5Stack::begin(bool LCDEnable, bool SDEnable, bool SerialEnable) {
|
2017-04-28 21:53:40 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
// Correct init once
|
|
|
|
|
if (isInited) return;
|
|
|
|
|
else isInited = true;
|
2017-06-05 09:26:37 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
// UART
|
|
|
|
|
if (SerialEnable) {
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
Serial.flush();
|
|
|
|
|
delay(50);
|
|
|
|
|
Serial.print("M5Stack initializing...");
|
|
|
|
|
}
|
2017-09-25 19:47:37 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
// LCD INIT
|
|
|
|
|
if (LCDEnable) {
|
|
|
|
|
Lcd.begin();
|
|
|
|
|
}
|
2017-09-25 19:47:37 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
// TF Card
|
|
|
|
|
if (SDEnable) {
|
|
|
|
|
SD.begin(TFCARD_CS_PIN, SPI, 40000000);
|
|
|
|
|
}
|
2017-09-27 19:40:43 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
// TONE
|
|
|
|
|
Speaker.begin();
|
|
|
|
|
|
|
|
|
|
// Set wakeup button
|
|
|
|
|
setWakeupButton(BUTTON_A_PIN);
|
|
|
|
|
|
|
|
|
|
// I2C init
|
|
|
|
|
#ifdef M5STACK_FIRE
|
|
|
|
|
Wire.begin(21, 22);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (SerialEnable) {
|
|
|
|
|
Serial.println("OK");
|
|
|
|
|
}
|
2017-04-28 21:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2017-07-16 16:27:27 +08:00
|
|
|
void M5Stack::update() {
|
2017-04-28 21:53:40 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
//Button update
|
|
|
|
|
BtnA.read();
|
|
|
|
|
BtnB.read();
|
|
|
|
|
BtnC.read();
|
2017-06-05 09:26:37 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
//Speaker update
|
|
|
|
|
Speaker.update();
|
2017-06-05 09:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-26 14:02:30 +08:00
|
|
|
#ifdef M5STACK_FIRE
|
2018-07-10 11:35:09 +08:00
|
|
|
// ================ 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)
|
|
|
|
|
{
|
2018-08-08 11:41:46 +08:00
|
|
|
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();
|
2018-07-10 11:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t M5Stack::isChargeFull()
|
|
|
|
|
{
|
2018-08-08 11:41:46 +08:00
|
|
|
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;
|
2018-07-10 11:35:09 +08:00
|
|
|
}
|
2018-07-26 14:02:30 +08:00
|
|
|
#endif
|
2018-07-10 11:35:09 +08:00
|
|
|
|
|
|
|
|
// ================== Low power mode =====================
|
2017-09-25 19:47:37 +08:00
|
|
|
void M5Stack::setWakeupButton(uint8_t button) {
|
2018-08-08 11:41:46 +08:00
|
|
|
_wakeupPin = button;
|
2017-09-25 19:47:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void M5Stack::powerOFF() {
|
2018-07-26 14:02:30 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
#ifdef M5STACK_FIRE
|
|
|
|
|
// Keep power keep boost on
|
|
|
|
|
setPowerBoostKeepOn(true);
|
|
|
|
|
#endif
|
2018-07-10 11:35:09 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
// power off the Lcd
|
|
|
|
|
Lcd.setBrightness(0);
|
|
|
|
|
Lcd.sleep();
|
2017-09-25 19:47:37 +08:00
|
|
|
|
2018-08-08 11:41:46 +08:00
|
|
|
// ESP32 into deep sleep
|
|
|
|
|
esp_sleep_enable_ext0_wakeup((gpio_num_t)_wakeupPin , LOW);
|
|
|
|
|
|
|
|
|
|
while(digitalRead(_wakeupPin) == LOW) {
|
|
|
|
|
delay(10);
|
|
|
|
|
}
|
|
|
|
|
esp_deep_sleep_start();
|
2017-09-25 19:47:37 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-10 11:35:09 +08:00
|
|
|
|
2017-10-11 17:09:55 +08:00
|
|
|
M5Stack M5;
|