Mark Liffiton | a550376 | 2018-04-28 18:21:16 -0500 | [diff] [blame] | 1 | Note |
| 2 | ~~~~ |
| 3 | |
| 4 | This is an unofficial port of Gordon's WiringPi library. Please do not |
| 5 | email Gordon if you have issues, he will not be able to help. |
| 6 | |
| 7 | For support, comments, questions, etc please join the WiringPi Discord |
| 8 | channel: https://discord.gg/SM4WUVG |
| 9 | |
| 10 | WiringPi for Python |
| 11 | =================== |
| 12 | |
| 13 | WiringPi: An implementation of most of the Arduino Wiring functions for |
| 14 | the Raspberry Pi. |
| 15 | |
| 16 | WiringPi implements new functions for managing IO expanders. |
| 17 | |
| 18 | Quick Install |
| 19 | ============= |
| 20 | |
Yang Deokgyu | a21ab60 | 2019-09-04 11:33:12 +0900 | [diff] [blame] | 21 | .. image:: https://badge.fury.io/py/odroid-wiringpi.svg |
| 22 | :alt: PyPI version badge |
| 23 | :target: https://badge.fury.io/py/odroid-wiringpi |
Mark Liffiton | 5b7ef30 | 2018-07-02 16:02:24 -0500 | [diff] [blame] | 24 | |
| 25 | The library is packaged on PyPI and can be installed with pip: |
| 26 | |
Yang Deokgyu | a21ab60 | 2019-09-04 11:33:12 +0900 | [diff] [blame] | 27 | ``pip install odroid-wiringpi`` |
Mark Liffiton | a550376 | 2018-04-28 18:21:16 -0500 | [diff] [blame] | 28 | |
| 29 | Usage |
| 30 | ===== |
| 31 | |
| 32 | .. code:: python |
| 33 | |
Yang Deokgyu | a21ab60 | 2019-09-04 11:33:12 +0900 | [diff] [blame] | 34 | import odroid_wiringpi as wiringpi |
Mark Liffiton | a550376 | 2018-04-28 18:21:16 -0500 | [diff] [blame] | 35 | |
| 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 | |
| 53 | WiringPi supports expanding your range of available "pins" by setting up |
| 54 | a port expander. The implementation details of your port expander will |
| 55 | be 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 | |
| 62 | This example was tested on a quick2wire board with one digital IO |
| 63 | expansion 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 | |
| 73 | Hook a speaker up to your Pi and generate music with softTone. Also |
| 74 | useful 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 | |
| 97 | The ``wiringPiSPIDataRW()`` function needs to be passed a ``bytes`` |
| 98 | object in Python 3. In Python 2, it takes a string. The following should |
| 99 | work 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 | |
| 107 | Now, ``retlen`` will contain the number of bytes received/read by the |
| 108 | call. ``retdata`` will contain the data itself, and in Python 3, ``buf`` |
| 109 | will have been modified to contain it as well (that won't happen in |
| 110 | Python 2, because then ``buf`` is a string, and strings are immutable). |
| 111 | |
| 112 | **Full details of the API at:** http://www.wiringpi.com |
| 113 | |
| 114 | Manual Build |
| 115 | ============ |
| 116 | |
| 117 | Get/setup repo |
| 118 | -------------- |
| 119 | |
| 120 | .. code:: bash |
| 121 | |
| 122 | git clone --recursive https://github.com/WiringPi/WiringPi-Python.git |
| 123 | cd WiringPi-Python |
| 124 | |
| 125 | Don't forget the ``--recursive``; it is required to also pull in the |
| 126 | WiringPi C code from its own repository. |
| 127 | |
| 128 | Prerequisites |
| 129 | ------------- |
| 130 | |
| 131 | To rebuild the bindings you **must** first have installed ``swig``, |
| 132 | ``python-dev``, and ``python-setuptools`` (or their ``python3-`` |
| 133 | equivalents). WiringPi should also be installed system-wide for access |
| 134 | to the ``gpio`` tool. |
| 135 | |
| 136 | .. code:: bash |
| 137 | |
| 138 | sudo apt-get install python-dev python-setuptools swig wiringpi |
| 139 | |
| 140 | Build & install with |
| 141 | -------------------- |
| 142 | |
| 143 | ``sudo python setup.py install`` |
| 144 | |
| 145 | Or Python 3: |
| 146 | |
| 147 | ``sudo python3 setup.py install`` |
| 148 | |