Skip to content

Commit

Permalink
Added a utility library with some generally useful code that is likel…
Browse files Browse the repository at this point in the history
…y to be

needed by programs, but is not part of the core functionality of libtempered.
  • Loading branch information
edorfaus committed Jun 27, 2012
1 parent 4da7b9d commit db33a52
Showing 5 changed files with 199 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -56,5 +56,6 @@ endif()
include_directories(${HIDAPI_HEADER_DIR})

add_subdirectory(libtempered)
add_subdirectory(libtempered-util)
add_subdirectory(utils)
add_subdirectory(examples)
24 changes: 24 additions & 0 deletions libtempered-util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 2.8)

file(GLOB_RECURSE libtempered_util_FILES *.[ch])

install(FILES tempered-util.h DESTINATION include)

if (BUILD_SHARED_LIB)
add_library(tempered-util-shared SHARED ${libtempered_util_FILES})
set_target_properties(tempered-util-shared PROPERTIES
OUTPUT_NAME tempered-util
SOVERSION 0
)
target_link_libraries(tempered-util-shared m)
install(TARGETS tempered-util-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if (BUILD_STATIC_LIB)
add_library(tempered-util-static STATIC ${libtempered_util_FILES})
set_target_properties(tempered-util-static PROPERTIES
OUTPUT_NAME tempered-util
)
target_link_libraries(tempered-util-static m)
install(TARGETS tempered-util-static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
18 changes: 18 additions & 0 deletions libtempered-util/dew-point.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <math.h>

/** Calculate the dew point for the given temperature and relative humidity. */
float tempered_util__get_dew_point( float tempC, float rel_hum )
{
// This is based on the Sensirion SHT1x datasheet, with some extra reading
// on Wikipedia.
double Tn = 243.12;
double m = 17.62;
if ( tempC < 0 )
{
Tn = 272.62;
m = 22.46;
}
double gamma = log( rel_hum / 100 ) + m * tempC / ( Tn + tempC );
double dew_point = Tn * gamma / ( m - gamma );
return dew_point;
}
103 changes: 103 additions & 0 deletions libtempered-util/temp-scale.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include "tempered-util.h"

static float celsius_to_celsius( float temperature )
{
// http://en.wikipedia.org/wiki/Celcius
return temperature;
}

static float celsius_to_kelvin( float temperature )
{
// http://en.wikipedia.org/wiki/Kelvin
return temperature + 273.15;
}

static float celsius_to_fahrenheit( float temperature )
{
// http://en.wikipedia.org/wiki/Fahrenheit
return temperature * 9 / 5 + 32;
}

static float celsius_to_rankine( float temperature )
{
// http://en.wikipedia.org/wiki/Rankine_scale
return ( temperature + 273.15 ) * 9 / 5;
}

static float celsius_to_newton( float temperature )
{
// The Newton temperature scale was devised by Isaac Newton around 1700.
// http://en.wikipedia.org/wiki/Newton_scale
return temperature * 33 / 100;
}

struct tempered_util__temp_scale const tempered_util__known_temp_scales[] = {
{
.name = "Celsius",
.symbol = "°C",
.from_celsius = celsius_to_celsius
},
{
.name = "Kelvin",
.symbol = "K",
.from_celsius = celsius_to_kelvin
},
{
.name = "Fahrenheit",
.symbol = "°F",
.from_celsius = celsius_to_fahrenheit
},
{
.name = "Rankine",
.symbol = "°R",
.from_celsius = celsius_to_rankine
},
{
.name = "Newton",
.symbol = "°N",
.from_celsius = celsius_to_newton
},
{ .name = NULL } // List terminator
};

struct tempered_util__temp_scale const * tempered_util__find_temperature_scale(
char const * const name
) {
if ( name == NULL || name[0] == '\0' )
{
return NULL;
}
struct tempered_util__temp_scale const *cur;
for ( cur = tempered_util__known_temp_scales ; cur->name != NULL ; cur++ )
{
if ( strcasecmp( cur->name, name ) == 0 )
{
return cur;
}
}
for ( cur = tempered_util__known_temp_scales ; cur->name != NULL ; cur++ )
{
if ( strcmp( cur->symbol, name ) == 0 )
{
return cur;
}
}
size_t name_len = strlen( name );
struct tempered_util__temp_scale const *found = NULL;
for ( cur = tempered_util__known_temp_scales ; cur->name != NULL ; cur++ )
{
if ( strncasecmp( cur->name, name, name_len ) == 0 )
{
if ( found != NULL )
{
found = NULL;
break;
}
found = cur;
}
}
return found;
}
53 changes: 53 additions & 0 deletions libtempered-util/tempered-util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef TEMPERED_UTIL_H
#define TEMPERED_UTIL_H

#ifdef __cplusplus
extern "C" {
#endif

/* temp-scale.c start */

/** Description of a known temperature scale. */
struct tempered_util__temp_scale {
/** The name of the temperature scale, e.g. "Celsius". */
char const * const name;

/** The symbol of the temperature scale, e.g. "°C" */
char const * const symbol;

/** The function to use to convert to this scale from degrees Celsius. */
float (* const from_celsius)( float );
};

/** The array of known temperature scales.
* This list is terminated by an element having a name that is NULL.
*/
extern struct tempered_util__temp_scale const tempered_util__known_temp_scales[];

/** Find a temperature scale with a name or symbol that matches the given name.
* This matches case-insensitively, and also matches on unique name prefixes.
* @param name The name to find a temperature scale that matches.
* @return The matching temperature scale, or NULL if no such scale was found.
*/
struct tempered_util__temp_scale const * tempered_util__find_temperature_scale(
char const * const name
);

/* temp-scale.c end */

/* dew-point.c start */

/** Calculate the dew point for the given temperature and relative humidity.
* @param tempC The temperature in degrees Celsius.
* @param rel_hum The relative humidity, in %RH.
* @return The dew point for the given temperature and relative humidity.
*/
float tempered_util__get_dew_point( float tempC, float rel_hum );

/* dew-point.c end */

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit db33a52

Please sign in to comment.