Skip to content

Commit

Permalink
Adding MCC 134 and fixing known issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Wright committed Jun 10, 2019
2 parents 697196d + 137be5e commit 8f7eea7
Show file tree
Hide file tree
Showing 112 changed files with 8,772 additions and 410 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<tr><td>Info</td><td>Contains C and Python Libraries for interacting with
Measurement Computing DAQ HAT boards.</td></tr>
<tr><td>Author</td><td>Measurement Computing</td></tr>
<tr><td>Library Version<td>1.1.0.2</td></tr>
<tr><td>Library Version<td>1.2.0.0</td></tr>
</table>

## About
Expand All @@ -13,11 +13,12 @@ This is the development repository for Measurement Computing DAQ HAT boards. The
#### Supported MCC DAQ HAT hardware
Hardware supported by this version of the MCC DAQ HAT Library:
- [MCC 118](https://mccdaq.github.io/daqhats/overview.html#mcc-118)
- [MCC 134](https://mccdaq.github.io/daqhats/overview.html#mcc-134)
- [MCC 152](https://mccdaq.github.io/daqhats/overview.html#mcc-152)

## Prerequisites
- Raspbian or Raspbian Lite image (may work with other Raspberry Pi operating systems)
- Raspberry Pi A+, B+, 2, or 3
- Raspberry Pi A+, B+, 2, or 3 (A+, B, B+)
- C, C++, Python 2.7 or Python 3.4

## Raspberry Pi Configuration
Expand Down
1 change: 1 addition & 0 deletions daqhats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
interrupt_callback_enable, interrupt_callback_disable, HatCallback
from daqhats.mcc118 import mcc118
from daqhats.mcc152 import mcc152, DIOConfigItem
from daqhats.mcc134 import mcc134, TcTypes
56 changes: 32 additions & 24 deletions daqhats/hats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
"""
from collections import namedtuple
from ctypes import cdll, Structure, c_ubyte, c_ushort, c_char, c_int, POINTER, \
CFUNCTYPE, cast, py_object, c_void_p, pointer
CFUNCTYPE, c_void_p
from enum import IntEnum

_HAT_CALLBACK = None

class HatIDs(IntEnum):
"""Known MCC HAT IDs."""
ANY = 0 #: Match any MCC ID in :py:func:`hat_list`
MCC_118 = 0x0142 #: MCC 118 ID
MCC_134 = 0x0143 #: MCC 134 ID
MCC_152 = 0x0144 #: MCC 152 ID

class TriggerModes(IntEnum):
Expand All @@ -28,6 +31,7 @@ class OptionFlags(IntEnum):
EXTCLOCK = 0x0004 #: Use an external clock source.
EXTTRIGGER = 0x0008 #: Use an external trigger source.
CONTINUOUS = 0x0010 #: Run until explicitly stopped.
TEMPERATURE = 0x0020 #: Return temperature (MCC 134)

# exception class
class HatError(Exception):
Expand Down Expand Up @@ -75,7 +79,7 @@ def __init__(self, function):
if not callable(function):
raise TypeError("Argument 1 must be a function or method.")
self.function = function
self.cbfunctype = CFUNCTYPE(None, c_void_p)
self.cbfunctype = CFUNCTYPE(None)
self.cbfunc = None
self.user_data = None

Expand All @@ -85,7 +89,7 @@ def get_callback_func(self):
get passed to the library function, and assign it to a variable to
avoid it getting garbage collected.
"""
def func(user_data):
def func():
"""
Function wrapper.
"""
Expand All @@ -96,7 +100,7 @@ def func(user_data):
def handle_callback(self):
"""
This is directly called from the interrupt thread. It calls the user's
callback, passing the user_data object that gets set with
callback, passing the user_data object that gets set with
interrupt_callback_enable().
"""
self.function(self.user_data)
Expand Down Expand Up @@ -180,15 +184,15 @@ def interrupt_state():
This function only applies when using devices that can generate an
interrupt:
* MCC 152
Returns:
bool: The interrupt status.
"""
_libc = _load_daqhats_library()
if _libc == 0:
return []
return False

_libc.hat_interrupt_state.argtypes = []
_libc.hat_interrupt_state.restype = c_int
Expand All @@ -204,11 +208,11 @@ def wait_for_interrupt(timeout):
Pass a timeout in seconds. Pass -1 to wait forever or 0 to return
immediately. If the interrupt has not occurred before the timeout elapses
the function will return 0.
the function will return False.
This function only applies when using devices that can generate an
interrupt:
* MCC 152
Returns:
Expand All @@ -217,7 +221,7 @@ def wait_for_interrupt(timeout):
"""
_libc = _load_daqhats_library()
if _libc == 0:
return []
return False

_libc.hat_wait_for_interrupt.argtypes = [c_int]
_libc.hat_wait_for_interrupt.restype = c_int
Expand Down Expand Up @@ -252,17 +256,16 @@ def interrupt_callback_enable(callback, user_data):
if an interrupt occurs. The data argument to this function will be passed
to the callback function when it is called.
The callback function must be encapsulated in a :py:class:`HatCallback`
class. For example: ::
The callback function must have the form "callback(user_data)". For
example: ::
def my_function(data):
# This is my callback function.
print("The interrupt occurred, and returned {}.".format(data))
data[0] += 1
value = [0]
callback = HatCallback(my_function)
interrupt_enable_callback(callback, value)
interrupt_enable_callback(my_function, value)
In this example *my_function()* will be called when the interrupt occurs,
and the list *value* will be passed as the user_data. Inside the callback it
Expand All @@ -277,12 +280,11 @@ def my_function(data):
This function only applies when using devices that can generate an
interrupt:
* MCC 152
Args:
callback (:py:class:`HatCallback`): The callback function encapsulated
in a :py:class:`HatCallback` class.
callback (callback function): The callback function.
user_data (object) Optional Python object or data to pass to the
callback function.
Expand All @@ -291,14 +293,17 @@ def my_function(data):
"""
_libc = _load_daqhats_library()
if _libc == 0:
return []
return

# callback must be an instance of HatCallback
if not isinstance(callback, HatCallback):
raise TypeError("Argument 1 must be an instance of HatCallback")
# callback must be an instance of HatCallback; legacy code may already
# encapsulate it, so handle both cases
if isinstance(callback, HatCallback):
my_callback = callback
else:
my_callback = HatCallback(callback)

# function argtype is provided by callback class
_libc.hat_interrupt_callback_enable.argtypes = [callback.cbfunctype,
_libc.hat_interrupt_callback_enable.argtypes = [my_callback.cbfunctype,
c_void_p]
_libc.hat_interrupt_callback_enable.restype = c_int

Expand All @@ -309,6 +314,9 @@ def my_function(data):
if (_libc.hat_interrupt_callback_enable(callback.get_callback_func(),
None) != 0):
raise Exception("Could not enable callback function.")
# save reference so it isn't garbage collected
global _HAT_CALLBACK # pylint: disable=global-statement
_HAT_CALLBACK = my_callback

def interrupt_callback_disable():
"""
Expand All @@ -319,13 +327,13 @@ def interrupt_callback_disable():
"""
_libc = _load_daqhats_library()
if _libc == 0:
return []
return

_libc.hat_interrupt_callback_disable.argtypes = []
_libc.hat_interrupt_callback_disable.restype = c_int

if _libc.hat_interrupt_callback_disable() != 0:
raise Exception("Could not disabled callback function.")
raise Exception("Could not disable callback function.")

class Hat(object): # pylint: disable=too-few-public-methods
"""
Expand Down
9 changes: 7 additions & 2 deletions daqhats/mcc118.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ def __init__(self, address=0):
self._lib.mcc118_test_trigger.argtypes = [c_ubyte, POINTER(c_ubyte)]
self._lib.mcc118_test_trigger.restype = c_int

if self._lib.mcc118_open(self._address) != self._RESULT_SUCCESS:
self._initialized = False
result = self._lib.mcc118_open(self._address)

if result == self._RESULT_SUCCESS:
self._initialized = True
elif result == self._RESULT_INVALID_DEVICE:
raise HatError(self._address, "Invalid board type.")
else:
raise HatError(self._address, "Board not responding.")

return
Expand Down
Loading

0 comments on commit 8f7eea7

Please sign in to comment.