Skip to content

Commit

Permalink
Add test for pin mode switching, analog read and write.
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon committed Sep 27, 2016
1 parent 8f5df07 commit f001119
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
87 changes: 87 additions & 0 deletions tests/test_pins.py
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

37 changes: 37 additions & 0 deletions tests/test_pwm.py
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

0 comments on commit f001119

Please sign in to comment.