Python library for working with the PD Buddy Sink Serial Console Configuration Interface
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

test_sink.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, complain and exit
  46. if not args.tty:
  47. parser.error('the following arguments are required: tty')
  48. # Define configurations for testing
  49. cfg_20v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  50. flags=pdbuddy.SinkFlags.NONE, v=20000, vmin=None, vmax=None,
  51. i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  52. cfg_9v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  53. flags=pdbuddy.SinkFlags.NONE, v=9000, vmin=None, vmax=None, i=1000,
  54. idim=pdbuddy.SinkDimension.CURRENT)
  55. # Run the testing procedure
  56. with pdbuddy.Sink(args.tty) as sink:
  57. # Make sure output is disabled to start
  58. sink.output = False
  59. # Set initial configuration
  60. print('Writing 20 V configuration object…')
  61. write_verify(sink, cfg_20v)
  62. print('Done.')
  63. # Turn on the output and ensure that it's correct
  64. sink.output = True
  65. output_verify(sink)
  66. # Turn off the output and ensure that it's off
  67. sink.output = False
  68. output_verify(sink)
  69. # Turn on the output again and ensure that it's back at 20 V
  70. sink.output = True
  71. output_verify(sink)
  72. # Set second configuration
  73. print('Writing 9 V configuration object…')
  74. write_verify(sink, cfg_9v)
  75. print('Done.')
  76. # Ensure that the output is 9 V now
  77. output_verify(sink)
  78. # Erase the sink's configuration
  79. sink.erase()
  80. # Ensure there's no output despite it being enabled
  81. sink.output = True
  82. output_verify(sink)
  83. # Congratulations, all tests passed!
  84. print('Test successful!')
  85. if __name__ == '__main__':
  86. main()