Try and fix weirdness with hwmon/i2c/platform options.
authorBrenden Matthews <brenden@rty.ca>
Sun, 24 May 2009 19:07:03 +0000 (13:07 -0600)
committerBrenden Matthews <brenden@rty.ca>
Sun, 24 May 2009 19:07:27 +0000 (13:07 -0600)
doc/variables.xml
src/conky.c

index 48df5bf..acc7eb9 100644 (file)
        <varlistentry>
                <term>
                        <command><option>i2c</option></command>
-                       <option>(dev) type n</option>
+                       <option>(dev) type n (factor offset)</option>
                </term>
                <listitem>
-                       I2C sensor from sysfs (Linux 2.6).  Parameter dev may be omitted if you have only one I2C device.  Parameter type is either 'in' or 'vol' meaning voltage; 'fan' meaning fan; 'temp' meaning temperature.  Parameter n is number of the sensor. See /sys/bus/i2c/devices/ on your local computer.
+                       I2C sensor from sysfs (Linux 2.6).  Parameter dev may be omitted if you have only one I2C device.  Parameter type is either 'in' or 'vol' meaning voltage; 'fan' meaning fan; 'temp' meaning temperature.  Parameter n is number of the sensor. See /sys/bus/i2c/devices/ on your local computer. The optional arguments 'factor' and 'offset' allow precalculation of the raw input, which is being modified as follows: 'input = input * factor + offset'. Note that they have to be given as decimal values (i.e. contain at least one decimal place).
                        <para></para></listitem>
        </varlistentry>
 
        <varlistentry>
                <term>
                        <command><option>platform</option></command>
-                       <option>(dev) type n</option>
+                       <option>(dev) type n (factor offset)</option>
                </term>
                <listitem>
-                       Platform sensor from sysfs (Linux 2.6). Parameter dev may be omitted if you have only one platform device. Platform type is either 'in' or 'vol' meaning voltage; 'fan' meaning fan; 'temp' meaning temperature. Parameter n is number of the sensor. See /sys/bus/platform/devices/ on your local computer.
+                       Platform sensor from sysfs (Linux 2.6). Parameter dev may be omitted if you have only one platform device. Platform type is either 'in' or 'vol' meaning voltage; 'fan' meaning fan; 'temp' meaning temperature. Parameter n is number of the sensor. See /sys/bus/platform/devices/ on your local computer.  The optional arguments 'factor' and 'offset' allow precalculation of the raw input, which is being modified as follows: 'input = input * factor + offset'. Note that they have to be given as decimal values (i.e. contain at least one decimal place).
                        <para></para></listitem>
        </varlistentry>
 
index bffc9ca..a711892 100644 (file)
@@ -1782,7 +1782,8 @@ static struct text_object *construct_text_object(const char *s,
 #ifdef __linux__
        END OBJ(i2c, INFO_SYSFS)
                char buf1[64], buf2[64];
-               int n;
+               float factor, offset;
+               int n, found = 0;
 
                if (!arg) {
                        ERR("i2c needs arguments");
@@ -1791,22 +1792,32 @@ static struct text_object *construct_text_object(const char *s,
                        return NULL;
                }
 
-               if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
-                       /* if scanf couldn't read three values, read type and num and use
-                        * default device */
-                       sscanf(arg, "%63s %d", buf2, &n);
-                       obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-                       strncpy(obj->data.sysfs.type, buf2, 63);
-               } else {
-                       obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-                       strncpy(obj->data.sysfs.type, buf2, 63);
+#define HWMON_RESET() {\
+               buf1[0] = 0; \
+               factor = 1.0; \
+               offset = 0.0; }
+
+               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
+               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
+               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
+               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
+
+               if (!found) {
+                       ERR("i2c failed to parse arguments");
+                       obj->type = OBJ_text;
+                       return NULL;
                }
+               DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
+               obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
+                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
+               strncpy(obj->data.sysfs.type, buf2, 63);
+               obj->data.sysfs.factor = factor;
+               obj->data.sysfs.offset = offset;
 
        END OBJ(platform, INFO_SYSFS)
                char buf1[64], buf2[64];
-               int n;
+               float factor, offset;
+               int n, found = 0;
 
                if (!arg) {
                        ERR("platform needs arguments");
@@ -1814,18 +1825,22 @@ static struct text_object *construct_text_object(const char *s,
                        return NULL;
                }
 
-               if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
-                       /* if scanf couldn't read three values, read type and num and use
-                        * default device */
-                       sscanf(arg, "%63s %d", buf2, &n);
-                       obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-                       strncpy(obj->data.sysfs.type, buf2, 63);
-               } else {
-                       obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-                       strncpy(obj->data.sysfs.type, buf2, 63);
+               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
+               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
+               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
+               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
+
+               if (!found) {
+                       ERR("platform failed to parse arguments");
+                       obj->type = OBJ_text;
+                       return NULL;
                }
+               DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
+               obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
+                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
+               strncpy(obj->data.sysfs.type, buf2, 63);
+               obj->data.sysfs.factor = factor;
+               obj->data.sysfs.offset = offset;
 
        END OBJ(hwmon, INFO_SYSFS)
                char buf1[64], buf2[64];
@@ -1838,17 +1853,11 @@ static struct text_object *construct_text_object(const char *s,
                        return NULL;
                }
 
-#define HWMON_RESET() {\
-               buf1[0] = 0; \
-               factor = 1.0; \
-               offset = 0.0; }
-
                if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
                if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
                if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
                if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
 
-
 #undef HWMON_RESET
 
                if (!found) {
@@ -4384,6 +4393,8 @@ static void generate_text_internal(char *p, int p_max_size,
                                r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
                                        obj->data.sysfs.devtype, obj->data.sysfs.type);
 
+                               r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
+
                                if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
                                        temp_print(p, p_max_size, r, TEMP_CELSIUS);
                                } else if (r >= 100.0 || r == 0) {
@@ -4398,6 +4409,8 @@ static void generate_text_internal(char *p, int p_max_size,
                                r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
                                        obj->data.sysfs.devtype, obj->data.sysfs.type);
 
+                               r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
+
                                if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
                                        temp_print(p, p_max_size, r, TEMP_CELSIUS);
                                } else if (r >= 100.0 || r == 0) {