forked from vsergeev/python-periphery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pwm.py
More file actions
147 lines (114 loc) · 3.9 KB
/
test_pwm.py
File metadata and controls
147 lines (114 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sys
import periphery
from .asserts import AssertRaises
if sys.version_info[0] == 3:
raw_input = input
pwm_channel = None
pwm_pin = None
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_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_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.")
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python -m tests.test_pwm <PMW channel> <PWM pin number>")
print("")
print("Hint: for BeagleBone Black, enable PWM2 (A=P8.19, B=8.13) with")
print(" echo BB-PWM2 > /sys/devices/platform/bone_capemgr/slots")
print("monitor P8.19, and run this test:")
print(" python -m tests.test_pwm 0 0")
sys.exit(1)
pwm_channel = int(sys.argv[1])
pwm_pin = int(sys.argv[2])
print("Starting PMW tests...")
test_arguments()
test_open_close()
test_interactive()
print("All PWM tests passed.")