/* Accelerometer initialization and offset settings Returns true if MPU is connected, false if not */ bool InitAccelero () { Wire.begin(SDA, SCL); accelgyro.initialize(); bool Success = accelgyro.testConnection(); // verify connection if (Success) { Serial.println("MPU6050 connection successful"); // gyro offsets, scaled for min sensitivity accelgyro.setXGyroOffset(29); accelgyro.setYGyroOffset(-30); accelgyro.setZGyroOffset(-45); accelgyro.setXAccelOffset(-3499); accelgyro.setYAccelOffset(-5049); accelgyro.setZAccelOffset(887); } else Serial.println("MPU6050 connection failed"); return Success; } /* Splashscreen on small display */ void SplashScreen() { Serial.println ("SPLASHSCREEN"); petit.setRotation(3); petit.setFont(&Open_Sans_ExtraBold_22); uint16_t w = petit.width(); uint16_t h = petit.height(); petit.setFullWindow(); petit.firstPage(); petit.fillScreen(GxEPD_WHITE); do { petit.drawRect(0, 0, w, h, GxEPD_BLACK); int x = w / 2 - TextCenter ("LESEPT'S", 0); petit.setCursor(x, h * 0.5); petit.print ("LESEPT'S"); petit.setFont(&Open_Sans_ExtraBold_14); x = w / 2 - TextCenter ("WEATHER BOX", 0); petit.setCursor(x, h * 0.65); petit.print ("WEATHER BOX"); } while (petit.nextPage()); petit.powerOff(); } /* Sensors and displays initialization */ bool InitSensorsDisplays () { // Sensors dht.setup(DHT_PIN, DHTesp::DHT22); bool AcceleroSuccess = InitAccelero (); // Init displays pinMode(RST_PIN, OUTPUT); delay(20); digitalWrite(RST_PIN, LOW); delay(20); digitalWrite(RST_PIN, HIGH); delay(200); petit.init(0); //115200); // enable diagnostic output on Serial grand.init(0); //115200); // enable diagnostic output on Serial petit.setTextColor(GxEPD_BLACK); grand.setTextColor(GxEPD_BLACK); if (bootCount == 0) SplashScreen(); return AcceleroSuccess; } /* Reads acceleratin values and returns orientation along xyz axes */ byte OrientAccelero () { int16_t ax, ay, az; int16_t gx, gy, gz; byte Orientation = 0; int Seuil = 8000; // read raw accel/gyro measurements from device accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); if (ax < -1 * Seuil) Orientation = Xmoins; else if (ax > Seuil) Orientation = Xplus; else if (ay < -1 * Seuil) Orientation = Ymoins; else if (ay > Seuil) Orientation = Yplus; else if (az < -1 * Seuil) Orientation = Zmoins; else if (az > Seuil) Orientation = Zplus; else Orientation = Unknown; return Orientation; } /* Decide what to display depending on orientation */ void SetDisplaysOrientation (byte Orientation, bool MaJ) { // Decide if the displays must be updated bool updateDisplay = (!sameOrientation) | MaJ; switch (Orientation) { case Xmoins: // 1 // time + daily forecast if (updateDisplay) PetitDailyWeatherH (1); break; case Yplus: // 2 // current weather + hourly forecast (time + 1 to 3) if (!updateDisplay) break; GrandCurrentWeatherV (); PetitHourlyWeather1V (1); break; case Ymoins: // 3 // date + hourly forecast (time + 6, 9, 12) if (!updateDisplay) break; GrandDateV (1); PetitHourlyWeather1V (3); break; case Xplus: // 0 // temperature, humidity + daily forecast { // Get temperature and humidity from DHT22 float temperature = dht.getTemperature(); float humidite = dht.getHumidity(); Serial.printf ("Temperature : %4.1fC\tHumidite : %4.1f%%\n", temperature, humidite); GrandTH2H (temperature, humidite, updateDisplay); if (updateDisplay) PetitDailyWeatherH (3); break; } case Zmoins: // 5 // display battery level { float Voltage = VoltMeasure(); if (updateDisplay || abs(Voltage - oldVoltage) > 0.1) PetitBattery (Voltage); oldVoltage = Voltage; break; } case Zplus: // 4 // display random citation { byte nbCit = esp_random() % totalCitNb; if (updateDisplay) CitationHoriz (Citation[nbCit], Author[nbCit]); break; } case Unknown: // 6 default: // time + daily forecast if (updateDisplay) PetitDailyWeatherH (1); break; } } /* Process orientation value to provide information to the displays */ void ProcessAccelero (bool AcceleroSuccess) { sameOrientation = false; if (AcceleroSuccess) { if (bootCount != 0) OrientationOld = Orientation; Orientation = OrientAccelero (); Serial.printf ("Orientation old : %d - Orientation : %d\n", OrientationOld, Orientation); if (OrientationOld == Orientation) sameOrientation = true; } }