1 # Copyright (C) 2011-2012 W. Trevor King <wking@tremily.us>
3 # This file is part of pycomedi.
5 # pycomedi is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 2 of the License, or (at your option) any later
10 # pycomedi is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # pycomedi. If not, see <http://www.gnu.org/licenses/>.
17 "Replace Comedi's CR_PACK and related macros with a Python class"
19 from pycomedi cimport _comedi_h
20 from . import constant as _constant
23 class ChanSpec (_constant.BitwiseOperator):
24 """Channel specification bitfield
26 >>> from .constant import AREF, CR
27 >>> c = ChanSpec(chan=1, range=3, aref=AREF.diff,
28 ... flags=CR.edge|CR.invert)
30 <ChanSpec chan:1 range:3 aref:diff flags:edge|invert>
37 _fields = ['chan', 'range', 'aref', 'flags']
40 def __init__(self, chan=0, range=0, aref=0, flags=0):
48 # TODO: consolidate to a utility class or function
49 fields = ['%s:%s' % (f, getattr(self, f)) for f in self._fields]
50 return '<%s %s>' % (self.__class__.__name__, ' '.join(fields))
56 return self.value & 0xff
57 def _chan_set(self, value):
58 self.value &= self._all - 0xff
59 self.value |= _constant.bitwise_value(value) & 0xff
60 chan = property(fget=_chan_get, fset=_chan_set)
63 return (self.value >> 16) & 0xff
64 def _range_set(self, value):
65 self.value &= self._all - (0xff << 16)
66 self.value |= (_constant.bitwise_value(value) & 0xff) << 16
67 range = property(fget=_range_get, fset=_range_set)
70 v = (self.value >> 24) & 0x03
71 return _constant.AREF.index_by_value(v)
72 def _aref_set(self, value):
73 self.value &= self._all - (0x03 << 24)
74 self.value |= (_constant.bitwise_value(value) & 0x03) << 24
75 aref = property(fget=_aref_get, fset=_aref_set)
78 v = self.value & _constant.CR._all.value
79 return _constant.FlagValue(_constant.CR, v)
80 def _flags_set(self, value):
81 self.value &= self._all - _constant.CR._all.value
82 self.value |= _constant.bitwise_value(value) & _constant.CR._all.value
83 flags = property(fget=_flags_get, fset=_flags_set)