#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  mon_cffi.py
#  
#  Copyright 2018 john@jcoppens.com
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

from cffi import FFI


ffi = FFI()

ffi.cdef("""                // declarations

typedef unsigned int lsampl_t;

struct comedi_t *comedi_open(
            const char *filename);

int comedi_data_read(
            struct comedi_t *handler,
            unsigned int subdevice,
            unsigned int channel,
            unsigned int range,
            unsigned int aref,
            lsampl_t *data);    
            
""")

C = ffi.verify("""          // passed to the real C compiler
#include <comedi.h>
""", libraries=["/usr/lib/libcomedi.so"]) 

p = C.comedi_open(b"/dev/comedi0")
print(C.comedi_data_read(p, 0, 0, 0, 0, None))