2 * Various utility functions for use by device drivers
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.
11 #ifndef INCLUDE_UTIL_H_
12 #define INCLUDE_UTIL_H_
19 #define max(a,b) ((a) > (b) ? (a) : (b))
22 #define min(a,b) ((a) < (b) ? (a) : (b))
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);
30 /// Generic Cyclic Redundancy Check CRC-8
32 /// Example polynomial: 0x31 = x8 + x5 + x4 + 1 (x8 is implicit)
33 /// Example polynomial: 0x80 = x8 + x7 (a normal bit-by-bit parity XOR)
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
40 uint8_t crc8(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init);
42 /// "Little-endian" Cyclic Redundancy Check CRC-8 LE
44 /// Based on code generated from PyCyc, (pycrc.org)
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
51 uint8_t crc8le(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init);
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
60 uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init);
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
69 uint16_t crc16_ccitt(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init);
71 /// compute bit parity of a single byte
73 /// @param inByte: single byte to check
74 /// @return 1 odd parity, 0 even parity
75 int byteParity(uint8_t inByte);
77 // buffer to hold localized timestamp YYYY-MM-DD HH:MM:SS
78 #define LOCAL_TIME_BUFLEN 32
80 /// Printable timestamp in local time
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);
87 /// Convert Celsius to Fahrenheit
89 /// @param celsius: temperature in Celsius
90 /// @return temperature value in Fahrenheit
91 float celsius2fahrenheit(float celsius);
94 /// Convert Fahrenheit to Celsius
96 /// @param celsius: temperature in Fahrenheit
97 /// @return temperature value in Celsius
98 float fahrenheit2celsius(float fahrenheit);
101 /// Convert Kilometers per hour (kph) to Miles per hour (mph)
103 /// @param kph: speed in Kilometers per hour
104 /// @return speed in miles per hour
105 float kmph2mph(float kph);
107 /// Convert Miles per hour (mph) to Kilometers per hour (kmph)
109 /// @param mph: speed in Kilometers per hour
110 /// @return speed in kilometers per hour
111 float mph2kmph(float kph);
114 #endif /* INCLUDE_UTIL_H_ */