Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResponderSensor.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library 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 GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * ResponderSensor.h
17  * Manages a sensor for a RDM responder.
18  * Copyright (C) 2013 Peter Newman
19  */
20 
29 #ifndef INCLUDE_OLA_RDM_RESPONDERSENSOR_H_
30 #define INCLUDE_OLA_RDM_RESPONDERSENSOR_H_
31 
32 #include <ola/rdm/RDMEnums.h>
33 #include <stdint.h>
34 
35 #include <algorithm>
36 #include <string>
37 #include <vector>
38 
39 namespace ola {
40 namespace rdm {
41 
45 class Sensor {
46  public:
47  struct SensorOptions {
48  public:
49  bool recorded_value_support;
50  bool recorded_range_support;
51  int16_t range_min;
52  int16_t range_max;
53  int16_t normal_min;
54  int16_t normal_max;
55 
56  // SensorOptions constructor to set all options, for use in
57  // initialisation lists. This also sets the defaults if called with no
58  // args
59  SensorOptions(bool recorded_value_support = true,
60  bool recorded_range_support = true,
61  int16_t range_min = SENSOR_DEFINITION_RANGE_MIN_UNDEFINED,
62  int16_t range_max = SENSOR_DEFINITION_RANGE_MAX_UNDEFINED,
63  int16_t normal_min =
64  SENSOR_DEFINITION_NORMAL_MIN_UNDEFINED,
65  int16_t normal_max =
66  SENSOR_DEFINITION_NORMAL_MAX_UNDEFINED)
67  : recorded_value_support(recorded_value_support),
68  recorded_range_support(recorded_range_support),
69  range_min(range_min),
70  range_max(range_max),
71  normal_min(normal_min),
72  normal_max(normal_max) {
73  }
74  };
75 
76  Sensor(ola::rdm::rdm_sensor_type type,
77  ola::rdm::rdm_pid_unit unit,
78  ola::rdm::rdm_pid_prefix prefix,
79  const std::string &description,
80  const SensorOptions &options)
81  : m_type(type),
82  m_unit(unit),
83  m_prefix(prefix),
84  m_description(description),
85  m_recorded_value_support(options.recorded_value_support),
86  m_recorded_range_support(options.recorded_range_support),
87  m_range_min(options.range_min),
88  m_range_max(options.range_max),
89  m_normal_min(options.normal_min),
90  m_normal_max(options.normal_max),
91  m_lowest(0),
92  m_highest(0),
93  m_recorded(0) {
94  }
95  virtual ~Sensor() {}
96 
97  rdm_sensor_type Type() const { return m_type; }
98  rdm_pid_unit Unit() const { return m_unit; }
99  rdm_pid_prefix Prefix() const { return m_prefix; }
100  int16_t RangeMin() const { return m_range_min; }
101  int16_t RangeMax() const { return m_range_max; }
102  int16_t NormalMin() const { return m_normal_min; }
103  int16_t NormalMax() const { return m_normal_max; }
104  const std::string& Description() const { return m_description; }
105 
106  int16_t Lowest() const {
107  if (m_recorded_range_support) {
108  return m_lowest;
109  } else {
110  return SENSOR_RECORDED_RANGE_UNSUPPORTED;
111  }
112  }
113 
114  int16_t Highest() const {
115  if (m_recorded_range_support) {
116  return m_highest;
117  } else {
118  return SENSOR_RECORDED_RANGE_UNSUPPORTED;
119  }
120  }
121 
122  int16_t Recorded() const {
123  if (m_recorded_value_support) {
124  return m_recorded;
125  } else {
126  return SENSOR_RECORDED_UNSUPPORTED;
127  }
128  }
129 
134  int16_t FetchValue() {
135  int16_t value = PollSensor();
136  m_lowest = std::min(value, m_lowest);
137  m_highest = std::max(value, m_highest);
138  return value;
139  }
140 
144  void Record() {
145  uint16_t value = FetchValue();
146  m_recorded = value;
147  }
148 
152  int16_t Reset() {
153  int16_t value = PollSensor();
154  m_lowest = value;
155  m_highest = value;
156  m_recorded = value;
157  return value;
158  }
159 
160  uint8_t RecordedSupportBitMask() const {
161  uint8_t bit_mask = 0;
162  if (m_recorded_value_support) {
163  bit_mask |= SENSOR_RECORDED_VALUE;
164  }
165  if (m_recorded_range_support) {
166  bit_mask |= SENSOR_RECORDED_RANGE_VALUES;
167  }
168  return bit_mask;
169  }
170 
171  protected:
176  virtual int16_t PollSensor() = 0;
177 
178  const ola::rdm::rdm_sensor_type m_type;
179  const ola::rdm::rdm_pid_unit m_unit;
180  const ola::rdm::rdm_pid_prefix m_prefix;
181  const std::string m_description;
182  const bool m_recorded_value_support;
183  const bool m_recorded_range_support;
184  const int16_t m_range_min;
185  const int16_t m_range_max;
186  const int16_t m_normal_min;
187  const int16_t m_normal_max;
188  int16_t m_lowest;
189  int16_t m_highest;
190  int16_t m_recorded;
191 };
192 
193 typedef std::vector<ola::rdm::Sensor*> Sensors;
194 } // namespace rdm
195 } // namespace ola
196 #endif // INCLUDE_OLA_RDM_RESPONDERSENSOR_H_