Skip to content

Commit cd2456b

Browse files
author
Matthew West
committed
Fix PWM tests to reflect 4.1+
1 parent b6cf76f commit cd2456b

File tree

3 files changed

+147
-94
lines changed

3 files changed

+147
-94
lines changed

source/c_adc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ BBIO_err initialize_adc(void)
4646
char test_path[40];
4747
#endif
4848
FILE *fh;
49+
BBIO_err err;
50+
4951
if (adc_initialized) {
5052
return BBIO_OK;
5153
}
5254

5355
#ifdef BBBVERSION41
54-
if (load_device_tree("BB-ADC")) {
56+
err = load_device_tree("BB-ADC");
57+
if (err == BBIO_OK) {
5558
strncat(adc_prefix_dir, "/sys/bus/iio/devices/iio:device0/in_voltage", sizeof(adc_prefix_dir));
5659
snprintf(test_path, sizeof(test_path), "%s%d_raw", adc_prefix_dir, 1);
5760
fh = fopen(test_path, "r");

source/py_pwm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
4141
char key[8];
4242
char *channel;
4343
float frequency = 2000.0;
44-
float duty_cycle = 50.0;
44+
float duty_cycle = 0.0;
4545
int polarity = 0;
4646
BBIO_err err;
4747
static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL};

test/test_pwm_setup.py

Lines changed: 142 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,200 +1,250 @@
11
import pytest
22
import os
3+
import platform
34

45
import Adafruit_BBIO.PWM as PWM
56

7+
kernel = platform.release()
8+
9+
610
def teardown_module(module):
711
PWM.cleanup()
812

9-
class TestPwmSetup:
13+
14+
class TestPwmSetup:
1015
def test_start_pwm(self):
1116
PWM.start("P9_14", 0)
1217

13-
files = os.listdir('/sys/devices')
14-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
15-
files = os.listdir(ocp)
16-
pwm_test = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
17-
18-
assert os.path.exists(pwm_test)
19-
duty = open(pwm_test + '/duty').read()
20-
period = open(pwm_test + '/period').read()
18+
if kernel >= '4.1.0':
19+
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
20+
else:
21+
files = os.listdir('/sys/devices')
22+
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
23+
files = os.listdir(ocp)
24+
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
25+
26+
assert os.path.exists(pwm_dir)
27+
if kernel >= '4.1.0':
28+
duty = open(pwm_dir + '/duty_cycle').read()
29+
else:
30+
duty = open(pwm_dir + '/duty').read()
31+
period = open(pwm_dir + '/period').read()
2132
assert int(duty) == 0
2233
assert int(period) == 500000
2334
PWM.cleanup()
2435

2536
def test_start_pwm_with_polarity_one(self):
2637
PWM.start("P9_14", 0, 2000, 1)
2738

28-
files = os.listdir('/sys/devices')
29-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
30-
files = os.listdir(ocp)
31-
pwm_test = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
32-
33-
assert os.path.exists(pwm_test)
34-
duty = open(pwm_test + '/duty').read()
35-
period = open(pwm_test + '/period').read()
36-
polarity = open(pwm_test + '/polarity').read()
39+
if kernel >= '4.1.0':
40+
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
41+
else:
42+
files = os.listdir('/sys/devices')
43+
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
44+
files = os.listdir(ocp)
45+
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
46+
47+
assert os.path.exists(pwm_dir)
48+
if kernel >= '4.1.0':
49+
duty = open(pwm_dir + '/duty_cycle').read()
50+
else:
51+
duty = open(pwm_dir + '/duty').read()
52+
period = open(pwm_dir + '/period').read()
53+
polarity = open(pwm_dir + '/polarity').read()
3754
assert int(duty) == 0
3855
assert int(period) == 500000
39-
assert int(polarity) == 1
40-
PWM.cleanup()
4156

42-
def test_start_pwm_with_polarity_default(self):
43-
PWM.start("P9_14", 0, 2000, 0)
44-
45-
files = os.listdir('/sys/devices')
46-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
47-
files = os.listdir(ocp)
48-
pwm_test = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
57+
if kernel >= '4.1.0':
58+
assert polarity == "inversed\n"
59+
else:
60+
assert int(polarity) == 1
61+
PWM.cleanup()
4962

50-
assert os.path.exists(pwm_test)
51-
duty = open(pwm_test + '/duty').read()
52-
period = open(pwm_test + '/period').read()
53-
polarity = open(pwm_test + '/polarity').read()
63+
def test_start_pwm_with_polarity_default(self):
64+
PWM.start("P9_14", 0, 2000)
65+
66+
if kernel >= '4.1.0':
67+
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
68+
else:
69+
files = os.listdir('/sys/devices')
70+
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
71+
files = os.listdir(ocp)
72+
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
73+
74+
assert os.path.exists(pwm_dir)
75+
if kernel >= '4.1.0':
76+
duty = open(pwm_dir + '/duty_cycle').read()
77+
else:
78+
duty = open(pwm_dir + '/duty').read()
79+
period = open(pwm_dir + '/period').read()
80+
polarity = open(pwm_dir + '/polarity').read()
5481
assert int(duty) == 0
5582
assert int(period) == 500000
56-
assert int(polarity) == 0
57-
PWM.cleanup()
83+
84+
if kernel >= '4.1.0':
85+
assert polarity == 'normal\n'
86+
else:
87+
assert int(polarity) == 0
88+
PWM.cleanup()
5889

5990
def test_start_pwm_with_polarity_zero(self):
6091
PWM.start("P9_14", 0, 2000, 0)
6192

62-
files = os.listdir('/sys/devices')
63-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
64-
files = os.listdir(ocp)
65-
pwm_test = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
66-
67-
assert os.path.exists(pwm_test)
68-
duty = open(pwm_test + '/duty').read()
69-
period = open(pwm_test + '/period').read()
70-
polarity = open(pwm_test + '/polarity').read()
93+
if kernel >= '4.1.0':
94+
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
95+
else:
96+
files = os.listdir('/sys/devices')
97+
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
98+
files = os.listdir(ocp)
99+
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
100+
101+
assert os.path.exists(pwm_dir)
102+
if kernel >= '4.1.0':
103+
duty = open(pwm_dir + '/duty_cycle').read()
104+
else:
105+
duty = open(pwm_dir + '/duty').read()
106+
period = open(pwm_dir + '/period').read()
107+
polarity = open(pwm_dir + '/polarity').read()
71108
assert int(duty) == 0
72109
assert int(period) == 500000
73-
assert int(polarity) == 0
74-
PWM.cleanup()
110+
111+
if kernel >= '4.1.0':
112+
assert polarity == 'normal\n'
113+
else:
114+
assert int(polarity) == 0
115+
PWM.cleanup()
75116

76117
def test_pwm_start_invalid_pwm_key(self):
77-
with pytest.raises(ValueError):
78-
PWM.start("P8_25", -1)
118+
with pytest.raises(ValueError):
119+
PWM.start("P8_25", -1)
79120

80121
def test_pwm_start_invalid_duty_cycle_negative(self):
81-
with pytest.raises(ValueError):
82-
PWM.start("P9_14", -1)
122+
with pytest.raises(ValueError):
123+
PWM.start("P9_14", -1)
83124

84125
def test_pwm_start_valid_duty_cycle_min(self):
85-
#testing an exception isn't thrown
86-
PWM.start("P9_14", 0)
126+
# testing an exception isn't thrown
127+
PWM.start("P9_14", 0)
87128
PWM.cleanup()
88129

89130
def test_pwm_start_valid_duty_cycle_max(self):
90-
#testing an exception isn't thrown
91-
PWM.start("P9_14", 100)
92-
PWM.cleanup()
131+
# testing an exception isn't thrown
132+
PWM.start("P9_14", 100)
133+
PWM.cleanup()
93134

94135
def test_pwm_start_invalid_duty_cycle_high(self):
95-
with pytest.raises(ValueError):
96-
PWM.start("P9_14", 101)
136+
with pytest.raises(ValueError):
137+
PWM.start("P9_14", 101)
97138

98139
def test_pwm_start_invalid_duty_cycle_string(self):
99-
with pytest.raises(TypeError):
100-
PWM.start("P9_14", "1")
140+
with pytest.raises(TypeError):
141+
PWM.start("P9_14", "1")
101142

102143
def test_pwm_start_invalid_frequency_negative(self):
103144
with pytest.raises(ValueError):
104145
PWM.start("P9_14", 0, -1)
105146

106147
def test_pwm_start_invalid_frequency_string(self):
107-
with pytest.raises(TypeError):
108-
PWM.start("P9_14", 0, "1")
148+
with pytest.raises(TypeError):
149+
PWM.start("P9_14", 0, "1")
109150

110151
def test_pwm_start_negative_polarity(self):
111-
with pytest.raises(ValueError):
112-
PWM.start("P9_14", 0, 100, -1)
152+
with pytest.raises(ValueError):
153+
PWM.start("P9_14", 0, 100, -1)
113154

114155
def test_pwm_start_invalid_positive_polarity(self):
115-
with pytest.raises(ValueError):
116-
PWM.start("P9_14", 0, 100, 2)
156+
with pytest.raises(ValueError):
157+
PWM.start("P9_14", 0, 100, 2)
117158

118159
def test_pwm_start_invalid_polarity_type(self):
119-
with pytest.raises(TypeError):
120-
PWM.start("P9_14", 0, 100, "1")
160+
with pytest.raises(TypeError):
161+
PWM.start("P9_14", 0, 100, "1")
121162

122163
def test_pwm_duty_modified(self):
123164
PWM.start("P9_14", 0)
124165

125-
files = os.listdir('/sys/devices')
126-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
127-
files = os.listdir(ocp)
128-
pwm_test = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
129-
130-
assert os.path.exists(pwm_test)
131-
duty = open(pwm_test + '/duty').read()
132-
period = open(pwm_test + '/period').read()
166+
if kernel >= '4.1.0':
167+
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
168+
else:
169+
files = os.listdir('/sys/devices')
170+
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
171+
files = os.listdir(ocp)
172+
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
173+
174+
assert os.path.exists(pwm_dir)
175+
if kernel >= '4.1.0':
176+
duty = open(pwm_dir + '/duty_cycle').read()
177+
else:
178+
duty = open(pwm_dir + '/duty').read()
179+
period = open(pwm_dir + '/period').read()
133180
assert int(duty) == 0
134181
assert int(period) == 500000
135182

136183
PWM.set_duty_cycle("P9_14", 100)
137-
duty = open(pwm_test + '/duty').read()
138-
period = open(pwm_test + '/period').read()
184+
if kernel >= '4.1.0':
185+
duty = open(pwm_dir + '/duty_cycle').read()
186+
else:
187+
duty = open(pwm_dir + '/duty').read()
188+
period = open(pwm_dir + '/period').read()
139189
assert int(duty) == 500000
140-
assert int(period) == 500000
190+
assert int(period) == 500000
141191
PWM.cleanup()
142192

143193
def test_pwm_duty_cycle_non_setup_key(self):
144194
with pytest.raises(RuntimeError):
145195
PWM.set_duty_cycle("P9_14", 100)
146-
PWM.cleanup()
196+
PWM.cleanup()
147197

148198
def test_pwm_duty_cycle_invalid_key(self):
149199
with pytest.raises(ValueError):
150200
PWM.set_duty_cycle("P9_15", 100)
151-
PWM.cleanup()
201+
PWM.cleanup()
152202

153203
def test_pwm_duty_cycle_invalid_value_high(self):
154204
PWM.start("P9_14", 0)
155205
with pytest.raises(ValueError):
156206
PWM.set_duty_cycle("P9_14", 101)
157-
PWM.cleanup()
207+
PWM.cleanup()
158208

159209
def test_pwm_duty_cycle_invalid_value_negative(self):
160210
PWM.start("P9_14", 0)
161211
with pytest.raises(ValueError):
162212
PWM.set_duty_cycle("P9_14", -1)
163-
PWM.cleanup()
213+
PWM.cleanup()
164214

165215
def test_pwm_duty_cycle_invalid_value_string(self):
166216
PWM.start("P9_14", 0)
167217
with pytest.raises(TypeError):
168218
PWM.set_duty_cycle("P9_14", "a")
169-
PWM.cleanup()
219+
PWM.cleanup()
170220

171221
def test_pwm_frequency_invalid_value_negative(self):
172222
PWM.start("P9_14", 0)
173223
with pytest.raises(ValueError):
174224
PWM.set_frequency("P9_14", -1)
175-
PWM.cleanup()
225+
PWM.cleanup()
176226

177227
def test_pwm_frequency_invalid_value_string(self):
178228
PWM.start("P9_14", 0)
179229
with pytest.raises(TypeError):
180230
PWM.set_frequency("P9_14", "11")
181-
PWM.cleanup()
231+
PWM.cleanup()
182232

183233
def test_pwm_freq_non_setup_key(self):
184234
with pytest.raises(RuntimeError):
185235
PWM.set_frequency("P9_14", 100)
186-
PWM.cleanup()
236+
PWM.cleanup()
187237

188-
def test_pwm_freq_non_setup_key(self):
238+
def test_pwm_freq_non_setup_invalid_key(self):
189239
with pytest.raises(ValueError):
190240
PWM.set_frequency("P9_15", 100)
191-
PWM.cleanup()
241+
PWM.cleanup()
192242

193243
def test_stop_pwm(self):
194244
pass
195-
#PWM.start("P9_14", 1)
196-
#PWM.stop("P9_14")
197-
#assert os.path.exists('/sys/class/gpio/gpio68')
198-
#direction = open('/sys/class/gpio/gpio68/direction').read()
199-
#assert direction == 'out\n'
200-
#PWM.cleanup()
245+
# PWM.start("P9_14", 1)
246+
# PWM.stop("P9_14")
247+
# assert os.path.exists('/sys/class/gpio/gpio68')
248+
# direction = open('/sys/class/gpio/gpio68/direction').read()
249+
# assert direction == 'out\n'
250+
# PWM.cleanup()

0 commit comments

Comments
 (0)