command: Fix except declaration for get_comedi_cmd_pointer
[pycomedi.git] / pycomedi / range.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 "Wrap `comedi_range` in a Python class"
18
19 from pycomedi.constant cimport BitwiseOperator as _BitwiseOperator
20 from . import constant as _constant
21
22
23 cdef class Range (_BitwiseOperator):
24     """Stucture displaying a possible channel range
25
26     Warning: You probably want to use `channel.Channel.get_range()` or
27     `channel.Channel.find_range()` rather than initializing this
28     stucture by hand.  If you do initialize it by hand (or set any
29     values by hand), remember that it may no longer correspond to your
30     devices built-in range with that index.
31
32     For consistency with other integer wrappers, the range index is
33     stored in the `.value` attribute.
34
35     >>> from constant import UNIT
36     >>> r = Range(1)
37     >>> r
38     <Range unit:volt min:0.0 max:0.0>
39     >>> r.value
40     1
41     >>> r.unit = UNIT.mA
42     >>> r.min = -2.71828
43     >>> r.max = 3.14159
44     >>> r
45     <Range unit:mA min:-2.71828 max:3.14159>
46     >>> r.unit
47     <_NamedInt mA>
48     """
49     def __cinit__(self):
50         self.value = -1
51
52     def __init__(self, value):
53         self.range.unit = 0
54         self.range.min = 0
55         self.range.max = 0
56         self.value = value
57
58     cdef set_comedi_range(self, _comedilib_h.comedi_range range):
59         self.range = range
60
61     def __str__(self):
62         fields = ['%s:%s' % (f, getattr(self, f))
63                   for f in ['unit', 'min', 'max']]
64         return '<%s %s>' % (self.__class__.__name__, ' '.join(fields))
65
66     def __repr__(self):
67         return self.__str__()
68
69     def _unit_get(self):
70         return _constant.UNIT.index_by_value(self.range.unit)
71     def _unit_set(self, value):
72         self.range.unit = _constant.bitwise_value(value)
73     unit = property(fget=_unit_get, fset=_unit_set)
74
75     def _min_get(self):
76         return self.range.min
77     def _min_set(self, value):
78         self.range.min = value
79     min = property(fget=_min_get, fset=_min_set)
80
81     def _max_get(self):
82         return self.range.max
83     def _max_set(self, value):
84         self.range.max = value
85     max = property(fget=_max_get, fset=_max_set)