Skip to contentSkip to conversation

iio-monitor: Add support for current and power

PATCH Applied Geert Uytterhoeven 2 participants 2 messages Dec 21, 2016 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio-monitor: Add support for current and power

Currently iio-monitor supports temperature and voltage values only. Everything else is treated like a voltage.

Add support for common values like current and power.

---
 examples/iio-monitor.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
examples/iio-monitor.c +27 -4
index 558c0f33a36ef7b9..b508049fdb5dfc3f 100644
@@ -93,6 +93,29 @@ static double get_channel_value(struct iio_channel *chn)
 	return val / 1000.0;
 }
 
+static struct {
+	const char *id;
+	const char *unit;
+} map[] = {
+	{ "current",	"A" },
+	{ "power",	"W" },
+	{ "temp",	"°C" },
+	{ "voltage",	"V" },
+	{ 0, },
+};
+
+static const char *id_to_unit(const char *id)
+{
+	unsigned int i;
+
+	for (i = 0; map[i].id; i++) {
+		if (!strncmp(id, map[i].id, strlen(map[i].id)))
+			return map[i].unit;
+	}
+
+	return "";
+}
+
 static void * read_thd(void *d)
 {
 	struct iio_context *ctx = d;
@@ -131,7 +154,7 @@ static void * read_thd(void *d)
 		nb_channels = iio_device_get_channels_count(dev);
 		for (i = 0; i < nb_channels; i++) {
 			const char *id;
-			bool is_temp = false;
+			const char *unit;
 			struct iio_channel *chn =
 				iio_device_get_channel(dev, i);
 			if (!is_valid_channel(chn))
@@ -142,7 +165,7 @@ static void * read_thd(void *d)
 			id = iio_channel_get_id(chn);
 			if (!name)
 				name = id;
-			is_temp = !strncmp(id, "temp", 4);
+			unit = id_to_unit(id);
 
 			sprintf(buf, "</%u></B>%s<!B><!%u>",
 					BLUE, name, BLUE);
@@ -152,8 +175,8 @@ static void * read_thd(void *d)
 			freeChtype(str);
 
 			sprintf(buf, "</%u></B>%.3lf %s<!B><!%u>",
-					YELLOW, get_channel_value(chn),
-					is_temp ? "°C" : "V", YELLOW);
+					YELLOW, get_channel_value(chn), unit,
+					YELLOW);
 			str = char2Chtype(buf, &len, &align);
 			writeChtype(right, col / 2, line++,
 					str, HORIZONTAL, 0, len);
Signature · 1 line
1.9.1
#1 lore raw

Hi,
Le mercredi 21 décembre 2016 à 15:10 +0100, Geert Uytterhoeven a écrit :

Quoted 74 lines

Currently iio-monitor supports temperature and voltage values only. Everything else is treated like a voltage.

Add support for common values like current and power.

---
 examples/iio-monitor.c | 31 +++++++++++++++++++++++++++----

 1 file changed, 27 insertions(+), 4 deletions(-)
examples/iio-monitor.c +0 -0
index 558c0f33a36ef7b9..b508049fdb5dfc3f 100644
@@ -93,6 +93,29 @@ static double get_channel_value(struct iio_channel
 *chn)
  	return val / 1000.0;
  }

examples/iio-monitor.c +23 -0
+static struct {
+	const char *id;
+	const char *unit;
+} map[] = {
+	{ "current",	"A" },
+	{ "power",	"W" },
+	{ "temp",	"°C" },
+	{ "voltage",	"V" },
+	{ 0, },
+};
+
+static const char *id_to_unit(const char *id)
+{
+	unsigned int i;
+
+	for (i = 0; map[i].id; i++) {
+		if (!strncmp(id, map[i].id, strlen(map[i].id)))
+			return map[i].unit;
+	}
+
+	return "";
+}
+

 static void * read_thd(void *d)
 {
  struct iio_context *ctx = d;

examples/iio-monitor.c +1 -1
@@ -131,7 +154,7 @@ static void * read_thd(void *d)
  		nb_channels = iio_device_get_channels_count(dev);
  		for (i = 0; i < nb_channels; i++) {
  			const char *id;
-			bool is_temp = false;
+			const char *unit;

  struct iio_channel *chn = iio_device_get_channel(dev, i); if (!is_valid_channel(chn))

examples/iio-monitor.c +1 -1
@@ -142,7 +165,7 @@ static void * read_thd(void *d)
  			id = iio_channel_get_id(chn);
  			if (!name)
  				name = id;
-			is_temp = !strncmp(id, "temp", 4);
+			unit = id_to_unit(id);


  sprintf(buf, "</%u></B>%s<!B><!%u>", BLUE, name, BLUE);

examples/iio-monitor.c +0 -0
@@ -152,8 +175,8 @@ static void * read_thd(void *d)
  			freeChtype(str);


  sprintf(buf, "</%u></B>%.3lf %s<!B><!%u>",

examples/iio-monitor.c +0 -1
-					YELLOW,

get_channel_value(chn),

examples/iio-monitor.c +0 -1
-					is_temp ? "°C" : "V",

YELLOW);

examples/iio-monitor.c +1 -0
+					YELLOW,

get_channel_value(chn), unit,

examples/iio-monitor.c +1 -0
+					YELLOW);
Quoted 3 lines

  str = char2Chtype(buf, &len, &align); writeChtype(right, col / 2, line++, str, HORIZONTAL, 0, len);

Applied. Thanks!

-Paul