]> pilppa.com Git - lib1wire.git/commitdiff
added W1CounterDevice
authorMika Laitio <lamikr@pilppa.org>
Sat, 30 Oct 2010 14:19:42 +0000 (17:19 +0300)
committerMika Laitio <lamikr@pilppa.org>
Sat, 30 Oct 2010 14:19:42 +0000 (17:19 +0300)
src/Makefile.am
src/W1CounterDevice.cc [new file with mode: 0644]
src/W1CounterDevice.hh [new file with mode: 0644]
src/W1Scanner.cc
src/W1TemperatureSensor.cc
src/W1TemperatureSensor.hh
src_test/test_w1.cc

index f34f44609bdc7729c2f695f40080716c5db9b90a..a243760e7a9cdb9332669d9e781e2ab08919d384 100644 (file)
@@ -2,7 +2,8 @@ lib_LTLIBRARIES = lib1wire.la
 lib1wire_la_SOURCES = \
        W1Device.cc W1Device.hh \
        W1Scanner.cc W1Scanner.hh \
-       W1TemperatureSensor.cc W1TemperatureSensor.hh
+       W1TemperatureSensor.cc W1TemperatureSensor.hh \
+       W1CounterDevice.cc W1CounterDevice.hh
 lib1wire_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined
 AM_CPPFLAGS = $(SRC_CFLAGS) -Iidl
 
@@ -12,5 +13,5 @@ lib1wireincludedir=$(includedir)/w1
 lib1wireinclude_HEADERS = \
        W1Device.hh \
        W1Scanner.hh \
-       W1TemperatureSensor.hh
-
+       W1TemperatureSensor.hh \
+       W1CounterDevice.hh
\ No newline at end of file
diff --git a/src/W1CounterDevice.cc b/src/W1CounterDevice.cc
new file mode 100644 (file)
index 0000000..d339562
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * W1CounterDevice.cc
+ *
+ *  Created on: Oct 30, 2010
+ *      Author: lamikr
+ */
+
+#include <iostream>
+#include <fstream>
+
+#include "W1CounterDevice.hh"
+
+using namespace std;
+using namespace w1;
+
+W1CounterDevice::W1CounterDevice(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) {
+               string text;
+
+               text    = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
+               cout << text << endl;
+               cout << "verify that you have w1_ds2423 kernel module loaded." << endl;
+       }
+       else {
+               ifs.close();
+       }
+}
+
+W1CounterDevice::~W1CounterDevice() {
+       // TODO Auto-generated destructor stub
+}
+
+bool W1CounterDevice::is_supported_family_code(int family_code) {
+       bool    ret_val;
+
+       ret_val = false;
+       switch(family_code) {
+               case 0x1d:
+                       ret_val = true;
+                       break;
+       }
+       return ret_val;
+}
+
+string W1CounterDevice::get_value() {
+       string          ret_val;
+       string          value_line;
+       int             pos;
+       int             length;
+       int             ii;
+
+       ret_val = "<could not read>";
+       ifstream ifs(slave_file.c_str());
+       if (ifs.is_open() == true) {
+               ret_val = "";
+               while(getline(ifs, value_line)) {
+                       length  = value_line.length();
+                       if (length > 0) {
+                               pos     = value_line.find("c=");
+                               if ((pos >= 0) &&
+                                   (pos + 2 < length)) {
+                                       value_line      = value_line.substr(pos + 2);
+                               }
+                               else {
+                                       value_line      = "";
+                               }
+                       }
+                       else {
+                               value_line      = "";
+                       }
+                       if (ret_val.length() == 0) {
+                               ret_val = value_line;
+                       }
+                       else {
+                               ret_val = ret_val + "|" + value_line;
+                       }
+               }
+               ifs.close();
+       }
+       add_to_memory_cache(ret_val);
+       return ret_val;
+}
+
+string W1CounterDevice::get_unit() {
+       return "";
+}
+
+string W1CounterDevice::get_devicetype_name() {
+       return "Counter Device";
+}
diff --git a/src/W1CounterDevice.hh b/src/W1CounterDevice.hh
new file mode 100644 (file)
index 0000000..816f3b8
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * W1CounterDevice.hh
+ *
+ *  Created on: Oct 30, 2010
+ *      Author: lamikr
+ */
+
+#ifndef W1COUNTERDEVICE_HH_
+#define W1COUNTERDEVICE_HH_
+
+#include "W1Device.hh"
+
+namespace w1 {
+       class W1CounterDevice: public w1::W1Device {
+               public:
+                       W1CounterDevice(dirent *direntry, int family_code_param, std::string id_param);
+                       virtual ~W1CounterDevice();
+                       std::string get_value();
+                       std::string get_unit();
+                       std::string get_devicetype_name();
+               protected:
+                       bool is_supported_family_code(int family_code);
+       };
+}
+
+#endif /* W1COUNTERDEVICE_HH_ */
index 6a55eeb9c87ccab421928ef928fe36a18dcabf7f..a27b23d5d41430c749f26e23b0521bd70f858a01 100644 (file)
 #include <plp/log.h>
 #include "W1Scanner.hh"
 #include "W1TemperatureSensor.hh"
+#include "W1CounterDevice.hh"
 
 using namespace w1;
 using namespace std;
 
 W1Scanner::W1Scanner() {
-       log_debug("created");
+       log_debug("created\n");
 }
 
 W1Scanner::~W1Scanner() {
-       log_debug("destroyed");
+       log_debug("destroyed\n");
 }
 
 bool W1Scanner::is_subdirectory(dirent *direntry) {
@@ -70,11 +71,14 @@ W1Device *W1Scanner::create_device(dirent *direntry) {
                                        cout << "temperature sensor: " << family_code << endl;
                                        ret_val = new W1TemperatureSensor(direntry, family_code, device_name);
                                        break;
+                               case    0x1d:
+                                       cout << "counter device: " << family_code << endl;
+                                       ret_val = new W1CounterDevice(direntry, family_code, device_name);
+                                       break;
                        }
                }
        }
        return ret_val;
-
 }
 
 list<W1Device *> W1Scanner::get_device_list() {
index 39977e528cc75d97ef0c1ce6b7a2162adb79c34b..84db272a83d7aa17a1b9f58a1054f70ac75ec955 100644 (file)
@@ -5,7 +5,6 @@
  *      Author: lamikr
  */
 #include <fstream>
-#include <vector>
 #include <iostream>
 #include <sstream>
 #include <iomanip>
@@ -48,9 +47,9 @@ W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param
        if (ifs.is_open() == false) {
                string text;
 
-               text    = get_time() + ": device type = temperature sensor, id = " + id + ", could not read file: " + slave_file;
+               text    = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
                cout << text << endl;
-               cout << "verify that you have w1_therm kernel module loaded" << endl;
+               cout << "verify that you have w1_therm kernel module loaded." << endl;
        }
        else {
                ifs.close();
@@ -74,7 +73,6 @@ bool W1TemperatureSensor::is_supported_family_code(int family_code) {
 }
 
 string W1TemperatureSensor::get_value() {
-       vector<string>  text_file;
        string          temp;
        string          ret_val;
        string          last_line;
index bef91c9907a68dc0d5611453ac097dc44c4e73da..bf55522f963f81c4eab8b40531f1d6bd82ca31b3 100644 (file)
@@ -18,9 +18,7 @@ namespace w1 {
                        std::string get_value();
                        std::string get_unit();
                        std::string get_devicetype_name();
-                       //void printout();
                protected:
-                       //std::string get_formatted_data();
                        bool is_supported_family_code(int family_code);
        };
 }
index 6c6c1167f31b311a89496be63c3e4ed6505d1090..3a07a0a37ab780f8322b69cc26312edabe639738 100644 (file)
@@ -8,7 +8,7 @@
 #include <string>
 #include <iostream>
 
-#include <unistd.h>
+#include <plp/log.h>
 
 #include "W1Scanner.hh"
 
@@ -17,38 +17,43 @@ using namespace std;
 
 int main(int argc, char** argv)
 {
-       W1Scanner                       *scanner;
+       W1Scanner               *scanner;
        list<W1Device *>        device_list;
-       int                                     round;
-       int                                     interval_seconds;
-       int                                     store_interval;
+       int                     round;
+       int                     interval_seconds;
+       int                     store_interval;
 
        interval_seconds        = 60;
        store_interval          = 10;
-       scanner                         = new W1Scanner();
-       device_list                     = scanner->get_device_list();
-       round                           = 0;
-       while(1) {
-               round++;
-               for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++)
-               {
-                       W1Device *device = (W1Device *)*list_iter;
+       scanner                 = new W1Scanner();
+       device_list             = scanner->get_device_list();
+       round                   = 0;
+       if (device_list.size() > 0) {
+               while(1) {
+                       round++;
+                       for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++)
+                       {
+                               W1Device *device = (W1Device *)*list_iter;
 /*
                        string name     = device->get_name();
                        string value    = device->get_value();
                        string unit             = device->get_unit();
                        cout << name << ": " << value << " " << unit << endl;
 */
-                       device->printout();
-                       sleep(1);
+                               device->printout();
+                               sleep(1);
+                               if (round >= store_interval) {
+                                       device->store();
+                               }
+                       }
+                       sleep(interval_seconds);
                        if (round >= store_interval) {
-                               device->store();
+                               round = 0;
                        }
                }
-               sleep(interval_seconds);
-               if (round >= store_interval) {
-                       round = 0;
-               }
+       }
+       else {
+               log_debug("could not find 1-wire devices.\n");
        }
        return 0;
 }