atMETEO
An ATmega based weather station
rfdevice.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
37 #include "demodulator.h"
38 #include "bitdecoder.h"
39 #include "sensor.h"
40 
41 namespace Sensors
42 {
43 
52 enum class RfDeviceStatus : uint8_t {
57  Complete = 0,
58 
63  Incomplete,
64 
70 };
71 
107 template <typename TDemodulator,
108  typename TBitDecoder,
109  typename TSensor,
110  uint16_t TBitLength = 0>
111 class RfDevice :
112  private TDemodulator,
113  private TBitDecoder,
114  public TSensor
115 {
116 public:
126  RfDeviceStatus addPulseWidth(uint16_t pulseWidth)
127  {
128  if (m_lastStatus == RfDeviceStatus::InvalidData)
129  reset();
130 
131  m_lastStatus = internalAddPulseWidth(pulseWidth);
132  return m_lastStatus;
133  }
134 
138  void reset()
139  {
140  TDemodulator::reset();
141  TBitDecoder::reset();
142  TSensor::reset();
143  m_bitLength = 0;
144  }
145 
146 private:
147  RfDeviceStatus internalAddPulseWidth(uint16_t pulseWidth)
148  {
149  auto demodulatorStatus = TDemodulator::addPulseWidth(pulseWidth);
150  if (demodulatorStatus == DemodulatorStatus::Incomplete) {
152  } else if (demodulatorStatus != DemodulatorStatus::Complete) {
154  }
155 
156  // Bit
157  auto decoderStatus = TBitDecoder::addBit(TDemodulator::getData());
158  if (decoderStatus == BitDecoderStatus::ParityError) {
160  }
161  ++m_bitLength;
162  if (m_bitLength != TBitLength &&
163  decoderStatus != BitDecoderStatus::Complete) {
165  }
166 
167  // Byte
168  auto sensorStatus = TSensor::addByte(TBitDecoder::getData());
169  if (sensorStatus == SensorStatus::Incomplete) {
171  } else if (sensorStatus != SensorStatus::Complete) {
173  }
174 
176  }
177 
178  uint16_t m_bitLength = 0;
180 };
181  // \addtogroup libsensors_rfdevice
183 
184 } // namespace Sensors
RfDeviceStatus addPulseWidth(uint16_t pulseWidth)
Adds the pulseWidth value to the RfDevice state.
Definition: rfdevice.h:126
Sensors::BitDecoder transforms continuous bit streams (for example from RF demodulation) into bytes...
Namespace containing all symbols of the Sensors library.
Definition: bitdecoder.h:40
Sensors::Sensor is the base class for sensor specific data decoding.
Connects Demodulator, Bit Decoder and Sensor for decoding sensor data from RF receivers.
Definition: rfdevice.h:111
Sensors::Demodulator transforms continuous streams with pulse widths (for example from RF receivers) ...
void reset()
Resets the device state.
Definition: rfdevice.h:138
RfDeviceStatus
RfDevice status returned from RfDevice::addPulseWidth().
Definition: rfdevice.h:52