atMETEO
An ATmega based weather station
bitdecoder.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 <limits.h> // AVR toolchain doesn't offer climits header
37 
38 #include "utils.h"
39 
40 namespace Sensors
41 {
42 
51 enum class BitDecoderStatus : uint8_t {
56  Complete = 0,
57 
62  Incomplete,
63 
69 };
70 
75 template <typename T>
77 {
79  static void bitWrite(T &data, uint8_t bit, bool value)
80  {
81  Sensors::bitWrite(data, sizeof(T) * CHAR_BIT - 1 - bit, value);
82  }
84 };
85 
90 template <typename T>
92 {
94  static void bitWrite(T &data, uint8_t bit, bool value)
95  {
96  Sensors::bitWrite(data, bit, value);
97  }
99 };
100 
104 template <typename T>
105 struct NoParity
106 {
107 };
108 
113 template <typename T>
115 {
117  static bool parityCheck(T data, bool parity)
118  {
119  return Sensors::parity(data) == parity;
120  }
122 };
123 template <typename T>
128 struct OddParity
129 {
131  static bool parityCheck(T data, bool parity)
132  {
133  return Sensors::parity(data) != parity;
134  }
136 };
137 
144 template <typename T, typename BitDecoder>
146 {
147 public:
155  {
156  if (static_cast<BitDecoder *>(this)->isComplete())
157  reset();
158 
159  return static_cast<BitDecoder *>(this)->internalAddBit(value);
160  }
161 
167  T getData() const
168  {
169  return m_data;
170  }
171 
178  void reset()
179  {
180  m_bitLength = 0;
181  m_data = 0;
182  }
183 
184 protected:
186  {
187  reset();
188  }
189 
190  T m_data;
191  uint8_t m_bitLength;
192 };
193 
224 template <typename T,
225  template <typename> class TParity,
226  template <typename> class TBitNumbering>
228  : public BitDecoderBase<T, BitDecoder<T, TParity, TBitNumbering>>
229 {
230  friend class BitDecoderBase<T, BitDecoder<T, TParity, TBitNumbering>>;
231 
232  BitDecoderStatus internalAddBit(bool value)
233  {
234  // Data bit
235  if (this->m_bitLength < sizeof(T) * CHAR_BIT) {
236  TBitNumbering<T>::bitWrite(this->m_data, this->m_bitLength, value);
237  }
238 
239  // Parity bit
240  else {
241  if (!TParity<T>::parityCheck(this->m_data, value)) {
243  }
244  }
245 
246  ++this->m_bitLength;
247  return isComplete() ? BitDecoderStatus::Complete :
249  }
250 
251  bool isComplete()
252  {
253  return this->m_bitLength == sizeof(T) * CHAR_BIT + 1;
254  }
255 };
256 
257 template <typename T,
258  template <typename> class TBitNumbering>
259 class BitDecoder<T, NoParity, TBitNumbering>
260  : public BitDecoderBase<T, BitDecoder<T, NoParity, TBitNumbering>>
261 {
262  friend class BitDecoderBase<T, BitDecoder<T, NoParity, TBitNumbering>>;
263 
264 private:
265  BitDecoderStatus internalAddBit(bool value)
266  {
267  TBitNumbering<T>::bitWrite(this->m_data, this->m_bitLength++, value);
268  return isComplete() ? BitDecoderStatus::Complete :
270  }
271 
272  bool isComplete()
273  {
274  return this->m_bitLength == sizeof(T) * CHAR_BIT;
275  }
276 };
277 
281 template <template <typename> class TParity,
282  template <typename> class TBitNumbering>
284  // \addtogroup libsensors_decoder
286 
287 } // namespace Sensors
void bitWrite(T &value, uint8_t bit, bool bitValue)
Writes the bit in the given value.
Definition: utils.h:106
BitDecoderStatus addBit(bool value)
Adds the bit value to the BitDecoder state.
Definition: bitdecoder.h:154
BitDecoderStatus
BitDecoder status returned from BitDecoder::addBit().
Definition: bitdecoder.h:51
Applies bit numbering and a parity method to transform continuous bit streams (for example from RF de...
Definition: bitdecoder.h:227
bool parity(int x)
Returns the even parity for the byte x.
Definition: utils.h:175
Bit manipulation utilities.
Configuration parameter for BitDecoder that enables even parity checking.
Definition: bitdecoder.h:114
Namespace containing all symbols of the Sensors library.
Definition: bitdecoder.h:40
void reset()
Resets the decoder state.
Definition: bitdecoder.h:178
Configuration parameter for BitDecoder that enables odd parity checking.
Definition: bitdecoder.h:128
Configuration parameter for BitDecoder that leads to new bits being added with LSB bit numbering (lea...
Definition: bitdecoder.h:91
BitDecoder base implementation.
Definition: bitdecoder.h:145
Configuration parameter for BitDecoder that disables parity checking.
Definition: bitdecoder.h:105
T getData() const
Returns the converted data.
Definition: bitdecoder.h:167
Configuration parameter for BitDecoder that leads to new bits being added with MSB bit numbering (mos...
Definition: bitdecoder.h:76