atMETEO
An ATmega based weather station
utils.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 <inttypes.h> // AVR toolchain doesn't offer cinttypes header
38 
39 namespace Sensors
40 {
41 
53 template <typename T>
54 inline void bitSet(T &value, uint8_t bit)
55 {
56  value |= (1 << bit);
57 }
58 
66 template <typename T>
67 inline void bitClear(T &value, uint8_t bit)
68 {
69  value &= ~(1 << bit);
70 }
71 
78 template <typename T>
79 inline void bitFlip(T &value, uint8_t bit)
80 {
81  value ^= (1 << bit);
82 }
83 
91 template <typename T>
92 inline bool bitRead(T &value, uint8_t bit)
93 {
94  return (value >> bit) & 0x01;
95 }
96 
105 template <typename T>
106 inline void bitWrite(T &value, uint8_t bit, bool bitValue)
107 {
108  bitValue ? bitSet(value, bit) : bitClear(value, bit);
109 }
110 
116 inline uint8_t byteReverse(uint8_t x)
117 {
118  x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
119  x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
120  x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
121  return x;
122 }
123 
129 inline uint8_t nibbleReverse(uint8_t x)
130 {
131  x = byteReverse(x);
132  x = ((x >> 4) & 0x0F) | ((x << 4) & 0xF0);
133  return x;
134 }
135 
141 inline uint8_t highNibble(uint8_t x)
142 {
143  return x >> 4;
144 }
145 
151 inline uint8_t lowNibble(uint8_t x)
152 {
153  return x & 0x0F;
154 }
155 
162 inline uint16_t word(uint8_t highByte, uint8_t lowByte)
163 {
164  return highByte << 8 | lowByte;
165 }
166 
175 inline bool parity(int x)
176 {
177  return __builtin_parity(x);
178 }
179 
187 template<typename T>
188 inline T min(T a, T b)
189 {
190  return a < b ? a : b;
191 }
192 
200 template<typename T>
201 inline T max(T a, T b)
202 {
203  return a > b ? a : b;
204 }
205  // \addtogroup libsensors_utils
207 
208 } // namespace Sensors
void bitWrite(T &value, uint8_t bit, bool bitValue)
Writes the bit in the given value.
Definition: utils.h:106
void bitFlip(T &value, uint8_t bit)
Flips the bit in the given value.
Definition: utils.h:79
bool parity(int x)
Returns the even parity for the byte x.
Definition: utils.h:175
uint8_t lowNibble(uint8_t x)
Returns the byte&#39;s x low nibble.
Definition: utils.h:151
Namespace containing all symbols of the Sensors library.
Definition: bitdecoder.h:40
uint8_t byteReverse(uint8_t x)
Returns the byte x in reversed bit order.
Definition: utils.h:116
T min(T a, T b)
Returns the minimum of the two values a and b.
Definition: utils.h:188
T max(T a, T b)
Returns the maximum of the two values a and b.
Definition: utils.h:201
uint8_t nibbleReverse(uint8_t x)
Returns the byte x in reversed bit order (nibble wise).
Definition: utils.h:129
bool bitRead(T &value, uint8_t bit)
Returns the bit in the given value.
Definition: utils.h:92
void bitSet(T &value, uint8_t bit)
Sets the bit in the given value to 1.
Definition: utils.h:54
uint8_t highNibble(uint8_t x)
Returns the byte&#39;s x high nibble.
Definition: utils.h:141
uint16_t word(uint8_t highByte, uint8_t lowByte)
Converts two bytes to a word.
Definition: utils.h:162
void bitClear(T &value, uint8_t bit)
Sets the bit in the given value to 0.
Definition: utils.h:67