#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

#define SCL 16
#define SDA 17

Adafruit_MPU6050 mpu;   // 声明设备
volatile bool initialized = false;

void setup() {
  Serial.begin(115200);
  Wire.begin(SDA, SCL);   // 初始化I2C总线
  initialized = mpu.begin();
  if (initialized) {      // 初始化陀螺仪
    Serial.println("MPU6050 初始化成功");
    mpu.setAccelerometerRange(MPU6050_RANGE_8_G);     // 设置重力加速度的测量范围为±8G
    mpu.setGyroRange(MPU6050_RANGE_500_DEG);          // 设置陀螺仪的测量范围为±500度/秒
    mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);       // 设置滤波器带宽
  } else {
    Serial.println("MPU6050 初始化失败");
  }
}

void loop() {
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  while(initialized){
    mpu.getEvent(&accel, &gyro, &temp);
    printf("-----===== MPU6050 =====-----\n");
    printf("Temp   : %.2f°C\n",temp.temperature);
    printf("Gyro-X : %.2f°\n",gyro.gyro.x *180 / PI);
    printf("Gyro-Y : %.2f°\n",gyro.gyro.y *180 / PI);
    printf("Gyro-Z : %.2f°\n",gyro.gyro.z *180 / PI);
    printf("Acce-X : %.2f g\n",accel.acceleration.x / 9.81);
    printf("Acce-Y : %.2f g\n",accel.acceleration.y / 9.81);
    printf("Acce-Z : %.2f g\n",accel.acceleration.z / 9.81);
    printf("==============================\n");

    vTaskDelay(pdMS_TO_TICKS(1000));
  }

  while(1){vTaskDelay(pdMS_TO_TICKS(100));}
}