Python library for working with the PD Buddy Sink Serial Console Configuration Interface
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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