Update. /JL
This commit is contained in:
6
begynder/billede.py
Normal file
6
begynder/billede.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from microbit import *
|
||||||
|
|
||||||
|
while True:
|
||||||
|
display.show(Image.HEART)
|
||||||
|
sleep(1000)
|
||||||
|
display.scroll('Hello')
|
505
microbit/__init__.py
Normal file
505
microbit/__init__.py
Normal file
@@ -0,0 +1,505 @@
|
|||||||
|
# Basics
|
||||||
|
import accelerometer
|
||||||
|
import compass
|
||||||
|
import display
|
||||||
|
import i2c
|
||||||
|
import microphone
|
||||||
|
import power
|
||||||
|
import speaker
|
||||||
|
import spi
|
||||||
|
import uart
|
||||||
|
# Extended
|
||||||
|
import audio
|
||||||
|
import machine
|
||||||
|
import music
|
||||||
|
import neopixel
|
||||||
|
import os
|
||||||
|
import radio
|
||||||
|
import random
|
||||||
|
import speech
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def panic():
|
||||||
|
""" Enter a panic mode that stops all execution, scrolls an
|
||||||
|
error code in the micro:bit display and requires restart.
|
||||||
|
Parameters: n – An arbitrary integer between 0 and 255
|
||||||
|
to indicate an error code. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def reset():
|
||||||
|
""" Restart the board. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def running_time():
|
||||||
|
""" Returns: The number of milliseconds since the board was switched on or restarted. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def scale(value, from_, to):
|
||||||
|
""" Converts a value from a range to another range.
|
||||||
|
For example, to convert 30 degrees from Celsius to Fahrenheit:
|
||||||
|
temp_fahrenheit = scale(30, from_=(0.0, 100.0), to=(32.0, 212.0))
|
||||||
|
|
||||||
|
This can be useful to convert values between inputs and outputs,
|
||||||
|
for example an accelerometer x value to a speaker volume.
|
||||||
|
|
||||||
|
If one of the numbers in the to parameter is a floating point
|
||||||
|
(i.e a decimal number like 10.0), this function will return a floating point
|
||||||
|
number. If they are both integers (i.e 10), it will return an integer:
|
||||||
|
|
||||||
|
returns_int = scale(accelerometer.get_x(), from_=(-2000, 2000), to=(0, 255))
|
||||||
|
|
||||||
|
Negative scaling is also supported, for example scale(25, from_=(0, 100),
|
||||||
|
to=(0, -200)) will return -50.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
value – A number to convert.
|
||||||
|
from – A tuple to define the range to convert from.
|
||||||
|
to – A tuple to define the range to convert to.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The value converted to the to range. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_volume(volume):
|
||||||
|
""" (V2 only) Configure the output volume of the micro:bit speaker and pins.
|
||||||
|
Parameters: volume – An integer between 0 and 255 to set the volume. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sleep(n):
|
||||||
|
""" Wait for n milliseconds. One second is 1000 milliseconds.
|
||||||
|
Parameters: n – An integer or floating point number indicating the number
|
||||||
|
of milliseconds to wait. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def run_every(callback, days=None, h=None, min=None, s=None, ms=None):
|
||||||
|
""" Schedule to run a function at the interval specified by the time arguments.
|
||||||
|
run_every can be used in two ways:
|
||||||
|
As a Decorator - placed on top of the function to schedule. For example:
|
||||||
|
|
||||||
|
@run_every(days=1, h=1, min=20, s=30, ms=50)
|
||||||
|
def my_function():
|
||||||
|
# Do something here
|
||||||
|
|
||||||
|
As a Function - passing the callback as a positional argument. For example:
|
||||||
|
|
||||||
|
def my_function():
|
||||||
|
# Do something here
|
||||||
|
run_every(my_function, s=30)
|
||||||
|
|
||||||
|
Each argument corresponds to a different time unit and they are additive.
|
||||||
|
So run_every(min=1, s=30) schedules the callback every minute and a half.
|
||||||
|
|
||||||
|
When an exception is thrown inside the callback function it deschedules the function.
|
||||||
|
To avoid this you can catch exceptions with try/except.
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
callback – Function to call at the provided interval.
|
||||||
|
days – Sets the days mark for the scheduling.
|
||||||
|
h – Sets the hour mark for the scheduling.
|
||||||
|
min – Sets the minute mark for the scheduling.
|
||||||
|
s – Sets the second mark for the scheduling.
|
||||||
|
ms – Sets the millisecond mark for the scheduling. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def temperature():
|
||||||
|
""" Returns: An integer with the temperature of the micro:bit in degrees Celcius. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
#Attributes-Buttons
|
||||||
|
|
||||||
|
class _Button():
|
||||||
|
""" Represents a button.
|
||||||
|
Note: This class is not actually available to the user, it is only used by the
|
||||||
|
two button instances, which are provided already initialized. """
|
||||||
|
def __init__(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_pressed():
|
||||||
|
""" Returns True if the specified button button is currently being held down,
|
||||||
|
and False otherwise. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def was_pressed():
|
||||||
|
""" Returns True or False to indicate if the button was pressed (went from up to down)
|
||||||
|
since the device started or the last time this method was called. Calling this
|
||||||
|
method will clear the press state so that the button must be pressed again before
|
||||||
|
this method will return True again. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_presses():
|
||||||
|
""" Returns the running total of button presses, and resets this total
|
||||||
|
to zero before returning. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
button_a = _Button()
|
||||||
|
""" A Button instance representing the left button. """
|
||||||
|
|
||||||
|
button_b = _Button()
|
||||||
|
""" Represents the right button. """
|
||||||
|
|
||||||
|
#Attributes-Input/Output Pins
|
||||||
|
|
||||||
|
""" There are three kinds of pins, differing in what is available for them. They
|
||||||
|
are represented by the classes listed below. Note that they form a hierarchy,
|
||||||
|
so that each class has all the functionality of the previous class, and adds
|
||||||
|
its own to that.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
Those classes are not actually available for the user, you can’t create new
|
||||||
|
instances of them. You can only use the instances already provided, representing
|
||||||
|
the physical pins on your board. """
|
||||||
|
|
||||||
|
class _MicroBitDigitalPin():
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def read_digital():
|
||||||
|
""" Return 1 if the pin is high, and 0 if it’s low. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def write_digital(value):
|
||||||
|
""" Set the pin to high if value is 1, or to low, if it is 0. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_pull(value):
|
||||||
|
""" Set the pull state to one of three possible values: pin.PULL_UP, pin.PULL_DOWN or
|
||||||
|
pin.NO_PULL (where pin is an instance of a pin).
|
||||||
|
The pull mode for a pin is automatically configured when the pin changes to an input
|
||||||
|
mode. Input modes are when you call read_analog / read_digital / is_touched. The
|
||||||
|
default pull mode for these is, respectively, NO_PULL, PULL_DOWN, PULL_UP. Calling
|
||||||
|
set_pull will configure the pin to be in read_digital mode with the given pull mode. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_pull():
|
||||||
|
""" Returns the pull configuration on a pin, which can be one of three possible values:
|
||||||
|
NO_PULL, PULL_DOWN, or PULL_UP. These are set using the set_pull() method or
|
||||||
|
automatically configured when a pin mode requires it. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_mode():
|
||||||
|
""" Returns the pin mode. When a pin is used for a specific function, like writing a
|
||||||
|
digital value, or reading an analog value, the pin mode changes. Pins can have one of
|
||||||
|
the following modes: "unused", "analog", "read_digital", "write_digital", "display",
|
||||||
|
"button", "music", "audio", "touch", "i2c", "spi". """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def write_analog(value):
|
||||||
|
""" Output a PWM signal on the pin, with the duty cycle proportional to the provided
|
||||||
|
value. The value may be either an integer or a floating point number between 0
|
||||||
|
(0% duty cycle) and 1023 (100% duty). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_analog_period(period):
|
||||||
|
""" Set the period of the PWM signal being output to period in milliseconds. The minimum
|
||||||
|
valid value is 1ms. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_analog_period_microseconds(period):
|
||||||
|
""" Set the period of the PWM signal being output to period in microseconds. The minimum
|
||||||
|
valid value is 256µs. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_analog_period_microseconds():
|
||||||
|
""" Returns the configured period of the PWM signal in microseconds. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
class _MicroBitAnalogDigitalPin(_MicroBitDigitalPin):
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def read_analog():
|
||||||
|
""" Read the voltage applied to the pin, and return it as an integer
|
||||||
|
between 0 (meaning 0V) and 1023 (meaning 3.3V). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
class _MicroBitTouchPin(_MicroBitAnalogDigitalPin):
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_touched():
|
||||||
|
""" Return True if the pin is being touched with a finger, otherwise return False.
|
||||||
|
Note:
|
||||||
|
The default touch mode for the pins on the edge connector is resistive. The default
|
||||||
|
for the logo pin V2 is capacitive.
|
||||||
|
Resistive touch This test is done by measuring how much resistance there is between
|
||||||
|
the pin and ground. A low resistance gives a reading of True. To get a reliable
|
||||||
|
reading using a finger you may need to touch the ground pin with another part of your
|
||||||
|
body, for example your other hand.
|
||||||
|
Capacitive touch This test is done by interacting with the electric field of a
|
||||||
|
capacitor using a finger as a conductor. Capacitive touch does not require you to make
|
||||||
|
a ground connection as part of a circuit. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_touch_mode(value):
|
||||||
|
""" Set the touch mode for the given pin. Value can be either CAPACITIVE or RESISTIVE.
|
||||||
|
For example, pin0.set_touch_mode(pin0.CAPACITIVE). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
pin_logo = _MicroBitTouchPin()
|
||||||
|
""" A touch sensitive logo pin on the front of the micro:bit, which by default is set to
|
||||||
|
capacitive touch mode. """
|
||||||
|
pin_speaker = _MicroBitAnalogDigitalPin()
|
||||||
|
""" A pin to address the micro:bit speaker. This API is intended only for use in Pulse-Width
|
||||||
|
Modulation pin operations eg. pin_speaker.write_analog(128). """
|
||||||
|
|
||||||
|
pin0 = _MicroBitTouchPin()
|
||||||
|
pin1 = _MicroBitTouchPin()
|
||||||
|
pin2 = _MicroBitTouchPin()
|
||||||
|
|
||||||
|
pin3 = _MicroBitAnalogDigitalPin()
|
||||||
|
pin4 = _MicroBitAnalogDigitalPin()
|
||||||
|
|
||||||
|
pin5 = _MicroBitDigitalPin()
|
||||||
|
pin6 = _MicroBitDigitalPin()
|
||||||
|
pin7 = _MicroBitDigitalPin()
|
||||||
|
pin8 = _MicroBitDigitalPin()
|
||||||
|
pin9 = _MicroBitDigitalPin()
|
||||||
|
|
||||||
|
pin10 = _MicroBitAnalogDigitalPin()
|
||||||
|
|
||||||
|
pin11 = _MicroBitDigitalPin()
|
||||||
|
pin12 = _MicroBitDigitalPin()
|
||||||
|
pin13 = _MicroBitDigitalPin()
|
||||||
|
pin14 = _MicroBitDigitalPin()
|
||||||
|
pin15 = _MicroBitDigitalPin()
|
||||||
|
pin16 = _MicroBitDigitalPin()
|
||||||
|
# Pin17 = 3v3
|
||||||
|
# Pin18 = 3v3
|
||||||
|
pin19 = _MicroBitDigitalPin()
|
||||||
|
pin20 = _MicroBitDigitalPin()
|
||||||
|
# pin21 = gnd
|
||||||
|
# pin22 = gnd
|
||||||
|
|
||||||
|
""" Note:
|
||||||
|
GPIO pins are also used for the display, as described in the table above. If you want to use
|
||||||
|
these pins for another purpose, you may need to turn the display off. For more info see the
|
||||||
|
edge connector data sheet. """
|
||||||
|
|
||||||
|
#Class-Image
|
||||||
|
""" The Image class is used to create images that can be displayed easily on the device’s LED
|
||||||
|
matrix. Given an image object it’s possible to display it via the display API:
|
||||||
|
display.show(Image.HAPPY) """
|
||||||
|
|
||||||
|
|
||||||
|
class Image():
|
||||||
|
""" There are four ways in which you can construct an image:
|
||||||
|
Image() - Create a blank 5x5 image
|
||||||
|
Image(string)
|
||||||
|
Create an image by parsing the string,
|
||||||
|
image = Image("90009:"
|
||||||
|
"09090:"
|
||||||
|
"00900:"
|
||||||
|
"09090:"
|
||||||
|
"90009")
|
||||||
|
will create a 5×5 image of an X. The end of a line is indicated by a colon. It’s also
|
||||||
|
possible to use a newline (\n) to indicate the end of a line.
|
||||||
|
A single character returns that glyph.
|
||||||
|
Image(width, height)
|
||||||
|
Creates an empty image with width columns and height rows.
|
||||||
|
Image(width, height, buffer)
|
||||||
|
Optionally buffer can be an array of width``×``height integers in range 0-9 to
|
||||||
|
initialize the image:
|
||||||
|
Image(2, 2, bytearray([9,9,9,9])) """
|
||||||
|
def __init__(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def width():
|
||||||
|
""" Return the number of columns in the image. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def height():
|
||||||
|
""" Return the numbers of rows in the image. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_pixel(x, y, value):
|
||||||
|
""" Set the brightness of the pixel at column x and row y to the value, which has to be
|
||||||
|
between 0 (dark) and 9 (bright).
|
||||||
|
This method will raise an exception when called on any of the built-in read-only
|
||||||
|
images, like Image.HEART. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_pixel(x, y):
|
||||||
|
""" Return the brightness of pixel at column x and row y as an integer between 0 and 9. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def shift_left(n):
|
||||||
|
""" Return a new image created by shifting the picture left by n columns. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def shift_right(n):
|
||||||
|
""" Same as image.shift_left(-n). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def shift_up(n):
|
||||||
|
""" Return a new image created by shifting the picture up by n rows. """
|
||||||
|
pass
|
||||||
|
def shift_down(n):
|
||||||
|
""" Same as image.shift_up(-n). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def crop(x, y, w, h):
|
||||||
|
""" Return a new image by cropping the picture to a width of w and a height of h,
|
||||||
|
starting with the pixel at column x and row y. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def copy():
|
||||||
|
""" Return an exact copy of the image. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def invert():
|
||||||
|
""" Return a new image by inverting the brightness of the pixels in the source image. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def fill(value):
|
||||||
|
""" Set the brightness of all the pixels in the image to the value, which has to be
|
||||||
|
between 0 (dark) and 9 (bright).
|
||||||
|
This method will raise an exception when called on any of the built-in read-only
|
||||||
|
images, like Image.HEART. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def blit(src, x, y, w, h, xdest=0, ydest=0):
|
||||||
|
""" Copy the rectangle defined by x, y, w, h from the image src into this image at xdest,
|
||||||
|
ydest. Areas in the source rectangle, but outside the source image are treated as
|
||||||
|
having a value of 0.
|
||||||
|
shift_left(), shift_right(), shift_up(), shift_down() and crop() can are all
|
||||||
|
implemented by using blit(). For example, img.crop(x, y, w, h) can be implemented as:
|
||||||
|
|
||||||
|
def crop(self, x, y, w, h):
|
||||||
|
res = Image(w, h)
|
||||||
|
res.blit(self, x, y, w, h)
|
||||||
|
return res """
|
||||||
|
pass
|
||||||
|
|
||||||
|
#Class-Image-Attributes
|
||||||
|
""" The Image class also has the following built-in instances of itself included as its attributes
|
||||||
|
(the attribute names indicate what the image represents): """
|
||||||
|
|
||||||
|
HEART = None
|
||||||
|
HEART_SMALL = None
|
||||||
|
HAPPY = None
|
||||||
|
SMILE = None
|
||||||
|
SAD = None
|
||||||
|
CONFUSED = None
|
||||||
|
ANGRY = None
|
||||||
|
ASLEEP = None
|
||||||
|
SURPRISED = None
|
||||||
|
SILLY = None
|
||||||
|
FABULOUS = None
|
||||||
|
MEH = None
|
||||||
|
YES = None
|
||||||
|
NO = None
|
||||||
|
CLOCK12 = None
|
||||||
|
CLOCK11 = None
|
||||||
|
CLOCK10 = None
|
||||||
|
CLOCK9 = None
|
||||||
|
CLOCK8 = None
|
||||||
|
CLOCK7 = None
|
||||||
|
CLOCK6 = None
|
||||||
|
CLOCK5 = None
|
||||||
|
CLOCK4 = None
|
||||||
|
CLOCK3 = None
|
||||||
|
CLOCK2 = None
|
||||||
|
CLOCK1 = None
|
||||||
|
ARROW_N = None
|
||||||
|
ARROW_NE = None
|
||||||
|
ARROW_E = None
|
||||||
|
ARROW_SE = None
|
||||||
|
ARROW_S = None
|
||||||
|
ARROW_SW = None
|
||||||
|
ARROW_W = None
|
||||||
|
ARROW_NW = None
|
||||||
|
TRIANGLE = None
|
||||||
|
TRIANGLE_LEFT = None
|
||||||
|
CHESSBOARD = None
|
||||||
|
DIAMOND = None
|
||||||
|
DIAMOND_SMALL = None
|
||||||
|
SQUARE = None
|
||||||
|
SQUARE_SMALL = None
|
||||||
|
RABBIT = None
|
||||||
|
COW = None
|
||||||
|
MUSIC_CROTCHET = None
|
||||||
|
MUSIC_QUAVER = None
|
||||||
|
MUSIC_QUAVERS = None
|
||||||
|
PITCHFORK = None
|
||||||
|
XMAS = None
|
||||||
|
PACMAN = None
|
||||||
|
TARGET = None
|
||||||
|
TSHIRT = None
|
||||||
|
ROLLERSKATE = None
|
||||||
|
DUCK = None
|
||||||
|
HOUSE = None
|
||||||
|
TORTOISE = None
|
||||||
|
BUTTERFLY = None
|
||||||
|
STICKFIGURE = None
|
||||||
|
GHOST = None
|
||||||
|
SWORD = None
|
||||||
|
GIRAFFE = None
|
||||||
|
SKULL = None
|
||||||
|
UMBRELLA = None
|
||||||
|
SNAKE = None
|
||||||
|
|
||||||
|
#Finally, related collections of images have been grouped together:
|
||||||
|
|
||||||
|
ALL_CLOCKS = [None] #A list of all CLOCK images.
|
||||||
|
ALL_ARROWS = [None] #A list of all ARROW images.
|
||||||
|
|
||||||
|
#Class-Image-Attributes-Operations
|
||||||
|
def repr(image_attribute):
|
||||||
|
""" Get a compact string representation of the image attribute.
|
||||||
|
Parameters: image_attribute - Name of image e.g. HEART"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def str(image_attribute):
|
||||||
|
""" Get a readable string representation of the image.
|
||||||
|
Parameters: image_attribute - Name of image e.g. HEART """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def addimg():
|
||||||
|
""" Create a new image by adding the brightness values from the two images for each pixel.
|
||||||
|
Parameters: image_attribute - Name of image e.g. HEART """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def multimg():
|
||||||
|
""" Create a new image by multiplying the brightness of each pixel by n.
|
||||||
|
Parameters: image_attribute - Name of image e.g. HEART """
|
||||||
|
pass
|
||||||
|
|
||||||
|
#Class-Sound
|
||||||
|
|
||||||
|
class Sound():
|
||||||
|
""" Built-in sounds V2. The built-in sounds can be called using audio.play(Sound.NAME). """
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
GIGGLE = None
|
||||||
|
HAPPY = None
|
||||||
|
HELLO = None
|
||||||
|
MYSTERIOUS = None
|
||||||
|
SAD = None
|
||||||
|
SLIDE = None
|
||||||
|
SOARING = None
|
||||||
|
SPRING = None
|
||||||
|
TWINKLE = None
|
||||||
|
YAWN = None
|
||||||
|
|
||||||
|
#Class-SoundEvent
|
||||||
|
|
||||||
|
class SoundEvent():
|
||||||
|
""" The microphone can respond to a pre-defined set of sound events that are based on the
|
||||||
|
amplitude and wavelength of the sound. These sound events are represented by instances of
|
||||||
|
the SoundEvent class, accessible via variables in microbit.SoundEvent:
|
||||||
|
|
||||||
|
microbit.SoundEvent.QUIET: Represents the transition of sound events, from loud to quiet
|
||||||
|
like speaking or background music.
|
||||||
|
microbit.SoundEvent.LOUD: Represents the transition of sound events, from quiet to loud
|
||||||
|
like clapping or shouting.
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
QUIET=None
|
||||||
|
LOUD=None
|
70
microbit/accelerometer.py
Normal file
70
microbit/accelerometer.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#Accelerometer
|
||||||
|
|
||||||
|
""" This module gives you access to the on-board accelerometer.
|
||||||
|
By default MicroPython sets the accelerometer range to +/- 2000 mg (g being a unit of
|
||||||
|
acceleration based on the standard gravity), which configures the maximum and minimum
|
||||||
|
values returned by the accelerometer functions. The range can be changed via set_range().
|
||||||
|
|
||||||
|
The accelerometer also provides convenience functions for detecting gestures. The
|
||||||
|
recognised gestures are represented as strings: up, down, left, right, face up, face down,
|
||||||
|
freefall, 3g, 6g, 8g, shake.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
Gestures are not updated in the background so there needs to be constant calls to some
|
||||||
|
accelerometer method to do the gesture detection. Usually gestures can be detected using a
|
||||||
|
loop with a small sleep() delay.
|
||||||
|
Functions """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def get_x():
|
||||||
|
""" Returns: The acceleration measurement in the x axis in milli-g, as a positive or
|
||||||
|
negative integer. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_y():
|
||||||
|
""" Returns: The acceleration measurement in the y axis in milli-g, as a positive or
|
||||||
|
negative integer. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_z():
|
||||||
|
""" Returns: The acceleration measurement in the z axis in milli-g, as a positive or
|
||||||
|
negative integer. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_values():
|
||||||
|
""" Returns: The acceleration measurements in all axes at once, as a three-element tuple
|
||||||
|
of integers ordered as X, Y, Z. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_strength():
|
||||||
|
""" Get the acceleration measurement of all axes combined, as a positive integer. This is the
|
||||||
|
Pythagorean sum of the X, Y and Z axes.
|
||||||
|
Returns: The combined acceleration strength of all the axes, in milli-g. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def current_gesture():
|
||||||
|
""" Returns: String with the name of the current gesture. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_gesture(name):
|
||||||
|
""" Parameters: name – String with the name of the gesture to check.
|
||||||
|
Returns: Boolean indicating if the named gesture is currently active. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def was_gesture(name):
|
||||||
|
""" Parameters: name – String with the name of the gesture to check.
|
||||||
|
Returns: Boolean indicating if the named gesture was active since the last call. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_gestures():
|
||||||
|
""" Get a historical list of the registered gestures.
|
||||||
|
Calling this function clears the gesture history before returning.
|
||||||
|
Returns: A tuple of the gesture history, most recent is listed last. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_range(value):
|
||||||
|
""" Set the accelerometer sensitivity range, in g (standard gravity), to the closest values
|
||||||
|
supported by the hardware, so it rounds to either 2, 4, or 8 g.
|
||||||
|
Parameters: value – New range for the accelerometer, an integer in g. """
|
||||||
|
pass
|
48
microbit/compass.py
Normal file
48
microbit/compass.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#Compass
|
||||||
|
""" This module lets you access the built-in electronic compass. Before using, the compass should
|
||||||
|
be calibrated, otherwise the readings may be wrong.
|
||||||
|
|
||||||
|
Warning:
|
||||||
|
Calibrating the compass will cause your program to pause until calibration is complete.
|
||||||
|
Calibration consists of a little game to draw a circle on the LED display by rotating the
|
||||||
|
device. """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def calibrate():
|
||||||
|
""" Starts the calibration process. An instructive message will be scrolled to the user after
|
||||||
|
which they will need to rotate the device in order to fill the LED display. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_calibrated():
|
||||||
|
""" Returns True if the compass has been successfully calibrated, and returns False otherwise. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def clear_calibration():
|
||||||
|
""" Undoes the calibration, making the compass uncalibrated again. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_x():
|
||||||
|
""" Gives the reading of the magnetic field strength on the x axis in nano tesla, as a
|
||||||
|
positive or negative integer, depending on the direction of the field. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_y():
|
||||||
|
""" Gives the reading of the magnetic field strength on the y axis in nano tesla, as a
|
||||||
|
positive or negative integer, depending on the direction of the field. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_z():
|
||||||
|
""" Gives the reading of the magnetic field strength on the z axis in nano tesla, as a
|
||||||
|
positive or negative integer, depending on the direction of the field. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def heading():
|
||||||
|
""" Gives the compass heading, calculated from the above readings, as an integer in the
|
||||||
|
range from 0 to 360, representing the angle in degrees, clockwise, with north as 0. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_field_strength():
|
||||||
|
""" Returns an integer indication of the magnitude of the magnetic field around the device
|
||||||
|
in nano tesla. """
|
||||||
|
pass
|
69
microbit/display.py
Normal file
69
microbit/display.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#Display
|
||||||
|
""" This module controls the 5×5 LED display on the front of your board. It can be used to
|
||||||
|
display images, animations and even text. """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def get_pixel(x, y):
|
||||||
|
""" Return the brightness of the LED at column x and row y as an integer between 0 (off)
|
||||||
|
and 9 (bright). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_pixel(x, y, value):
|
||||||
|
""" Set the brightness of the LED at column x and row y to value, which has to be an integer
|
||||||
|
between 0 and 9. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def clear():
|
||||||
|
""" Set the brightness of all LEDs to 0 (off). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def show(image, delay=400, *, wait=True, loop=False, clear=False):
|
||||||
|
""" If image is a string, float or integer, display letters/digits in sequence. Otherwise,
|
||||||
|
if image is an iterable sequence of images, display these images in sequence. Each letter,
|
||||||
|
digit or image is shown with delay milliseconds between them.
|
||||||
|
|
||||||
|
If wait is True, this function will block until the animation is finished, otherwise the
|
||||||
|
animation will happen in the background.
|
||||||
|
|
||||||
|
If loop is True, the animation will repeat forever.
|
||||||
|
|
||||||
|
If clear is True, the display will be cleared after the iterable has finished.
|
||||||
|
|
||||||
|
Note that the wait, loop and clear arguments must be specified using their keyword. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def scroll(text, delay=150, *, wait=True, loop=False, monospace=False):
|
||||||
|
""" Scrolls text horizontally on the display. If text is an integer or float it is first
|
||||||
|
converted to a string using str(). The delay parameter controls how fast the text is
|
||||||
|
scrolling.
|
||||||
|
|
||||||
|
If wait is True, this function will block until the animation is finished, otherwise the
|
||||||
|
animation will happen in the background.
|
||||||
|
|
||||||
|
If loop is True, the animation will repeat forever.
|
||||||
|
|
||||||
|
If monospace is True, the characters will all take up 5 pixel-columns in width, otherwise
|
||||||
|
there will be exactly 1 blank pixel-column between each character as they scroll.
|
||||||
|
|
||||||
|
Note that the wait, loop and monospace arguments must be specified using their keyword. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on():
|
||||||
|
""" Use on() to turn on the display. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def off():
|
||||||
|
""" Use off() to turn off the display (thus allowing you to re-use the GPIO pins associated
|
||||||
|
with the display for other purposes). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_on():
|
||||||
|
""" Returns True if the display is on, otherwise returns False. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def read_light_level():
|
||||||
|
""" Use the display’s LEDs in reverse-bias mode to sense the amount of light falling on
|
||||||
|
the display. Returns an integer between 0 and 255 representing the light level, with
|
||||||
|
larger meaning more light. """
|
||||||
|
pass
|
35
microbit/i2c.py
Normal file
35
microbit/i2c.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from microbit import pin19, pin20
|
||||||
|
#I²C
|
||||||
|
|
||||||
|
""" The i2c module lets you communicate with devices connected to your board using the I²C bus
|
||||||
|
protocol. There can be multiple slave devices connected at the same time, and each one has
|
||||||
|
its own unique address, that is either fixed for the device or configured on it. Your board
|
||||||
|
acts as the I²C master. """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def init(freq=100000, sda=pin20, scl=pin19):
|
||||||
|
""" Re-initialize peripheral with the specified clock frequency freq on the specified sda
|
||||||
|
and scl pins.
|
||||||
|
|
||||||
|
Warning
|
||||||
|
On a micro:bit V1 board, changing the I²C pins from defaults will make the accelerometer
|
||||||
|
and compass stop working, as they are connected internally to those pins. This warning
|
||||||
|
does not apply to the V2 revision of the micro:bit as this has separate I²C lines for the
|
||||||
|
motion sensors and the edge connector. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def scan():
|
||||||
|
""" Scan the bus for devices. Returns a list of 7-bit addresses corresponding to those
|
||||||
|
devices that responded to the scan. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def read(addr, n, repeat=False):
|
||||||
|
""" Read n bytes from the device with 7-bit address addr. If repeat is True, no stop bit
|
||||||
|
will be sent. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def write(addr, buf, repeat=False):
|
||||||
|
""" Write bytes from buf to the device with 7-bit address addr. If repeat is True, no stop
|
||||||
|
bit will be sent. """
|
||||||
|
pass
|
51
microbit/microphone.py
Normal file
51
microbit/microphone.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#Microphone V2
|
||||||
|
|
||||||
|
""" This object lets you access the built-in microphone available on the micro:bit V2. It can
|
||||||
|
be used to respond to sound. The microphone input is located on the front of the board
|
||||||
|
alongside a microphone activity LED, which is lit when the microphone is in use. """
|
||||||
|
|
||||||
|
|
||||||
|
#Sound events
|
||||||
|
|
||||||
|
""" The microphone can respond to a pre-defined set of sound events that are based on the
|
||||||
|
amplitude and wavelength of the sound. These sound events are represented by instances of
|
||||||
|
the SoundEvent class, accessible via variables in SoundEvent:
|
||||||
|
SoundEvent.QUIET: Represents the transition of sound events, from loud to quiet
|
||||||
|
like speaking or background music.
|
||||||
|
SoundEvent.LOUD: Represents the transition of sound events, from quiet to loud
|
||||||
|
like clapping or shouting. """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def current_event():
|
||||||
|
""" return: the name of the last recorded sound event, SoundEvent('loud') or
|
||||||
|
SoundEvent('quiet'). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def was_event(event):
|
||||||
|
""" event: a sound event, such as SoundEvent.LOUD or SoundEvent.QUIET.
|
||||||
|
return: true if sound was heard at least once since the last call,
|
||||||
|
otherwise false. was_event() also clears the sound event history before returning. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_event(event):
|
||||||
|
""" event: a sound event, such as SoundEvent.LOUD or SoundEvent.QUIET.
|
||||||
|
return: true if sound event is the most recent since the last call, otherwise false.
|
||||||
|
It does not clear the sound event history. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_events():
|
||||||
|
""" return: a tuple of the event history. The most recent is listed last. get_events()
|
||||||
|
also clears the sound event history before returning. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_threshold(event, value):
|
||||||
|
""" event: a sound event, such as SoundEvent.LOUD or SoundEvent.QUIET.
|
||||||
|
value: The threshold level in the range 0-255. For example,
|
||||||
|
set_threshold(SoundEvent.LOUD, 250) will only trigger if the sound is very
|
||||||
|
loud (>= 250). """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sound_level():
|
||||||
|
""" return: a representation of the sound pressure level in the range 0 to 255. """
|
||||||
|
pass
|
50
microbit/power.py
Normal file
50
microbit/power.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#Power Management V2
|
||||||
|
|
||||||
|
""" This module lets you manage the power modes of the micro:bit V2.
|
||||||
|
|
||||||
|
There are two micro:bit board low power modes that can be requested
|
||||||
|
from MicroPython:
|
||||||
|
|
||||||
|
Deep Sleep: Low power mode where the board can be woken up via
|
||||||
|
multiple sources (pins, button presses, uart data, or a timer)
|
||||||
|
and resume operation.
|
||||||
|
Off: The power mode with the lowest power consumption, the only
|
||||||
|
way to wake up the board is via the reset button, or by plugging
|
||||||
|
the USB cable while on battery power. When the board wakes up it
|
||||||
|
will restart and execute your programme from the beginning. """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def off():
|
||||||
|
""" Power down the board to the lowest possible power mode. This is
|
||||||
|
the equivalent to pressing the reset button for a few second,
|
||||||
|
to set the board in “Off mode”.
|
||||||
|
The micro:bit will only wake up if the reset button is pressed or,
|
||||||
|
if on battery power, when a USB cable is connected.
|
||||||
|
When the board wakes up it will start for a reset state, so your
|
||||||
|
programme will start running from the beginning. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def deep_sleep(ms=None, wake_on=None, run_every=True):
|
||||||
|
""" Set the micro:bit into a low power mode where it can wake up and
|
||||||
|
continue operation.
|
||||||
|
The programme state is preserved and when it wakes up it will resume
|
||||||
|
operation where it left off.
|
||||||
|
Deep Sleep mode will consume more battery power than Off mode.
|
||||||
|
The wake up sources are configured via arguments.
|
||||||
|
The board will always wake up when receiving UART data, when the reset
|
||||||
|
button is pressed (which resets the board) or, in battery power, when
|
||||||
|
the USB cable is inserted.
|
||||||
|
|
||||||
|
When the run_every parameter is set to True (the default), any function
|
||||||
|
scheduled with microbit.run_every will momentarily wake up the board to
|
||||||
|
run and when it finishes it will go back to sleep.
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
ms – A time in milliseconds to wait before it wakes up.
|
||||||
|
wake_on – A single instance or a tuple of pins and/or buttons
|
||||||
|
to wake up the board, e.g. deep_sleep(wake_on=button_a) or
|
||||||
|
deep_sleep(wake_on=(pin0, pin2, button_b)).
|
||||||
|
run_every – A boolean to configure if the functions scheduled
|
||||||
|
with microbit.run_every will continue to run while it sleeps. """
|
||||||
|
pass
|
19
microbit/speaker.py
Normal file
19
microbit/speaker.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#Speaker V2
|
||||||
|
|
||||||
|
""" The micro:bit V2 has a built-in speaker located on the rear of the board.
|
||||||
|
By default sound output will be via the edge connector on pin 0 and the built-in speaker V2.
|
||||||
|
You can connect wired headphones or a speaker to pin 0 and GND on the edge connector to hear
|
||||||
|
the sounds.
|
||||||
|
The speaker can be turned off or on using the functions listed here. """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def off():
|
||||||
|
""" Use off() to turn off the speaker. This does not disable sound output to an
|
||||||
|
edge connector pin. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on():
|
||||||
|
""" Use on() to turn on the speaker. """
|
||||||
|
pass
|
||||||
|
|
52
microbit/spi.py
Normal file
52
microbit/spi.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
from microbit import pin13, pin15, pin14
|
||||||
|
#SPI
|
||||||
|
|
||||||
|
""" The SPI module lets you talk to a device connected to your board using a serial
|
||||||
|
peripheral interface (SPI) bus. SPI uses a so-called master-slave architecture with
|
||||||
|
a single master. You will need to specify the connections for three signals:
|
||||||
|
|
||||||
|
SCLK : Serial Clock (output from master).
|
||||||
|
MOSI : Master Output, Slave Input (output from master).
|
||||||
|
MISO : Master Input, Slave Output (output from slave). """
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def init(baudrate=1000000, bits=8, mode=0, sclk=pin13, mosi=pin15, miso=pin14):
|
||||||
|
""" Initialize SPI communication with the specified parameters on the specified pins.
|
||||||
|
Note that for correct communication, the parameters have to be the same on both
|
||||||
|
communicating devices.
|
||||||
|
|
||||||
|
The baudrate defines the speed of communication.
|
||||||
|
|
||||||
|
The bits defines the size of bytes being transmitted. Currently only bits=8 is
|
||||||
|
supported. However, this may change in the future.
|
||||||
|
|
||||||
|
The mode determines the combination of clock polarity and phase according to the
|
||||||
|
following convention, with polarity as the high order bit and phase as the low
|
||||||
|
order bit:
|
||||||
|
SPI Mode Polarity (CPOL) Phase (CPHA)
|
||||||
|
0 0 0
|
||||||
|
1 0 1
|
||||||
|
2 1 0
|
||||||
|
3 1 1
|
||||||
|
|
||||||
|
Polarity (aka CPOL) 0 means that the clock is at logic value 0 when idle and goes high
|
||||||
|
(logic value 1) when active; polarity 1 means the clock is at logic value 1 when idle
|
||||||
|
and goes low (logic value 0) when active. Phase (aka CPHA) 0 means that data is sampled
|
||||||
|
on the leading edge of the clock, and 1 means on the trailing edge
|
||||||
|
(viz. https://en.wikipedia.org/wiki/Signal_edge).
|
||||||
|
|
||||||
|
The sclk, mosi and miso arguments specify the pins to use for each type of signal. """
|
||||||
|
pass
|
||||||
|
def read(nbytes):
|
||||||
|
""" Read at most nbytes. Returns what was read. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def write(buffer):
|
||||||
|
""" Write the buffer of bytes to the bus. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
def write_readinto(Out, In):
|
||||||
|
""" Write the out buffer to the bus and read any response into the in buffer.
|
||||||
|
The length of the buffers should be the same. The buffers can be the same object. """
|
||||||
|
pass
|
18
microbit/uart.py
Normal file
18
microbit/uart.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#UART
|
||||||
|
|
||||||
|
""" The uart module lets you talk to a device connected to your board
|
||||||
|
using a serial interface.
|
||||||
|
|
||||||
|
Warning
|
||||||
|
Initializing the UART on external pins will cause the Python console on USB to become
|
||||||
|
unaccessible, as it uses the same hardware. To bring the console back you must
|
||||||
|
reinitialize the UART without passing anything for tx or rx (or passing None to these
|
||||||
|
arguments). This means that calling uart.init(115200) is enough to restore the Python
|
||||||
|
console."""
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
|
||||||
|
def init(baudrate=9600, bits=8, parity=None, stop=1, *, tx=None, rx=None):
|
||||||
|
""" Initialize serial communication with the specified parameters on the specified
|
||||||
|
tx and rx pins. Note that for correct communication, the parameters have to be
|
||||||
|
the same on both communicating devices. """
|
Reference in New Issue
Block a user