Skip to content

Commit

Permalink
[fix] comedi_bind and pyspcm tools were raising errors during doc com…
Browse files Browse the repository at this point in the history
…pilation

A workaround was found in case the C modules cannot be loaded
  • Loading branch information
WeisLeDocto committed Sep 7, 2021
1 parent ca5641a commit 7b1c720
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 91 deletions.
105 changes: 66 additions & 39 deletions crappy/tool/comedi_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
"""More documentation coming soon !"""

from ctypes import *
from .._global import OptionalModule

c = cdll.LoadLibrary("/usr/lib/libcomedi.so")
try:
c = cdll.LoadLibrary("/usr/lib/libcomedi.so")
is_installed = True
except OSError:
c = OptionalModule('libcomedi.so')
is_installed = False


class comedi_t(Structure):
Expand All @@ -15,53 +21,74 @@ class comedi_range(Structure):
pass


AREF_GROUND = 0
lsampl_t = c_uint
data = lsampl_t() # To store data for data_read before returning it
if is_installed:
AREF_GROUND = 0
lsampl_t = c_uint
data = lsampl_t() # To store data for data_read before returning it

comedi_open = c.comedi_open
comedi_open.restype = POINTER(comedi_t) # Device handle
comedi_open.argtypes = [c_char_p] # Device path
comedi_open = c.comedi_open
comedi_open.restype = POINTER(comedi_t) # Device handle
comedi_open.argtypes = [c_char_p] # Device path

comedi_close = c.comedi_close
comedi_close.restype = c_int # Error code: 0 on success else -1
comedi_close.argtypes = [POINTER(comedi_t)] # device handle
comedi_close = c.comedi_close
comedi_close.restype = c_int # Error code: 0 on success else -1
comedi_close.argtypes = [POINTER(comedi_t)] # device handle

comedi_to_phys = c.comedi_to_phys
comedi_to_phys.restype = c_double # Physical value
comedi_to_phys.argtypes = [lsampl_t, POINTER(comedi_range), lsampl_t]
# Comedi value, range, maxdata
comedi_to_phys = c.comedi_to_phys
comedi_to_phys.restype = c_double # Physical value
comedi_to_phys.argtypes = [lsampl_t, POINTER(comedi_range), lsampl_t]
# Comedi value, range, maxdata

comedi_from_phys = c.comedi_from_phys
comedi_from_phys.restype = lsampl_t # Comedi value
comedi_from_phys.argtypes = [c_double, POINTER(comedi_range), lsampl_t]
# Physical value, range, maxdata
comedi_from_phys = c.comedi_from_phys
comedi_from_phys.restype = lsampl_t # Comedi value
comedi_from_phys.argtypes = [c_double, POINTER(comedi_range), lsampl_t]
# Physical value, range, maxdata

comedi_get_maxdata = c.comedi_get_maxdata
comedi_get_maxdata.restype = lsampl_t # Maxdata
comedi_get_maxdata.argtypes = [POINTER(comedi_t), c_uint, c_uint]
# handle, subdevice, channel
comedi_get_maxdata = c.comedi_get_maxdata
comedi_get_maxdata.restype = lsampl_t # Maxdata
comedi_get_maxdata.argtypes = [POINTER(comedi_t), c_uint, c_uint]
# handle, subdevice, channel

comedi_get_range = c.comedi_get_range
comedi_get_range.restype = POINTER(comedi_range) # Range
comedi_get_range.argtypes = [POINTER(comedi_t), c_uint, c_uint, c_uint]
# handle, subdevice, channel, range_num
comedi_get_range = c.comedi_get_range
comedi_get_range.restype = POINTER(comedi_range) # Range
comedi_get_range.argtypes = [POINTER(comedi_t), c_uint, c_uint, c_uint]
# handle, subdevice, channel, range_num

c.comedi_data_read.restype = c_int # Error code (1 if fine else -1)
c.comedi_data_read.argtypes = [POINTER(comedi_t),
c_uint, c_uint, c_uint,
c_uint, POINTER(lsampl_t)]
# Handle, subdevice, channel, range_num, aref, data pointer

c.comedi_data_read.restype = c_int # Error code (1 if fine else -1)
c.comedi_data_read.argtypes = [POINTER(comedi_t),
c_uint, c_uint, c_uint,
c_uint, POINTER(lsampl_t)]
# Handle, subdevice, channel, range_num, aref, data pointer

def comedi_data_read(*args):
assert c.comedi_data_read(*(args+(byref(data),))) == 1, "Data read failed!"
return data

def comedi_data_read(*args):
assert c.comedi_data_read(*(args+(byref(data),))) == 1, "Data read failed!"
return data
comedi_data_write = c.comedi_data_write
comedi_data_write.restype = c_int # Error code (1 if fine else -1)
comedi_data_write.argtypes = [POINTER(comedi_t), c_uint, c_uint, c_uint,
c_uint, lsampl_t]
# Handle, subdevice, channel, range_num, aref, data


comedi_data_write = c.comedi_data_write
comedi_data_write.restype = c_int # Error code (1 if fine else -1)
comedi_data_write.argtypes = [POINTER(comedi_t), c_uint, c_uint, c_uint,
c_uint, lsampl_t]
# Handle, subdevice, channel, range_num, aref, data
else:
AREF_GROUND = OptionalModule('libcomedi.so')
lsampl_t = OptionalModule('libcomedi.so')
data = OptionalModule('libcomedi.so')

comedi_open = OptionalModule('libcomedi.so')

comedi_close = OptionalModule('libcomedi.so')

comedi_to_phys = OptionalModule('libcomedi.so')

comedi_from_phys = OptionalModule('libcomedi.so')

comedi_get_maxdata = OptionalModule('libcomedi.so')

comedi_get_range = OptionalModule('libcomedi.so')

comedi_data_read = OptionalModule('libcomedi.so')

comedi_data_write = OptionalModule('libcomedi.so')
140 changes: 88 additions & 52 deletions crappy/tool/pyspcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ctypes import *
from sys import platform
from .._global import OptionalModule

# ======= Constants definition ======

Expand Down Expand Up @@ -53,82 +54,117 @@
new_buffer = create_string_buffer # To allow to create a buffer without ctypes

if "linux" in platform.lower():
mod = cdll.LoadLibrary("libspcm_linux.so")
msg = "libspcm_linux.so"
try:
mod = cdll.LoadLibrary("libspcm_linux.so")
is_installed = True
except OSError:
mod = OptionalModule(msg)
is_installed = False
elif "darwin" in platform.lower():
mod = cdll.LoadLibrary("libc.so.6")
msg = "libc.so.6"
try:
mod = cdll.LoadLibrary("libc.so.6")
is_installed = True
except OSError:
mod = OptionalModule(msg)
is_installed = False
else:
mod = windll.LoadLibrary("C:\\Windows\\system32\\spcm_win64.dll")
msg = "spcm_win64.dll"
try:
mod = windll.LoadLibrary("C:\\Windows\\system32\\spcm_win64.dll")
is_installed = True
except OSError:
mod = OptionalModule(msg)
is_installed = False


class SpectrumError(Exception):
pass


dwGetErrorInfo_i32 = mod.spcm_dwGetErrorInfo_i32
dwGetErrorInfo_i32.argtype = [
c_void_p, POINTER(c_uint32), POINTER(c_int32), c_char_p]
dwGetErrorInfo_i32.restype = c_uint32
if is_installed:
dwGetErrorInfo_i32 = mod.spcm_dwGetErrorInfo_i32
dwGetErrorInfo_i32.argtype = [
c_void_p, POINTER(c_uint32), POINTER(c_int32), c_char_p]
dwGetErrorInfo_i32.restype = c_uint32


def check(h, code):
if code == 0:
return
print("Error: return code=", code)
sz_error_text_buffer = create_string_buffer(ERRORTEXTLEN)
dwGetErrorInfo_i32(h, None, None, sz_error_text_buffer)
print(sz_error_text_buffer.value)
vClose(h)
raise SpectrumError(sz_error_text_buffer.value)
def check(h, code):
if code == 0:
return
print("Error: return code=", code)
sz_error_text_buffer = create_string_buffer(ERRORTEXTLEN)
dwGetErrorInfo_i32(h, None, None, sz_error_text_buffer)
print(sz_error_text_buffer.value)
vClose(h)
raise SpectrumError(sz_error_text_buffer.value)


hOpen = mod.spcm_hOpen
hOpen.argtype = [c_char_p]
hOpen.restype = c_void_p
hOpen = mod.spcm_hOpen
hOpen.argtype = [c_char_p]
hOpen.restype = c_void_p

vClose = mod.spcm_vClose
vClose.argtype = [c_char_p]
vClose.restype = None
vClose = mod.spcm_vClose
vClose.argtype = [c_char_p]
vClose.restype = None

my_i64 = c_int64() # C ints to read args when calling getparam
my_i32 = c_int32()
my_i64 = c_int64() # C ints to read args when calling getparam
my_i32 = c_int32()

mod.spcm_dwGetParam_i32.argtype = [c_void_p, c_int32, POINTER(c_int32)]
mod.spcm_dwGetParam_i32.restype = c_uint32
mod.spcm_dwGetParam_i32.argtype = [c_void_p, c_int32, POINTER(c_int32)]
mod.spcm_dwGetParam_i32.restype = c_uint32

mod.spcm_dwGetParam_i64.argtype = [c_void_p, c_int32, POINTER(c_int64)]
mod.spcm_dwGetParam_i64.restype = c_uint32
mod.spcm_dwGetParam_i64.argtype = [c_void_p, c_int32, POINTER(c_int64)]
mod.spcm_dwGetParam_i64.restype = c_uint32


def dw_get_param(h, reg):
if reg in double_reg:
check(h, mod.spcm_dwGetParam_i64(h, reg, byref(my_i64)))
return my_i64.value
else:
check(h, mod.spcm_dwGetParam_i32(h, reg, byref(my_i32)))
return my_i32.value
def dw_get_param(h, reg):
if reg in double_reg:
check(h, mod.spcm_dwGetParam_i64(h, reg, byref(my_i64)))
return my_i64.value
else:
check(h, mod.spcm_dwGetParam_i32(h, reg, byref(my_i32)))
return my_i32.value


mod.spcm_dwSetParam_i32.argtype = [c_void_p, c_int32, c_int32]
mod.spcm_dwSetParam_i32.restype = c_uint32
mod.spcm_dwSetParam_i32.argtype = [c_void_p, c_int32, c_int32]
mod.spcm_dwSetParam_i32.restype = c_uint32

mod.spcm_dwSetParam_i64.argtype = [c_void_p, c_int32, c_int64]
mod.spcm_dwSetParam_i64.restype = c_uint32
mod.spcm_dwSetParam_i64.argtype = [c_void_p, c_int32, c_int64]
mod.spcm_dwSetParam_i64.restype = c_uint32


def dw_set_param(h, reg, val):
if reg in double_reg:
check(h, mod.spcm_dwSetParam_i64(h, reg, val))
else:
check(h, mod.spcm_dwSetParam_i32(h, reg, val))
def dw_set_param(h, reg, val):
if reg in double_reg:
check(h, mod.spcm_dwSetParam_i64(h, reg, val))
else:
check(h, mod.spcm_dwSetParam_i32(h, reg, val))


mod.spcm_dwDefTransfer_i64.argtype = [c_void_p, c_uint32, c_uint32, c_uint32,
c_void_p, c_uint64, c_uint64]
mod.spcm_dwDefTransfer_i64.restype = c_uint32
mod.spcm_dwDefTransfer_i64.argtype = [c_void_p, c_uint32, c_uint32, c_uint32,
c_void_p, c_uint64, c_uint64]
mod.spcm_dwDefTransfer_i64.restype = c_uint32


def dw_def_transfer(h, buff_type, direction, notify_size, buff, offset,
buff_size):
check(h, mod.spcm_dwDefTransfer_i64(h, buff_type, direction, notify_size,
buff, c_uint64(offset),
c_uint64(buff_size)))
def dw_def_transfer(h, buff_type, direction, notify_size, buff, offset,
buff_size):
check(h, mod.spcm_dwDefTransfer_i64(h, buff_type, direction, notify_size,
buff, c_uint64(offset),
c_uint64(buff_size)))

else:
dwGetErrorInfo_i32 = OptionalModule(msg)

hOpen = OptionalModule(msg)

vClose = OptionalModule(msg)

my_i64 = OptionalModule(msg)
my_i32 = OptionalModule(msg)

dw_get_param = OptionalModule(msg)

dw_set_param = OptionalModule(msg)

dw_def_transfer = OptionalModule(msg)

0 comments on commit 7b1c720

Please sign in to comment.