Skip to content

Commit

Permalink
Fixing interrupt callback issue in python
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Wright committed Mar 11, 2019
1 parent 1b2249c commit 697196d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
24 changes: 13 additions & 11 deletions daqhats/hats.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class HatCallback(object):
DAQ HAT interrupt callback function class.
This class handles passing Python functions to the shared library as a
callback then retrieving the passed user data as a Python object when
the function is called from the library.
callback. It stores the user data internally and passes it to the callback
function to avoid issues with passing the object through the library.
The callback function should have a single argument (optional) that is a
Python object the user provides to :py:func:`interrupt_callback_enable` that
Expand All @@ -77,6 +77,7 @@ def __init__(self, function):
self.function = function
self.cbfunctype = CFUNCTYPE(None, c_void_p)
self.cbfunc = None
self.user_data = None

def get_callback_func(self):
"""
Expand All @@ -88,17 +89,17 @@ def func(user_data):
"""
Function wrapper.
"""
self.handle_callback(user_data)
self.handle_callback()
self.cbfunc = self.cbfunctype(func)
return self.cbfunc

def handle_callback(self, user_data):
def handle_callback(self):
"""
This is directly called from the interrupt thread. It converts the
void* back into a python object then calls the user's callback.
This is directly called from the interrupt thread. It calls the user's
callback, passing the user_data object that gets set with
interrupt_callback_enable().
"""
p_user_data = cast(user_data, POINTER(py_object)).contents.value
self.function(p_user_data)
self.function(self.user_data)

def _load_daqhats_library():
"""
Expand Down Expand Up @@ -301,11 +302,12 @@ def my_function(data):
c_void_p]
_libc.hat_interrupt_callback_enable.restype = c_int

# cast the python user_data object to a void*
c_user_data = cast(pointer(py_object(user_data)), c_void_p)
# save the user data in the HatCallback object
callback.user_data = user_data

# pass the callback class handler function and void * to the library
if (_libc.hat_interrupt_callback_enable(callback.get_callback_func(),
c_user_data) != 0):
None) != 0):
raise Exception("Could not enable callback function.")

def interrupt_callback_disable():
Expand Down
11 changes: 8 additions & 3 deletions version_history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
MCC 118 - updated both library and trigger examples.
- Lowered the max SPI clock rate for the MCC 118 to fix communications
issues on some systems.
- Fixed an issue with MCC 152 DIO interrupt callbacks in Python when
running a scan on a separate MCC 118

1.1.0.1: Fixed an issue with resource conflicts when using multiple threads in
1.1.0.1:
- Fixed an issue with resource conflicts when using multiple threads in
the same process (such as using multiple MCC 118s.)

1.1.0: Added support for the MCC 152.
1.1.0:
- Added support for the MCC 152.

1.0.0: Initial release for the MCC 118.
1.0.0:
- Initial release for the MCC 118.

0 comments on commit 697196d

Please sign in to comment.