diff --git a/.gitmodules b/.gitmodules index 0016469..1dbeda0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "WiringPi"] path = WiringPi - url = http://github.com/wiringPi/WiringPi + url = https://github.com/wiringPi/WiringPi diff --git a/MANIFEST.in b/MANIFEST.in index d252ee2..5bec85d 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,7 +1,8 @@ -graft WiringPi/wiringPi -graft WiringPi/devLib -include README.md +recursive-include WiringPi *.h +include README.rst include LICENSE.txt -include setup.cfg -include wiringpi.py +include bindings.i +include constants.py +include wiringpi-class.py +include wiringpi.i include wiringpi_wrap.c diff --git a/Makefile b/Makefile deleted file mode 100644 index 08c1d7b..0000000 --- a/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -all: bindings - python setup.py build - -bindings: - swig3.0 -python -threads wiringpi.i - -clean: - rm -rf build/ - rm -rf dist/ - -install: - sudo python setup.py install diff --git a/README.md b/README.md deleted file mode 100644 index a2034ff..0000000 --- a/README.md +++ /dev/null @@ -1,106 +0,0 @@ -#WiringPi for Python - -WiringPi: An implementation of most of the Arduino Wiring - functions for the Raspberry Pi - -WiringPi implements new functions for managing IO expanders. - -#Quick Build - -A quick and dirty build script is supplied to install WiringPi-Python for Python 2 and 3. Just: - -``` -git clone --recursive https://github.com/WiringPi/WiringPi-Python.git -cd WiringPi-Python -./build.sh -``` - -#Manual Build - -##Get/setup repo -```bash -git clone --recursive https://github.com/WiringPi/WiringPi-Python.git -cd WiringPi-Python -git submodule update --init -``` - -##Prerequisites -To rebuild the bindings -you **must** first have python-dev, python-setuptools and swig installed. -```bash -sudo apt-get install python-dev python-setuptools swig -``` - -##Build WiringPi -```bash -cd WiringPi -sudo ./build -``` - -##Generate Bindings - -Return to the root directory of the repository and: - -`swig2.0 -python wiringpi.i` - -or - -`swig3.0 -thread -python wiringpi.i` - -##Build & install with - -`sudo python setup.py install` - -Or Python 3: - -`sudo python3 setup.py install` - -##Usage - - import wiringpi - - wiringpi.wiringPiSetup() # For sequential pin numbering, one of these MUST be called before using IO functions - # OR - wiringpi.wiringPiSetupSys() # For /sys/class/gpio with GPIO pin numbering - # OR - wiringpi.wiringPiSetupGpio() # For GPIO pin numbering - - -Setting up IO expanders (This example was tested on a quick2wire board with one digital IO expansion board connected via I2C): - - wiringpi.mcp23017Setup(65,0x20) - wiringpi.pinMode(65,1) - wiringpi.digitalWrite(65,1) - -**General IO:** - - wiringpi.pinMode(6,1) # Set pin 6 to 1 ( OUTPUT ) - wiringpi.digitalWrite(6,1) # Write 1 ( HIGH ) to pin 6 - wiringpi.digitalRead(6) # Read pin 6 - -**Setting up a peripheral:** -WiringPi2 supports expanding your range of available "pins" by setting up a port expander. The implementation details of -your port expander will be handled transparently, and you can write to the additional pins ( starting from PIN_OFFSET >= 64 ) -as if they were normal pins on the Pi. - - wiringpi.mcp23017Setup(PIN_OFFSET,I2C_ADDR) - -**Soft Tone** - -Hook a speaker up to your Pi and generate music with softTone. Also useful for generating frequencies for other uses such as modulating A/C. - - wiringpi.softToneCreate(PIN) - wiringpi.softToneWrite(PIN,FREQUENCY) - -**Bit shifting:** - - wiringpi.shiftOut(1,2,0,123) # Shift out 123 (b1110110, byte 0-255) to data pin 1, clock pin 2 - -**Serial:** - - serial = wiringpi.serialOpen('/dev/ttyAMA0',9600) # Requires device/baud and returns an ID - wiringpi.serialPuts(serial,"hello") - wiringpi.serialClose(serial) # Pass in ID - -**Full details at:** -http://www.wiringpi.com diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..855e84a --- /dev/null +++ b/README.rst @@ -0,0 +1,154 @@ +:Warning: WiringPi was deprecated by its author in August 2019. As of 31st October 2023 nobody has shown an interest in properly maintaining it. Between this, and changes to GPIO in Rasberry Pi OS Bookworm and on the Raspberry Pi 5, this project is going nowhere. It has been archived to more clearly indicate this status. + +Note +~~~~ + +This is an unofficial port of Gordon's WiringPi library. Please do not +email Gordon if you have issues, he will not be able to help. + +For support, comments, questions, etc please join the WiringPi Discord +channel: https://discord.gg/SM4WUVG + +WiringPi for Python +~~~~~~~~~~~~~~~~~~~ + +WiringPi: An implementation of most of the Arduino Wiring functions for +the Raspberry Pi. + +WiringPi implements new functions for managing IO expanders. + +**Alternative** + +* `GPIO Zero `_ is another Python library for controlling GPIO. It is installed by default in Raspberry Pi OS and is used in the `Raspberry Pi GPIO documentation `_. + +Quick Install +============= + +.. image:: https://badge.fury.io/py/wiringpi.svg + :alt: PyPI version badge + :target: https://pypi.org/project/wiringpi/ + +The library is packaged on PyPI and can be installed with pip: + +``pip install wiringpi`` + +Usage +===== + +.. code:: python + + import wiringpi + + # One of the following MUST be called before using IO functions: + wiringpi.wiringPiSetup() # For sequential pin numbering + # OR + wiringpi.wiringPiSetupSys() # For /sys/class/gpio with GPIO pin numbering + # OR + wiringpi.wiringPiSetupGpio() # For GPIO pin numbering + +**General IO:** + +.. code:: python + + wiringpi.pinMode(6, 1) # Set pin 6 to 1 ( OUTPUT ) + wiringpi.digitalWrite(6, 1) # Write 1 ( HIGH ) to pin 6 + wiringpi.digitalRead(6) # Read pin 6 + +**Setting up a peripheral:** + +WiringPi supports expanding your range of available "pins" by setting up +a port expander. The implementation details of your port expander will +be handled transparently, and you can write to the additional pins +(starting from PIN\_OFFSET >= 64) as if they were normal pins on the Pi. + +.. code:: python + + wiringpi.mcp23017Setup(PIN_OFFSET, I2C_ADDR) + +This example was tested on a quick2wire board with one digital IO +expansion board connected via I2C: + +.. code:: python + + wiringpi.mcp23017Setup(65, 0x20) + wiringpi.pinMode(65, 1) + wiringpi.digitalWrite(65, 1) + +**Soft Tone:** + +Hook a speaker up to your Pi and generate music with softTone. Also +useful for generating frequencies for other uses such as modulating A/C. + +.. code:: python + + wiringpi.softToneCreate(PIN) + wiringpi.softToneWrite(PIN, FREQUENCY) + +**Bit shifting:** + +.. code:: python + + wiringpi.shiftOut(1, 2, 0, 123) # Shift out 123 (b1110110, byte 0-255) to data pin 1, clock pin 2 + +**Serial:** + +.. code:: python + + serial = wiringpi.serialOpen('/dev/ttyAMA0', 9600) # Requires device/baud and returns an ID + wiringpi.serialPuts(serial, "hello") + wiringpi.serialClose(serial) # Pass in ID + +**SPI:** + +The ``wiringPiSPIDataRW()`` function needs to be passed a ``bytes`` +object in Python 3. In Python 2, it takes a string. The following should +work in either Python 2 or 3: + +.. code:: python + + wiringpi.wiringPiSPISetup(channel, speed) + buf = bytes([your data here]) + retlen, retdata = wiringpi.wiringPiSPIDataRW(0, buf) + +Now, ``retlen`` will contain the number of bytes received/read by the +call. ``retdata`` will contain the data itself, and in Python 3, ``buf`` +will have been modified to contain it as well (that won't happen in +Python 2, because then ``buf`` is a string, and strings are immutable). + +**Full details of the API at:** http://www.wiringpi.com + +Manual Build +============ + +Get/setup repo +-------------- + +.. code:: bash + + git clone --recursive https://github.com/WiringPi/WiringPi-Python.git + cd WiringPi-Python + +Don't forget the ``--recursive``; it is required to also pull in the +WiringPi C code from its own repository. + +Prerequisites +------------- + +To rebuild the bindings you **must** first have installed ``swig``, +``python-dev``, and ``python-setuptools`` (or their ``python3-`` +equivalents). WiringPi should also be installed system-wide for access +to the ``gpio`` tool. + +.. code:: bash + + sudo apt-get install python-dev python-setuptools swig wiringpi + +Build & install with +-------------------- + +``sudo python setup.py install`` + +Or Python 3: + +``sudo python3 setup.py install`` + diff --git a/WiringPi b/WiringPi index 9a8f8be..e9821ab 160000 --- a/WiringPi +++ b/WiringPi @@ -1 +1 @@ -Subproject commit 9a8f8bee5df60061645918231110a7c2e4d3fa6b +Subproject commit e9821abdb4b4fe46a2ea9243471d339435fa7bde diff --git a/bindings.i b/bindings.i index dc8bf05..0766221 100644 --- a/bindings.i +++ b/bindings.i @@ -4,31 +4,35 @@ extern int wiringPiFailure (int fatal, const char *message, ...) ; extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ; extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ; +extern void wiringPiVersion (int *major, int *minor) ; extern int wiringPiSetup (void) ; extern int wiringPiSetupSys (void) ; extern int wiringPiSetupGpio (void) ; extern int wiringPiSetupPhys (void) ; -extern void pinModeAlt (int pin, int mode) ; -extern void pinMode (int pin, int mode) ; -extern void pullUpDnControl (int pin, int pud) ; -extern int digitalRead (int pin) ; -extern void digitalWrite (int pin, int value) ; -extern void pwmWrite (int pin, int value) ; -extern int analogRead (int pin) ; -extern void analogWrite (int pin, int value) ; -extern int piBoardRev (void) ; +extern void pinModeAlt (int pin, int mode) ; +extern void pinMode (int pin, int mode) ; +extern void pullUpDnControl (int pin, int pud) ; +extern int digitalRead (int pin) ; +extern void digitalWrite (int pin, int value) ; +extern void pwmWrite (int pin, int value) ; +extern int analogRead (int pin) ; +extern void analogWrite (int pin, int value) ; +extern int piGpioLayout (void) ; +extern int piBoardRev (void) ; // Deprecated extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ; extern int wpiPinToGpio (int wpiPin) ; extern int physPinToGpio (int physPin) ; extern void setPadDrive (int group, int value) ; extern int getAlt (int pin) ; extern void pwmToneWrite (int pin, int freq) ; -extern void digitalWriteByte (int value) ; -extern unsigned int digitalReadByte (void) ; extern void pwmSetMode (int mode) ; extern void pwmSetRange (unsigned int range) ; extern void pwmSetClock (int divisor) ; extern void gpioClockSet (int pin, int freq) ; +extern unsigned int digitalReadByte (void) ; +extern unsigned int digitalReadByte2 (void) ; +extern void digitalWriteByte (int value) ; +extern void digitalWriteByte2 (int value) ; extern int waitForInterrupt (int pin, int mS) ; extern int piThreadCreate (void *(*fn)(void *)) ; extern void piLock (int key) ; @@ -141,6 +145,27 @@ extern void softToneWrite (int pin, int freq) ; extern int sr595Setup (const int pinBase, const int numPins, const int dataPin, const int clockPin, const int latchPin) ; +// Header file WiringPi/wiringPi/bmp180.h +extern int bmp180Setup (const int pinBase) ; + +// Header file WiringPi/wiringPi/drcNet.h +extern int drcSetupNet (const int pinBase, const int numPins, const char *ipAddress, const char *port, const char *password) ; + +// Header file WiringPi/wiringPi/ds18b20.h +extern int ds18b20Setup (const int pinBase, const char *serialNum) ; + +// Header file WiringPi/wiringPi/htu21d.h +extern int htu21dSetup (const int pinBase) ; + +// Header file WiringPi/wiringPi/pseudoPins.h +extern int pseudoPinsSetup (const int pinBase) ; + +// Header file WiringPi/wiringPi/rht03.h +extern int rht03Setup (const int pinBase, const int devicePin) ; + +// Header file WiringPi/wiringPi/wpiExtensions.h +extern int loadWPiExtension (char *progName, char *extensionData, int verbose) ; + // Header file WiringPi/devLib/ds1302.h extern unsigned int ds1302rtcRead (const int reg) ; extern void ds1302rtcWrite (const int reg, const unsigned int data) ; @@ -220,3 +245,6 @@ extern void scrollPhatPrintf (const char *message, ...) ; extern void scrollPhatPrintSpeed (const int cps10) ; extern void scrollPhatIntensity (const int percent) ; extern int scrollPhatSetup (void) ; + +// Header file WiringPi/devLib/piFace.h +extern int piFaceSetup (const int pinBase) ; diff --git a/build.sh b/build.sh deleted file mode 100755 index e57689f..0000000 --- a/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -swig2.0 -python -threads wiringpi.i -sudo python setup.py build install -sudo python test.py diff --git a/constants.py b/constants.py index 75ce015..f501698 100644 --- a/constants.py +++ b/constants.py @@ -38,4 +38,10 @@ INT_EDGE_FALLING = 1; INT_EDGE_RISING = 2; INT_EDGE_BOTH = 3; + +# Shifting (from wiringShift.h) + +LSBFIRST = 0; +MSBFIRST = 1; + %} diff --git a/examples/softtone.py b/examples/softtone.py new file mode 100644 index 0000000..4a90ce7 --- /dev/null +++ b/examples/softtone.py @@ -0,0 +1,16 @@ +# Test of the softTone module in wiringPi +# Plays a scale out on pin 3 - connect pizeo disc to pin 3 & 0v +import wiringpi + +PIN = 3 + +SCALE = [262, 294, 330, 349, 392, 440, 494, 525] + +wiringpi.wiringPiSetup() +wiringpi.softToneCreate(PIN) + +while True: + for idx in range(8): + print(idx) + wiringpi.softToneWrite(PIN, SCALE[idx]) + wiringpi.delay(500) diff --git a/setup.cfg b/setup.cfg index b88034e..360d7aa 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,7 @@ [metadata] -description-file = README.md +author = Philip Howard +author_email = phil@gadgetoid.com +url = https://github.com/WiringPi/WiringPi-Python/ +description = A python interface to WiringPi 2.0 library which allows for easily interfacing with the GPIO pins of the Raspberry Pi. Also supports i2c and SPI. +long_description = file:README.rst +license = LGPL diff --git a/setup.py b/setup.py index 0189589..4d2e3f1 100755 --- a/setup.py +++ b/setup.py @@ -1,32 +1,68 @@ #!/usr/bin/env python -from setuptools import setup, find_packages, Extension +import os +import sys + +from setuptools import setup, Extension +from setuptools.command.build_py import build_py +from setuptools.command.sdist import sdist +from distutils.spawn import find_executable from glob import glob sources = glob('WiringPi/devLib/*.c') sources += glob('WiringPi/wiringPi/*.c') -sources += ['wiringpi_wrap.c'] +# If we have swig, use it. Otherwise, use the pre-generated +# wrapper from the source distribution. +if find_executable('swig'): + sources += ['wiringpi.i'] +elif os.path.exists('wiringpi_wrap.c'): + sources += ['wiringpi_wrap.c'] +else: + print("Error: Building this module requires either that swig is installed\n" + " (e.g., 'sudo apt install swig') or that wiringpi_wrap.c from the\n" + " source distribution (on pypi) is available.") + sys.exit(1) + +try: + sources.remove('WiringPi/devLib/piFaceOld.c') +except ValueError: + # the file is already excluded in the source distribution + pass + + +# Fix so that build_ext runs before build_py +# Without this, wiringpi.py is generated too late and doesn't +# end up in the distribution when running setup.py bdist or bdist_wheel. +# Based on: +# https://stackoverflow.com/a/29551581/7938656 +# and +# https://blog.niteoweb.com/setuptools-run-custom-code-in-setup-py/ +class build_py_ext_first(build_py): + def run(self): + self.run_command("build_ext") + return build_py.run(self) + + +# Make sure wiringpi_wrap.c is available for the source dist, also. +class sdist_ext_first(sdist): + def run(self): + self.run_command("build_ext") + return sdist.run(self) -sources.remove('WiringPi/devLib/piFaceOld.c') _wiringpi = Extension( '_wiringpi', include_dirs=['WiringPi/wiringPi','WiringPi/devLib'], - sources=sources + sources=sources, + swig_opts=['-threads'], + extra_link_args=['-lcrypt', '-lrt'] ) setup( name = 'wiringpi', - version = '2.32.1', - author = "Philip Howard", - author_email = "phil@gadgetoid.com", - url = 'https://github.com/WiringPi/WiringPi-Python/', - description = """A python interface to WiringPi 2.0 library which allows for - easily interfacing with the GPIO pins of the Raspberry Pi. Also supports - i2c and SPI""", - long_description=open('README.md').read(), + version = '2.60.1', ext_modules = [ _wiringpi ], py_modules = ["wiringpi"], install_requires=[], - headers=glob('WiringPi/wiringPi/*.h')+glob('WiringPi/devLib/*.h') + cmdclass = {'build_py' : build_py_ext_first, 'sdist' : sdist_ext_first}, ) diff --git a/wiringpi-class.py b/wiringpi-class.py index 74ca602..a14df56 100644 --- a/wiringpi-class.py +++ b/wiringpi-class.py @@ -154,7 +154,7 @@ def wiringPiISR(self,*args): def softPwmCreate(self,*args): return softPwmCreate(*args) def softPwmWrite(self,*args): - return sofPwmWrite(*args) + return softPwmWrite(*args) def softToneCreate(self,*args): return softToneCreate(*args) diff --git a/wiringpi.i b/wiringpi.i index 847ac76..fc62075 100644 --- a/wiringpi.i +++ b/wiringpi.i @@ -34,6 +34,13 @@ #include "WiringPi/wiringPi/softServo.h" #include "WiringPi/wiringPi/softTone.h" #include "WiringPi/wiringPi/sr595.h" +#include "WiringPi/wiringPi/bmp180.h" +#include "WiringPi/wiringPi/drcNet.h" +#include "WiringPi/wiringPi/ds18b20.h" +#include "WiringPi/wiringPi/htu21d.h" +#include "WiringPi/wiringPi/pseudoPins.h" +#include "WiringPi/wiringPi/rht03.h" +#include "WiringPi/wiringPi/wpiExtensions.h" #include "WiringPi/devLib/ds1302.h" #include "WiringPi/devLib/font.h" #include "WiringPi/devLib/gertboard.h" @@ -43,6 +50,7 @@ #include "WiringPi/devLib/piGlow.h" #include "WiringPi/devLib/piNes.h" #include "WiringPi/devLib/scrollPhat.h" +#include "WiringPi/devLib/piFace.h" %} %apply unsigned char { uint8_t }; @@ -52,12 +60,12 @@ }; // Grab a Python function object as a Python object. -%typemap(in) PyObject *pyfunc { - if (!PyCallable_Check($1)) { +%typemap(in) PyObject *PyFunc { + if (!PyCallable_Check($input)) { PyErr_SetString(PyExc_TypeError, "Need a callable object!"); return NULL; } - $1 = $2; + $1 = $input; } %{