2021-03-15 20:06:50 +00:00
|
|
|
/*
|
|
|
|
xsns_84_tof10120.ino - TOF10120 sensor support for Tasmota
|
|
|
|
|
|
|
|
Copyright (C) 2021 Stephan Hadinger 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 USE_I2C
|
2021-06-11 12:27:34 +01:00
|
|
|
#if defined(USE_MPU6886) || defined(USE_MPU_ACCEL) // USE_MPU6886 deprecated, prefer USE_MPU_ACCEL
|
2021-03-15 20:06:50 +00:00
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
#include <MPU_accel.h>
|
2021-03-15 20:06:50 +00:00
|
|
|
/*********************************************************************************************\
|
2021-06-11 12:27:34 +01:00
|
|
|
* MPU6886/MPU9250
|
2021-03-15 20:06:50 +00:00
|
|
|
* Internal chip found in M5Stack devices, using `Wire1` internal I2C bus
|
|
|
|
*
|
|
|
|
* I2C Address: 0x68
|
|
|
|
*
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_85 85
|
|
|
|
#define XI2C_58 58 // See I2CDEVICES.md
|
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
#define MPU_ACCEL_ADDRESS 0x68
|
2021-03-15 20:06:50 +00:00
|
|
|
|
|
|
|
struct {
|
2021-06-11 12:27:34 +01:00
|
|
|
MPU_accel Mpu;
|
|
|
|
uint32_t mpu_model;
|
2021-03-15 20:06:50 +00:00
|
|
|
int16_t ax=0, ay=0, az=0; // accelerator data
|
|
|
|
int16_t gyx=0, gyy=0, gyz=0; // accelerator data
|
2021-06-11 12:27:34 +01:00
|
|
|
uint8_t bus = 0; // I2C bus
|
|
|
|
bool ready = false;
|
|
|
|
} mpu_sensor;
|
2021-03-15 20:06:50 +00:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
const char HTTP_MPU6686[] PROGMEM =
|
2021-06-11 12:27:34 +01:00
|
|
|
"{s}MPU%d acc_x" "{m}%3_f G" "{e}"
|
|
|
|
"{s}MPU%d acc_y" "{m}%3_f G" "{e}"
|
|
|
|
"{s}MPU%d acc_z" "{m}%3_f G" "{e}"
|
|
|
|
"{s}MPU%d gyr_x" "{m}%i dps" "{e}"
|
|
|
|
"{s}MPU%d gyr_y" "{m}%i dps" "{e}"
|
|
|
|
"{s}MPU%d gyr_z" "{m}%i dps" "{e}"
|
2021-03-15 20:06:50 +00:00
|
|
|
;
|
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
void MPU_Show(uint32_t json) {
|
2021-03-15 20:06:50 +00:00
|
|
|
if (json) {
|
2021-06-11 12:27:34 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"MPU%d\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}"),
|
|
|
|
mpu_sensor.mpu_model,
|
|
|
|
mpu_sensor.ax, mpu_sensor.ay, mpu_sensor.az,
|
|
|
|
mpu_sensor.gyx, mpu_sensor.gyy, mpu_sensor.gyz);
|
2021-03-15 20:06:50 +00:00
|
|
|
} else {
|
2021-06-11 12:27:34 +01:00
|
|
|
float ax = mpu_sensor.ax / 1000.0f;
|
|
|
|
float ay = mpu_sensor.ay / 1000.0f;
|
|
|
|
float az = mpu_sensor.az / 1000.0f;
|
|
|
|
WSContentSend_PD(HTTP_MPU6686, mpu_sensor.mpu_model, &ax, mpu_sensor.mpu_model, &ay, mpu_sensor.mpu_model, &az,
|
|
|
|
mpu_sensor.mpu_model, mpu_sensor.gyx, mpu_sensor.mpu_model, mpu_sensor.gyy, mpu_sensor.mpu_model, mpu_sensor.gyz);
|
2021-03-15 20:06:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
void MPU_Detect(void) {
|
2021-03-15 20:06:50 +00:00
|
|
|
#ifdef ESP32
|
2021-06-11 12:27:34 +01:00
|
|
|
if (!I2cSetDevice(MPU_ACCEL_ADDRESS, 0)) {
|
|
|
|
if (!I2cSetDevice(MPU_ACCEL_ADDRESS, 1)) { return; } // check on bus 1
|
|
|
|
mpu_sensor.bus = 1;
|
|
|
|
mpu_sensor.Mpu.setBus(mpu_sensor.bus); // switch to bus 1
|
2021-03-15 20:06:50 +00:00
|
|
|
}
|
|
|
|
#else
|
2021-06-11 12:27:34 +01:00
|
|
|
if (!I2cSetDevice(MPU_ACCEL_ADDRESS)) { return; }
|
2021-03-15 20:06:50 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
if (mpu_sensor.Mpu.Init() == 0) {
|
|
|
|
char model_name[16];
|
|
|
|
mpu_sensor.mpu_model = mpu_sensor.Mpu.getModel();
|
|
|
|
snprintf_P(model_name, sizeof(model_name), PSTR("MPU%04d"), mpu_sensor.mpu_model);
|
|
|
|
I2cSetActiveFound(MPU_ACCEL_ADDRESS, model_name, mpu_sensor.bus);
|
|
|
|
mpu_sensor.ready = true;
|
|
|
|
}
|
2021-03-15 20:06:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
void MPU_Every_Second(void) {
|
|
|
|
mpu_sensor.Mpu.getAccelDataInt(&mpu_sensor.ax, &mpu_sensor.ay, &mpu_sensor.az);
|
|
|
|
mpu_sensor.Mpu.getGyroDataInt(&mpu_sensor.gyx, &mpu_sensor.gyy, &mpu_sensor.gyz);
|
2021-03-15 20:06:50 +00:00
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR(">> Acc x=%i y=%i z=%i gx=%i gy=%i gz=%i"), mpu_sensor.ax, mpu_sensor.ay, mpu_sensor.az,
|
|
|
|
// mpu_sensor.gyx, mpu_sensor.gyy, mpu_sensor.gyz);
|
2021-03-15 20:06:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xsns85(uint8_t function) {
|
|
|
|
if (!I2cEnabled(XI2C_58)) { return false; }
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (FUNC_INIT == function) {
|
2021-06-11 12:27:34 +01:00
|
|
|
MPU_Detect();
|
2021-03-15 20:06:50 +00:00
|
|
|
}
|
2021-06-11 12:27:34 +01:00
|
|
|
else if (mpu_sensor.ready) {
|
2021-03-15 20:06:50 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
2021-06-11 12:27:34 +01:00
|
|
|
MPU_Every_Second();
|
2021-03-15 20:06:50 +00:00
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
2021-06-11 12:27:34 +01:00
|
|
|
MPU_Show(1);
|
2021-03-15 20:06:50 +00:00
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
2021-06-11 12:27:34 +01:00
|
|
|
MPU_Show(0);
|
2021-03-15 20:06:50 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:27:34 +01:00
|
|
|
#endif // USE_MPU6886 USE_MPU_ACCEL
|
2021-03-15 20:06:50 +00:00
|
|
|
#endif // USE_I2C
|