|
| 1 | +/* |
| 2 | + * This file is part of INAV Project. |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 6 | + * You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + * |
| 8 | + * Alternatively, the contents of this file may be used under the terms |
| 9 | + * of the GNU General Public License Version 3, as described below: |
| 10 | + * |
| 11 | + * This file is free software: you may copy, redistribute and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by the |
| 13 | + * Free Software Foundation, either version 3 of the License, or (at your |
| 14 | + * option) any later version. |
| 15 | + * |
| 16 | + * This file is distributed in the hope that it will be useful, but |
| 17 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 19 | + * Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU General Public License |
| 22 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdbool.h> |
| 26 | +#include <stdint.h> |
| 27 | + |
| 28 | +#include "platform.h" |
| 29 | + |
| 30 | +#if defined(USE_BARO_CRSF) |
| 31 | + |
| 32 | +#include "build/build_config.h" |
| 33 | + |
| 34 | +#include "common/utils.h" |
| 35 | +#include "common/time.h" |
| 36 | + |
| 37 | +#include "drivers/time.h" |
| 38 | +#include "drivers/barometer/barometer.h" |
| 39 | +#include "drivers/barometer/barometer_crsf.h" |
| 40 | + |
| 41 | +#include "sensors/sensors.h" |
| 42 | +#include "sensors/barometer.h" |
| 43 | +#include "fc/runtime_config.h" |
| 44 | + |
| 45 | +#define CRSF_BARO_TIMEOUT_MS 250 |
| 46 | + |
| 47 | +static int32_t crsfBaroPressure; |
| 48 | +static int32_t crsfBaroTemperature; |
| 49 | +static timeMs_t crsfBaroLastUpdateMs; |
| 50 | +static bool crsfBaroStarted = false; |
| 51 | + |
| 52 | +static bool crsfBaroStartGet(baroDev_t *baro) |
| 53 | +{ |
| 54 | + UNUSED(baro); |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +static bool crsfBaroCalculate(baroDev_t *baro, int32_t *pressure, int32_t *temperature) |
| 59 | +{ |
| 60 | + UNUSED(baro); |
| 61 | + |
| 62 | + if ((millis() - crsfBaroLastUpdateMs) > CRSF_BARO_TIMEOUT_MS) { |
| 63 | + sensorsClear(SENSOR_BARO); |
| 64 | + crsfBaroStarted = false; |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if (pressure) |
| 69 | + *pressure = crsfBaroPressure; |
| 70 | + |
| 71 | + if (temperature) |
| 72 | + *temperature = crsfBaroTemperature; |
| 73 | + |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +void crsfBaroReceiveNewData(int32_t pressurePa, int32_t temperature) |
| 78 | +{ |
| 79 | + crsfBaroPressure = pressurePa; |
| 80 | + crsfBaroTemperature = temperature; |
| 81 | + crsfBaroLastUpdateMs = millis(); |
| 82 | + |
| 83 | + if (crsfBaroStarted == false && !ARMING_FLAG(WAS_EVER_ARMED)) { |
| 84 | + baroStartCalibration(); |
| 85 | + crsfBaroStarted = true; |
| 86 | + sensorsSet(SENSOR_BARO); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +bool crsfBaroDetect(baroDev_t *baro) |
| 91 | +{ |
| 92 | + crsfBaroPressure = 101325; |
| 93 | + crsfBaroTemperature = 2500; |
| 94 | + crsfBaroLastUpdateMs = 0; |
| 95 | + |
| 96 | + baro->ut_delay = 10000; |
| 97 | + baro->get_ut = crsfBaroStartGet; |
| 98 | + baro->start_ut = crsfBaroStartGet; |
| 99 | + |
| 100 | + baro->up_delay = 10000; |
| 101 | + baro->start_up = crsfBaroStartGet; |
| 102 | + baro->get_up = crsfBaroStartGet; |
| 103 | + |
| 104 | + baro->calculate = crsfBaroCalculate; |
| 105 | + |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +#endif |
0 commit comments