blob: ef61583b3e1d4ea771b1b256ae413cdd05d2f509 [file] [log] [blame]
Mark Liffitona5503762018-04-28 18:21:16 -05001Note
2~~~~
3
4This is an unofficial port of Gordon's WiringPi library. Please do not
5email Gordon if you have issues, he will not be able to help.
6
7For support, comments, questions, etc please join the WiringPi Discord
8channel: https://discord.gg/SM4WUVG
9
10WiringPi for Python
11===================
12
13WiringPi: An implementation of most of the Arduino Wiring functions for
14the Raspberry Pi.
15
16WiringPi implements new functions for managing IO expanders.
17
18Quick Install
19=============
20
21``pip install wiringpi``
22
23Usage
24=====
25
26.. code:: python
27
28 import wiringpi
29
30 # One of the following MUST be called before using IO functions:
31 wiringpi.wiringPiSetup() # For sequential pin numbering
32 # OR
33 wiringpi.wiringPiSetupSys() # For /sys/class/gpio with GPIO pin numbering
34 # OR
35 wiringpi.wiringPiSetupGpio() # For GPIO pin numbering
36
37**General IO:**
38
39.. code:: python
40
41 wiringpi.pinMode(6, 1) # Set pin 6 to 1 ( OUTPUT )
42 wiringpi.digitalWrite(6, 1) # Write 1 ( HIGH ) to pin 6
43 wiringpi.digitalRead(6) # Read pin 6
44
45**Setting up a peripheral:**
46
47WiringPi supports expanding your range of available "pins" by setting up
48a port expander. The implementation details of your port expander will
49be handled transparently, and you can write to the additional pins
50(starting from PIN\_OFFSET >= 64) as if they were normal pins on the Pi.
51
52.. code:: python
53
54 wiringpi.mcp23017Setup(PIN_OFFSET, I2C_ADDR)
55
56This example was tested on a quick2wire board with one digital IO
57expansion board connected via I2C:
58
59.. code:: python
60
61 wiringpi.mcp23017Setup(65, 0x20)
62 wiringpi.pinMode(65, 1)
63 wiringpi.digitalWrite(65, 1)
64
65**Soft Tone:**
66
67Hook a speaker up to your Pi and generate music with softTone. Also
68useful for generating frequencies for other uses such as modulating A/C.
69
70.. code:: python
71
72 wiringpi.softToneCreate(PIN)
73 wiringpi.softToneWrite(PIN, FREQUENCY)
74
75**Bit shifting:**
76
77.. code:: python
78
79 wiringpi.shiftOut(1, 2, 0, 123) # Shift out 123 (b1110110, byte 0-255) to data pin 1, clock pin 2
80
81**Serial:**
82
83.. code:: python
84
85 serial = wiringpi.serialOpen('/dev/ttyAMA0', 9600) # Requires device/baud and returns an ID
86 wiringpi.serialPuts(serial, "hello")
87 wiringpi.serialClose(serial) # Pass in ID
88
89**SPI:**
90
91The ``wiringPiSPIDataRW()`` function needs to be passed a ``bytes``
92object in Python 3. In Python 2, it takes a string. The following should
93work in either Python 2 or 3:
94
95.. code:: python
96
97 wiringpi.wiringPiSPISetup(channel, speed)
98 buf = bytes([your data here])
99 retlen, retdata = wiringpi.wiringPiSPIDataRW(0, buf)
100
101Now, ``retlen`` will contain the number of bytes received/read by the
102call. ``retdata`` will contain the data itself, and in Python 3, ``buf``
103will have been modified to contain it as well (that won't happen in
104Python 2, because then ``buf`` is a string, and strings are immutable).
105
106**Full details of the API at:** http://www.wiringpi.com
107
108Manual Build
109============
110
111Get/setup repo
112--------------
113
114.. code:: bash
115
116 git clone --recursive https://github.com/WiringPi/WiringPi-Python.git
117 cd WiringPi-Python
118
119Don't forget the ``--recursive``; it is required to also pull in the
120WiringPi C code from its own repository.
121
122Prerequisites
123-------------
124
125To rebuild the bindings you **must** first have installed ``swig``,
126``python-dev``, and ``python-setuptools`` (or their ``python3-``
127equivalents). WiringPi should also be installed system-wide for access
128to the ``gpio`` tool.
129
130.. code:: bash
131
132 sudo apt-get install python-dev python-setuptools swig wiringpi
133
134Build & install with
135--------------------
136
137``sudo python setup.py install``
138
139Or Python 3:
140
141``sudo python3 setup.py install``
142