2020-09-01 06:20:47 +01:00
|
|
|
/*
|
|
|
|
xdrv_83_esp32watch.ino - ESP32 TTGO watch support for Tasmota
|
|
|
|
|
|
|
|
Copyright (C) 2020 Gerhard Mutz and Theo Arends
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ESP32
|
|
|
|
#ifdef USE_TTGO_WATCH
|
|
|
|
|
|
|
|
#include <axp20x.h>
|
|
|
|
#include <i2c_bus.h>
|
|
|
|
#include <bma.h>
|
|
|
|
#include <soc/rtc.h>
|
|
|
|
|
|
|
|
#define XDRV_83 83
|
|
|
|
|
|
|
|
#define AXP202_INT 35
|
|
|
|
|
|
|
|
struct TTGO_ADC {
|
|
|
|
float vbus_v;
|
|
|
|
float vbus_c;
|
|
|
|
float batt_v;
|
|
|
|
float batt_c;
|
|
|
|
float temp;
|
|
|
|
uint16_t per;
|
|
|
|
} ttgo_adc;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
Q_EVENT_WIFI_SCAN_DONE,
|
|
|
|
Q_EVENT_WIFI_CONNECT,
|
|
|
|
Q_EVENT_BMA_INT,
|
|
|
|
Q_EVENT_AXP_INT,
|
|
|
|
} ;
|
|
|
|
|
|
|
|
#define BMA423_INT1 39
|
|
|
|
#define BMA423_INT2 0
|
|
|
|
|
|
|
|
#define WATCH_FLAG_SLEEP_MODE _BV(1)
|
|
|
|
#define WATCH_FLAG_SLEEP_EXIT _BV(2)
|
|
|
|
#define WATCH_FLAG_BMA_IRQ _BV(3)
|
|
|
|
#define WATCH_FLAG_AXP_IRQ _BV(4)
|
|
|
|
|
|
|
|
struct TTGO_globs {
|
|
|
|
AXP20X_Class *ttgo_power = nullptr;
|
|
|
|
I2CBus *i2c = nullptr;
|
|
|
|
BMA *bma = nullptr;
|
|
|
|
QueueHandle_t g_event_queue_handle = NULL;
|
|
|
|
//EventGroupHandle_t g_event_group = NULL;
|
|
|
|
EventGroupHandle_t isr_group = NULL;
|
|
|
|
bool lenergy = false;
|
|
|
|
bool bma_double_click = false;
|
|
|
|
bool bma_click = false;
|
|
|
|
bool bma_button = false;
|
2020-09-23 11:53:52 +01:00
|
|
|
bool power_ok = false;
|
2020-09-01 06:20:47 +01:00
|
|
|
} ttgo_globs;
|
|
|
|
|
|
|
|
|
|
|
|
void TTGO_Init(void) {
|
|
|
|
ttgo_globs.ttgo_power = new AXP20X_Class();
|
|
|
|
ttgo_globs.i2c = new I2CBus();
|
|
|
|
initPower();
|
|
|
|
|
|
|
|
#ifdef USE_BMA423
|
|
|
|
ttgo_globs.bma = new BMA(*ttgo_globs.i2c);
|
|
|
|
if (ttgo_globs.bma->begin()) {
|
|
|
|
I2cSetActiveFound(BMA4_I2C_ADDR_SECONDARY, "BMA423");
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ttgo_globs.bma->attachInterrupt();
|
|
|
|
pinMode(BMA423_INT1, INPUT);
|
|
|
|
attachInterrupt(BMA423_INT1, [] {
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
EventBits_t bits = xEventGroupGetBitsFromISR(ttgo_globs.isr_group);
|
|
|
|
if (bits & WATCH_FLAG_SLEEP_MODE)
|
|
|
|
{
|
|
|
|
// Use an XEvent when waking from low energy sleep mode.
|
|
|
|
xEventGroupSetBitsFromISR(ttgo_globs.isr_group, WATCH_FLAG_SLEEP_EXIT | WATCH_FLAG_BMA_IRQ, &xHigherPriorityTaskWoken);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// Use the XQueue mechanism when we are already awake.
|
|
|
|
uint8_t data = Q_EVENT_BMA_INT;
|
|
|
|
xQueueSendFromISR(ttgo_globs.g_event_queue_handle, &data, &xHigherPriorityTaskWoken);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xHigherPriorityTaskWoken)
|
|
|
|
{
|
|
|
|
portYIELD_FROM_ISR ();
|
|
|
|
}
|
|
|
|
}, RISING);
|
|
|
|
struct bma423_axes_remap remap_data;
|
|
|
|
|
|
|
|
remap_data.x_axis = 0;
|
|
|
|
remap_data.x_axis_sign = 1;
|
|
|
|
remap_data.y_axis = 1;
|
|
|
|
remap_data.y_axis_sign = 0;
|
|
|
|
remap_data.z_axis = 2;
|
|
|
|
remap_data.z_axis_sign = 1;
|
|
|
|
|
|
|
|
ttgo_globs.bma->set_remap_axes(&remap_data);
|
|
|
|
// Enable the double tap wakeup.
|
|
|
|
ttgo_globs.bma->enableWakeupInterrupt(true);
|
|
|
|
ttgo_globs.bma->enableAnyNoMotionInterrupt(true);
|
|
|
|
ttgo_globs.bma->enableAccel();
|
2020-09-23 11:53:52 +01:00
|
|
|
#endif // USE_BMA423
|
2020-09-01 06:20:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initPower(void) {
|
|
|
|
int ret = ttgo_globs.ttgo_power->begin(axpReadBytes, axpWriteBytes);
|
|
|
|
if (ret == AXP_FAIL) {
|
|
|
|
//DBGX("AXP Power begin failed");
|
2020-09-23 11:53:52 +01:00
|
|
|
// Serial.printf("AXP202 failed\n" );
|
2020-09-01 06:20:47 +01:00
|
|
|
} else {
|
|
|
|
I2cSetActiveFound(AXP202_SLAVE_ADDRESS, "AXP202");
|
2020-09-23 11:53:52 +01:00
|
|
|
ttgo_globs.power_ok = true;
|
|
|
|
// Serial.printf("AXP202 OK\n" );
|
|
|
|
|
2020-09-01 06:20:47 +01:00
|
|
|
//Change the button boot time to 4 seconds
|
|
|
|
ttgo_globs.ttgo_power->setShutdownTime(AXP_POWER_OFF_TIME_4S);
|
|
|
|
// Turn off the charging instructions, there should be no
|
|
|
|
ttgo_globs.ttgo_power->setChgLEDMode(AXP20X_LED_OFF);
|
|
|
|
// Turn off external enable
|
|
|
|
ttgo_globs.ttgo_power->setPowerOutPut(AXP202_EXTEN, false);
|
|
|
|
//axp202 allows maximum charging current of 1800mA, minimum 300mA
|
|
|
|
ttgo_globs.ttgo_power->setChargeControlCur(300);
|
|
|
|
|
|
|
|
ttgo_globs.ttgo_power->adc1Enable(AXP202_VBUS_VOL_ADC1 | AXP202_VBUS_CUR_ADC1 | AXP202_BATT_CUR_ADC1 | AXP202_BATT_VOL_ADC1, true);
|
|
|
|
|
|
|
|
ttgo_globs.ttgo_power->adc2Enable(AXP202_TEMP_MONITORING_ADC2, true);
|
|
|
|
|
|
|
|
ttgo_globs.ttgo_power->enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_FINISHED_IRQ, AXP202_ON);
|
|
|
|
ttgo_globs.ttgo_power->clearIRQ();
|
|
|
|
|
|
|
|
ttgo_globs.ttgo_power->setPowerOutPut(AXP202_LDO2, AXP202_ON);
|
|
|
|
|
|
|
|
ttgo_globs.isr_group = xEventGroupCreate();
|
|
|
|
ttgo_globs.g_event_queue_handle = xQueueCreate(20, sizeof(uint8_t));
|
|
|
|
|
|
|
|
// Connection interrupted to the specified pin
|
|
|
|
pinMode(AXP202_INT, INPUT);
|
|
|
|
attachInterrupt(AXP202_INT, [] {
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
EventBits_t bits = xEventGroupGetBitsFromISR(ttgo_globs.isr_group);
|
|
|
|
if (bits & WATCH_FLAG_SLEEP_MODE) {
|
|
|
|
// Use an XEvent when waking from low energy sleep mode.
|
|
|
|
xEventGroupSetBitsFromISR(ttgo_globs.isr_group, WATCH_FLAG_SLEEP_EXIT | WATCH_FLAG_AXP_IRQ, &xHigherPriorityTaskWoken);
|
|
|
|
} else {
|
|
|
|
// Use the XQueue mechanism when we are already awake.
|
|
|
|
uint8_t data = Q_EVENT_AXP_INT;
|
|
|
|
xQueueSendFromISR(ttgo_globs.g_event_queue_handle, &data, &xHigherPriorityTaskWoken);
|
|
|
|
}
|
|
|
|
if (xHigherPriorityTaskWoken) {
|
|
|
|
portYIELD_FROM_ISR ();
|
|
|
|
}
|
|
|
|
}, FALLING);
|
2020-09-23 11:53:52 +01:00
|
|
|
|
2020-09-01 06:20:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t axpWriteBytes(uint8_t devAddress, uint8_t regAddress, uint8_t *data, uint8_t len) {
|
|
|
|
ttgo_globs.i2c->writeBytes(devAddress, regAddress, data, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t axpReadBytes(uint8_t devAddress, uint8_t regAddress, uint8_t *data, uint8_t len) {
|
|
|
|
ttgo_globs.i2c->readBytes(devAddress, regAddress, data, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TTGO_GetADC(void) {
|
|
|
|
ttgo_adc.vbus_v = ttgo_globs.ttgo_power->getVbusVoltage();
|
|
|
|
ttgo_adc.vbus_c = ttgo_globs.ttgo_power->getVbusCurrent();
|
|
|
|
ttgo_adc.batt_v = ttgo_globs.ttgo_power->getBattVoltage();
|
|
|
|
ttgo_adc.per = ttgo_globs.ttgo_power->getBattPercentage();
|
|
|
|
ttgo_adc.batt_c = ttgo_globs.ttgo_power->getBattDischargeCurrent();
|
|
|
|
ttgo_adc.temp = ttgo_globs.ttgo_power->getTemp();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_TTGO[] PROGMEM =
|
|
|
|
"{s}TTGO " "VBUS Voltage" "{m}%s mV" "{e}"
|
|
|
|
"{s}TTGO " "VBUS Current" "{m}%s mA" "{e}"
|
|
|
|
"{s}TTGO " "BATT Voltage" "{m}%s mV" "{e}"
|
|
|
|
"{s}TTGO " "BATT Current" "{m}%s mA" "{e}"
|
|
|
|
"{s}TTGO " "BATT Percentage" "{m}%d %%" "{e}"
|
|
|
|
"{s}TTGO " "Temperature" "{m}%s C" "{e}";
|
|
|
|
#ifdef USE_BMA423
|
|
|
|
const char HTTP_TTGO_BMA[] PROGMEM =
|
|
|
|
"{s}TTGO " "BMA x" "{m}%d mg" "{e}"
|
|
|
|
"{s}TTGO " "BMA y" "{m}%d mg" "{e}"
|
|
|
|
"{s}TTGO " "BMA z" "{m}%d mg" "{e}";
|
|
|
|
#endif // USE_BMA423
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
|
|
|
|
|
|
|
void TTGO_WebShow(uint32_t json) {
|
|
|
|
|
2020-09-23 11:53:52 +01:00
|
|
|
if (ttgo_globs.power_ok == false) return;
|
|
|
|
|
2020-09-01 06:20:47 +01:00
|
|
|
TTGO_GetADC();
|
|
|
|
|
|
|
|
char vstring[32];
|
|
|
|
char cstring[32];
|
|
|
|
char bvstring[32];
|
|
|
|
char bcstring[32];
|
|
|
|
char tstring[32];
|
|
|
|
dtostrfd(ttgo_adc.vbus_v,2,vstring);
|
|
|
|
dtostrfd(ttgo_adc.vbus_c,2,cstring);
|
|
|
|
dtostrfd(ttgo_adc.batt_v,2,bvstring);
|
|
|
|
dtostrfd(ttgo_adc.batt_c,2,bcstring);
|
|
|
|
dtostrfd(ttgo_adc.temp,2,tstring);
|
|
|
|
|
|
|
|
#ifdef USE_BMA423
|
|
|
|
Accel acc;
|
|
|
|
bool res = ttgo_globs.bma->getAccel(acc);
|
|
|
|
#endif // USE_BMA423
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(PSTR(",\"TTGO_WATCH\":{\"VBV\":%s,\"VBC\":%s,\"BV\":%s,\"BC\":%s,\"BP\":%d,\"CT\":%s"),
|
|
|
|
vstring, cstring, bvstring, bcstring, ttgo_adc.per, tstring);
|
|
|
|
#ifdef USE_BMA423
|
|
|
|
ResponseAppend_P(PSTR(",\"BMAX\":%d,\"BMAY\":%d,\"BMAZ\":%d"),acc.x,acc.y,acc.z);
|
|
|
|
#endif
|
|
|
|
ResponseJsonEnd();
|
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_TTGO,vstring,cstring,bvstring,bcstring,ttgo_adc.per,tstring);
|
|
|
|
#ifdef USE_BMA423
|
|
|
|
WSContentSend_PD(HTTP_TTGO_BMA,acc.x,acc.y,acc.z);
|
|
|
|
#endif // USE_BMA423
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void enableLDO3(bool en = true) {
|
|
|
|
if (!ttgo_globs.ttgo_power) return;
|
|
|
|
ttgo_globs.ttgo_power->setLDO3Mode(1);
|
|
|
|
ttgo_globs.ttgo_power->setPowerOutPut(AXP202_LDO3, en);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTGO_audio_power(bool power) {
|
|
|
|
enableLDO3(power);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char TTGO_Commands[] PROGMEM = "TTGO|"
|
|
|
|
"LSLP";
|
|
|
|
|
|
|
|
void (* const TTTGO_Command[])(void) PROGMEM = {
|
|
|
|
&TTGO_LightSleep};
|
|
|
|
|
|
|
|
void TTGO_LightSleep(void) {
|
|
|
|
int32_t ttgo_sleeptime;
|
|
|
|
|
|
|
|
// switch device off
|
|
|
|
if ((abs(XdrvMailbox.payload) >= 10) && (abs(XdrvMailbox.payload) <= 3600*24)) {
|
|
|
|
ttgo_sleeptime = XdrvMailbox.payload;
|
|
|
|
} else {
|
|
|
|
ttgo_sleeptime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResponseCmndNumber(ttgo_sleeptime);
|
|
|
|
|
|
|
|
TTGO_Sleep(ttgo_sleeptime);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTGO_Sleep(int32_t stime) {
|
|
|
|
int32_t ttgo_sleeptime;
|
|
|
|
|
|
|
|
ttgo_sleeptime = stime;
|
|
|
|
|
2020-09-23 11:53:52 +01:00
|
|
|
#ifdef USE_DISPLAY
|
2020-09-01 06:20:47 +01:00
|
|
|
DisplayOnOff(0);
|
2020-09-23 11:53:52 +01:00
|
|
|
#endif
|
2020-09-01 06:20:47 +01:00
|
|
|
if (ttgo_sleeptime>=0) {
|
|
|
|
// ligh sleep mode
|
|
|
|
WifiShutdown();
|
|
|
|
SettingsSaveAll();
|
|
|
|
RtcSettingsSave();
|
|
|
|
ttgo_globs.lenergy = true;
|
|
|
|
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_2M);
|
|
|
|