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.
14 uint8_t reverse8(uint8_t x) {
15 x = (x & 0xF0) >> 4 | (x & 0x0F) << 4;
16 x = (x & 0xCC) >> 2 | (x & 0x33) << 2;
17 x = (x & 0xAA) >> 1 | (x & 0x55) << 1;
22 uint8_t crc8(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init) {
23 uint8_t remainder = init;
26 for (byte = 0; byte < nBytes; ++byte) {
27 remainder ^= message[byte];
28 for (bit = 0; bit < 8; ++bit) {
29 if (remainder & 0x80) {
30 remainder = (remainder << 1) ^ polynomial;
33 remainder = (remainder << 1);
41 uint8_t crc8le(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init) {
42 uint8_t crc = init, i;
47 for (byte = 0; byte < nBytes; ++byte) {
48 for (i = 0x01; i & 0xff; i <<= 1) {
49 bit = (crc & 0x80) == 0x80;
50 if (message[byte] & i) {
64 uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init) {
65 uint16_t remainder = init;
68 for (byte = 0; byte < nBytes; ++byte) {
69 remainder ^= message[byte];
70 for (bit = 0; bit < 8; ++bit) {
72 remainder = (remainder >> 1) ^ polynomial;
75 remainder = (remainder >> 1);
82 uint16_t crc16_ccitt(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init) {
83 uint16_t remainder = init;
86 for (byte = 0; byte < nBytes; ++byte) {
87 remainder ^= message[byte] << 8;
88 for (bit = 0; bit < 8; ++bit) {
89 if (remainder & 0x8000) {
90 remainder = (remainder << 1) ^ polynomial;
93 remainder = (remainder << 1);
102 int byteParity(uint8_t inByte){
103 inByte ^= inByte >> 4;
105 return (0x6996 >> inByte) & 1;
109 char* local_time_str(time_t time_secs, char *buf) {
113 if (time_secs == 0) {
114 extern float sample_file_pos;
115 if (sample_file_pos != -1.0) {
116 snprintf(buf, LOCAL_TIME_BUFLEN, "@%fs", sample_file_pos);
124 tm_info = localtime(&etime);
126 strftime(buf, LOCAL_TIME_BUFLEN, "%Y-%m-%d %H:%M:%S", tm_info);
130 float celsius2fahrenheit(float celsius)
132 return celsius * 9 / 5 + 32;
136 float fahrenheit2celsius(float fahrenheit)
138 return (fahrenheit - 32) / 1.8;
142 float kmph2mph(float kmph)
144 return kmph / 1.609344;
147 float mph2kmph(float mph)
149 return mph * 1.609344;
154 // gcc -I include/ -std=gnu99 -D _TEST src/util.c
156 int main(int argc, char **argv) {
157 fprintf(stderr, "util:: test\n");
159 uint8_t msg[] = {0x08, 0x0a, 0xe8, 0x80};
161 fprintf(stderr, "util::crc8(): odd parity: %02X\n", crc8(msg, 3, 0x80));
162 fprintf(stderr, "util::crc8(): even parity: %02X\n", crc8(msg, 4, 0x80));