qemu-trivial
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC PATCH 08/17] hw/misc/tmp421: Extract set_temp_mC() helper


From: Philippe Mathieu-Daudé
Subject: [RFC PATCH 08/17] hw/misc/tmp421: Extract set_temp_mC() helper
Date: Tue, 21 Apr 2020 14:16:17 +0200

Since we are going to reuse this code, extract it first.

Signed-off-by: Philippe Mathieu-Daudé <address@hidden>
---
 hw/misc/tmp421.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/hw/misc/tmp421.c b/hw/misc/tmp421.c
index 8003356307..270e7d5510 100644
--- a/hw/misc/tmp421.c
+++ b/hw/misc/tmp421.c
@@ -122,6 +122,22 @@ static int64_t get_temp_mC(TMP421State *s, unsigned int id)
     return ((s->temperature[id] - offset) * 1000 + 128) / 256;
 }
 
+static void set_temp_mC(TMP421State *s, unsigned int id,
+                        int64_t temp, Error **errp)
+{
+    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
+    int offset = ext_range * 64 * 256;
+
+    assert(id < SENSORS_COUNT);
+    if (temp >= maxs[ext_range] || temp < mins[ext_range]) {
+        error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
+                   temp / 1000, temp % 1000);
+        return;
+    }
+
+    s->temperature[id] = (int16_t) ((temp * 256 - 128) / 1000) + offset;
+}
+
 static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
@@ -149,11 +165,8 @@ static void tmp421_get_temperature(Object *obj, Visitor 
*v, const char *name,
 static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
-    TMP421State *s = TMP421(obj);
     Error *local_err = NULL;
     int64_t temp;
-    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
-    int offset = ext_range * 64 * 256;
     int tempid;
 
     visit_type_int(v, name, &temp, &local_err);
@@ -162,12 +175,6 @@ static void tmp421_set_temperature(Object *obj, Visitor 
*v, const char *name,
         return;
     }
 
-    if (temp >= maxs[ext_range] || temp < mins[ext_range]) {
-        error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
-                   temp / 1000, temp % 1000);
-        return;
-    }
-
     if (sscanf(name, "temperature%d", &tempid) != 1) {
         error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
@@ -178,7 +185,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, 
const char *name,
         return;
     }
 
-    s->temperature[tempid] = (int16_t) ((temp * 256 - 128) / 1000) + offset;
+    set_temp_mC(TMP421(obj), tempid, temp, errp);
 }
 
 static void tmp421_read(TMP421State *s)
-- 
2.21.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]