atMETEO
An ATmega based weather station
demodulator.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 
36 #include "utils.h"
37 
38 namespace Sensors
39 {
40 
49 enum class DemodulatorStatus : uint8_t {
54  Complete = 0,
55 
60  Incomplete,
61 
68 };
69 
84 template <uint16_t TShortMin, uint16_t TShortMax,
85  uint16_t TLongMin, uint16_t TLongMax>
87 {
88  static_assert(TShortMin <= TShortMax,
89  "TShortMax must be less or equal than TShortMax");
90  static_assert(TShortMax <= TLongMin,
91  "TShortMax must be less or equal than TLongMin");
92  static_assert(TLongMin <= TLongMax,
93  "TLongMin must be less or equal than TLongMax");
94 };
95 
102 template <typename Demodulator>
104 {
105 public:
112  DemodulatorStatus addPulseWidth(uint16_t pulseWidth)
113  {
114  return static_cast<Demodulator *>(this)->internalAddPulseWidth(
115  pulseWidth);
116  }
117 
123  bool getData() const
124  {
125  return m_data;
126  }
127 
135  void reset()
136  {
137  m_data = false;
138  return static_cast<Demodulator *>(this)->internalReset();
139  }
140 
141 protected:
143  {
144  reset();
145  }
146 
147  bool m_data;
148 };
149 
179 template <typename TDemodulatorType>
181 
182 template <
183  uint16_t TShortMin, uint16_t TShortMax,
184  uint16_t TLongMin, uint16_t TLongMax>
185 class Demodulator<BiphaseMark<TShortMin, TShortMax, TLongMin, TLongMax>>
186  : public DemodulatorBase<Demodulator<
187  BiphaseMark<TShortMin, TShortMax, TLongMin, TLongMax>>>
188 {
190  TShortMin, TShortMax, TLongMin, TLongMax>>>;
191 
192  DemodulatorStatus internalAddPulseWidth(uint16_t pulseWidth)
193  {
195 
196  // Long pulse
197  if (isLong(pulseWidth)) {
198  m_expectShort = false; // Ignore single short pulse
199  this->m_data = true;
201  }
202 
203  // Short pulse
204  else if (isShort(pulseWidth)) {
205 
206  // First short
207  if (!m_expectShort) {
208  m_expectShort = true;
210  }
211 
212  // Second short
213  else {
214  m_expectShort = false;
215  this->m_data = false;
217  }
218  }
219 
220  return result;
221  }
222 
223  void internalReset()
224  {
225  m_expectShort = false;
226  }
227 
228 private:
229  static bool isLong(uint16_t value)
230  {
231  return (value >= TLongMin && value < TLongMax);
232  }
233 
234  static bool isShort(uint16_t value)
235  {
236  return (value >= TShortMin && value < TShortMax);
237  }
238 
239  // For BMC, the bit value 0 is represented by two continuous short pulses.
240  bool m_expectShort;
241 };
242  // \addtogroup libsensors_demodulator
244 
245 } // namespace Sensors
Configuration parameter for Demodulator that enables Biphase Mark demodulation.
Definition: demodulator.h:86
Transforms continuous streams with pulse widths (for example from RF receivers) into bits...
Definition: demodulator.h:180
void reset()
Resets the demodulator state.
Definition: demodulator.h:135
bool getData() const
Returns the converted data.
Definition: demodulator.h:123
Bit manipulation utilities.
DemodulatorStatus
Demodulator status returned from DemodulatorBase::addPulseWidth.
Definition: demodulator.h:49
Namespace containing all symbols of the Sensors library.
Definition: bitdecoder.h:40
Demodulator base implementation.
Definition: demodulator.h:103
DemodulatorStatus addPulseWidth(uint16_t pulseWidth)
Adds the pulseWidth value to the Demodulator state.
Definition: demodulator.h:112