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 | |
| 21 | ``pip install wiringpi`` |
| 22 | |
| 23 | Usage |
| 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 | |
| 47 | WiringPi supports expanding your range of available "pins" by setting up |
| 48 | a port expander. The implementation details of your port expander will |
| 49 | be 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 | |
| 56 | This example was tested on a quick2wire board with one digital IO |
| 57 | expansion 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 | |
| 67 | Hook a speaker up to your Pi and generate music with softTone. Also |
| 68 | useful 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 | |
| 91 | The ``wiringPiSPIDataRW()`` function needs to be passed a ``bytes`` |
| 92 | object in Python 3. In Python 2, it takes a string. The following should |
| 93 | work 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 | |
| 101 | Now, ``retlen`` will contain the number of bytes received/read by the |
| 102 | call. ``retdata`` will contain the data itself, and in Python 3, ``buf`` |
| 103 | will have been modified to contain it as well (that won't happen in |
| 104 | Python 2, because then ``buf`` is a string, and strings are immutable). |
| 105 | |
| 106 | **Full details of the API at:** http://www.wiringpi.com |
| 107 | |
| 108 | Manual Build |
| 109 | ============ |
| 110 | |
| 111 | Get/setup repo |
| 112 | -------------- |
| 113 | |
| 114 | .. code:: bash |
| 115 | |
| 116 | git clone --recursive https://github.com/WiringPi/WiringPi-Python.git |
| 117 | cd WiringPi-Python |
| 118 | |
| 119 | Don't forget the ``--recursive``; it is required to also pull in the |
| 120 | WiringPi C code from its own repository. |
| 121 | |
| 122 | Prerequisites |
| 123 | ------------- |
| 124 | |
| 125 | To rebuild the bindings you **must** first have installed ``swig``, |
| 126 | ``python-dev``, and ``python-setuptools`` (or their ``python3-`` |
| 127 | equivalents). WiringPi should also be installed system-wide for access |
| 128 | to the ``gpio`` tool. |
| 129 | |
| 130 | .. code:: bash |
| 131 | |
| 132 | sudo apt-get install python-dev python-setuptools swig wiringpi |
| 133 | |
| 134 | Build & install with |
| 135 | -------------------- |
| 136 | |
| 137 | ``sudo python setup.py install`` |
| 138 | |
| 139 | Or Python 3: |
| 140 | |
| 141 | ``sudo python3 setup.py install`` |
| 142 | |