Python library for working with the PD Buddy Sink Serial Console Configuration Interface
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test_sink.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """Test a freshly-flashed PD Buddy Sink"""
  2. import argparse
  3. import sys
  4. import pdbuddy
  5. class ExitCode:
  6. configuration_error = 1
  7. wrong_output = 2
  8. def write_verify(sink, cfg):
  9. """Write a SinkConfig to a Sink and verify that it was written"""
  10. sink.set_tmpcfg(cfg)
  11. sink.write()
  12. actual_cfg = sink.get_cfg()
  13. if actual_cfg != cfg:
  14. print('Configuration error; expected config:')
  15. print(cfg)
  16. print('actual config:')
  17. print(actual_cfg)
  18. sys.exit(ExitCode.configuration_error)
  19. def output_verify(sink):
  20. """Verify that the Sink's output is correct"""
  21. # Get the configured voltage
  22. voltage = sink.get_cfg().v
  23. if voltage is None:
  24. voltage = 0
  25. answer = input("Is the Sink's output stable at {:.1f} V [Y,n]? ".format(
  26. voltage/1000. * sink.output))
  27. if answer.lower().startswith('n'):
  28. print('Wrong output, exiting.')
  29. sys.exit(ExitCode.wrong_output)
  30. def main():
  31. # Make the argument parser
  32. parser = argparse.ArgumentParser(description='Test a PD Buddy Sink')
  33. parser.add_argument('tty', metavar='tty', type=str, nargs='?',
  34. help="the Sink's tty")
  35. parser.add_argument('-l', '--list-devices', action='store_true',
  36. help='list connected PD Buddy Sinks and exit')
  37. # Parse arguments
  38. args = parser.parse_args()
  39. # If asked to, list devices and exit
  40. if args.list_devices:
  41. for sinkinfo in pdbuddy.Sink.get_devices():
  42. print(sinkinfo.device, '-', sinkinfo.manufacturer,
  43. sinkinfo.product, sinkinfo.serial_number)
  44. sys.exit(0)
  45. # If there's no device file specified, try to auto-detect
  46. if not args.tty:
  47. devices = list(pdbuddy.Sink.get_devices())
  48. # If the wrong number of devices is present, complain and exit.
  49. if len(devices) < 1:
  50. print('No PD Buddy Sink devices found')
  51. sys.exit(1)
  52. elif len(devices) > 1:
  53. print('Multiple PD Buddy Sink devices found')
  54. sys.exit(1)
  55. # Set args.tty to the auto-detected Sink
  56. args.tty = devices[0].device
  57. # Define configurations for testing
  58. cfg_20v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  59. flags=pdbuddy.SinkFlags.NONE, v=20000, vmin=None, vmax=None,
  60. i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  61. cfg_9v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  62. flags=pdbuddy.SinkFlags.NONE, v=9000, vmin=None, vmax=None, i=1000,
  63. idim=pdbuddy.SinkDimension.CURRENT)
  64. # Run the testing procedure
  65. with pdbuddy.Sink(args.tty) as sink:
  66. # Make sure output is disabled to start
  67. sink.output = False
  68. # Set initial configuration
  69. print('Writing 20 V configuration object…')
  70. write_verify(sink, cfg_20v)
  71. print('Done.')
  72. # Turn on the output and ensure that it's correct
  73. sink.output = True
  74. output_verify(sink)
  75. # Turn off the output and ensure that it's off
  76. sink.output = False
  77. output_verify(sink)
  78. # Turn on the output again and ensure that it's back at 20 V
  79. sink.output = True
  80. output_verify(sink)
  81. # Set second configuration
  82. print('Writing 9 V configuration object…')
  83. write_verify(sink, cfg_9v)
  84. print('Done.')
  85. # Ensure that the output is 9 V now
  86. output_verify(sink)
  87. # Erase the sink's configuration
  88. sink.erase()
  89. # Ensure there's no output despite it being enabled
  90. sink.output = True
  91. output_verify(sink)
  92. # Congratulations, all tests passed!
  93. print('Test successful!')
  94. if __name__ == '__main__':
  95. main()