diff --git a/src/i2c_communication.cpp b/src/i2c_communication.cpp index e8baa2a..736723c 100644 --- a/src/i2c_communication.cpp +++ b/src/i2c_communication.cpp @@ -58,29 +58,66 @@ void onReceiveEvent(int bytes) { } } +// // 更新传感器状态和数据 +// unsigned long last_gps_data_time = 0; +// static uint8_t last_receive_cnt = 0; + +// void update_sensor_data() { +// //gps +// //if (millis() > 5000 && gps.charsProcessed() < 10 && digitalRead(GPS_POWER_PIN) == HIGH) +// if (gps.charsProcessed() < 10) { +// sensor_data.gps_sta = 0x00; // 无设备 +// sensor_data.gps_lat = 0.0f; +// sensor_data.gps_lon = 0.0f; +// } else if (!gps.location.isUpdated()) { +// sensor_data.gps_sta = 0x02; // 未搜到信号 +// sensor_data.gps_lat = 0.0f; +// sensor_data.gps_lon = 0.0f; +// } else { +// sensor_data.gps_sta = 0x03; // 正常工作 +// sensor_data.gps_lat = gps.location.lat(); +// sensor_data.gps_lon = gps.location.lng(); +// } + +// //测距 +// //if (receive_cnt == last_receive_cnt && digitalRead(RANGING_POWER_PIN) == HIGH) +// if (receive_cnt == last_receive_cnt){ +// sensor_data.height_sta = 0x00; // 无设备 +// sensor_data.height = 0.0f; +// }else { +// last_receive_cnt = receive_cnt; +// sensor_data.height_sta = 0x03; // 有效高度,正常工作 +// sensor_data.height = (float)distance / 1000.0f; // mm -> m +// } +// } + // 更新传感器状态和数据 unsigned long last_gps_data_time = 0; static uint8_t last_receive_cnt = 0; - +uint32_t count = 0; void update_sensor_data() { - //gps - //if (millis() > 5000 && gps.charsProcessed() < 10 && digitalRead(GPS_POWER_PIN) == HIGH) if (gps.charsProcessed() < 10) { sensor_data.gps_sta = 0x00; // 无设备 sensor_data.gps_lat = 0.0f; sensor_data.gps_lon = 0.0f; - } else if (!gps.location.isUpdated()) { - sensor_data.gps_sta = 0x02; // 未搜到信号 - sensor_data.gps_lat = 0.0f; - sensor_data.gps_lon = 0.0f; - } else { - sensor_data.gps_sta = 0x03; // 正常工作 - sensor_data.gps_lat = gps.location.lat(); - sensor_data.gps_lon = gps.location.lng(); + } + else + { + if (gps.location.isUpdated()) { + count = 0; + sensor_data.gps_sta = 0x03; // 正常工作 + sensor_data.gps_lat = gps.location.lat(); + sensor_data.gps_lon = gps.location.lng(); + } + else + { + count ++; + if (count > 50) { + sensor_data.gps_sta = 0x02; // 未搜到信号 + } + } } - - //测距 - //if (receive_cnt == last_receive_cnt && digitalRead(RANGING_POWER_PIN) == HIGH) + if (receive_cnt == last_receive_cnt){ sensor_data.height_sta = 0x00; // 无设备 sensor_data.height = 0.0f;