File: exercise.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 (75 lines) | stat: -rw-r--r-- 1,707 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
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
# Exercises the micro:bit - NOT EXHAUSTIVE!
from microbit import *
import music
import random


# Press A to start.
while True:
    if button_a.was_pressed():
        break
    else:
        display.show(Image.ARROW_W)
        sleep(200)
        display.clear()
        sleep(200)


# Asyncronously play a jolly little tune (connect speaker to pin0 and GND)
music.play(music.NYAN, wait=False)


# Grab all the built in images.
images = [getattr(Image, img) for img in dir(Image)
          if type(getattr(Image, img)) == Image]
# ... and cycle through them on the display.
pause = 1000
for img in images:
    display.show(img)
    sleep(pause)
    pause -= 50
    if pause < 100:
        pause = 100
display.clear()


# Aural testing of the accelerometer.
display.scroll("Accelerometer")
display.show("X")
while not button_a.is_pressed():
    music.pitch(abs(accelerometer.get_x()), 20)
sleep(500)
display.show("Y")
while not button_a.is_pressed():
    music.pitch(abs(accelerometer.get_y()), 20)
sleep(500)
display.show("Z")
while not button_a.is_pressed():
    music.pitch(abs(accelerometer.get_z()), 20)


# Aural testing of the compass.
display.scroll("Compass")
compass.calibrate()
while not button_b.is_pressed():
    music.pitch(abs(compass.heading()), 20)


# Pixel brightness.
display.scroll("Display")
dots = [ [0]*5, [0]*5, [0]*5, [0]*5, [0]*5 ]
while not button_a.is_pressed():
    dots[random.randrange(5)][random.randrange(5)] = 9
    for i in range(5):
        for j in range(5):
            display.set_pixel(i, j, dots[i][j])
            dots[i][j] = max(dots[i][j] - 1, 0)
    sleep(50)


# ??? Add further tests here...


# Finished!
display.scroll("Finished!")
display.show(Image.HAPPY)