Browse Source

Added voltage ranges to the SinkConfig object

Also, the related HV_Preferred flag.  Test cases too.
Clara Hobbs 6 years ago
parent
commit
a21f6fd4d8
2 changed files with 101 additions and 33 deletions
  1. 28
    7
      pdbuddy/__init__.py
  2. 73
    26
      test_pdbuddy/__init__.py

+ 28
- 7
pdbuddy/__init__.py View File

@@ -217,13 +217,13 @@ class Sink:
217 217
             cls.pid))
218 218
 
219 219
 
220
-class SinkConfig(namedtuple("SinkConfig", "status flags v i")):
220
+class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i")):
221 221
     """Python representation of a PD Buddy Sink configuration object
222 222
 
223 223
     ``status`` should be a `SinkStatus` object.  ``flags`` should be zero or
224
-    more `SinkFlags` values.  ``v`` is the voltage in millivolts, and ``i``
225
-    is the current in milliamperes.  `None` is also an acceptible value for
226
-    any of the fields.
224
+    more `SinkFlags` values.  ``v``, ``vmin``, and ``vmax`` are voltages in
225
+    millivolts, and ``i`` is the current in milliamperes.  `None` is also an
226
+    acceptible value for any of the fields.
227 227
     """
228 228
     __slots__ = ()
229 229
 
@@ -248,10 +248,18 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v i")):
248 248
             else:
249 249
                 if self.flags & SinkFlags.GIVEBACK:
250 250
                     s += "GiveBack"
251
+                if self.flags & SinkFlags.HV_PREFERRED:
252
+                    s += "HV_Preferred"
251 253
             s += "\n"
252 254
 
253 255
         if self.v is not None:
254
-            s += "v: {:.2f} V\n".format(self.v / 1000.0)
256
+            s += "v: {:.3f} V\n".format(self.v / 1000.0)
257
+
258
+        if self.vmin is not None:
259
+            s += "vmin: {:.3f} V\n".format(self.vmin / 1000.0)
260
+
261
+        if self.vmax is not None:
262
+            s += "vmax: {:.3f} V\n".format(self.vmax / 1000.0)
255 263
 
256 264
         if self.i is not None:
257 265
             s += "i: {:.2f} A\n".format(self.i / 1000.0)
@@ -276,6 +284,8 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v i")):
276 284
         status = None
277 285
         flags = None
278 286
         v = None
287
+        vmin = None
288
+        vmax = None
279 289
         i = None
280 290
 
281 291
         # Iterate over all lines of text
@@ -285,7 +295,7 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v i")):
285 295
                 raise IndexError("configuration index out of range")
286 296
             # If there is no configuration, return an empty SinkConfig
287 297
             elif line.startswith(b"No configuration"):
288
-                return cls(None, None, None, None)
298
+                return cls(None, None, None, None, None, None)
289 299
             # If this line is the status field
290 300
             elif line.startswith(b"status: "):
291 301
                 line = line.split()[1:]
@@ -305,17 +315,27 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v i")):
305 315
                         break
306 316
                     elif word == b"GiveBack":
307 317
                         flags |= SinkFlags.GIVEBACK
318
+                    elif word == b"HV_Preferred":
319
+                        flags |= SinkFlags.HV_PREFERRED
308 320
             # If this line is the v field
309 321
             elif line.startswith(b"v: "):
310 322
                 word = line.split()[1]
311 323
                 v = round(1000*float(word))
324
+            # If this line is the vmin field
325
+            elif line.startswith(b"vmin: "):
326
+                word = line.split()[1]
327
+                vmin = round(1000*float(word))
328
+            # If this line is the vmax field
329
+            elif line.startswith(b"vmax: "):
330
+                word = line.split()[1]
331
+                vmax = round(1000*float(word))
312 332
             # If this line is the i field
313 333
             elif line.startswith(b"i: "):
314 334
                 word = line.split()[1]
315 335
                 i = round(1000*float(word))
316 336
 
317 337
         # Create a new SinkConfig object with the values we just read
318
-        return cls(status=status, flags=flags, v=v, i=i)
338
+        return cls(status=status, flags=flags, v=v, vmin=vmin, vmax=vmax, i=i)
319 339
 
320 340
 
321 341
 class SinkStatus(enum.Enum):
@@ -329,6 +349,7 @@ class SinkFlags(enum.Flag):
329 349
     """Flags field of a PD Buddy Sink configuration object"""
330 350
     NONE = 0
331 351
     GIVEBACK = enum.auto()
352
+    HV_PREFERRED = enum.auto()
332 353
 
333 354
 
334 355
 class UnknownPDO(namedtuple("UnknownPDO", "value")):

+ 73
- 26
test_pdbuddy/__init__.py View File

@@ -17,23 +17,31 @@ class SinkTestCase(unittest.TestCase):
17 17
         self.pdbs = pdbuddy.Sink(pdbs_devices[0])
18 18
 
19 19
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
20
-                flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
20
+                flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
21
+                i=3000)
21 22
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
22
-                flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, i=3000)
23
+                flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
24
+                vmax=None, i=3000)
23 25
 
24 26
         self.obj_huge_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
25
-                flags=pdbuddy.SinkFlags.NONE, v=65536, i=1000)
27
+                flags=pdbuddy.SinkFlags.NONE, v=65536, vmin=None, vmax=None,
28
+                i=1000)
26 29
         self.obj_big_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
27
-                flags=pdbuddy.SinkFlags.NONE, v=21001, i=1000)
30
+                flags=pdbuddy.SinkFlags.NONE, v=21001, vmin=None, vmax=None,
31
+                i=1000)
28 32
         self.obj_neg_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
29
-                flags=pdbuddy.SinkFlags.NONE, v=-1, i=1000)
33
+                flags=pdbuddy.SinkFlags.NONE, v=-1, vmin=None, vmax=None,
34
+                i=1000)
30 35
 
31 36
         self.obj_huge_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
32
-                flags=pdbuddy.SinkFlags.NONE, v=5000, i=65536)
37
+                flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
38
+                i=65536)
33 39
         self.obj_big_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
34
-                flags=pdbuddy.SinkFlags.NONE, v=5000, i=5001)
40
+                flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
41
+                i=5001)
35 42
         self.obj_neg_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
36
-                flags=pdbuddy.SinkFlags.NONE, v=5000, i=-1)
43
+                flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
44
+                i=-1)
37 45
 
38 46
     def tearDown(self):
39 47
         # Close the connection to the PD Buddy Sink
@@ -145,19 +153,30 @@ class SinkConfigTestCase(unittest.TestCase):
145 153
 
146 154
     def setUp(self):
147 155
         self.obj_none = pdbuddy.SinkConfig(status=None, flags=None, v=None,
148
-                i=None)
156
+                vmin=None, vmax=None, i=None)
149 157
         self.obj_empty = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.EMPTY,
150
-                flags=None, v=None, i=None)
158
+                flags=None, v=None, vmin=None, vmax=None, i=None)
151 159
         self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
152
-                flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
160
+                flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
161
+                i=3000)
153 162
         self.obj_invalid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.INVALID,
154
-                flags=pdbuddy.SinkFlags.NONE, v=15000, i=3000)
163
+                flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
164
+                i=3000)
155 165
         self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
156
-                flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, i=3000)
166
+                flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
167
+                vmax=None, i=3000)
157 168
         self.obj_valid_5v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
158
-                flags=pdbuddy.SinkFlags.NONE, v=5000, i=3000)
169
+                flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
170
+                i=3000)
159 171
         self.obj_valid_1a = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
160
-                flags=pdbuddy.SinkFlags.NONE, v=15000, i=1000)
172
+                flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
173
+                i=1000)
174
+        self.obj_valid_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
175
+                flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=12000, vmax=16000,
176
+                i=1000)
177
+        self.obj_valid_hv = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
178
+                flags=pdbuddy.SinkFlags.HV_PREFERRED, v=15000, vmin=12000,
179
+                vmax=16000, i=1000)
161 180
 
162 181
     def test_str_none(self):
163 182
         self.assertEqual(str(self.obj_none), "No configuration")
@@ -167,23 +186,33 @@ class SinkConfigTestCase(unittest.TestCase):
167 186
 
168 187
     def test_str_valid(self):
169 188
         self.assertEqual(str(self.obj_valid),
170
-                "status: valid\nflags: (none)\nv: 15.00 V\ni: 3.00 A")
189
+                "status: valid\nflags: (none)\nv: 15.000 V\ni: 3.00 A")
171 190
 
172 191
     def test_str_invalid(self):
173 192
         self.assertEqual(str(self.obj_invalid),
174
-                "status: invalid\nflags: (none)\nv: 15.00 V\ni: 3.00 A")
193
+                "status: invalid\nflags: (none)\nv: 15.000 V\ni: 3.00 A")
175 194
 
176 195
     def test_str_valid_gb(self):
177 196
         self.assertEqual(str(self.obj_valid_gb),
178
-                "status: valid\nflags: GiveBack\nv: 15.00 V\ni: 3.00 A")
197
+                "status: valid\nflags: GiveBack\nv: 15.000 V\ni: 3.00 A")
179 198
 
180 199
     def test_str_valid_5v(self):
181 200
         self.assertEqual(str(self.obj_valid_5v),
182
-                "status: valid\nflags: (none)\nv: 5.00 V\ni: 3.00 A")
201
+                "status: valid\nflags: (none)\nv: 5.000 V\ni: 3.00 A")
183 202
 
184 203
     def test_str_valid_1a(self):
185 204
         self.assertEqual(str(self.obj_valid_1a),
186
-                "status: valid\nflags: (none)\nv: 15.00 V\ni: 1.00 A")
205
+                "status: valid\nflags: (none)\nv: 15.000 V\ni: 1.00 A")
206
+
207
+    def test_str_valid_range(self):
208
+        self.assertEqual(str(self.obj_valid_range),
209
+                "status: valid\nflags: (none)\nv: 15.000 V\nvmin: 12.000 V\n"
210
+                "vmax: 16.000 V\ni: 1.00 A")
211
+
212
+    def test_str_valid_hv(self):
213
+        self.assertEqual(str(self.obj_valid_hv),
214
+                "status: valid\nflags: HV_Preferred\nv: 15.000 V\n"
215
+                "vmin: 12.000 V\nvmax: 16.000 V\ni: 1.00 A")
187 216
 
188 217
     def test_from_text_none(self):
189 218
         ft_none = pdbuddy.SinkConfig.from_text([])
@@ -196,38 +225,56 @@ class SinkConfigTestCase(unittest.TestCase):
196 225
     def test_from_text_valid(self):
197 226
         ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
198 227
                 b"flags: (none)",
199
-                b"v: 15.00 V",
228
+                b"v: 15.000 V",
200 229
                 b"i: 3.00 A"])
201 230
         self.assertEqual(ft_valid, self.obj_valid)
202 231
 
203 232
     def test_from_text_invalid(self):
204 233
         ft_invalid = pdbuddy.SinkConfig.from_text([b"status: invalid",
205 234
                 b"flags: (none)",
206
-                b"v: 15.00 V",
235
+                b"v: 15.000 V",
207 236
                 b"i: 3.00 A"])
208 237
         self.assertEqual(ft_invalid, self.obj_invalid)
209 238
 
210 239
     def test_from_text_valid_gb(self):
211 240
         ft_valid_gb = pdbuddy.SinkConfig.from_text([b"status: valid",
212 241
                 b"flags: GiveBack",
213
-                b"v: 15.00 V",
242
+                b"v: 15.000 V",
214 243
                 b"i: 3.00 A"])
215 244
         self.assertEqual(ft_valid_gb, self.obj_valid_gb)
216 245
 
217 246
     def test_from_text_valid_5v(self):
218 247
         ft_valid_5v = pdbuddy.SinkConfig.from_text([b"status: valid",
219 248
                 b"flags: (none)",
220
-                b"v: 5.00 V",
249
+                b"v: 5.000 V",
221 250
                 b"i: 3.00 A"])
222 251
         self.assertEqual(ft_valid_5v, self.obj_valid_5v)
223 252
 
224 253
     def test_from_text_valid_1a(self):
225 254
         ft_valid_1a = pdbuddy.SinkConfig.from_text([b"status: valid",
226 255
                 b"flags: (none)",
227
-                b"v: 15.00 V",
256
+                b"v: 15.000 V",
228 257
                 b"i: 1.00 A"])
229 258
         self.assertEqual(ft_valid_1a, self.obj_valid_1a)
230 259
 
260
+    def test_from_text_valid_range(self):
261
+        ft_valid_range = pdbuddy.SinkConfig.from_text([b"status: valid",
262
+                b"flags: (none)",
263
+                b"v: 15.000 V",
264
+                b"vmin: 12.000 V",
265
+                b"vmax: 16.000 V",
266
+                b"i: 1.00 A"])
267
+        self.assertEqual(ft_valid_range, self.obj_valid_range)
268
+
269
+    def test_from_text_valid_hv(self):
270
+        ft_valid_hv = pdbuddy.SinkConfig.from_text([b"status: valid",
271
+                b"flags: HV_Preferred",
272
+                b"v: 15.000 V",
273
+                b"vmin: 12.000 V",
274
+                b"vmax: 16.000 V",
275
+                b"i: 1.00 A"])
276
+        self.assertEqual(ft_valid_hv, self.obj_valid_hv)
277
+
231 278
     def test_from_text_invalid_index(self):
232 279
         with self.assertRaises(IndexError):
233 280
             pdbuddy.SinkConfig.from_text([b"Invalid index"])
@@ -240,7 +287,7 @@ class SinkConfigTestCase(unittest.TestCase):
240 287
         ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
241 288
                 b"flags: (none)",
242 289
                 b"This is an extra line, which shouldn't hurt anything.",
243
-                b"v: 15.00 V",
290
+                b"v: 15.000 V",
244 291
                 b"i: 3.00 A"])
245 292
         self.assertEqual(ft_valid, self.obj_valid)
246 293
 

Loading…
Cancel
Save