1#######
2Example
3#######
4Here are some examples of how to use mraa, common convention is to import mraa
5as mraa to keep it short but feel free to import it globally! As a general rule
6the API is very similar to the C++ API so there are only basic examples to show
7quick usage. The mraa module can be built with python3 or python2. All examples
8may not run on python3 as this is not tested.
9
10Hello GPIO
11==========
12
13Here is the simplest Gpio program in mraa.
14
15.. literalinclude:: ../../../examples/python/hello_gpio.py
16  :prepend: import mraa
17  :start-after: import mraa
18
19GPIO Interupt (isr)
20===================
21
22The GPIO module allows you to set an interupt on a GPIO. This interupt is
23controlled by the mode that the 'edge' is in. Before setting another isr please
24remove the first one, multiple isrs on one pin are not supported. Some
25platforms will not support interupts on all pins so please check your return
26values.
27
28**Note:** Galileo Gen1 only supports EDGE_BOTH
29
30.. literalinclude:: ../../../examples/python/hello_isr.py
31  :prepend: import mraa
32  :start-after: import mraa
33
34**Note:** If the python script is ended the destructors will run meaning that
35the ISR will not run. The sleep call is there for that function.
36
37**Note:** The python isr module treats only objects. This means that int
38counters will not work inside your isr. Please use the different edge modes.
39
40I2c
41===
42
43The I2c module module has a number of different ways of interacting with the
44i2c bus, including a number of overloaded read() calls and the writeReg()
45helper function.
46
47.. literalinclude:: ../../../examples/python/bmp85.py
48  :prepend: x = m.I2c(0)
49  :start-after: x = m.I2c(0)
50
51.. literalinclude:: ../../../docs/i2c.txt
52
53Pwm
54===
55
56The PWM module is rather simple, note that different hardware support PWM
57generation is various different ways so results may vary.
58
59.. literalinclude:: ../../../examples/python/cycle-pwm3.py
60  :prepend: import mraa
61  :start-after: import mraa
62
63Aio
64===
65
66The ADC is typically provided on a dedicated or shared SPI bus, this is
67abstracted by the Linux kernel as spidev and abstracted again by mraa. It is
68fairly simple in use.
69
70.. literalinclude:: ../../../examples/python/aio.py
71  :prepend: import mraa
72  :start-after: import mraa
73
74Uart
75====
76
77Uart is the Universal asynchronous receiver/transmitter interface in mraa.
78It allows the exposure of UART pins on supported boards, with basic
79configuration operations supported.
80
81Here's a simple pair of programs comprising a sender and receiver pair.
82
83Sender:
84
85.. literalinclude:: ../../../examples/python/uart_sender.py
86  :prepend: import mraa
87  :start-after: import mraa
88
89Receiver:
90
91.. literalinclude:: ../../../examples/python/uart_receiver.py
92  :prepend: import mraa
93  :start-after: import mraa
94