b974a28d5e4a0422e4e296691c3272a19d8114a2
[contrib/mraa.git] / src / python / docs / example.rst
1 #######
2 Example
3 #######
4 Here are some examples of how to use mraa, common convention is to import mraa
5 as mraa to keep it short but feel free to import it globally! As a general rule
6 the API is very similar to the C++ API so there are only basic examples to show
7 quick usage. The mraa module can be built with python3 or python2. All examples
8 may not run on python3 as this is not tested.
9
10 Hello GPIO
11 ==========
12
13 Here 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
19 GPIO Interupt (isr)
20 ===================
21
22 The GPIO module allows you to set an interupt on a GPIO. This interupt is
23 controlled by the mode that the 'edge' is in. Before setting another isr please
24 remove the first one, multiple isrs on one pin are not supported. Some
25 platforms will not support interupts on all pins so please check your return
26 values.
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
35 the 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
38 counters will not work inside your isr. Please use the different edge modes.
39
40 I2c
41 ===
42
43 The I2c module module has a number of different ways of interacting with the
44 i2c bus, including a number of overloaded read() calls and the writeReg()
45 helper 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
53 Pwm
54 ===
55
56 The PWM module is rather simple, note that different hardware support PWM
57 generation 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
63 Aio
64 ===
65
66 The ADC is typically provided on a dedicated or shared SPI bus, this is
67 abstracted by the Linux kernel as spidev and abstracted again by mraa. It is
68 fairly simple in use.
69
70 .. literalinclude:: ../../../examples/python/aio.py
71   :prepend: import mraa
72   :start-after: import mraa
73