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.

test_sink.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, help="the Sink's tty")
  34. # Parse arguments
  35. args = parser.parse_args()
  36. # Define configurations for testing
  37. cfg_20v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  38. flags=pdbuddy.SinkFlags.NONE, v=20000, i=1000)
  39. cfg_9v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  40. flags=pdbuddy.SinkFlags.NONE, v=9000, i=1000)
  41. # Run the testing procedure
  42. with pdbuddy.Sink(args.tty) as sink:
  43. # Make sure output is disabled to start
  44. sink.output = False
  45. # Set initial configuration
  46. print('Writing 20 V configuration object…')
  47. write_verify(sink, cfg_20v)
  48. print('Done.')
  49. # Turn on the output and ensure that it's correct
  50. sink.output = True
  51. output_verify(sink)
  52. # Turn off the output and ensure that it's off
  53. sink.output = False
  54. output_verify(sink)
  55. # Turn on the output again and ensure that it's back at 20 V
  56. sink.output = True
  57. output_verify(sink)
  58. # Set second configuration
  59. print('Writing 9 V configuration object…')
  60. write_verify(sink, cfg_9v)
  61. print('Done.')
  62. # Ensure that the output is 9 V now
  63. output_verify(sink)
  64. # Erase the sink's configuration
  65. sink.erase()
  66. # Ensure there's no output despite it being enabled
  67. sink.output = True
  68. output_verify(sink)
  69. # Congratulations, all tests passed!
  70. print('Test successful!')
  71. if __name__ == '__main__':
  72. main()