Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : #pragma once 16 : 17 : #include "AP_TemperatureSensor_config.h" 18 : 19 : #if AP_TEMPERATURE_SENSOR_ENABLED 20 : 21 : #include "AP_TemperatureSensor_Params.h" 22 : 23 : // maximum number of Temperature Sensors 24 : #ifndef AP_TEMPERATURE_SENSOR_MAX_INSTANCES 25 : #define AP_TEMPERATURE_SENSOR_MAX_INSTANCES 3 26 : #endif 27 : 28 : // first sensor is always the primary sensor 29 : #ifndef AP_TEMPERATURE_SENSOR_PRIMARY_INSTANCE 30 : #define AP_TEMPERATURE_SENSOR_PRIMARY_INSTANCE 0 31 : #endif 32 : 33 : // declare backend class 34 : class AP_TemperatureSensor_Backend; 35 : class AP_TemperatureSensor_TSYS01; 36 : class AP_TemperatureSensor_MCP9600; 37 : class AP_TemperatureSensor_MAX31865; 38 : class AP_TemperatureSensor_TSYS03; 39 : class AP_TemperatureSensor_Analog; 40 : class AP_TemperatureSensor_MLX90614; 41 : class AP_TemperatureSensor_TMP119; 42 : 43 : class AP_TemperatureSensor 44 : { 45 : friend class AP_TemperatureSensor_Backend; 46 : friend class AP_TemperatureSensor_TSYS01; 47 : friend class AP_TemperatureSensor_MCP9600; 48 : friend class AP_TemperatureSensor_MAX31865; 49 : friend class AP_TemperatureSensor_TSYS03; 50 : friend class AP_TemperatureSensor_Analog; 51 : friend class AP_TemperatureSensor_DroneCAN; 52 : friend class AP_TemperatureSensor_MLX90614; 53 : friend class AP_TemperatureSensor_TMP119; 54 : 55 : public: 56 : 57 : // Constructor 58 : AP_TemperatureSensor(); 59 : 60 : CLASS_NO_COPY(AP_TemperatureSensor); 61 : 62 0 : static AP_TemperatureSensor *get_singleton() { return _singleton; } 63 : 64 : // Return the number of temperature sensors instances 65 : uint8_t num_instances(void) const { return _num_instances; } 66 : 67 : // detect and initialise any available temperature sensors 68 : __INITFUNC__ void init(); 69 : 70 : // Update the temperature for all temperature sensors 71 : void update(); 72 : 73 : // return temperature from sensor - in degrees Celsius 74 : bool get_temperature(float &temp, const uint8_t instance = AP_TEMPERATURE_SENSOR_PRIMARY_INSTANCE) const; 75 : 76 : bool healthy(const uint8_t instance = AP_TEMPERATURE_SENSOR_PRIMARY_INSTANCE) const; 77 : 78 : // accessors to params 79 : AP_TemperatureSensor_Params::Type get_type(const uint8_t instance = AP_TEMPERATURE_SENSOR_PRIMARY_INSTANCE) const; 80 : AP_TemperatureSensor_Params::Source get_source(const uint8_t instance = AP_TEMPERATURE_SENSOR_PRIMARY_INSTANCE) const; 81 : int32_t get_source_id(const uint8_t instance = AP_TEMPERATURE_SENSOR_PRIMARY_INSTANCE) const; 82 : 83 : static const struct AP_Param::GroupInfo var_info[]; 84 : static const struct AP_Param::GroupInfo *backend_var_info[AP_TEMPERATURE_SENSOR_MAX_INSTANCES]; 85 : 86 : protected: 87 : // parameters 88 : AP_TemperatureSensor_Params _params[AP_TEMPERATURE_SENSOR_MAX_INSTANCES]; 89 : 90 : private: 91 : static AP_TemperatureSensor *_singleton; 92 : 93 : // The TemperatureSensor_State structure is filled in by the backend driver 94 : struct TemperatureSensor_State { 95 : uint32_t last_time_ms; // time when the sensor was last read in milliseconds 96 : float temperature; // temperature (deg C) 97 : uint8_t instance; // instance number 98 : const struct AP_Param::GroupInfo *var_info; 99 : }; 100 : 101 : TemperatureSensor_State _state[AP_TEMPERATURE_SENSOR_MAX_INSTANCES]; 102 : AP_TemperatureSensor_Backend *drivers[AP_TEMPERATURE_SENSOR_MAX_INSTANCES]; 103 : 104 : uint8_t _num_instances; // number of temperature sensors 105 : 106 : #if HAL_LOGGING_ENABLED 107 : enum class LoggingType : uint8_t { 108 : All = 1, 109 : SourceNone = 2, 110 : }; 111 : AP_Enum<LoggingType> _logging_type; 112 : #endif 113 : 114 : }; 115 : 116 : namespace AP { 117 : AP_TemperatureSensor &temperature_sensor(); 118 : }; 119 : 120 : #endif // AP_TEMPERATURE_SENSOR_ENABLED