From a67989aa201817efaaefebdc7fd491358da45df8 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Mon, 21 Nov 2022 14:10:02 +0100 Subject: 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 --- docs/develop/cmodules.rst | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'docs') 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 `_ 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 -- cgit v1.2.3