def test_open_close(): print("Starting open/close test...") # Open non-existent PWM channel with AssertRaises(ValueError): periphery.PWM(9999, pwm_pin) # Open non-existent PWM pin with AssertRaises(periphery.PWMError): periphery.PWM(pwm_channel, 9999) # Open legitimate PWM channel/pin pwm = periphery.PWM(pwm_channel, pwm_pin) assert pwm.channel == pwm_channel assert pwm.pin == pwm_pin # Set period, check period and frequency pwm.period = 1e-3 assert (pwm.period - 1e-3) < 1e-4 assert (pwm.frequency - 1000) < 100 pwm.period = 5e-4 assert (pwm.period - 5e-4) < 1e-5 assert (pwm.frequency - 2000) < 100 # Set frequency, check frequency and period pwm.frequency = 1000 assert (pwm.frequency - 1000) < 100 assert (pwm.period - 1e-3) < 1e-4 pwm.frequency = 2000 assert (pwm.frequency - 2000) < 100 assert (pwm.period - 5e-4) < 1e-5 # Set duty cycle, check duty cycle pwm.duty_cycle = 0.25 assert (pwm.duty_cycle - 0.25) < 1e-3 pwm.duty_cycle = 0.50 assert (pwm.duty_cycle - 0.50) < 1e-3 pwm.duty_cycle = 0.75 assert (pwm.duty_cycle - 0.75) < 1e-3 # Set polarity, check polarity pwm.polarity = "normal" assert pwm.polarity == "normal" pwm.polarity = "inversed" assert pwm.polarity == "inversed" # Set enabled, check enabled pwm.enabled = True assert pwm.enabled == True pwm.enabled = False assert pwm.enabled == False # Use enable()/disable(), check enabled pwm.enable() assert pwm.enabled == True pwm.disable() assert pwm.enabled == False # Set invalid polarity with AssertRaises(ValueError): pwm.polarity = "foo" pwm.close() print("Open/close test passed.")
def test_arguments(): ptest() # Invalid open types with AssertRaises("invalid open types", TypeError): periphery.PWM("foo", 0) with AssertRaises("invalid open types", TypeError): periphery.PWM(0, "foo")
def test_arguments(): print("Starting arguments test...") # Invalid open types with AssertRaises(TypeError): periphery.PWM("foo", 0) with AssertRaises(TypeError): periphery.PWM(0, "foo") print("Arguments test passed.")
def test_interactive(): ptest() pwm = periphery.PWM(pwm_chip, pwm_channel) print("Starting interactive test. Get out your oscilloscope, buddy!") raw_input("Press enter to continue...") # Set initial parameters and enable PWM pwm.duty_cycle = 0.0 pwm.frequency = 1e3 pwm.polarity = "normal" pwm.enabled = True # Check tostring print("PWM description: {}".format(str(pwm))) passert("interactive success", raw_input("PWM description looks ok? y/n ") == "y") # Set 1 kHz frequency, 0.25 duty cycle pwm.frequency = 1e3 pwm.duty_cycle = 0.25 passert("interactive success", raw_input("Frequency is 1 kHz, duty cycle is 25%? y/n ") == "y") # Set 1 kHz frequency, 0.50 duty cycle pwm.frequency = 1e3 pwm.duty_cycle = 0.50 passert("interactive success", raw_input("Frequency is 1 kHz, duty cycle is 50%? y/n ") == "y") # Set 2 kHz frequency, 0.25 duty cycle pwm.frequency = 2e3 pwm.duty_cycle = 0.25 passert("interactive success", raw_input("Frequency is 2 kHz, duty cycle is 25%? y/n ") == "y") # Set 2 kHz frequency, 0.50 duty cycle pwm.frequency = 2e3 pwm.duty_cycle = 0.50 passert("interactive success", raw_input("Frequency is 2 kHz, duty cycle is 50%? y/n ") == "y") pwm.duty_cycle = 0.0 pwm.enabled = False pwm.close()
def test_interactive(): print("Starting interactive test...") pwm = periphery.PWM(pwm_channel, pwm_pin) print("Starting interactive test. Get out your oscilloscope, buddy!") raw_input("Press enter to continue...") # Set initial parameters and enable PWM pwm.duty_cycle = 0.0 pwm.frequency = 1e3 pwm.polarity = "normal" pwm.enabled = True # Set 1 kHz frequency, 0.25 duty cycle pwm.frequency = 1e3 pwm.duty_cycle = 0.25 assert raw_input("Frequency is 1 kHz, duty cycle is 25%? y/n ") == "y" # Set 1 kHz frequency, 0.50 duty cycle pwm.frequency = 1e3 pwm.duty_cycle = 0.50 assert raw_input("Frequency is 1 kHz, duty cycle is 50%? y/n ") == "y" # Set 2 kHz frequency, 0.25 duty cycle pwm.frequency = 2e3 pwm.duty_cycle = 0.25 assert raw_input("Frequency is 2 kHz, duty cycle is 25%? y/n ") == "y" # Set 2 kHz frequency, 0.50 duty cycle pwm.frequency = 2e3 pwm.duty_cycle = 0.50 assert raw_input("Frequency is 2 kHz, duty cycle is 50%? y/n ") == "y" pwm.duty_cycle = 0.0 pwm.enabled = False pwm.close() print("Interactive test passed.")
def test_open_close(): print("Starting open/close test...") # Open non-existent PWM chip with AssertRaises(LookupError): periphery.PWM(9999, pwm_channel) # Open non-existent PWM channel with AssertRaises(periphery.PWMError): periphery.PWM(pwm_chip, 9999) # Open legitimate PWM chip/channel pwm = periphery.PWM(pwm_chip, pwm_channel) assert pwm.chip == pwm_chip assert pwm.channel == pwm_channel # Initialize duty cycle to 0 pwm.duty_cycle = 0 # Set period, check period, check period_ns, check frequency pwm.period = 1e-3 assert abs(pwm.period - 1e-3) < 1e-4 assert abs(pwm.period_ns - 1000000) < 1e5 assert abs(pwm.frequency - 1000) < 100 pwm.period = 5e-4 assert abs(pwm.period - 5e-4) < 1e-5 assert abs(pwm.period_ns - 500000) < 1e4 assert abs(pwm.frequency - 2000) < 100 # Set frequency, check frequency, check period, check period_ns pwm.frequency = 1000 assert abs(pwm.frequency - 1000) < 100 assert abs(pwm.period - 1e-3) < 1e-4 assert abs(pwm.period_ns - 1000000) < 1e5 pwm.frequency = 2000 assert abs(pwm.frequency - 2000) < 100 assert abs(pwm.period - 5e-4) < 1e-5 assert abs(pwm.period_ns - 500000) < 1e4 # Set period_ns, check period_ns, check period, check frequency pwm.period_ns = 1000000 assert abs(pwm.period_ns - 1000000) < 1e5 assert abs(pwm.period - 1e-3) < 1e-4 assert abs(pwm.frequency - 1000) < 100 pwm.period_ns = 500000 assert abs(pwm.period_ns - 500000) < 1e4 assert abs(pwm.period - 5e-4) < 1e-5 assert abs(pwm.frequency - 2000) < 100 pwm.period_ns = 1000000 # Set duty cycle, check duty cycle, check duty_cycle_ns pwm.duty_cycle = 0.25 assert abs(pwm.duty_cycle - 0.25) < 1e-3 assert abs(pwm.duty_cycle_ns - 250000) < 1e4 pwm.duty_cycle = 0.50 assert abs(pwm.duty_cycle - 0.50) < 1e-3 assert abs(pwm.duty_cycle_ns - 500000) < 1e4 pwm.duty_cycle = 0.75 assert abs(pwm.duty_cycle - 0.75) < 1e-3 assert abs(pwm.duty_cycle_ns - 750000) < 1e4 # Set duty_cycle_ns, check duty_cycle_ns, check duty_cycle pwm.duty_cycle_ns = 250000 assert abs(pwm.duty_cycle_ns - 250000) < 1e4 assert abs(pwm.duty_cycle - 0.25) < 1e-3 pwm.duty_cycle_ns = 500000 assert abs(pwm.duty_cycle_ns - 500000) < 1e4 assert abs(pwm.duty_cycle - 0.50) < 1e-3 pwm.duty_cycle_ns = 750000 assert abs(pwm.duty_cycle_ns - 750000) < 1e4 assert abs(pwm.duty_cycle - 0.75) < 1e-3 # Set polarity, check polarity pwm.polarity = "normal" assert pwm.polarity == "normal" pwm.polarity = "inversed" assert pwm.polarity == "inversed" # Set enabled, check enabled pwm.enabled = True assert pwm.enabled == True pwm.enabled = False assert pwm.enabled == False # Use enable()/disable(), check enabled pwm.enable() assert pwm.enabled == True pwm.disable() assert pwm.enabled == False # Set invalid polarity with AssertRaises(ValueError): pwm.polarity = "foo" pwm.close() print("Open/close test passed.")
def test_open_close(): ptest() # Open non-existent PWM chip with AssertRaises("non-existent PWM chip", LookupError): periphery.PWM(9999, pwm_channel) # Open non-existent PWM channel with AssertRaises("non-existent PWM channel", periphery.PWMError): periphery.PWM(pwm_chip, 9999) # Open legitimate PWM chip/channel pwm = periphery.PWM(pwm_chip, pwm_channel) passert("property chip", pwm.chip == pwm_chip) passert("property channel", pwm.channel == pwm_channel) # Initialize period and duty cycle pwm.period = 5e-3 pwm.duty_cycle = 0 # Set period, check period, check period_ns, check frequency pwm.period = 1e-3 passert("period is correct", abs(pwm.period - 1e-3) < 1e-4) passert("period_ns is correct", abs(pwm.period_ns - 1000000) < 1e5) passert("frequency is correct", abs(pwm.frequency - 1000) < 100) pwm.period = 5e-4 passert("period is correct", abs(pwm.period - 5e-4) < 1e-5) passert("period_ns is correct", abs(pwm.period_ns - 500000) < 1e4) passert("frequency is correct", abs(pwm.frequency - 2000) < 100) # Set frequency, check frequency, check period, check period_ns pwm.frequency = 1000 passert("frequency is correct", abs(pwm.frequency - 1000) < 100) passert("period is correct", abs(pwm.period - 1e-3) < 1e-4) passert("period_ns is correct", abs(pwm.period_ns - 1000000) < 1e5) pwm.frequency = 2000 passert("frequency is correct", abs(pwm.frequency - 2000) < 100) passert("period is correct", abs(pwm.period - 5e-4) < 1e-5) passert("period_ns is correct", abs(pwm.period_ns - 500000) < 1e4) # Set period_ns, check period_ns, check period, check frequency pwm.period_ns = 1000000 passert("period_ns is correct", abs(pwm.period_ns - 1000000) < 1e5) passert("period is correct", abs(pwm.period - 1e-3) < 1e-4) passert("frequency is correct", abs(pwm.frequency - 1000) < 100) pwm.period_ns = 500000 passert("period_ns is correct", abs(pwm.period_ns - 500000) < 1e4) passert("period is correct", abs(pwm.period - 5e-4) < 1e-5) passert("frequency is correct", abs(pwm.frequency - 2000) < 100) pwm.period_ns = 1000000 # Set duty cycle, check duty cycle, check duty_cycle_ns pwm.duty_cycle = 0.25 passert("duty_cycle is correct", abs(pwm.duty_cycle - 0.25) < 1e-3) passert("duty_cycle_ns is correct", abs(pwm.duty_cycle_ns - 250000) < 1e4) pwm.duty_cycle = 0.50 passert("duty_cycle is correct", abs(pwm.duty_cycle - 0.50) < 1e-3) passert("duty_cycle_ns is correct", abs(pwm.duty_cycle_ns - 500000) < 1e4) pwm.duty_cycle = 0.75 passert("duty_cycle is correct", abs(pwm.duty_cycle - 0.75) < 1e-3) passert("duty_cycle_ns is correct", abs(pwm.duty_cycle_ns - 750000) < 1e4) # Set duty_cycle_ns, check duty_cycle_ns, check duty_cycle pwm.duty_cycle_ns = 250000 passert("duty_cycle_ns is correct", abs(pwm.duty_cycle_ns - 250000) < 1e4) passert("duty_cycle is correct", abs(pwm.duty_cycle - 0.25) < 1e-3) pwm.duty_cycle_ns = 500000 passert("duty_cycle_ns is correct", abs(pwm.duty_cycle_ns - 500000) < 1e4) passert("duty_cycle is correct", abs(pwm.duty_cycle - 0.50) < 1e-3) pwm.duty_cycle_ns = 750000 passert("duty_cycle_ns is correct", abs(pwm.duty_cycle_ns - 750000) < 1e4) passert("duty_cycle is correct", abs(pwm.duty_cycle - 0.75) < 1e-3) # Set polarity, check polarity pwm.polarity = "normal" passert("polarity is normal", pwm.polarity == "normal") pwm.polarity = "inversed" passert("polarity is inversed", pwm.polarity == "inversed") # Set enabled, check enabled pwm.enabled = True passert("pwm is enabled", pwm.enabled == True) pwm.enabled = False passert("pwm is disabled", pwm.enabled == False) # Use enable()/disable(), check enabled pwm.enable() passert("pwm is enabled", pwm.enabled == True) pwm.disable() passert("pwm is disabled", pwm.enabled == False) # Set invalid polarity with AssertRaises("set invalid polarity", ValueError): pwm.polarity = "foo" pwm.close()