summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Dobrowolski <admin@tastycode.pl>2018-06-10 19:49:49 +0200
committerPiotr Dobrowolski <admin@tastycode.pl>2018-06-10 19:50:25 +0200
commit9adff5141e462414589c954f45e19383e52bcbe5 (patch)
treee253c0980e0284cfb6fea08e2ca75c74f07f2df2
parent37ba4aa07b62fc11c2ee28781e7827bb49f04c6f (diff)
downloadbitvend-9adff5141e462414589c954f45e19383e52bcbe5.tar.gz
bitvend-9adff5141e462414589c954f45e19383e52bcbe5.tar.bz2
bitvend-9adff5141e462414589c954f45e19383e52bcbe5.tar.xz
bitvend-9adff5141e462414589c954f45e19383e52bcbe5.zip
cygpio: basic cython-based MDB backend for raspi & pigpio
-rw-r--r--cygpio/cygpio.pyx86
-rw-r--r--cygpio/cygpio_test.py2
-rw-r--r--cygpio/setup.py7
3 files changed, 95 insertions, 0 deletions
diff --git a/cygpio/cygpio.pyx b/cygpio/cygpio.pyx
new file mode 100644
index 0000000..9049cda
--- /dev/null
+++ b/cygpio/cygpio.pyx
@@ -0,0 +1,86 @@
+RX_PIN = 4
+TX_PIN = 17
+
+cdef extern from "pigpio.h":
+ int gpioInitialise()
+ int gpioCfgInterfaces(unsigned ifFlags)
+
+ int gpioSetMode(unsigned gpio, unsigned mode)
+
+ int gpioSerialReadOpen(unsigned user_gpio, unsigned baud, unsigned data_bits)
+ int gpioSerialRead(unsigned user_gpio, void *buf, size_t bufSize) nogil
+ int gpioSerialReadClose(unsigned user_gpio)
+
+ int gpioWaveCreate()
+ int gpioWaveDelete(unsigned wave_id)
+ int gpioWaveClear()
+
+ int gpioWaveTxSend(unsigned wave_id, unsigned wave_mode)
+ int gpioWaveTxBusy()
+
+ int gpioWaveAddSerial(unsigned user_gpio, unsigned baud, unsigned data_bits, unsigned stop_bits, unsigned offset, unsigned numBytes, char *str)
+
+ cdef int INPUT "PI_INPUT"
+ cdef int OUTPUT "PI_OUTPUT"
+ cdef int PI_DISABLE_FIFO_IF
+ cdef int PI_DISABLE_SOCK_IF
+
+ cdef int PI_WAVE_MODE_ONE_SHOT
+
+cdef extern from "unistd.h" nogil:
+ unsigned int sleep(unsigned int seconds)
+ unsigned int usleep(unsigned int usecs)
+
+def test():
+ b = CythonRaspiBackend()
+ b.open()
+ while True:
+ print(repr(b.read()))
+
+cdef class CythonRaspiBackend(object):
+ cdef int rx_pin
+ cdef int tx_pin
+
+ def __init__(self, rx_pin=RX_PIN, tx_pin=TX_PIN):
+ self.rx_pin = rx_pin
+ self.tx_pin = tx_pin
+
+ cpdef open(self):
+ gpioCfgInterfaces(PI_DISABLE_FIFO_IF | PI_DISABLE_SOCK_IF);
+ gpioInitialise()
+ gpioWaveClear()
+ gpioSetMode(self.tx_pin, INPUT)
+
+ # gpioSerClose...
+
+ cdef int resp = gpioSerialReadOpen(self.rx_pin, 9600, 9)
+ if resp != 0:
+ raise Exception('Serial open failed: %d' % resp)
+
+ cpdef read(self):
+ cdef unsigned char buf[1024]
+ cdef int read_size
+
+ with nogil:
+ while 1:
+ read_size = gpioSerialRead(self.rx_pin, &buf, sizeof(buf))
+ if read_size > 0:
+ break
+
+ usleep(100)
+
+ return bytes(buf[0:read_size])
+
+ cpdef write(self, data):
+ cdef char* c_data = data
+ gpioWaveAddSerial(self.tx_pin, 9600, 9, 6, 0, len(data), c_data)
+ wid = gpioWaveCreate()
+
+ gpioSetMode(self.tx_pin, OUTPUT)
+ gpioWaveTxSend(wid, PI_WAVE_MODE_ONE_SHOT)
+
+ while gpioWaveTxBusy():
+ usleep(100)
+
+ gpioWaveDelete(wid)
+ gpioSetMode(self.tx_pin, INPUT)
diff --git a/cygpio/cygpio_test.py b/cygpio/cygpio_test.py
new file mode 100644
index 0000000..a9d8108
--- /dev/null
+++ b/cygpio/cygpio_test.py
@@ -0,0 +1,2 @@
+import cygpio
+cygpio.test()
diff --git a/cygpio/setup.py b/cygpio/setup.py
new file mode 100644
index 0000000..4ce5599
--- /dev/null
+++ b/cygpio/setup.py
@@ -0,0 +1,7 @@
+from distutils.core import setup
+from distutils.extension import Extension
+from Cython.Build import cythonize
+
+setup(
+ ext_modules = cythonize([Extension("cygpio", ["cygpio.pyx"], libraries=["pigpio"])])
+ )