-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for pin mode switching, analog read and write.
- Loading branch information
1 parent
8f5df07
commit f001119
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#This tests that the mode of pins can be configured properly and that pull works OK. | ||
|
||
from microbit import * | ||
import music | ||
|
||
BIG_PINS = pin0, pin1, pin2 | ||
DISPLAY_PINS = pin3, pin4, pin6, pin7, pin9, pin10 | ||
|
||
def test_mode_switching(): | ||
print ("Switching mode from analog to digital on big pins") | ||
for p0 in BIG_PINS: | ||
p0.write_analog(100) | ||
for p1 in BIG_PINS: | ||
if p0 == p1: | ||
try: | ||
p1.write_digital(0) | ||
except ValueError: | ||
pass | ||
else: | ||
p1.write_digital(0) | ||
p0.write_analog(0) | ||
|
||
def test_display_pins(): | ||
print ("Switching mode to digital for display pins") | ||
for p0 in DISPLAY_PINS: | ||
try: | ||
p0.write_digital(0) | ||
except ValueError: | ||
pass | ||
display.off() | ||
for p0 in DISPLAY_PINS: | ||
p0.write_digital(0) | ||
display.on() | ||
display.off() | ||
for p0 in DISPLAY_PINS: | ||
p0.write_analog(200) | ||
try: | ||
display.on() | ||
except ValueError: | ||
pass | ||
for p0 in DISPLAY_PINS: | ||
p0.write_analog(0) | ||
display.on() | ||
|
||
def test_music(): | ||
print ("Switching mode to digital for music pins") | ||
music.play(music.DADADADUM,pin=pin0,wait=False) | ||
try: | ||
pin0.write_digital(0) | ||
except ValueError: | ||
pass | ||
music.stop() | ||
pin0.write_digital(0) | ||
|
||
def test_pull(): | ||
print ("Setting pull on big pins") | ||
for p in BIG_PINS: | ||
p.set_pull(p.PULL_UP) | ||
assert p.read_digital() | ||
p.set_pull(p.PULL_DOWN) | ||
assert p.read_digital() == 0 | ||
for p in BIG_PINS: | ||
p.set_pull(p.PULL_UP) | ||
assert p.read_analog() > 900 | ||
p.set_pull(p.NO_PULL) | ||
assert 100 < p.read_analog() < 900 | ||
p.set_pull(p.PULL_DOWN) | ||
assert p.read_analog() < 100 | ||
display.off() | ||
for p in DISPLAY_PINS: | ||
p.set_pull(p.PULL_UP) | ||
assert p.read_digital() | ||
p.set_pull(p.PULL_DOWN) | ||
assert p.read_digital() == 0 | ||
display.on() | ||
|
||
try: | ||
test_mode_switching() | ||
test_display_pins() | ||
test_music() | ||
test_pull() | ||
print("Pin test: PASS") | ||
display.show(Image.HAPPY) | ||
except Exception as ae: | ||
display.show(Image.SAD) | ||
raise | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#This tests that the duty cycle of PWM is (approximately) correct. | ||
|
||
from microbit import * | ||
|
||
def pins_connected(p0, p1): | ||
for i in 0,1,0,1,0,1,0,1: | ||
p0.write_digital(i) | ||
if p1.read_digital() != i: | ||
return False | ||
p1.write_digital(i) | ||
if p0.read_digital() != i: | ||
return False | ||
return True | ||
|
||
def test_pwm(p0, p1): | ||
for val in 100, 200, 300, 500, 800, 900: | ||
print("Testing duty cycle", val) | ||
p0.write_analog(val) | ||
sleep(1) | ||
count = 0 | ||
for i in range(40*1024): | ||
count += p1.read_digital() | ||
# Allow +- 12.5% | ||
assert abs(count - val*40) < val * 5 | ||
|
||
try: | ||
if pins_connected(pin0, pin1): | ||
test_pwm(pin0, pin1) | ||
print("PWM test: PASS") | ||
display.show(Image.HAPPY) | ||
else: | ||
print("Connect pin0 to pin1") | ||
display.show("?") | ||
except Exception as ae: | ||
display.show(Image.SAD) | ||
raise | ||
|