| 153 | } |
| 154 | |
| 155 | void loop() { |
| 156 | tpsCounter.update(); |
| 157 | globalTimer.tick(); |
| 158 | SerialCommands::update(); |
| 159 | OTA::otaUpdate(); |
| 160 | networkManager.update(); |
| 161 | |
| 162 | #if DEBUG_MEASURE_SENSOR_TIME_TAKEN |
| 163 | sensorMeasurer.before(); |
| 164 | #endif |
| 165 | sensorManager.update(); |
| 166 | #if DEBUG_MEASURE_SENSOR_TIME_TAKEN |
| 167 | sensorMeasurer.after(); |
| 168 | #endif |
| 169 | |
| 170 | battery.Loop(); |
| 171 | ledManager.update(); |
| 172 | I2CSCAN::update(); |
| 173 | #ifdef TARGET_LOOPTIME_MICROS |
| 174 | long elapsed = (micros() - loopTime); |
| 175 | if (elapsed < TARGET_LOOPTIME_MICROS) { |
| 176 | long sleepus = TARGET_LOOPTIME_MICROS - elapsed - 100; // µs to sleep |
| 177 | long sleepms = sleepus / 1000; // ms to sleep |
| 178 | if (sleepms > 0) // if >= 1 ms |
| 179 | { |
| 180 | delay(sleepms); // sleep ms = save power |
| 181 | sleepus -= sleepms * 1000; |
| 182 | } |
| 183 | if (sleepus > 100) { |
| 184 | delayMicroseconds(sleepus); |
| 185 | } |
| 186 | } |
| 187 | loopTime = micros(); |
| 188 | #endif |
| 189 | #if defined(PRINT_STATE_EVERY_MS) && PRINT_STATE_EVERY_MS > 0 |
| 190 | unsigned long now = millis(); |
| 191 | if (lastStatePrint + PRINT_STATE_EVERY_MS < now) { |
| 192 | lastStatePrint = now; |
| 193 | SerialCommands::printState(); |
| 194 | } |
| 195 | #endif |
| 196 | } |