8000 docs: Add microbit.scale(value, from_, to) function. (#762) · bbcmicrobit/micropython@1435b6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1435b6a

Browse files
docs: Add microbit.scale(value, from_, to) function. (#762)
1 parent 174a86f commit 1435b6a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/microbit.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ Functions
3131
:returns: The number of milliseconds since the board was switched on or
3232
restarted.
3333

34+
.. py:function:: scale(value, from_, to)
35+
36+
Converts a value from a range to another range.
37+
38+
For example, to convert 30 degrees from Celsius to Fahrenheit::
39+
40+
temp_fahrenheit = scale(30, from_=(0, 100), to=(32, 212))
41+
42+
This can be useful to convert values between inputs and outputs,
43+
for example an accelerometer x value to a speaker volume.
44+
45+
Negative scaling is also supported, for example
46+
``scale(25, from_=(0, 100), to=(0, -200))`` will return ``-50``.
47+
48+
:param value: A number to convert.
49+
:param from_: A tuple to define the range to convert from.
50+
:param to: A tuple to define the range to convert to.
51+
52+
:returns: The ``value`` converted to the ``to`` range.
3453

3554
.. py:function:: sleep(n)
3655

docs/microbit_micropython_api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ There are a few functions available directly::
1616
sleep(ms)
1717
# returns the number of milliseconds since the micro:bit was last switched on.
1818
running_time()
19+
# returns the temperature in Celcius
20+
temperature()
21+
# converts a number from one scale of values to another
22+
scale(value_to_convert, from_=(min_value, max_value), to=(min_value, max_value))
1923
# makes the micro:bit enter panic mode (this usually happens when the DAL runs
2024
# out of memory, and causes a sad face to be drawn on the display). The error
2125
# code can be any arbitrary integer value.

0 commit comments

Comments
 (0)
0