Ran update-copyright.py.
[pycomedi.git] / pycomedi / chanspec.pyx
1 # Copyright (C) 2011-2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of pycomedi.
4 #
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
8 # version.
9 #
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.
13 #
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/>.
16
17 "Replace Comedi's CR_PACK and related macros with a Python class"
18
19 cimport _comedi_h
20 import constant as _constant
21
22
23 class ChanSpec (_constant.BitwiseOperator):
24     """Channel specification bitfield
25
26     >>> from .constant import AREF, CR
27     >>> c = ChanSpec(chan=1, range=3, aref=AREF.diff,
28     ...     flags=CR.edge|CR.invert)
29     >>> print c
30     <ChanSpec chan:1 range:3 aref:diff flags:edge|invert>
31     >>> c.chan
32     1L
33     >>> c.chan = 2
34     >>> c.chan
35     2L
36     """
37     _fields = ['chan', 'range', 'aref', 'flags']
38     _all = 0xffffffffL
39
40     def __init__(self, chan=0, range=0, aref=0, flags=0):
41         self.value = 0L
42         self.chan = chan
43         self.range = range
44         self.aref = aref
45         self.flags = flags
46
47     def __str__(self):
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))
51
52     def __repr__(self):
53         return self.__str__()
54
55     def _chan_get(self):
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)
61
62     def _range_get(self):
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)
68
69     def _aref_get(self):
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)
76
77     def _flags_get(self):
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)