From 59d68ecec1412ce20406856fdb8e81f7c5c2d193 Mon Sep 17 00:00:00 2001 From: Seb Flippence <144435+SebFlippence@users.noreply.github.com> Date: Thu, 26 Mar 2015 19:35:42 +0000 Subject: [PATCH 1/4] Added new option to get numeric value of sensor only --- utils/tempered.c | 115 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 27 deletions(-) diff --git a/utils/tempered.c b/utils/tempered.c index 65b72c5..1296b50 100644 --- a/utils/tempered.c +++ b/utils/tempered.c @@ -6,6 +6,7 @@ #include struct my_options { + char * output_type; bool enumerate; struct tempered_util__temp_scale const * temp_scale; int calibration_count; @@ -30,6 +31,9 @@ void show_help() "Known options:\n" " -h\n" " --help Show this help text\n" +" -t\n" +" --type Returns only the numeric value of the sensor.\n" +" Known sensors: temperature, humidity, due-point\n" " -e\n" " --enumerate Enumerate the found devices without reading them.\n" " -s \n" @@ -70,6 +74,7 @@ void show_help() struct my_options* parse_options( int argc, char *argv[] ) { struct my_options options = { + .output_type = NULL, .enumerate = false, .temp_scale = NULL, .calibration_count = 0, @@ -79,12 +84,13 @@ struct my_options* parse_options( int argc, char *argv[] ) char *temp_scale = "Celsius", *calibration_string = NULL; struct option const long_options[] = { { "help", no_argument, NULL, 'h' }, + { "type", required_argument, NULL, 't' }, { "enumerate", no_argument, NULL, 'e' }, { "scale", required_argument, NULL, 's' }, { "calibrate-temp", required_argument, NULL, 'c' }, { NULL, 0, NULL, 0 } }; - char const * const short_options = "hes:c:"; + char const * const short_options = "ht:es:c:"; while ( true ) { int opt = getopt_long( argc, argv, short_options, long_options, NULL ); @@ -110,6 +116,10 @@ struct my_options* parse_options( int argc, char *argv[] ) show_help(); return NULL; } break; + case 't': + { + options.output_type = optarg; + } break; case 'e': { options.enumerate = true; @@ -124,6 +134,11 @@ struct my_options* parse_options( int argc, char *argv[] ) } break; } } + if ( options.output_type != NULL && !( strcmp(options.output_type, "temperature") == 0 || strcmp(options.output_type, "humidity") == 0 || strcmp(options.output_type, "due-point") == 0 ) ) + { + fprintf( stderr, "Output type not found: %s\n", options.output_type ); + return NULL; + } options.temp_scale = tempered_util__find_temperature_scale( temp_scale ); if ( options.temp_scale == NULL ) { @@ -193,36 +208,82 @@ void print_device_sensor( ( type & TEMPERED_SENSOR_TYPE_TEMPERATURE ) && ( type & TEMPERED_SENSOR_TYPE_HUMIDITY ) ) { - printf( - "%s %i: temperature %.2f %s" - ", relative humidity %.1f%%" - ", dew point %.1f %s\n", - tempered_get_device_path( device ), sensor, - options->temp_scale->from_celsius( tempC ), - options->temp_scale->symbol, - rel_hum, - options->temp_scale->from_celsius( - tempered_util__get_dew_point( tempC, rel_hum ) - ), - options->temp_scale->symbol - ); + if ( options->output_type == NULL ) + { + printf( + "%s %i: temperature %.2f %s" + ", relative humidity %.1f%%" + ", dew point %.1f %s\n", + tempered_get_device_path( device ), sensor, + options->temp_scale->from_celsius( tempC ), + options->temp_scale->symbol, + rel_hum, + options->temp_scale->from_celsius( + tempered_util__get_dew_point( tempC, rel_hum ) + ), + options->temp_scale->symbol + ); + } + else if ( strcmp(options->output_type, "temperature") == 0 ) + { + printf( + "%.2f", + options->temp_scale->from_celsius( tempC ) + ); + } + else if ( strcmp(options->output_type, "humidity") == 0 ) + { + printf( + "%.1f%%", + rel_hum + ); + } + else if ( strcmp(options->output_type, "due-point") == 0 ) + { + printf( + "%.1f", + options->temp_scale->from_celsius( + tempered_util__get_dew_point( tempC, rel_hum ) + ) + ); + } } else if ( type & TEMPERED_SENSOR_TYPE_TEMPERATURE ) { - printf( - "%s %i: temperature %.2f %s\n", - tempered_get_device_path( device ), sensor, - options->temp_scale->from_celsius( tempC ), - options->temp_scale->symbol - ); + if ( options->output_type == NULL ) + { + printf( + "%s %i: temperature %.2f %s\n", + tempered_get_device_path( device ), sensor, + options->temp_scale->from_celsius( tempC ), + options->temp_scale->symbol + ); + } + else if ( strcmp(options->output_type, "temperature") == 0 ) + { + printf( + "%.2f", + options->temp_scale->from_celsius( tempC ) + ); + } } else if ( type & TEMPERED_SENSOR_TYPE_HUMIDITY ) { - printf( - "%s %i: relative humidity %.1f%%\n", - tempered_get_device_path( device ), sensor, - rel_hum - ); + if ( options->output_type == NULL ) + { + printf( + "%s %i: relative humidity %.1f%%\n", + tempered_get_device_path( device ), sensor, + rel_hum + ); + } + else if ( strcmp(options->output_type, "humidity") == 0 ) + { + printf( + "%.1f%%", + rel_hum + ); + } } else { @@ -290,7 +351,7 @@ int main( int argc, char *argv[] ) free_options( options ); return 1; } - + struct tempered_device_list *list = tempered_enumerate( &error ); if ( list == NULL ) { @@ -336,7 +397,7 @@ int main( int argc, char *argv[] ) } tempered_free_device_list( list ); } - + if ( !tempered_exit( &error ) ) { fprintf( stderr, "%s\n", error ); From 010b194659ff92fadf20c447bd1756e69b75c264 Mon Sep 17 00:00:00 2001 From: Seb Flippence <144435+SebFlippence@users.noreply.github.com> Date: Mon, 9 Dec 2019 16:23:09 +0000 Subject: [PATCH 2/4] Fixed typo --- utils/tempered.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/tempered.c b/utils/tempered.c index 1296b50..023ca5f 100644 --- a/utils/tempered.c +++ b/utils/tempered.c @@ -33,7 +33,7 @@ void show_help() " --help Show this help text\n" " -t\n" " --type Returns only the numeric value of the sensor.\n" -" Known sensors: temperature, humidity, due-point\n" +" Known sensors: temperature, humidity, dew point\n" " -e\n" " --enumerate Enumerate the found devices without reading them.\n" " -s \n" @@ -134,7 +134,7 @@ struct my_options* parse_options( int argc, char *argv[] ) } break; } } - if ( options.output_type != NULL && !( strcmp(options.output_type, "temperature") == 0 || strcmp(options.output_type, "humidity") == 0 || strcmp(options.output_type, "due-point") == 0 ) ) + if ( options.output_type != NULL && !( strcmp(options.output_type, "temperature") == 0 || strcmp(options.output_type, "humidity") == 0 || strcmp(options.output_type, "dew point") == 0 ) ) { fprintf( stderr, "Output type not found: %s\n", options.output_type ); return NULL; @@ -238,7 +238,7 @@ void print_device_sensor( rel_hum ); } - else if ( strcmp(options->output_type, "due-point") == 0 ) + else if ( strcmp(options->output_type, "dew point") == 0 ) { printf( "%.1f", From 6f8320b25738d94fdfb21985e3ca3d58b716bd87 Mon Sep 17 00:00:00 2001 From: Seb Flippence <144435+SebFlippence@users.noreply.github.com> Date: Tue, 10 Dec 2019 09:58:11 +0000 Subject: [PATCH 3/4] Updated type help text --- utils/tempered.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/tempered.c b/utils/tempered.c index 023ca5f..64b591a 100644 --- a/utils/tempered.c +++ b/utils/tempered.c @@ -32,8 +32,9 @@ void show_help() " -h\n" " --help Show this help text\n" " -t\n" -" --type Returns only the numeric value of the sensor.\n" -" Known sensors: temperature, humidity, dew point\n" +" -t \n" +" --type Returns only the numeric value of the sensor.\n" +" Known types: temperature, humidity, \"dew point\"\n" " -e\n" " --enumerate Enumerate the found devices without reading them.\n" " -s \n" From 506fb68b9c7ac7b1867bdc21bf80280c1123bffd Mon Sep 17 00:00:00 2001 From: Seb Flippence <144435+SebFlippence@users.noreply.github.com> Date: Tue, 10 Dec 2019 09:59:25 +0000 Subject: [PATCH 4/4] Removed extra line --- utils/tempered.c | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/tempered.c b/utils/tempered.c index 64b591a..d9b3ab1 100644 --- a/utils/tempered.c +++ b/utils/tempered.c @@ -31,7 +31,6 @@ void show_help() "Known options:\n" " -h\n" " --help Show this help text\n" -" -t\n" " -t \n" " --type Returns only the numeric value of the sensor.\n" " Known types: temperature, humidity, \"dew point\"\n"