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