77InitialiseDbd ()
88LoadDbdFile (os .path .join (os .path .dirname (__file__ ), 'device.dbd' ))
99
10- from . import pythonSoftIoc # noqa
10+ from . import device , pythonSoftIoc # noqa
11+
1112PythonDevice = pythonSoftIoc .PythonDevice ()
1213
1314
@@ -120,7 +121,7 @@ def Action(name, **fields):
120121 'l' : 'LONG' , # int_
121122 'f' : 'FLOAT' , # single
122123 'd' : 'DOUBLE' , # float_
123- 'S' : 'STRING' , # str_
124+ # 'S': 'STRING', # str_
124125
125126 # The following type codes are weakly supported by pretending that
126127 # they're related types.
@@ -135,6 +136,19 @@ def Action(name, **fields):
135136 # O object_ U unicode_ V void
136137}
137138
139+ # Coverts FTVL string to numpy type
140+ DbfStringToNumpy = {
141+ # 'STRING': numpy.dtype('S40'), # Don't think we want this!
142+ 'CHAR' : numpy .dtype ('int8' ),
143+ 'UCHAR' : numpy .dtype ('uint8' ),
144+ 'SHORT' : numpy .dtype ('int16' ),
145+ 'USHORT' : numpy .dtype ('uint16' ),
146+ 'LONG' : numpy .dtype ('int32' ),
147+ 'ULONG' : numpy .dtype ('uint32' ),
148+ 'FLOAT' : numpy .dtype ('float32' ),
149+ 'DOUBLE' : numpy .dtype ('float64' ),
150+ }
151+
138152
139153def _waveform (value , fields ):
140154 '''Helper routine for waveform construction. If a value is given it is
@@ -146,29 +160,34 @@ def _waveform(value, fields):
146160 assert not value , 'Can\' t specify initial value twice!'
147161 value = (fields .pop ('initial_value' ),)
148162
163+ # Datatype can be specified as keyword argument, taken from FTVL, or derived
164+ # from the initial value
165+ if 'datatype' in fields :
166+ assert 'FTVL' not in fields , \
167+ 'Can\' t specify FTVL and datatype together'
168+ datatype = numpy .dtype (fields .pop ('datatype' ))
169+ elif 'FTVL' in fields :
170+ datatype = DbfStringToNumpy [fields ['FTVL' ]]
171+ else :
172+ # No datatype specified, will have to infer from initial value
173+ datatype = None
174+
149175 if value :
150- # If a value is specified it should be the *only* non keyword
151- # argument.
176+ # If a value is specified it should be the *only* non keyword argument.
152177 value , = value
153- value = numpy .array (value )
154- fields ['initial_value' ] = value
155-
156- # Pick up default length and datatype from initial value
157- length = len (value )
158- FTVL = NumpyCharCodeToFtvl [value .dtype .char ]
178+ initial_value = device ._require_waveform (value , datatype )
179+ length = fields .pop ('length' , len (initial_value ))
159180 else :
160- # No value specified, so require length and datatype to be specified.
181+ initial_value = numpy . array ([], dtype = datatype )
161182 length = fields .pop ('length' )
162- FTVL = 'FLOAT'
183+ datatype = initial_value . dtype
163184
164- datatype = fields .pop ('datatype' , None )
165- if datatype is not None :
166- assert 'FTVL' not in fields , \
167- 'Can\' t specify FTVL and datatype together'
168- FTVL = NumpyCharCodeToFtvl [numpy .dtype (datatype ).char ]
185+ fields ['initial_value' ] = initial_value
186+ fields ['_wf_nelm' ] = length
187+ fields ['_wf_dtype' ] = datatype
169188
170189 fields ['NELM' ] = length
171- fields . setdefault ( 'FTVL' , FTVL )
190+ fields [ 'FTVL' ] = NumpyCharCodeToFtvl [ datatype . char ]
172191
173192
174193def Waveform (name , * value , ** fields ):
@@ -191,6 +210,10 @@ def _long_string(fields):
191210 # Default length of 256
192211 length = 256
193212
213+ fields .setdefault ('initial_value' , '' )
214+ fields ['_wf_nelm' ] = length
215+ fields ['_wf_dtype' ] = numpy .dtype ('uint8' )
216+
194217 fields ['NELM' ] = length
195218 fields ['FTVL' ] = 'UCHAR'
196219
0 commit comments