Skip to content

Commit c748e12

Browse files
Docs updates. (#72)
Scale docs are now duplicated due to overloads. This will result in a translation key change but hopefully we can restore any translations. Co-authored-by: Robert Knight <robert@microbit.org>
1 parent 905935e commit c748e12

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

lang/en/typeshed/stdlib/microbit/__init__.pyi

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Pins, images, sounds, temperature and volume.
22
"""
33

4-
from typing import Any, Callable, List, Optional, Tuple, overload
4+
from typing import Any, Callable, List, Optional, Tuple, Union, overload
55

66
from _typeshed import ReadableBuffer
77

@@ -69,14 +69,41 @@ def panic(n: int) -> None:
6969
def reset() -> None:
7070
"""Restart the board."""
7171

72+
73+
@overload
74+
def scale(value: float, from_: Tuple[float, float], to: Tuple[int, int]) -> int:
75+
"""Converts a value from a range to an integer range.
76+
77+
Example: ``volume = scale(accelerometer.get_x(), from_=(-2000, 2000), to=(0, 255))``
78+
79+
For example, to convert an accelerometer X value to a speaker volume.
80+
81+
If one of the numbers in the ``to`` parameter is a floating point
82+
(i.e a decimal number like ``10.0``), this function will return a
83+
floating point number.
84+
85+
temp_fahrenheit = scale(30, from_=(0.0, 100.0), to=(32.0, 212.0))
86+
87+
:param value: A number to convert.
88+
:param from_: A tuple to define the range to convert from.
89+
:param to: A tuple to define the range to convert to.
90+
:return: The ``value`` converted to the ``to`` range.
91+
"""
92+
93+
@overload
7294
def scale(value: float, from_: Tuple[float, float], to: Tuple[float, float]) -> float:
73-
"""Converts a value from a range to another range.
95+
"""Converts a value from a range to a floating point range.
96+
97+
Example: ``temp_fahrenheit = scale(30, from_=(0.0, 100.0), to=(32.0, 212.0))``
7498
75-
Example: ``temp_fahrenheit = scale(30, from_=(0, 100), to=(32, 212))``
99+
For example, to convert temperature from a Celsius scale to Fahrenheit.
76100
77-
This can be useful to convert values between inputs and outputs, for example an accelerometer X value to a speaker volume.
101+
If one of the numbers in the ``to`` parameter is a floating point
102+
(i.e a decimal number like ``10.0``), this function will return a
103+
floating point number.
104+
If they are both integers (i.e ``10``), it will return an integer::
78105
79-
Negative scaling is also supported, for example ``scale(25, from_=(0, 100), to=(0, -200))`` will return ``-50``.
106+
returns_int = scale(accelerometer.get_x(), from_=(-2000, 2000), to=(0, 255))
80107
81108
:param value: A number to convert.
82109
:param from_: A tuple to define the range to convert from.

lang/en/typeshed/stdlib/music.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Create and play melodies.
22
"""
3-
from typing import Tuple, Union, List
3+
from typing import Optional, Tuple, Union, List
44

55
from .microbit import MicroBitDigitalPin, pin0
66

@@ -106,7 +106,7 @@ def get_tempo() -> Tuple[int, int]:
106106

107107
def play(
108108
music: Union[str, List[str], Tuple[str, ...]],
109-
pin: Union[MicroBitDigitalPin, None] = pin0,
109+
pin: Optional[MicroBitDigitalPin] = pin0,
110110
wait: bool = True,
111111
loop: bool = False,
112112
) -> None:
@@ -126,7 +126,7 @@ def play(
126126
def pitch(
127127
frequency: int,
128128
duration: int = -1,
129-
pin: MicroBitDigitalPin = pin0,
129+
pin: Optional[MicroBitDigitalPin] = pin0,
130130
wait: bool = True,
131131
) -> None:
132132
"""Play a note.
@@ -145,7 +145,7 @@ def pitch(
145145
"""
146146
...
147147

148-
def stop(pin: MicroBitDigitalPin = pin0) -> None:
148+
def stop(pin: Optional[MicroBitDigitalPin] = pin0) -> None:
149149
"""Stops all music playback on the built-in speaker and any pin outputting sound.
150150
151151
Example: ``music.stop()``

lang/en/typeshed/stdlib/power.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ def deep_sleep(
3131
3232
Example: ``power.deep_sleep(wake_on=(button_a, button_b))``
3333
34-
The program state is preserved and when it wakes up it will resume operation where it left off.
34+
The program state is preserved and when it wakes up it will resume
35+
operation where it left off.
3536
3637
Deep Sleep mode will consume more battery power than Off mode.
3738
3839
The wake up sources are configured via arguments.
3940
40-
If no wake up sources have been configured it will sleep until the reset button is pressed (which resets the Target MCU) or, in battery power, when the USB cable is inserted.
41+
The board will always wake up when receiving UART data, when the reset
42+
button is pressed (which resets the board) or, in battery power,
43+
when the USB cable is inserted.
4144
42-
When the ``run_every`` parameter is set to ``True`` (the default), any function scheduled with ``microbit.run_every`` will still run while the board sleeps. When the scheduled time is reached the micro:bit will momentarily wake up to run the scheduled function and then automatically go back to sleep.
45+
When the ``run_every`` parameter is set to ``True`` (the default), any
46+
function scheduled with ``run_every`` will momentarily wake up the board
47+
to run and when it finishes it will go back to sleep.
4348
4449
:param ms: A time in milliseconds to wait before it wakes up.
4550
:param 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))``.

0 commit comments

Comments
 (0)