Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 3 | #include "interfaces.hpp" |
| 4 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | #include <string> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 6 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 7 | namespace pid_control |
| 8 | { |
| 9 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 10 | /** |
| 11 | * Abstract base class for all sensors. |
| 12 | */ |
| 13 | class Sensor |
| 14 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 15 | public: |
Patrick Venture | a510ea2 | 2019-02-08 09:30:49 -0800 | [diff] [blame] | 16 | /** |
| 17 | * Given a sensor's type, return the default timeout value. |
| 18 | * A timeout of 0 means there isn't a timeout for this sensor. |
| 19 | * By default a fan sensor isn't checked for a timeout, whereas |
| 20 | * any of sensor is meant to be sampled once per second. By default. |
| 21 | * |
| 22 | * @param[in] type - the sensor type (e.g. fan) |
| 23 | * @return the default timeout for that type (in seconds). |
| 24 | */ |
| 25 | static int64_t getDefaultTimeout(const std::string& type) |
| 26 | { |
| 27 | return (type == "fan") ? 0 : 2; |
| 28 | } |
| 29 | |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 30 | Sensor(const std::string& name, int64_t timeout) : |
| 31 | _name(name), _timeout(timeout) |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 32 | {} |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 33 | |
Patrick Williams | 8c05112 | 2023-05-10 07:50:59 -0500 | [diff] [blame] | 34 | virtual ~Sensor() {} |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 35 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 36 | virtual ReadReturn read(void) = 0; |
| 37 | virtual void write(double value) = 0; |
Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 38 | |
| 39 | virtual void write(double value, bool force, int64_t* written) |
| 40 | { |
| 41 | (void)force; |
| 42 | (void)written; |
| 43 | return write(value); |
| 44 | } |
| 45 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 46 | virtual bool getFailed(void) |
| 47 | { |
| 48 | return false; |
| 49 | }; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 50 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 51 | std::string getName(void) const |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 52 | { |
| 53 | return _name; |
| 54 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 55 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 56 | /* Returns the configurable timeout period |
| 57 | * for this sensor in seconds (undecorated). |
| 58 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 59 | int64_t getTimeout(void) const |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 60 | { |
| 61 | return _timeout; |
| 62 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 63 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 64 | private: |
| 65 | std::string _name; |
| 66 | int64_t _timeout; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 67 | }; |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 68 | |
| 69 | } // namespace pid_control |