Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_recorded(0) {
92  }
93  virtual ~Sensor() {}
94 
95  rdm_sensor_type Type() const { return m_type; }
96  rdm_pid_unit Unit() const { return m_unit; }
97  rdm_pid_prefix Prefix() const { return m_prefix; }
98  int16_t RangeMin() const { return m_range_min; }
99  int16_t RangeMax() const { return m_range_max; }
100  int16_t NormalMin() const { return m_normal_min; }
101  int16_t NormalMax() const { return m_normal_max; }
102  const std::string& Description() const { return m_description; }
103 
104  int16_t Lowest() const {
105  if (m_recorded_range_support) {
106  return m_lowest;
107  } else {
108  return SENSOR_RECORDED_RANGE_UNSUPPORTED;
109  }
110  }
111 
112  int16_t Highest() const {
113  if (m_recorded_range_support) {
114  return m_highest;
115  } else {
116  return SENSOR_RECORDED_RANGE_UNSUPPORTED;
117  }
118  }
119 
120  int16_t Recorded() const {
121  if (m_recorded_value_support) {
122  return m_recorded;
123  } else {
124  return SENSOR_RECORDED_UNSUPPORTED;
125  }
126  }
127 
132  int16_t FetchValue() {
133  int16_t value = PollSensor();
134  m_lowest = std::min(value, m_lowest);
135  m_highest = std::max(value, m_highest);
136  return value;
137  }
138 
142  void Record() {
143  uint16_t value = FetchValue();
144  m_recorded = value;
145  }
146 
150  int16_t Reset() {
151  int16_t value = PollSensor();
152  m_lowest = value;
153  m_highest = value;
154  m_recorded = value;
155  return value;
156  }
157 
158  uint8_t RecordedSupportBitMask() const {
159  uint8_t bit_mask = 0;
160  if (m_recorded_value_support) {
161  bit_mask |= SENSOR_RECORDED_VALUE;
162  }
163  if (m_recorded_range_support) {
164  bit_mask |= SENSOR_RECORDED_RANGE_VALUES;
165  }
166  return bit_mask;
167  }
168 
169  protected:
174  virtual int16_t PollSensor() = 0;
175 
176  const ola::rdm::rdm_sensor_type m_type;
177  const ola::rdm::rdm_pid_unit m_unit;
178  const ola::rdm::rdm_pid_prefix m_prefix;
179  const std::string m_description;
180  const bool m_recorded_value_support;
181  const bool m_recorded_range_support;
182  const int16_t m_range_min;
183  const int16_t m_range_max;
184  const int16_t m_normal_min;
185  const int16_t m_normal_max;
186  int16_t m_lowest;
187  int16_t m_highest;
188  int16_t m_recorded;
189 };
190 
191 typedef std::vector<ola::rdm::Sensor*> Sensors;
192 } // namespace rdm
193 } // namespace ola
194 #endif // INCLUDE_OLA_RDM_RESPONDERSENSOR_H_