diff options
author | Laurens Valk <laurens@pybricks.com> | 2022-11-21 14:10:02 +0100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2022-11-23 11:46:17 +1100 |
commit | a67989aa201817efaaefebdc7fd491358da45df8 (patch) | |
tree | 52da627e96de13906b4641f68054152f58f28d3a /docs/develop/cmodules.rst | |
parent | 1d27c7d423f4e7b2c1f2e9b68d99a33ac26d81eb (diff) |
examples/usercmodule: Add example of a native C class.
This shows how ports can add their own custom types/classes.
It is part of the unix coverage build, so we can use it for tests too.
Signed-off-by: Laurens Valk <laurens@pybricks.com>
Diffstat (limited to 'docs/develop/cmodules.rst')
-rw-r--r-- | docs/develop/cmodules.rst | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index 1b3ba04da..6bc2f62ab 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -95,9 +95,12 @@ A MicroPython user C module is a directory with the following files: Basic example ------------- -This simple module named ``cexample`` provides a single function -``cexample.add_ints(a, b)`` which adds the two integer args together and returns -the result. It can be found in the MicroPython source tree +The ``cexample`` module provides examples for a function and a class. The +``cexample.add_ints(a, b)`` function adds two integer args together and returns +the result. The ``cexample.Timer()`` type creates timers that can be used to +measure the elapsed time since the object is instantiated. + +The module can be found in the MicroPython source tree `in the examples directory <https://github.com/micropython/micropython/tree/master/examples/usercmodule/cexample>`_ and has a source file and a Makefile fragment with content as described above:: @@ -272,3 +275,13 @@ can now be accessed in Python just like any other builtin module, e.g. import cexample print(cexample.add_ints(1, 3)) # should display 4 + +.. code-block:: python + + from cexample import Timer + from time import sleep_ms + + watch = Timer() + sleep_ms(1000) + print(watch.time()) + # should display approximately 1000 |