Browse Source

Added SinkDimension

The i member in SinkConfig now has its dimension, and thus its unit,
set by the new idim member.  It's a bit clunky, but the other
alternative I thought of is even clunkier.
Clara Hobbs 6 years ago
parent
commit
b3c063bf5f
2 changed files with 79 additions and 25 deletions
  1. 32
    6
      pdbuddy/__init__.py
  2. 47
    19
      test_pdbuddy/__init__.py

+ 32
- 6
pdbuddy/__init__.py View File

@@ -238,13 +238,14 @@ class Sink:
238 238
             cls.pid))
239 239
 
240 240
 
241
-class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i")):
241
+class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i idim")):
242 242
     """Python representation of a PD Buddy Sink configuration object
243 243
 
244 244
     ``status`` should be a `SinkStatus` object.  ``flags`` should be zero or
245 245
     more `SinkFlags` values.  ``v``, ``vmin``, and ``vmax`` are voltages in
246
-    millivolts, and ``i`` is the current in milliamperes.  `None` is also an
247
-    acceptible value for any of the fields.
246
+    millivolts, ``i`` is the value used to set current in the appropriate
247
+    milli- SI unit, and ``idim`` is the dimension of ``i``.  `None` is also
248
+    an acceptible value for any of the fields.
248 249
     """
249 250
     __slots__ = ()
250 251
 
@@ -283,7 +284,12 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i")):
283 284
             s += "vmax: {:.3f} V\n".format(self.vmax / 1000.0)
284 285
 
285 286
         if self.i is not None:
286
-            s += "i: {:.2f} A\n".format(self.i / 1000.0)
287
+            if self.idim is SinkDimension.CURRENT:
288
+                s += "i: {:.2f} A\n".format(self.i / 1000.0)
289
+            if self.idim is SinkDimension.POWER:
290
+                s += "p: {:.2f} W\n".format(self.i / 1000.0)
291
+            if self.idim is SinkDimension.RESISTANCE:
292
+                s += "r: {:.2f} \u03A9\n".format(self.i / 1000.0)
287 293
 
288 294
         # Return all but the last character of s to remove the trailing newline
289 295
         if s:
@@ -308,6 +314,7 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i")):
308 314
         vmin = None
309 315
         vmax = None
310 316
         i = None
317
+        idim = None
311 318
 
312 319
         # Iterate over all lines of text
313 320
         for line in text:
@@ -316,7 +323,7 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i")):
316 323
                 raise IndexError("configuration index out of range")
317 324
             # If there is no configuration, return an empty SinkConfig
318 325
             elif line.startswith(b"No configuration"):
319
-                return cls(None, None, None, None, None, None)
326
+                return cls(None, None, None, None, None, None, None)
320 327
             # If this line is the status field
321 328
             elif line.startswith(b"status: "):
322 329
                 line = line.split()[1:]
@@ -354,9 +361,21 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i")):
354 361
             elif line.startswith(b"i: "):
355 362
                 word = line.split()[1]
356 363
                 i = round(1000*float(word))
364
+                idim = SinkDimension.CURRENT
365
+            # If this line is the p field
366
+            elif line.startswith(b"p: "):
367
+                word = line.split()[1]
368
+                i = round(1000*float(word))
369
+                idim = SinkDimension.POWER
370
+            # If this line is the r field
371
+            elif line.startswith(b"r: "):
372
+                word = line.split()[1]
373
+                i = round(1000*float(word))
374
+                idim = SinkDimension.RESISTANCE
357 375
 
358 376
         # Create a new SinkConfig object with the values we just read
359
-        return cls(status=status, flags=flags, v=v, vmin=vmin, vmax=vmax, i=i)
377
+        return cls(status=status, flags=flags, v=v, vmin=vmin, vmax=vmax, i=i,
378
+                idim=idim)
360 379
 
361 380
 
362 381
 class SinkStatus(enum.Enum):
@@ -366,6 +385,13 @@ class SinkStatus(enum.Enum):
366 385
     INVALID = 3
367 386
 
368 387
 
388
+class SinkDimension(enum.Enum):
389
+    """Dimension of the value used to set the current requested"""
390
+    CURRENT = 1
391
+    POWER = 2
392
+    RESISTANCE = 3
393
+
394
+
369 395
 class SinkFlags(enum.Flag):
370 396
     """Flags field of a PD Buddy Sink configuration object"""
371 397
     NONE = 0

+ 47
- 19
test_pdbuddy/__init__.py View File

@@ -18,37 +18,37 @@ class SinkTestCase(unittest.TestCase):
18 18
 
19 19
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
20 20
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
21
-                i=3000)
21
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
22 22
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
23 23
                 flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
24
-                vmax=None, i=3000)
24
+                vmax=None, i=3000, idim=pdbuddy.SinkDimension.CURRENT)
25 25
         self.obj_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
26 26
                 flags=pdbuddy.SinkFlags.HV_PREFERRED, v=13800, vmin=12000,
27
-                vmax=16000, i=2000)
27
+                vmax=16000, i=2000, idim=pdbuddy.SinkDimension.CURRENT)
28 28
 
29 29
         self.obj_huge_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
30 30
                 flags=pdbuddy.SinkFlags.NONE, v=65536, vmin=None, vmax=None,
31
-                i=1000)
31
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
32 32
         self.obj_big_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
33 33
                 flags=pdbuddy.SinkFlags.NONE, v=21001, vmin=None, vmax=None,
34
-                i=1000)
34
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
35 35
         self.obj_neg_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
36 36
                 flags=pdbuddy.SinkFlags.NONE, v=-1, vmin=None, vmax=None,
37
-                i=1000)
37
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
38 38
 
39 39
         self.obj_inv_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
40 40
                 flags=pdbuddy.SinkFlags.HV_PREFERRED, v=13800, vmin=16000,
41
-                vmax=12000, i=2000)
41
+                vmax=12000, i=2000, idim=pdbuddy.SinkDimension.CURRENT)
42 42
 
43 43
         self.obj_huge_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
44 44
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
45
-                i=65536)
45
+                i=65536, idim=pdbuddy.SinkDimension.CURRENT)
46 46
         self.obj_big_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
47 47
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
48
-                i=5001)
48
+                i=5001, idim=pdbuddy.SinkDimension.CURRENT)
49 49
         self.obj_neg_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
50 50
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
51
-                i=-1)
51
+                i=-1, idim=pdbuddy.SinkDimension.CURRENT)
52 52
 
53 53
     def tearDown(self):
54 54
         # Close the connection to the PD Buddy Sink
@@ -168,30 +168,36 @@ class SinkConfigTestCase(unittest.TestCase):
168 168
 
169 169
     def setUp(self):
170 170
         self.obj_none = pdbuddy.SinkConfig(status=None, flags=None, v=None,
171
-                vmin=None, vmax=None, i=None)
171
+                vmin=None, vmax=None, i=None, idim=None)
172 172
         self.obj_empty = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.EMPTY,
173
-                flags=None, v=None, vmin=None, vmax=None, i=None)
173
+                flags=None, v=None, vmin=None, vmax=None, i=None, idim=None)
174 174
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
175 175
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
176
-                i=3000)
176
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
177 177
         self.obj_invalid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.INVALID,
178 178
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
179
-                i=3000)
179
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
180 180
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
181 181
                 flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
182
-                vmax=None, i=3000)
182
+                vmax=None, i=3000, idim=pdbuddy.SinkDimension.CURRENT)
183 183
         self.obj_valid_5v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
184 184
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
185
-                i=3000)
185
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
186 186
         self.obj_valid_1a = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
187 187
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
188
-                i=1000)
188
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
189 189
         self.obj_valid_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
190 190
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=12000, vmax=16000,
191
-                i=1000)
191
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
192 192
         self.obj_valid_hv = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
193 193
                 flags=pdbuddy.SinkFlags.HV_PREFERRED, v=15000, vmin=12000,
194
-                vmax=16000, i=1000)
194
+                vmax=16000, i=1000, idim=pdbuddy.SinkDimension.CURRENT)
195
+        self.obj_valid_10w = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
196
+                flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
197
+                i=10000, idim=pdbuddy.SinkDimension.POWER)
198
+        self.obj_valid_10r = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
199
+                flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
200
+                i=10000, idim=pdbuddy.SinkDimension.RESISTANCE)
195 201
 
196 202
     def test_str_none(self):
197 203
         self.assertEqual(str(self.obj_none), "No configuration")
@@ -229,6 +235,14 @@ class SinkConfigTestCase(unittest.TestCase):
229 235
                 "status: valid\nflags: HV_Preferred\nv: 15.000 V\n"
230 236
                 "vmin: 12.000 V\nvmax: 16.000 V\ni: 1.00 A")
231 237
 
238
+    def test_str_valid_10w(self):
239
+        self.assertEqual(str(self.obj_valid_10w),
240
+                "status: valid\nflags: (none)\nv: 15.000 V\np: 10.00 W")
241
+
242
+    def test_str_valid_10r(self):
243
+        self.assertEqual(str(self.obj_valid_10r),
244
+                "status: valid\nflags: (none)\nv: 15.000 V\nr: 10.00 \u03A9")
245
+
232 246
     def test_from_text_none(self):
233 247
         ft_none = pdbuddy.SinkConfig.from_text([])
234 248
         self.assertEqual(ft_none, self.obj_none)
@@ -290,6 +304,20 @@ class SinkConfigTestCase(unittest.TestCase):
290 304
                 b"i: 1.00 A"])
291 305
         self.assertEqual(ft_valid_hv, self.obj_valid_hv)
292 306
 
307
+    def test_from_text_valid_10w(self):
308
+        ft_valid_10w = pdbuddy.SinkConfig.from_text([b"status: valid",
309
+                b"flags: (none)",
310
+                b"v: 15.000 V",
311
+                b"p: 10.00 W"])
312
+        self.assertEqual(ft_valid_10w, self.obj_valid_10w)
313
+
314
+    def test_from_text_valid_10r(self):
315
+        ft_valid_10r = pdbuddy.SinkConfig.from_text([b"status: valid",
316
+                b"flags: (none)",
317
+                b"v: 15.000 V",
318
+                b"r: 10.00 \u03A9"])
319
+        self.assertEqual(ft_valid_10r, self.obj_valid_10r)
320
+
293 321
     def test_from_text_invalid_index(self):
294 322
         with self.assertRaises(IndexError):
295 323
             pdbuddy.SinkConfig.from_text([b"Invalid index"])

Loading…
Cancel
Save