2014-05-20 15:39:58 +01:00
|
|
|
#######
|
|
|
|
|
Example
|
|
|
|
|
#######
|
2014-08-05 10:58:16 +01:00
|
|
|
Here are some examples of how to use mraa, common convention is to import mraa
|
|
|
|
|
as mraa to keep it short but feel free to import it globally! As a general rule
|
|
|
|
|
the API is very similar to the C++ API so there are only basic examples to show
|
|
|
|
|
quick usage. The mraa module can be built with python3 or python2. All examples
|
|
|
|
|
may not run on python3 as this is not tested.
|
2014-05-20 15:39:58 +01:00
|
|
|
|
|
|
|
|
Hello GPIO
|
|
|
|
|
==========
|
|
|
|
|
|
2014-07-31 10:25:24 +02:00
|
|
|
Here is the simplest Gpio program in mraa.
|
2014-05-20 15:39:58 +01:00
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
.. literalinclude:: ../../../../examples/python/gpio.py
|
2014-07-31 10:25:24 +02:00
|
|
|
:prepend: import mraa
|
|
|
|
|
:start-after: import mraa
|
2014-05-20 15:39:58 +01:00
|
|
|
|
2016-04-06 13:44:06 +03:00
|
|
|
GPIO Interrupt (isr)
|
2017-07-28 10:48:48 -03:00
|
|
|
====================
|
2014-05-20 15:39:58 +01:00
|
|
|
|
2016-04-06 13:44:06 +03:00
|
|
|
The GPIO module allows you to set an interrupt on a GPIO. This interrupt is
|
2014-10-03 10:18:13 +01:00
|
|
|
controlled by the mode that the 'edge' is in. Before setting another isr please
|
|
|
|
|
remove the first one, multiple isrs on one pin are not supported. Some
|
2016-04-06 13:44:06 +03:00
|
|
|
platforms will not support interrupts on all pins so please check your return
|
2014-10-03 10:18:13 +01:00
|
|
|
values.
|
2014-05-20 15:39:58 +01:00
|
|
|
|
|
|
|
|
**Note:** Galileo Gen1 only supports EDGE_BOTH
|
|
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
.. literalinclude:: ../../../../examples/python/gpio_advanced.py
|
2014-07-31 10:25:24 +02:00
|
|
|
:prepend: import mraa
|
|
|
|
|
:start-after: import mraa
|
2014-05-20 15:39:58 +01:00
|
|
|
|
2014-09-19 01:06:59 +01:00
|
|
|
**Note:** If the python script is ended the destructors will run meaning that
|
|
|
|
|
the ISR will not run. The sleep call is there for that function.
|
|
|
|
|
|
2014-10-03 10:18:13 +01:00
|
|
|
**Note:** The python isr module treats only objects. This means that int
|
|
|
|
|
counters will not work inside your isr. Please use the different edge modes.
|
|
|
|
|
|
2014-08-05 10:58:16 +01:00
|
|
|
I2c
|
|
|
|
|
===
|
|
|
|
|
|
|
|
|
|
The I2c module module has a number of different ways of interacting with the
|
|
|
|
|
i2c bus, including a number of overloaded read() calls and the writeReg()
|
|
|
|
|
helper function.
|
|
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
.. literalinclude:: ../../../../examples/python/i2c_bmp85.py
|
2015-04-03 19:41:52 +01:00
|
|
|
:prepend: x = m.I2c(0)
|
|
|
|
|
:start-after: x = m.I2c(0)
|
2014-08-05 10:58:16 +01:00
|
|
|
|
2017-07-28 10:48:48 -03:00
|
|
|
.. literalinclude:: ../../../../docs/i2c.txt
|
2014-09-07 21:20:06 +01:00
|
|
|
|
2014-08-05 10:58:16 +01:00
|
|
|
Pwm
|
|
|
|
|
===
|
|
|
|
|
|
|
|
|
|
The PWM module is rather simple, note that different hardware support PWM
|
|
|
|
|
generation is various different ways so results may vary.
|
|
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
.. literalinclude:: ../../../../examples/python/pwm.py
|
2014-08-05 10:58:16 +01:00
|
|
|
:prepend: import mraa
|
|
|
|
|
:start-after: import mraa
|
|
|
|
|
|
|
|
|
|
Aio
|
|
|
|
|
===
|
|
|
|
|
|
|
|
|
|
The ADC is typically provided on a dedicated or shared SPI bus, this is
|
|
|
|
|
abstracted by the Linux kernel as spidev and abstracted again by mraa. It is
|
|
|
|
|
fairly simple in use.
|
|
|
|
|
|
2017-07-28 10:48:48 -03:00
|
|
|
.. literalinclude:: ../../../../examples/python/aio.py
|
2014-08-05 10:58:16 +01:00
|
|
|
:prepend: import mraa
|
|
|
|
|
:start-after: import mraa
|
|
|
|
|
|
2015-12-13 15:57:27 +01:00
|
|
|
Uart
|
|
|
|
|
====
|
|
|
|
|
|
|
|
|
|
Uart is the Universal asynchronous receiver/transmitter interface in mraa.
|
|
|
|
|
It allows the exposure of UART pins on supported boards, with basic
|
|
|
|
|
configuration operations supported.
|
|
|
|
|
|
|
|
|
|
Here's a simple pair of programs comprising a sender and receiver pair.
|
|
|
|
|
|
|
|
|
|
Sender:
|
|
|
|
|
|
2017-07-28 10:48:48 -03:00
|
|
|
.. literalinclude:: ../../../../examples/python/uart_sender.py
|
2015-12-13 15:57:27 +01:00
|
|
|
:prepend: import mraa
|
|
|
|
|
:start-after: import mraa
|
|
|
|
|
|
|
|
|
|
Receiver:
|
|
|
|
|
|
2017-07-28 10:48:48 -03:00
|
|
|
.. literalinclude:: ../../../../examples/python/uart_receiver.py
|
2015-12-13 15:57:27 +01:00
|
|
|
:prepend: import mraa
|
|
|
|
|
:start-after: import mraa
|
2018-01-30 11:32:18 +05:30
|
|
|
|
|
|
|
|
LED
|
|
|
|
|
===
|
|
|
|
|
|
|
|
|
|
LED module is used for controlling the on-board LEDs. With the
|
|
|
|
|
help of this module, we can control the brightness, trigger etc...
|
|
|
|
|
|
|
|
|
|
.. literalinclude:: ../../../../examples/python/led.py
|
|
|
|
|
:prepend: import mraa
|
|
|
|
|
:start-after: import mraa
|