From c5db24757ed9d1461eca0a78b786a1261ea71380 Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Sat, 23 Oct 2010 12:28:20 +0300 Subject: [PATCH] formating fixes for celcius values --- src/W1TemperatureSensor.cc | 32 ++++++++++++++++++++++++++++++++ src_test/test_w1.cc | 1 + 2 files changed, 33 insertions(+) diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index 38baecb..20dc3f3 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -7,12 +7,42 @@ #include #include #include +#include +#include #include "W1TemperatureSensor.hh" using namespace std; using namespace w1; +template +bool string_to_number(T& result, + const std::string& string_param, + std::ios_base& (*format)(std::ios_base&)) +{ + std::istringstream iss(string_param); + return !(iss >> format >> result).fail(); +} + +string convert_for_3_digits_value(string raw_value) { + string ret_val; + int int_val; + bool sucFlg; + double dbl_val; + + sucFlg = string_to_number(dbl_val, raw_value, dec); + if (sucFlg == true) { + dbl_val = dbl_val / 1000; + std::ostringstream out; + out << fixed << setprecision(3) << dbl_val; + ret_val = out.str(); + } + else { + ret_val = raw_value; + } + return ret_val; +} + W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) { ifstream ifs(slave_file.c_str()); if (ifs.is_open() == false) { @@ -51,6 +81,7 @@ string W1TemperatureSensor::get_value() { int pos; int length; string formatted_data; + int int_value; ret_val = ""; ifstream ifs(slave_file.c_str()); @@ -71,6 +102,7 @@ string W1TemperatureSensor::get_value() { } } } + ret_val = convert_for_3_digits_value(ret_val); formatted_data = get_formatted_data(ret_val); add_to_memory_cache(formatted_data); return ret_val; diff --git a/src_test/test_w1.cc b/src_test/test_w1.cc index 4b94a61..6c6c116 100644 --- a/src_test/test_w1.cc +++ b/src_test/test_w1.cc @@ -40,6 +40,7 @@ int main(int argc, char** argv) cout << name << ": " << value << " " << unit << endl; */ device->printout(); + sleep(1); if (round >= store_interval) { device->store(); } -- 2.41.1