-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheat_sheet.py
128 lines (90 loc) · 2.36 KB
/
cheat_sheet.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Cheat sheet with all the commands and functions that are useful for this tutorial.
For more in-depth information, please refer to the official documentation in
the Python editor (https://python.microbit.org/) or the official MicroPython
documentation (https://microbit-micropython.readthedocs.io/en/latest/).
"""
# ***** Display *****
from microbit import *
# Display an image
display.show(Image.HEART)
# Display custom image
display.show(
Image(
"00000:"
"33333:"
"55555:"
"77777:"
"99999"
)
)
# Scroll text
display.scroll("PyCon US")
# Set pixels
display.set_pixel(0, 0, 9)
# Clear display
display.clear()
# ***** Push Buttons *****
from microbit import *
# Check if button A was pressed
if button_a.was_pressed():
pass
# Check if button B was pressed
if button_b.was_pressed():
pass
# Check if button A is pressed
if button_a.is_pressed():
pass
# Check if button B is pressed
if button_b.is_pressed():
pass
# Get how many times button A was pressed
quantity = button_a.get_presses()
# Get how many times button B was pressed
quantity = button_b.get_presses()
# ***** Speaker *****
from microbit import *
import music, speech, audio
# Play pre-defined music
music.play(music.POWER_UP)
# Create your own music - Format: '{note}{octave}:{duration}'
music.play(
[
"C4:2", "D4:2", "E4:2", "F4:2",
"G4:2", "A4:2", "B4:2",
]
)
# Text to speech
speech.say("Hello, PyCon!")
# Play expressive sounds
audio.play(Sound.YAWN)
# ***** Michophone *****
from microbit import *
# SoundEvent can be LOUD or QUIET
# Read current event
event = microphone.current_event()
# Get the sound history as a tuple
history = microphone.get_events()
# Get sound level (0-255)
level = microphone.sound_level()
# ***** Accelerometer *****
from microbit import *
# Read acceleration in each axis
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
# Read pre-defined current gesture
gesture = accelerometer.is_gesture("face up")
# Read pre-defined past gesture
gesture = accelerometer.was_gesture("shake")
# ***** Radio *****
import radio
# Turn on the radio
radio.on()
# Set the radio group and power
# Group: 0-255, Power: 0-7 (signal intensity)
radio.config(group=23, power=7)
# Receive a message from the group
message = radio.receive()
# Send a message to the group
radio.send("PyCon US")