Skip to content

Commit

Permalink
docs: Add MicroPython 1.0 features (#578)
Browse files Browse the repository at this point in the history
- Update warnings and notes about the v1.0 release
- uart.readall() removed and uart.read() recommended instead
- Add micropython.rst based on upstream docs
- Add utime.rst based on upstream docs
- Add machine.rst based on upstream docs
- Ensure all docs are present in index.rst
- Add get_pull(), get_mode() to pin.rst
- Add note about help('modules')
  • Loading branch information
microbit-rosslowe authored and microbit-carlos committed Dec 12, 2018
1 parent bb0903e commit afcd63a
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ Projects related to MicroPython on the BBC micro:bit include:
microbit_micropython_api.rst
microbit.rst
accelerometer.rst
audio.rst
ble.rst
button.rst
compass.rst
display.rst
filesystem.rst
i2c.rst
image.rst
machine.rst
micropython.rst
music.rst
neopixel.rst
os.rst
Expand All @@ -77,6 +80,7 @@ Projects related to MicroPython on the BBC micro:bit include:
speech.rst
spi.rst
uart.rst
utime.rst

.. toctree::
:maxdepth: 2
Expand Down
99 changes: 99 additions & 0 deletions docs/machine.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
..
MicroPython license information
===============================
The MIT License (MIT)

Copyright (c) 2013-2017 Damien P. George, and others

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


Machine
*******

.. py:module:: machine
The machine module contains specific functions related to the micro:bit
hardware. Most functions in this module allow to achieve direct and
unrestricted access to and control of hardware blocks on a system (like CPU,
timers, buses, etc.). Used incorrectly, this can lead to malfunction, lockups,
crashes of your board, and in extreme cases, hardware damage.


Functions
=========

.. method:: machine.unique_id()

Returns a byte string with a unique identifier of a board. It will vary
from one board instance to another.


.. method:: machine.reset()

Resets the device in a manner similar to pushing the external RESET button.


.. method:: machine.freq()

Returns CPU frequency in hertz.


.. method:: machine.disable_irq()

Disable interrupt requests. Returns the previous IRQ state which should be
considered an opaque value. This return value should be passed to the
:func:`machine.enable_irq()` function to restore interrupts to their
original state, before :func:`machine.disable_irq()` was called.


.. method:: machine.enable_irq()

Re-enable interrupt requests. The *state* parameter should be the value
that was returned from the most recent call to the
:func:`machine.disable_irq()` function.


.. method:: machine.time_pulse_us(pin, pulse_level, timeout_us=1000000)

Time a pulse on the given *pin*, and return the duration of the pulse in
microseconds. The *pulse_level* argument should be 0 to time a low pulse or
1 to time a high pulse.

If the current input value of the pin is different to *pulse_level*, the
function first (*) waits until the pin input becomes equal to
*pulse_level*, then (**) times the duration that the pin is equal to
*pulse_level*. If the pin is already equal to *pulse_level* then timing
starts straight away.

The function will return -2 if there was timeout waiting for condition
marked (*) above, and -1 if there was timeout during the main measurement,
marked (**) above. The timeout is the same for both cases and given by
*timeout_us* (which is in microseconds).


Reading Memory
==============

The ``machine`` module allows you to read from the device's memory, getting 1
byte (8 bits; ``mem8``), 2 byte (16 bits; ``mem16``), or 4 byte (32 bits;
``mem32``) words from physical addresses. For example: ``mem8[0x00]`` reads 1
byte on physical address ``0x00``. This has a number of uses, for example if
you'd like to read data from the nRF51 registers.
8 changes: 4 additions & 4 deletions docs/microbit_micropython_api.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
micro:bit Micropython API
*************************

.. warning::
As we work towards a 1.0 release, this API is subject to frequent changes. This page reflects the current micro:bit API in a developer-friendly (but not necessarily kid-friendly) way. The tutorials associated with this documentation are a good place to start for non-developers looking for information.

The microbit module
===================

Expand All @@ -30,6 +27,9 @@ The rest of the functionality is provided by objects and classes in the microbit

Note that the API exposes integers only (ie no floats are needed, but they may be accepted). We thus use milliseconds for the standard time unit.

.. note::
You can see a list of all available modules by writing ``help('modules')`` in the REPL.

Buttons
-------

Expand Down Expand Up @@ -279,7 +279,7 @@ Use ``uart`` to communicate with a serial device connected to the device's I/O p
# return (read) n incoming characters.
uart.read(n)
# return (read) as much incoming data as possible.
uart.readall()
uart.read()
# return (read) all the characters to a newline character is reached.
uart.readline()
# read bytes into the referenced buffer.
Expand Down
117 changes: 117 additions & 0 deletions docs/micropython.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
..
MicroPython license information
===============================
The MIT License (MIT)

Copyright (c) 2013-2017 Damien P. George, and others

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


MicroPython
***********

.. py:module:: micropython
Access and control MicroPython internals.

Functions
=========

.. py:function:: micropython.const(expr)
Used to declare that the expression is a constant so that the compiler can
optimise it. The use of this function should be as follows:

.. code-block:: python
from micropython import const
CONST_X = const(123)
CONST_Y = const(2 * CONST_X + 1)
Constants declared this way are still accessible as global variables from
outside the module they are declared in. On the other hand, if a constant
begins with an underscore then it is hidden, it is not available as a
global variable, and does not take up any memory during execution.


.. py:function:: micropython.opt_level([level])
If level is given then this function sets the optimisation level for
subsequent compilation of scripts, and returns None. Otherwise it returns
the current optimisation level.

The optimisation level controls the following compilation features:

* Assertions: at level 0 assertion statements are enabled and compiled
into the bytecode; at levels 1 and higher assertions are not compiled.

* Built-in ``__debug__`` variable: at level 0 this variable expands to
True; at levels 1 and higher it expands to False.

* Source-code line numbers: at levels 0, 1 and 2 source-code line number
are stored along with the bytecode so that exceptions can report the
line number they occurred at; at levels 3 and higher line numbers are
not stored.

The default optimisation level is usually level 0.


.. py:function:: micropython.mem_info([verbose])
Print information about currently used memory. If the verbose argument is
given then extra information is printed.


.. py:function:: micropython.qstr_info([verbose])
Print information about currently interned strings. If the verbose argument
is given then extra information is printed.

This includes the number of interned strings and the amount of RAM they
use. In verbose mode it prints out the names of all RAM-interned strings.


.. py:function:: micropython.stack_use()
Return an integer representing the current amount of stack that is being
used. The absolute value of this is not particularly useful, rather it
should be used to compute differences in stack usage at different points.


.. py:function:: micropython.heap_lock()
.. py:function:: micropython.heap_unlock()
Lock or unlock the heap. When locked no memory allocation can occur and a
``MemoryError`` will be raised if any heap allocation is attempted.


.. py:function:: micropython.kbd_intr(chr)
Set the character that will raise a KeyboardInterrupt exception. By default
this is set to 3 during script execution, corresponding to Ctrl-C. Passing
-1 to this function will disable capture of Ctrl-C, and passing 3 will
restore it.

This function can be used to prevent the capturing of Ctrl-C on the
incoming stream of characters that is usually used for the REPL, in case
that stream is used for other purposes
26 changes: 26 additions & 0 deletions docs/pin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ its own to that.
``pin.PULL_DOWN`` or ``pin.NO_PULL`` (where ``pin`` is an instance of
a pin). See below for discussion of default pull states.


.. py:method::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.

.. py:method::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: ``MODE_UNUSED``,
``MODE_WRITE_ANALOG``, ``MODE_READ_DIGITAL``, ``MODE_WRITE_DIGITAL``,
``MODE_DISPLAY``, ``MODE_BUTTON``, ``MODE_MUSIC``, ``MODE_AUDIO_PLAY``,
``MODE_TOUCH``, ``MODE_I2C``, ``MODE_SPI``.


.. py:class:: MicroBitAnalogDigitalPin
.. py:method:: read_analog()
Read the voltage applied to the pin, and return it as an integer
between 0 (meaning 0V) and 1023 (meaning 3.3V).


.. py:method:: write_analog(value)
Output a PWM signal on the pin, with the duty cycle proportional to
Expand Down
5 changes: 2 additions & 3 deletions docs/speech.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ Speech

.. warning::

WARNING! THIS IS ALPHA CODE.

We reserve the right to change this API as development continues.
WARNING! This is still work in progress; we reserve the right to change this API as development continues.

The quality of the speech is not great, merely "good enough". Given the
constraints of the device you may encounter memory errors and / or
unexpected extra sounds during playback. It's early days and we're
improving the code for the speech synthesiser all the time. Bug reports
and pull requests are most welcome.


.. py:module:: speech
This module makes microbit talk, sing and make other speech like sounds
Expand Down

0 comments on commit afcd63a

Please sign in to comment.