Add conversion hPA/inHG, kPa/PSI ()

* Add conversion hPA/inHG, kPa/PSI
* Minor style updates
This commit is contained in:
Christian W. Zuckschwerdt 2018-05-01 14:52:34 +02:00 committed by GitHub
parent 8e8d5505be
commit 11246199fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 179 additions and 70 deletions

View file

@ -12,6 +12,7 @@
#define INCLUDE_UTIL_H_
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
// Helper macros
@ -133,7 +134,38 @@ float mm2inch(float mm);
float inch2mm(float inch);
/// Convert kilo Pascal (kPa) to pounds per square inch (PSI)
///
/// @param kpa: pressure in kPa
/// @return pressure in PSI
float kpa2psi(float kpa);
/// Convert pounds per square inch (PSI) to kilo Pascal (kPa)
///
/// @param psi: pressure in PSI
/// @return pressure in kPa
float psi2kpa(float psi);
/// Convert hecto Pascal (hPa) to inches of mercury (inHg)
///
/// @param kpa: pressure in kPa
/// @return pressure in inHg
float hpa2inhg(float hpa);
/// Convert inches of mercury (inHg) to hecto Pascal (hPa)
///
/// @param kpa: pressure in inHg
/// @return pressure in hPa
float inhg2hpa(float inhg);
/// Return true if the string ends with the specified suffix, otherwise return false.
///
/// @param str: string to search for patterns
/// @param suffix: the pattern to search
/// @return true if the string ends with the specified suffix, false otherwise.
bool str_endswith(const char *restrict str, const char *restrict suffix);
/// Replace a pattern in a string. This utility function is
/// useful when converting native units to si or customary.

View file

@ -239,73 +239,111 @@ void data_acquired_handler(data_t *data)
{
if (conversion_mode == CONVERT_SI) {
for (data_t *d = data; d; d = d->next) {
if ((d->type == DATA_DOUBLE) &&
!strcmp(d->key, "temperature_F")) {
*(double*)d->value = fahrenheit2celsius(*(double*)d->value);
free(d->key);
d->key = strdup("temperature_C");
char *pos;
if (d->format &&
(pos = strrchr(d->format, 'F'))) {
*pos = 'C';
}
// Convert double type fields ending in _F to _C
if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_F")) {
*(double*)d->value = fahrenheit2celsius(*(double*)d->value);
char *new_label = str_replace(d->key, "_F", "_C");
free(d->key);
d->key = new_label;
char *pos;
if (d->format && (pos = strrchr(d->format, 'F'))) {
*pos = 'C';
}
}
// Convert any fields ending in _mph to _kph
else if ((d->type == DATA_DOUBLE) && (strstr(d->key, "_mph") != NULL)) {
*(double*)d->value = mph2kmph(*(double*)d->value);
char *new_label = str_replace(d->key, "_mph", "_kph");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "mph", "kph");
free(d->format);
d->format = new_format_label;
// Convert double type fields ending in _mph to _kph
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_mph")) {
*(double*)d->value = mph2kmph(*(double*)d->value);
char *new_label = str_replace(d->key, "_mph", "_kph");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "mph", "kph");
free(d->format);
d->format = new_format_label;
}
// Convert any fields ending in _mph to _kph
else if ((d->type == DATA_DOUBLE) && (strstr(d->key, "_inch") != NULL)) {
*(double*)d->value = inch2mm(*(double*)d->value);
char *new_label = str_replace(d->key, "_inch", "_mm");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "inch", "mm");
free(d->format);
d->format = new_format_label;
// Convert double type fields ending in _mph to _kph
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_inch")) {
*(double*)d->value = inch2mm(*(double*)d->value);
char *new_label = str_replace(d->key, "_inch", "_mm");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "inch", "mm");
free(d->format);
d->format = new_format_label;
}
// Convert double type fields ending in _inHg to _hPa
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_inHg")) {
*(double*)d->value = inhg2hpa(*(double*)d->value);
char *new_label = str_replace(d->key, "_inHg", "_hPa");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "inHg", "hPa");
free(d->format);
d->format = new_format_label;
}
// Convert double type fields ending in _PSI to _kPa
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_PSI")) {
*(double*)d->value = psi2kpa(*(double*)d->value);
char *new_label = str_replace(d->key, "_PSI", "_kPa");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "PSI", "kPa");
free(d->format);
d->format = new_format_label;
}
}
}
if (conversion_mode == CONVERT_CUSTOMARY) {
for (data_t *d = data; d; d = d->next) {
if ((d->type == DATA_DOUBLE) &&
!strcmp(d->key, "temperature_C")) {
*(double*)d->value = celsius2fahrenheit(*(double*)d->value);
free(d->key);
d->key = strdup("temperature_F");
char *pos;
if (d->format &&
(pos = strrchr(d->format, 'C'))) {
*pos = 'F';
}
// Convert double type fields ending in _C to _F
if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_C")) {
*(double*)d->value = celsius2fahrenheit(*(double*)d->value);
char *new_label = str_replace(d->key, "_C", "_F");
free(d->key);
d->key = new_label;
char *pos;
if (d->format && (pos = strrchr(d->format, 'C'))) {
*pos = 'F';
}
}
// Convert any fields ending in _kph to _mph
else if ((d->type == DATA_DOUBLE) && (strstr(d->key, "_kph") != NULL)) {
*(double*)d->value = kmph2mph(*(double*)d->value);
char *new_label = str_replace(d->key, "_kph", "_mph");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "kph", "mph");
free(d->format);
d->format = new_format_label;
// Convert double type fields ending in _kph to _mph
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_kph")) {
*(double*)d->value = kmph2mph(*(double*)d->value);
char *new_label = str_replace(d->key, "_kph", "_mph");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "kph", "mph");
free(d->format);
d->format = new_format_label;
}
// Convert any fields ending in _mm to _inch
else if ((d->type == DATA_DOUBLE) && (strstr(d->key, "_mm") != NULL)) {
*(double*)d->value = mm2inch(*(double*)d->value);
char *new_label = str_replace(d->key, "_mm", "_inch");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "mm", "inch");
free(d->format);
d->format = new_format_label;
// Convert double type fields ending in _mm to _inch
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_mm")) {
*(double*)d->value = mm2inch(*(double*)d->value);
char *new_label = str_replace(d->key, "_mm", "_inch");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "mm", "inch");
free(d->format);
d->format = new_format_label;
}
// Convert double type fields ending in _hPa to _inHg
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_hPa")) {
*(double*)d->value = hpa2inhg(*(double*)d->value);
char *new_label = str_replace(d->key, "_hPa", "_inHg");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "hPa", "inHg");
free(d->format);
d->format = new_format_label;
}
// Convert double type fields ending in _kPa to _PSI
else if ((d->type == DATA_DOUBLE) && str_endswith(d->key, "_kPa")) {
*(double*)d->value = kpa2psi(*(double*)d->value);
char *new_label = str_replace(d->key, "_kPa", "_PSI");
free(d->key);
d->key = new_label;
char *new_format_label = str_replace(d->format, "kPa", "PSI");
free(d->format);
d->format = new_format_label;
}
}
}

View file

@ -13,7 +13,8 @@
#include <stdio.h>
#include <string.h>
uint8_t reverse8(uint8_t x) {
uint8_t reverse8(uint8_t x)
{
x = (x & 0xF0) >> 4 | (x & 0x0F) << 4;
x = (x & 0xCC) >> 2 | (x & 0x33) << 2;
x = (x & 0xAA) >> 1 | (x & 0x55) << 1;
@ -21,7 +22,8 @@ uint8_t reverse8(uint8_t x) {
}
uint8_t crc7(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init) {
uint8_t crc7(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init)
{
unsigned remainder = init << 1; // LSB is unused
unsigned poly = polynomial << 1;
unsigned byte, bit;
@ -60,12 +62,12 @@ uint8_t crc8(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8
}
uint8_t crc8le(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init) {
uint8_t crc8le(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init)
{
uint8_t crc = init, i;
unsigned byte;
uint8_t bit;
for (byte = 0; byte < nBytes; ++byte) {
for (i = 0x01; i & 0xff; i <<= 1) {
bit = (crc & 0x80) == 0x80;
@ -83,7 +85,8 @@ uint8_t crc8le(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uin
return reverse8(crc);
}
uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init) {
uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init)
{
uint16_t remainder = init;
unsigned byte, bit;
@ -101,7 +104,8 @@ uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, ui
return remainder;
}
uint16_t crc16_ccitt(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init) {
uint16_t crc16_ccitt(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init)
{
uint16_t remainder = init;
unsigned byte, bit;
@ -120,15 +124,16 @@ uint16_t crc16_ccitt(uint8_t const message[], unsigned nBytes, uint16_t polynomi
}
int byteParity(uint8_t inByte){
int byteParity(uint8_t inByte)
{
inByte ^= inByte >> 4;
inByte &= 0xf;
return (0x6996 >> inByte) & 1;
}
char* local_time_str(time_t time_secs, char *buf) {
char* local_time_str(time_t time_secs, char *buf)
{
time_t etime;
struct tm *tm_info;
@ -172,20 +177,54 @@ float mph2kmph(float mph)
}
float mm2inch(float mm) {
float mm2inch(float mm)
{
return mm * 0.039370;
}
float inch2mm(float inch) {
float inch2mm(float inch)
{
return inch / 0.039370;
}
float kpa2psi(float kpa)
{
return kpa / 6.89475729;
}
float psi2kpa(float psi)
{
return psi * 6.89475729;
}
float hpa2inhg(float hpa)
{
return hpa / 33.8639;
}
float inhg2hpa(float inhg)
{
return inhg * 33.8639;
}
bool str_endswith(const char *restrict str, const char *restrict suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return (str_len >= suffix_len) &&
(0 == strcmp(str + (str_len - suffix_len), suffix));
}
// Original string replacement function was found here:
// https://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c/779960#779960
//
// You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with) {
char *str_replace(char *orig, char *rep, char *with)
{
char *result; // the return string
char *ins; // the next insert point
char *tmp; // varies