]> pilppa.com Git - lib1wire.git/commitdiff
formating fixes for celcius values
authorMika Laitio <lamikr@pilppa.org>
Sat, 23 Oct 2010 09:28:20 +0000 (12:28 +0300)
committerMika Laitio <lamikr@pilppa.org>
Sat, 23 Oct 2010 09:28:20 +0000 (12:28 +0300)
src/W1TemperatureSensor.cc
src_test/test_w1.cc

index 38baecbdb51be505a9af2fa479dcfe68c65e27df..20dc3f388b9fb228736f9143c3d9188dc3b6b150 100644 (file)
@@ -7,12 +7,42 @@
 #include <fstream>
 #include <vector>
 #include <iostream>
+#include <sstream>
+#include <iomanip>
 
 #include "W1TemperatureSensor.hh"
 
 using namespace std;
 using namespace w1;
 
+template <class T>
+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<double>(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 = "<could not read>";
        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;
index 4b94a612784c7582fc3b5cabaeaccd42d5658d38..6c6c1167f31b311a89496be63c3e4ed6505d1090 100644 (file)
@@ -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();
                        }