M5Stick MPU9250.h



  • Hi, somebody knows why the display doesn't flip when I turn the device??

    #include <esp_now.h>
    #include <WiFi.h>
    
    #include <Arduino.h>
    #include <U8g2lib.h>
    
    #include "MPU9250.h"
    
    // an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
    MPU9250 IMU(Wire, 0x68);
    //MPU9250 IMU(Wire,0x71);
    int status;
    int orienta;
    
    U8X8_HAVE_HW_SPI
    #include <SPI.h>
    #include <Wire.h>
    #define LedPin 19
    #define IrPin 17
    #define BuzzerPin 26
    
    #define BtnPin 35
    
    
    #define WIFI_CHANNEL 1
    uint8_t localCustomMac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
    const byte maxDataFrameSize = 200;
    
    // must match the controller struct
    struct __attribute__((packed)) DataStruct {
      // char text[32];
      unsigned long time;
    };
    
    DataStruct myData;
    int x = 870; //15 cm dal piano focale in milliseconi
    int cm;
    int feet;
    int inches;
    int INCHES;
    bool unit = 0;
    //bool rotation;
    
    U8G2_SH1107_64X128_1_4W_HW_SPI u8g2(U8G2_R1, /* cs=*/ 14, /* dc=*/ 27, /* reset=*/ 33);
    //U8G2_SH1107_64X128_1_4W_HW_SPI u8g2(rotation, /* cs=*/ 14, /* dc=*/ 27, /* reset=*/ 33);
    
    void setup() {
      delay (1000);
      Serial.begin(115200);
      u8g2.begin();
    
    
      pinMode(BtnPin, INPUT_PULLUP);
      pinMode(BuzzerPin, OUTPUT);
      digitalWrite(BuzzerPin, LOW);
    
      if (digitalRead(BtnPin) == 0) {
        unit = !unit;
    
        //bounce sound
        for (int f = 0; f < 500; f++) {
          digitalWrite(BuzzerPin, HIGH);
          delay(1);
          digitalWrite(BuzzerPin, LOW);
          delay(1);
        }
    
      }
    
      Serial.print("\r\n\r\n");
      WiFi.mode(WIFI_AP);
      Serial.println( WiFi.softAPmacAddress() );
      WiFi.disconnect();
      if (esp_now_init() == ESP_OK)
      {
        Serial.println("ESPNow Init Success!");
      }
      else
      {
        Serial.println("ESPNow Init Failed....");
      }
      esp_now_register_recv_cb(OnDataRecv);
    
    
    
    }
    
    void loop() {
      yield();
    }
    
    void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int data_len)
    {
      memcpy(&myData, incomingData, sizeof(myData));
    
      for (byte n = 0; n < 6; n++) {
        Serial.print (mac_addr[n], HEX);
      }
      cm = (myData.time + x) / 29 / 2;
      inches = (myData.time + x) / 74 / 2;
      feet = inches / 12;
      INCHES = inches % 12; //es x = 7% 5; // X ora contiene 2
    
      if (unit) {
        stampaCM();
      } else {
        stampaFT();
      }
      if (digitalRead(BtnPin) == 0) {
        x = 5800 - myData.time ;
      }
      cappotta();
    
    }
    void cappotta() {
      IMU.readSensor();
      orienta = IMU.getAccelY_mss();
      if (orienta < 0) {
        u8g2.setDisplayRotation(U8G2_R1);
        u8g2.setFlipMode(1);
        //rotation = !rotation;
      } else {
        u8g2.setDisplayRotation(U8G2_R3);
        u8g2.setFlipMode(1);
      }
    }
    void stampaCM() {
      u8g2.firstPage();
      do {
        u8g2.setFont(u8g2_font_7Segments_26x42_mn);
    
        if (cm < 10) {
          u8g2.setCursor(70, 52);
          u8g2.print(cm);
        }
        if (cm < 100 && cm >= 10) {
          u8g2.setCursor(40, 52);
          u8g2.print(cm);
    
        }
        if (cm >= 100) {
          u8g2.setCursor(20, 52);
          u8g2.print(cm);
        }
      } while (u8g2.nextPage());
    }
    
    void stampaFT() {
      u8g2.firstPage();
      IMU.readSensor();
      do {
        u8g2.setFont(u8g2_font_7Segments_26x42_mn);
        u8g2.drawDisc(63, 52, 2, U8G2_DRAW_ALL);
    
        if (feet < 10) {
          u8g2.setCursor(30, 52);
          u8g2.print(feet);
        } else {
          u8g2.setCursor(1, 52);
          u8g2.print(feet);
        }
    
        if (INCHES < 10) {
          u8g2.setCursor(75, 52);
          u8g2.print(INCHES);
        } else {
          u8g2.setCursor(60, 52);
          u8g2.print(INCHES);
        }
      } while (u8g2.nextPage());
    }
    

    thanks a lot


登录后回复
 

与 M5Stack Community 的连接断开,我们正在尝试重连,请耐心等待