File: test_pwm.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (44 lines) | stat: -rw-r--r-- 1,169 bytes parent folder | download | duplicates (3)
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
#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):
    #Flip between modes many times
    for i in range(100):
        pin0.write_digital(i & 1)
        pin0.write_analog(i*10)
    #Now make sure it is still working.
    p0.write_digital(0)
    for val in 100, 200, 300, 500, 800, 900:
        print("Testing duty cycle", val)
        p0.write_analog(val)
        sleep(20)
        count = 0
        for i in range(10*1024):
            count += p1.read_digital()
        print("%+0.2f%%" % ((count-val*10)/val*10))
        # Allow +- 5%
        assert abs(count - val*10) < val//2

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