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
             cls.pid))
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
     """Python representation of a PD Buddy Sink configuration object
242
     """Python representation of a PD Buddy Sink configuration object
243
 
243
 
244
     ``status`` should be a `SinkStatus` object.  ``flags`` should be zero or
244
     ``status`` should be a `SinkStatus` object.  ``flags`` should be zero or
245
     more `SinkFlags` values.  ``v``, ``vmin``, and ``vmax`` are voltages in
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
     __slots__ = ()
250
     __slots__ = ()
250
 
251
 
283
             s += "vmax: {:.3f} V\n".format(self.vmax / 1000.0)
284
             s += "vmax: {:.3f} V\n".format(self.vmax / 1000.0)
284
 
285
 
285
         if self.i is not None:
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
         # Return all but the last character of s to remove the trailing newline
294
         # Return all but the last character of s to remove the trailing newline
289
         if s:
295
         if s:
308
         vmin = None
314
         vmin = None
309
         vmax = None
315
         vmax = None
310
         i = None
316
         i = None
317
+        idim = None
311
 
318
 
312
         # Iterate over all lines of text
319
         # Iterate over all lines of text
313
         for line in text:
320
         for line in text:
316
                 raise IndexError("configuration index out of range")
323
                 raise IndexError("configuration index out of range")
317
             # If there is no configuration, return an empty SinkConfig
324
             # If there is no configuration, return an empty SinkConfig
318
             elif line.startswith(b"No configuration"):
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
             # If this line is the status field
327
             # If this line is the status field
321
             elif line.startswith(b"status: "):
328
             elif line.startswith(b"status: "):
322
                 line = line.split()[1:]
329
                 line = line.split()[1:]
354
             elif line.startswith(b"i: "):
361
             elif line.startswith(b"i: "):
355
                 word = line.split()[1]
362
                 word = line.split()[1]
356
                 i = round(1000*float(word))
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
         # Create a new SinkConfig object with the values we just read
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
 class SinkStatus(enum.Enum):
381
 class SinkStatus(enum.Enum):
366
     INVALID = 3
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
 class SinkFlags(enum.Flag):
395
 class SinkFlags(enum.Flag):
370
     """Flags field of a PD Buddy Sink configuration object"""
396
     """Flags field of a PD Buddy Sink configuration object"""
371
     NONE = 0
397
     NONE = 0

+ 47
- 19
test_pdbuddy/__init__.py View File

18
 
18
 
19
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
19
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
20
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
20
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
21
-                i=3000)
21
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
22
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
22
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
23
                 flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
23
                 flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
24
-                vmax=None, i=3000)
24
+                vmax=None, i=3000, idim=pdbuddy.SinkDimension.CURRENT)
25
         self.obj_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
25
         self.obj_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
26
                 flags=pdbuddy.SinkFlags.HV_PREFERRED, v=13800, vmin=12000,
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
         self.obj_huge_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
29
         self.obj_huge_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
30
                 flags=pdbuddy.SinkFlags.NONE, v=65536, vmin=None, vmax=None,
30
                 flags=pdbuddy.SinkFlags.NONE, v=65536, vmin=None, vmax=None,
31
-                i=1000)
31
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
32
         self.obj_big_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
32
         self.obj_big_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
33
                 flags=pdbuddy.SinkFlags.NONE, v=21001, vmin=None, vmax=None,
33
                 flags=pdbuddy.SinkFlags.NONE, v=21001, vmin=None, vmax=None,
34
-                i=1000)
34
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
35
         self.obj_neg_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
35
         self.obj_neg_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
36
                 flags=pdbuddy.SinkFlags.NONE, v=-1, vmin=None, vmax=None,
36
                 flags=pdbuddy.SinkFlags.NONE, v=-1, vmin=None, vmax=None,
37
-                i=1000)
37
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
38
 
38
 
39
         self.obj_inv_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
39
         self.obj_inv_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
40
                 flags=pdbuddy.SinkFlags.HV_PREFERRED, v=13800, vmin=16000,
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
         self.obj_huge_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
43
         self.obj_huge_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
44
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
44
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
45
-                i=65536)
45
+                i=65536, idim=pdbuddy.SinkDimension.CURRENT)
46
         self.obj_big_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
46
         self.obj_big_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
47
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
47
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
48
-                i=5001)
48
+                i=5001, idim=pdbuddy.SinkDimension.CURRENT)
49
         self.obj_neg_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
49
         self.obj_neg_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
50
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
50
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
51
-                i=-1)
51
+                i=-1, idim=pdbuddy.SinkDimension.CURRENT)
52
 
52
 
53
     def tearDown(self):
53
     def tearDown(self):
54
         # Close the connection to the PD Buddy Sink
54
         # Close the connection to the PD Buddy Sink
168
 
168
 
169
     def setUp(self):
169
     def setUp(self):
170
         self.obj_none = pdbuddy.SinkConfig(status=None, flags=None, v=None,
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
         self.obj_empty = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.EMPTY,
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
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
174
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
175
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
175
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
176
-                i=3000)
176
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
177
         self.obj_invalid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.INVALID,
177
         self.obj_invalid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.INVALID,
178
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
178
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
179
-                i=3000)
179
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
180
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
180
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
181
                 flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
181
                 flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
182
-                vmax=None, i=3000)
182
+                vmax=None, i=3000, idim=pdbuddy.SinkDimension.CURRENT)
183
         self.obj_valid_5v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
183
         self.obj_valid_5v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
184
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
184
                 flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
185
-                i=3000)
185
+                i=3000, idim=pdbuddy.SinkDimension.CURRENT)
186
         self.obj_valid_1a = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
186
         self.obj_valid_1a = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
187
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
187
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
188
-                i=1000)
188
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
189
         self.obj_valid_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
189
         self.obj_valid_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
190
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=12000, vmax=16000,
190
                 flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=12000, vmax=16000,
191
-                i=1000)
191
+                i=1000, idim=pdbuddy.SinkDimension.CURRENT)
192
         self.obj_valid_hv = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
192
         self.obj_valid_hv = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
193
                 flags=pdbuddy.SinkFlags.HV_PREFERRED, v=15000, vmin=12000,
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
     def test_str_none(self):
202
     def test_str_none(self):
197
         self.assertEqual(str(self.obj_none), "No configuration")
203
         self.assertEqual(str(self.obj_none), "No configuration")
229
                 "status: valid\nflags: HV_Preferred\nv: 15.000 V\n"
235
                 "status: valid\nflags: HV_Preferred\nv: 15.000 V\n"
230
                 "vmin: 12.000 V\nvmax: 16.000 V\ni: 1.00 A")
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
     def test_from_text_none(self):
246
     def test_from_text_none(self):
233
         ft_none = pdbuddy.SinkConfig.from_text([])
247
         ft_none = pdbuddy.SinkConfig.from_text([])
234
         self.assertEqual(ft_none, self.obj_none)
248
         self.assertEqual(ft_none, self.obj_none)
290
                 b"i: 1.00 A"])
304
                 b"i: 1.00 A"])
291
         self.assertEqual(ft_valid_hv, self.obj_valid_hv)
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
     def test_from_text_invalid_index(self):
321
     def test_from_text_invalid_index(self):
294
         with self.assertRaises(IndexError):
322
         with self.assertRaises(IndexError):
295
             pdbuddy.SinkConfig.from_text([b"Invalid index"])
323
             pdbuddy.SinkConfig.from_text([b"Invalid index"])

Loading…
Cancel
Save