X-Git-Url: http://pilppa.org/gitweb/?a=blobdiff_plain;f=src%2FW1TemperatureSensor.cc;h=5f954cfaa1403923fd58f639128510a2288ee33b;hb=9e569babf7e271015fedac3b7153d9035ff17ee0;hp=b02bb29dd01518738b2fd59136d3b94f8a7d37d4;hpb=83dba70b46600014932da34e8744ba70584077aa;p=lib1wire.git diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index b02bb29..5f954cf 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -9,14 +9,15 @@ #include #include -#include "W1Util.hh" -#include "W1TemperatureSensor.hh" - #include +#include "W1TemperatureSensor.hh" + using namespace std; using namespace w1; +#define CONST_UNIT_CELCIUS "C" + template bool string_to_number(NumberDataType& result, const std::string& string_param, @@ -26,89 +27,84 @@ bool string_to_number(NumberDataType& result, return !(iss >> format >> result).fail(); } -string convert_celcius_value_to_three_digits(string raw_value) { - string ret_val; +double convert_w1_temperature_to_celcius(string raw_value, + int *err_flg) { bool suc_flg; double dbl_val; + dbl_val = 0; suc_flg = string_to_number(dbl_val, raw_value, dec); if (suc_flg == true) { - dbl_val = dbl_val / 1000; + dbl_val = dbl_val / 1000; + *err_flg = 0; +/* std::ostringstream out; out << fixed << setprecision(3) << dbl_val; ret_val = out.str(); +*/ } else { - ret_val = raw_value; + log_error("Failed to convert temperature %s to celcius value.", raw_value.c_str()); + *err_flg = 1; } - return ret_val; + return dbl_val; } -W1TemperatureSensor::W1TemperatureSensor(int family_code_param, - string device_id_param, - dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) { - string text; - - log_debug("trying to open file: %s\n", slave_file.c_str()); +W1TemperatureSensor::W1TemperatureSensor(string device_id_param, + string device_type_param, + dirent *direntry_param): W1Device(device_id_param, device_type_param, direntry_param) { ifstream ifs(slave_file.c_str()); if (ifs.is_open() == false) { - text = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file + "\n"; - log_debug(text.c_str()); - log_debug("verify that you have w1_therm kernel module loaded.\n"); + log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_type().c_str(), slave_file.c_str()); + log_error("Verify that you have w1_therm kernel module loaded.\n"); ifs.close(); } } W1TemperatureSensor::~W1TemperatureSensor() { + log_debug("destructor\n"); } -bool W1TemperatureSensor::is_supported_family_code(int family_code) { - bool ret_val; - - ret_val = false; - switch(family_code) { - case 0x10: - case 0x28: - ret_val = true; - break; - } - return ret_val; -} - -string W1TemperatureSensor::get_raw_value() { - string temp; - string ret_val; - string last_line; +vector *W1TemperatureSensor::get_raw_data() { + vector *ret_val; + string tmp_str; + string val_str; + double val_dbl; int pos; - int length; - string format; + int b_cnt; + int err_flg; - ret_val = ""; + ret_val = NULL; + err_flg = 0; ifstream ifs(slave_file.c_str()); if (ifs.is_open() == true) { - while(getline(ifs, temp)) { - if (temp.length() > 0) { - last_line = temp; + while(getline(ifs, tmp_str)) { + if (tmp_str.empty() == false) { + val_str = tmp_str; } } ifs.close(); - length = last_line.length(); - if (length > 0) { - pos = last_line.find("t="); + b_cnt = val_str.length(); + if (b_cnt > 0) { + pos = val_str.find("t="); if ((pos >= 0) && - (pos + 2 < length)) { - ret_val = last_line.substr(pos + 2); + ((pos + 2) < b_cnt)) { + val_str = val_str.substr(pos + 2); + val_dbl = convert_w1_temperature_to_celcius(val_str, &err_flg); + if (err_flg == 0) { + ret_val = new vector(); + ret_val->push_back(val_dbl); + } } } } - ret_val = convert_celcius_value_to_three_digits(ret_val); return ret_val; } string W1TemperatureSensor::get_unit() { - return "C"; + return CONST_UNIT_CELCIUS; } -string W1TemperatureSensor::get_device_type() { - return "Temperature Sensor"; +unsigned int W1TemperatureSensor::get_data_decimal_precision() { + return 3; }