What methods can I use to return a struct to a Python Ctypes call to the function in a shared object? -
i have following c file compiling shared object. load .so shared object via ctypes in python. can call function ctypes, , function prints correct temp , humidity, can't seem struct main code. how can struct c function , how can retrieve fields within python.
#!/bin/python ctypes import * class hmtemp(structure): _fields_ = [ ("temp", c_double) , ("humidity", c_double) ] dhtlib = 'libdht4py.so' hlibc = cdll(dhtlib) hmtemp = hlibc.readdht() print hmtemp.temp
#define bcm2708_peri_base 0x20000000 #define gpio_base (bcm2708_peri_base + 0x200000) /* gpio controller */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <dirent.h> #include <fcntl.h> #include <assert.h> #include <unistd.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> #include <bcm2835.h> #include <unistd.h> #define maxtimings 100 struct dhstruct { double temp; double humidity; } ; struct dhstruct readdht(); int bits[250], data[100]; int bitidx = 0; struct dhstruct readdht() { bcm2835_init() ; int type = 11 ; int pin = 4 ; struct dhstruct dhts; int counter = 0; int laststate = high; int j=0; // set gpio pin output bcm2835_gpio_fsel(pin, bcm2835_gpio_fsel_outp); bcm2835_gpio_write(pin, high); usleep(500000); // 500 ms bcm2835_gpio_write(pin, low); usleep(20000); bcm2835_gpio_fsel(pin, bcm2835_gpio_fsel_inpt); data[0] = data[1] = data[2] = data[3] = data[4] = 0; // wait pin drop? while (bcm2835_gpio_lev(pin) == 1) { usleep(1); } //while // read data! (int i=0; i< maxtimings; i++) { counter = 0; while ( bcm2835_gpio_lev(pin) == laststate) { counter++; //nanosleep(1); // overclocking might change this? if (counter == 1000) break; }//while laststate = bcm2835_gpio_lev(pin); if (counter == 1000) break; bits[bitidx++] = counter; if ((i>3) && (i%2 == 0)) { // shove each bit storage bytes data[j/8] <<= 1; if (counter > 200) data[j/8] |= 1; j++; }//if } //for dhts.temp = data[2] ; dhts.humidity = data[0] ; printf("temp = %5.2f *c, hum = %5.2f \%\n", dhts.temp , dhts.humidity ); return dhts; }//function
to combine c/c++ , python recommend use cython. cython able pass objects (eg. numpy arrays) c/c++, fill data , python-code.
here minmal example:
the c-skript: (c_example.c)
#include <stdlib.h> #include <math.h> void c_claculate(double *x, int n) { int i; (i = 0; i<n;i++) { x[i]+=i*i; } }
the python-skript: (example.py)
from numpy import * example import * data=zeros(10) calculate(data) print data
the .pyx file: (example.pyx)
import cython import numpy cimport numpy # declare interface c code cdef extern void c_claculate(double *x, int n) # cython interface c function def calculate(numpy.ndarray[double, ndim=1, mode='c'] x not none): cdef int n = x.shape[0] c_claculate(&x[0],n) return x
and setup file: (setup.py)
from distutils.core import setup distutils.extension import extension cython.distutils import build_ext import numpy setup( cmdclass = {'build_ext': build_ext}, ext_modules = [ extension("example", sources=["example.pyx", "c_example.c"], include_dirs=[numpy.get_include()] ) ], )
now can compile skript running
python setup.py build_ext -fi
and execute python skript.
cython should available via pip on pi.
Comments
Post a Comment