Python library for working with the PD Buddy Sink Serial Console Configuration Interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. """Python bindings for PD Buddy Sink configuration"""
  2. from collections import namedtuple
  3. try:
  4. # Try importing enum from the standard library
  5. import enum
  6. # Make sure Flag is available
  7. enum.Flag
  8. except (ImportError, NameError):
  9. # If something above failed, try aenum instead
  10. import aenum as enum
  11. import serial
  12. import serial.tools.list_ports
  13. class Sink:
  14. """Interface for configuring a PD Buddy Sink"""
  15. vid = 0x1209
  16. pid = 0x9DB5
  17. def __init__(self, sp):
  18. """Open a serial port to communicate with the PD Buddy Sink
  19. :param sp: the serial port of the device
  20. :type sp: str or `serial.tools.list_ports.ListPortInfo`
  21. """
  22. try:
  23. self._port = serial.Serial(sp, baudrate=115200)
  24. except ValueError:
  25. self._port = serial.Serial(sp.device, baudrate=115200)
  26. # Put communications in a known state, cancelling any partially-entered
  27. # command that may be sitting in the buffer.
  28. self.send_command("\x04", newline=False)
  29. def __enter__(self):
  30. return self
  31. def __exit__(self, exc_type, exc_value, traceback):
  32. self._port.close()
  33. def send_command(self, cmd, newline=True):
  34. """Send a command to the PD Buddy Sink, returning the result
  35. :param cmd: the text to send to the Sink
  36. :param newline: whether to append a ``\r\n`` to the command
  37. :type cmd: str
  38. :type newline: bool
  39. :returns: a list of zero or more bytes objects, each being one line
  40. printed as a response to the command.
  41. """
  42. # Build the command
  43. cmd = cmd.encode("utf-8")
  44. if newline:
  45. cmd += b"\r\n"
  46. # Send the command
  47. self._port.write(cmd)
  48. self._port.flush()
  49. # Read the result
  50. answer = b""
  51. while not answer.endswith(b"PDBS) "):
  52. answer += self._port.read(1)
  53. answer = answer.split(b"\r\n")
  54. # Remove the echoed command and prompt
  55. answer = answer[1:-1]
  56. # Raise an exception if the command wasn't recognized
  57. if len(answer) and answer[0] == cmd.strip().split()[0] + b" ?":
  58. raise KeyError("command not found")
  59. return answer
  60. def close(self):
  61. """Close the serial port"""
  62. self._port.close()
  63. def help(self):
  64. """Returns the help text from the PD Buddy Sink"""
  65. return self.send_command("help")
  66. def license(self):
  67. """Returns the license text from the PD Buddy Sink"""
  68. return self.send_command("license")
  69. def erase(self):
  70. """Synchronously erases all stored configuration from flash"""
  71. self.send_command("erase")
  72. def write(self):
  73. """Synchronously writes the contents of the configuration buffer to flash"""
  74. self.send_command("write")
  75. def load(self):
  76. """Loads the current configuration from flash into the buffer
  77. :raises: KeyError
  78. """
  79. text = self.send_command("load")
  80. if len(text) > 0 and text[0].startswith(b"No configuration"):
  81. raise KeyError("no configuration")
  82. def get_cfg(self, index=None):
  83. """Reads configuration from flash
  84. :param index: optional index of configuration object in flash to read
  85. :returns: a `SinkConfig` object
  86. """
  87. if index is None:
  88. cfg = self.send_command("get_cfg")
  89. else:
  90. cfg = self.send_command("get_cfg {}".format(index))
  91. return SinkConfig.from_text(cfg)
  92. def get_tmpcfg(self):
  93. """Reads the contents of the configuration buffer
  94. :returns: a `SinkConfig` object
  95. """
  96. cfg = self.send_command("get_tmpcfg")
  97. return SinkConfig.from_text(cfg)
  98. def clear_flags(self):
  99. """Clears all the flags in the configuration buffer"""
  100. self.send_command("clear_flags")
  101. def toggle_giveback(self):
  102. """Toggles the GiveBack flag in the configuration buffer"""
  103. self.send_command("toggle_giveback")
  104. def set_v(self, mv):
  105. """Sets the voltage of the configuration buffer, in millivolts"""
  106. out = self.send_command("set_v {}".format(mv))
  107. # If that command gave any output, that indicates an error. Raise an
  108. # exception to make that clear.
  109. if len(out):
  110. raise ValueError(out[0])
  111. def set_i(self, ma):
  112. """Sets the current of the configuration buffer, in milliamperes"""
  113. out = self.send_command("set_i {}".format(ma))
  114. # If that command gave any output, that indicates an error. Raise an
  115. # exception to make that clear.
  116. if len(out):
  117. raise ValueError(out[0])
  118. def identify(self):
  119. """Blinks the LED quickly"""
  120. self.send_command("identify")
  121. def output(self, state=None):
  122. """Gets or sets the state of a Sink's output
  123. Raises KeyError if the ``output`` command is not available on the Sink.
  124. Raises ValueError if an invalid output is read.
  125. :param state: optional value of the output to set
  126. :returns: the output state if state is None, None otherwise
  127. """
  128. # With no parameter, return the output state
  129. if state is None:
  130. value = self.send_command("output")
  131. if value[0] == b"enabled":
  132. return True
  133. elif value[0] == b"disabled":
  134. return False
  135. else:
  136. # If unexpected text is returned, raise an exception indicating a
  137. # firmware error
  138. raise ValueError("unknown output state")
  139. # With a parameter, set the output state
  140. if state:
  141. self.send_command("output enable")
  142. else:
  143. self.send_command("output disable")
  144. def set_tmpcfg(self, sc):
  145. """Writes a SinkConfig object to the device's configuration buffer
  146. Note: the value of the status field is ignored; it will always be
  147. `SinkStatus.VALID`.
  148. """
  149. # Set flags
  150. self.clear_flags()
  151. if sc.flags & SinkFlags.GIVEBACK:
  152. self.toggle_giveback()
  153. # Set voltage
  154. self.set_v(sc.v)
  155. # Set current
  156. self.set_i(sc.i)
  157. @classmethod
  158. def get_devices(cls):
  159. """Get an iterable of PD Buddy Sink devices
  160. :returns: an iterable of `serial.tools.list_ports.ListPortInfo` objects
  161. """
  162. return serial.tools.list_ports.grep("{:04X}:{:04X}".format(cls.vid,
  163. cls.pid))
  164. class SinkConfig(namedtuple("SinkConfig", "status flags v i")):
  165. """Python representation of a PD Buddy Sink configuration object
  166. ``status`` should be a `SinkStatus` object. ``flags`` should be zero or
  167. more `SinkFlags` values. ``v`` is the voltage in millivolts, and ``i``
  168. is the current in milliamperes. `None` is also an acceptible value for
  169. any of the fields."""
  170. __slots__ = ()
  171. def __str__(self):
  172. """Print the SinkStatus in the manner of the configuration shell"""
  173. s = ""
  174. if self.status is not None:
  175. s += "status: "
  176. if self.status is SinkStatus.EMPTY:
  177. s += "empty"
  178. elif self.status is SinkStatus.VALID:
  179. s += "valid"
  180. elif self.status is SinkStatus.INVALID:
  181. s += "invalid"
  182. s += "\n"
  183. if self.flags is not None:
  184. s += "flags: "
  185. if self.flags is SinkFlags.NONE:
  186. s += "(none)"
  187. else:
  188. if self.flags & SinkFlags.GIVEBACK:
  189. s += "GiveBack"
  190. s += "\n"
  191. if self.v is not None:
  192. s += "v: {:.2f} V\n".format(self.v / 1000)
  193. if self.i is not None:
  194. s += "i: {:.2f} A\n".format(self.i / 1000)
  195. # Return all but the last character of s to remove the trailing newline
  196. if s:
  197. return s[:-1]
  198. else:
  199. return "No configuration"
  200. @classmethod
  201. def from_text(cls, text):
  202. """Creates a SinkConfig from text returned by Sink.send_command
  203. :param text: the text to load
  204. :type text: a list of bytes objects
  205. :returns: a new `SinkConfig` object.
  206. :raises: IndexError
  207. """
  208. # Assume the parameters will all be None
  209. status = None
  210. flags = None
  211. v = None
  212. i = None
  213. # Iterate over all lines of text
  214. for line in text:
  215. # If the configuration said invalid index, raise an IndexError
  216. if line.startswith(b"Invalid index"):
  217. raise IndexError("configuration index out of range")
  218. # If there is no configuration, return an empty SinkConfig
  219. elif line.startswith(b"No configuration"):
  220. return cls(None, None, None, None)
  221. # If this line is the status field
  222. elif line.startswith(b"status: "):
  223. line = line.split()[1:]
  224. if line[0] == b"empty":
  225. status = SinkStatus.EMPTY
  226. elif line[0] == b"valid":
  227. status = SinkStatus.VALID
  228. elif line[0] == b"invalid":
  229. status = SinkStatus.INVALID
  230. # If this line is the flags field
  231. elif line.startswith(b"flags: "):
  232. line = line.split()[1:]
  233. flags = SinkFlags.NONE
  234. for word in line:
  235. if word == b"(none)":
  236. # If there are no flags set, stop looking
  237. break
  238. elif word == b"GiveBack":
  239. flags |= SinkFlags.GIVEBACK
  240. # If this line is the v field
  241. elif line.startswith(b"v: "):
  242. word = line.split()[1]
  243. v = round(1000*float(word))
  244. # If this line is the i field
  245. elif line.startswith(b"i: "):
  246. word = line.split()[1]
  247. i = round(1000*float(word))
  248. # Create a new SinkConfig object with the values we just read
  249. return cls(status=status, flags=flags, v=v, i=i)
  250. class SinkStatus(enum.Enum):
  251. """Status field of a PD Buddy Sink configuration object"""
  252. EMPTY = 1
  253. VALID = 2
  254. INVALID = 3
  255. class SinkFlags(enum.Flag):
  256. """Flags field of a PD Buddy Sink configuration object"""
  257. NONE = 0
  258. GIVEBACK = enum.auto()