Редизайн на основе текущей ветки мейнстрима + новые устройства.
[rtl-433.git] / include / util.h
1 /**
2  * Various utility functions for use by device drivers
3  * 
4  * Copyright (C) 2015 Tommy Vestermark
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
11 #ifndef INCLUDE_UTIL_H_
12 #define INCLUDE_UTIL_H_
13
14 #include <stdint.h>
15 #include <time.h>
16
17 // Helper macros
18 #ifndef max
19 #define max(a,b) ((a) > (b) ? (a) : (b))
20 #endif
21 #ifndef min
22 #define min(a,b) ((a) < (b) ? (a) : (b))
23 #endif
24
25 /// Reverse the bits in an 8 bit byte
26 /// @param x: input byte
27 /// @return bit reversed byte
28 uint8_t reverse8(uint8_t x);
29
30 /// Generic Cyclic Redundancy Check CRC-8
31 ///
32 /// Example polynomial: 0x31 = x8 + x5 + x4 + 1 (x8 is implicit)
33 /// Example polynomial: 0x80 = x8 + x7                  (a normal bit-by-bit parity XOR)
34 ///
35 /// @param message[]: array of bytes to check
36 /// @param nBytes: number of bytes in message
37 /// @param polynomial: byte is from x^7 to x^0 (x^8 is implicitly one)
38 /// @param init: starting crc value
39 /// @return CRC value
40 uint8_t crc8(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init);
41
42 /// "Little-endian" Cyclic Redundancy Check CRC-8 LE
43 ///
44 /// Based on code generated from PyCyc, (pycrc.org)
45 ///
46 /// @param message[]: array of bytes to check
47 /// @param nBytes: number of bytes in message
48 /// @param polynomial: CRC polynomial
49 /// @param init: starting crc value
50 /// @return CRC value
51 uint8_t crc8le(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init);
52
53 /// CRC-16
54 ///
55 /// @param message[]: array of bytes to check
56 /// @param nBytes: number of bytes in message
57 /// @param polynomial: CRC polynomial
58 /// @param init: starting crc value
59 /// @return CRC value
60 uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init);
61
62 /// CRC-16 CCITT
63 ///
64 /// @param message[]: array of bytes to check
65 /// @param nBytes: number of bytes in message
66 /// @param polynomial: CRC polynomial
67 /// @param init: starting crc value
68 /// @return CRC value
69 uint16_t crc16_ccitt(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init);
70
71 /// compute bit parity of a single byte
72 ///
73 /// @param inByte: single byte to check
74 /// @return 1 odd parity, 0 even parity
75 int byteParity(uint8_t inByte);
76
77 // buffer to hold localized timestamp YYYY-MM-DD HH:MM:SS
78 #define LOCAL_TIME_BUFLEN       32
79
80 /// Printable timestamp in local time
81 ///
82 /// @param time_secs: 0 for now, or seconds since the epoch
83 /// @param buf: output buffer, long enough for YYYY-MM-DD HH:MM:SS
84 /// @return buf pointer (for short hand use as operator)
85 char* local_time_str(time_t time_secs, char *buf);
86
87 /// Convert Celsius to Fahrenheit
88 ///
89 /// @param celsius: temperature in Celsius
90 /// @return temperature value in Fahrenheit
91 float celsius2fahrenheit(float celsius);
92
93
94 /// Convert Fahrenheit to Celsius
95 ///
96 /// @param celsius: temperature in Fahrenheit
97 /// @return temperature value in Celsius
98 float fahrenheit2celsius(float fahrenheit);
99
100
101 /// Convert Kilometers per hour (kph) to Miles per hour (mph)
102 ///
103 /// @param kph: speed in Kilometers per hour
104 /// @return speed in miles per hour
105 float kmph2mph(float kph);
106
107 /// Convert Miles per hour (mph) to Kilometers per hour (kmph)
108 ///
109 /// @param mph: speed in Kilometers per hour
110 /// @return speed in kilometers per hour
111 float mph2kmph(float kph);
112
113
114 #endif /* INCLUDE_UTIL_H_ */