summaryrefslogtreecommitdiff
path: root/peripheral/libmraa/src/python/docs/example.rst
blob: 4daf98c72b22a5807b44d18b9dedfe942b5d7302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#######
Example
#######
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.

Hello GPIO
==========

Here is the simplest Gpio program in mraa.

.. literalinclude:: ../../../examples/python/hello_gpio.py
  :prepend: import mraa
  :start-after: import mraa

GPIO Interupt (isr)
===================

The GPIO module allows you to set an interupt on a GPIO. This interupt is
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
platforms will not support interupts on all pins so please check your return
values.

**Note:** Galileo Gen1 only supports EDGE_BOTH

.. literalinclude:: ../../../examples/python/hello_isr.py
  :prepend: import mraa
  :start-after: import mraa

**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.

**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.

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.

.. literalinclude:: ../../../examples/python/bmp85.py
  :prepend: x = m.I2c(0)
  :start-after: x = m.I2c(0)

.. literalinclude:: ../../../docs/i2c.txt

Pwm
===

The PWM module is rather simple, note that different hardware support PWM
generation is various different ways so results may vary.

.. literalinclude:: ../../../examples/python/cycle-pwm3.py
  :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.

.. literalinclude:: ../../../examples/python/aio.py
  :prepend: import mraa
  :start-after: import mraa

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:

.. literalinclude:: ../../../examples/python/uart_sender.py
  :prepend: import mraa
  :start-after: import mraa

Receiver:

.. literalinclude:: ../../../examples/python/uart_receiver.py
  :prepend: import mraa
  :start-after: import mraa