瀏覽代碼

Changed Sink.output to a property, fix README

Rather than having one method doing double-duty as getter and setter,
with ugly syntax, why not use a property?  Now the Sink's output can
simply be read and written as a boolean.

The README has been updated to show how to manipulate the new SinkConfig
objects, as well as what the output really looks like now.
Clara Hobbs 6 年之前
父節點
當前提交
3226e3efd7
共有 3 個文件被更改,包括 26 次插入29 次删除
  1. 6
    6
      README.rst
  2. 16
    19
      pdbuddy/__init__.py
  3. 4
    4
      test_pdbuddy/__init__.py

+ 6
- 6
README.rst 查看文件

11
 -  Configuration is represented as a SinkConfig object
11
 -  Configuration is represented as a SinkConfig object
12
 -  SinkConfig objects can be manipulated locally and written to the
12
 -  SinkConfig objects can be manipulated locally and written to the
13
    device with one method call
13
    device with one method call
14
+-  Allows control of whether or not the output is enabled
14
 
15
 
15
 Examples
16
 Examples
16
 --------
17
 --------
23
     >>> import pdbuddy
24
     >>> import pdbuddy
24
     >>> pdbs = pdbuddy.Sink(list(pdbuddy.Sink.get_devices())[0])
25
     >>> pdbs = pdbuddy.Sink(list(pdbuddy.Sink.get_devices())[0])
25
     >>> pdbs.get_cfg()
26
     >>> pdbs.get_cfg()
26
-    SinkConfig(status=SinkStatus.VALID, flags=SinkFlags.NONE, v=5000, i=3000)
27
+    SinkConfig(status=<SinkStatus.VALID: 2>, flags=<SinkFlags.NONE: 0>, v=5000, i=3000)
27
     >>> print(pdbs.get_cfg())
28
     >>> print(pdbs.get_cfg())
28
     status: valid
29
     status: valid
29
     flags: (none)
30
     flags: (none)
36
 ::
37
 ::
37
 
38
 
38
     >>> cfg = pdbs.get_cfg()
39
     >>> cfg = pdbs.get_cfg()
39
-    >>> cfg.v = 20000
40
-    >>> cfg.i = 2250
41
-    >>> cfg.flags |= pdbuddy.SinkFlags.GIVEBACK
40
+    >>> cfg = cfg._replace(v=20000, i=2250)
41
+    >>> cfg = cfg._replace(flags=cfg.flags | pdbuddy.SinkFlags.GIVEBACK)
42
     >>> cfg
42
     >>> cfg
43
-    SinkConfig(status=SinkStatus.VALID, flags=SinkFlags.GIVEBACK, v=20000, i=2250)
43
+    SinkConfig(status=<SinkStatus.VALID: 2>, flags=<SinkFlags.GIVEBACK: 1>, v=20000, i=2250)
44
 
44
 
45
 Write the SinkConfig object to flash
45
 Write the SinkConfig object to flash
46
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50
     >>> pdbs.set_tmpcfg(cfg)
50
     >>> pdbs.set_tmpcfg(cfg)
51
     >>> pdbs.write()
51
     >>> pdbs.write()
52
     >>> pdbs.get_cfg()
52
     >>> pdbs.get_cfg()
53
-    SinkConfig(status=SinkStatus.VALID, flags=SinkFlags.GIVEBACK, v=20000, i=2250)
53
+    SinkConfig(status=<SinkStatus.VALID: 2>, flags=<SinkFlags.GIVEBACK: 1>, v=20000, i=2250)
54
 
54
 
55
 Requirements
55
 Requirements
56
 ------------
56
 ------------

+ 16
- 19
pdbuddy/__init__.py 查看文件

156
         """Blinks the LED quickly"""
156
         """Blinks the LED quickly"""
157
         self.send_command("identify")
157
         self.send_command("identify")
158
 
158
 
159
-    def output(self, state=None):
160
-        """Gets or sets the state of a Sink's output
159
+    @property
160
+    def output(self):
161
+        """The state of the Sink's output
161
 
162
 
162
         Raises KeyError if the ``output`` command is not available on the Sink.
163
         Raises KeyError if the ``output`` command is not available on the Sink.
163
         Raises ValueError if an invalid output is read.
164
         Raises ValueError if an invalid output is read.
164
-
165
-        :param state: optional value of the output to set
166
-
167
-        :returns: the output state if state is None, None otherwise
168
         """
165
         """
169
-        # With no parameter, return the output state
170
-        if state is None:
171
-            value = self.send_command("output")
172
-            if value[0] == b"enabled":
173
-                return True
174
-            elif value[0] == b"disabled":
175
-                return False
176
-            else:
177
-                # If unexpected text is returned, raise an exception indicating a
178
-                # firmware error
179
-                raise ValueError("unknown output state")
166
+        value = self.send_command("output")
167
+        if value[0] == b"enabled":
168
+            return True
169
+        elif value[0] == b"disabled":
170
+            return False
171
+        else:
172
+            # If unexpected text is returned, raise an exception indicating a
173
+            # firmware error
174
+            raise ValueError("unknown output state")
180
 
175
 
181
-        # With a parameter, set the output state
176
+    @output.setter
177
+    def output(self, state):
182
         if state:
178
         if state:
183
             self.send_command("output enable")
179
             self.send_command("output enable")
184
         else:
180
         else:
218
     ``status`` should be a `SinkStatus` object.  ``flags`` should be zero or
214
     ``status`` should be a `SinkStatus` object.  ``flags`` should be zero or
219
     more `SinkFlags` values.  ``v`` is the voltage in millivolts, and ``i``
215
     more `SinkFlags` values.  ``v`` is the voltage in millivolts, and ``i``
220
     is the current in milliamperes.  `None` is also an acceptible value for
216
     is the current in milliamperes.  `None` is also an acceptible value for
221
-    any of the fields."""
217
+    any of the fields.
218
+    """
222
     __slots__ = ()
219
     __slots__ = ()
223
 
220
 
224
     def __str__(self):
221
     def __str__(self):

+ 4
- 4
test_pdbuddy/__init__.py 查看文件

103
 
103
 
104
     def test_output(self):
104
     def test_output(self):
105
         try:
105
         try:
106
-            self.pdbs.output(False)
107
-            self.assertFalse(self.pdbs.output())
106
+            self.pdbs.output = False
107
+            self.assertFalse(self.pdbs.output)
108
 
108
 
109
-            self.pdbs.output(True)
110
-            self.assertTrue(self.pdbs.output())
109
+            self.pdbs.output = True
110
+            self.assertTrue(self.pdbs.output)
111
         except KeyError:
111
         except KeyError:
112
             self.skipTest("Command output not supported")
112
             self.skipTest("Command output not supported")
113
         except ValueError:
113
         except ValueError:

Loading…
取消
儲存