-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuilder.py
More file actions
234 lines (178 loc) · 7.33 KB
/
Copy pathbuilder.py
File metadata and controls
234 lines (178 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import os
import numpy
from .softioc import dbLoadDatabase
from epicsdbbuilder import *
InitialiseDbd()
LoadDbdFile(os.path.join(os.path.dirname(__file__), 'device.dbd'))
from . import pythonSoftIoc # noqa
PythonDevice = pythonSoftIoc.PythonDevice()
# ----------------------------------------------------------------------------
# Wrappers for PythonDevice record constructors.
#
# The SCAN field for all input records defaults to I/O Intr.
def _in_record(record, name, **fields):
'''For input records we provide some automatic extra features: scanning,
initialisation as appropriate, and blocking puts from outside the IOC.'''
fields.setdefault('SCAN', 'I/O Intr')
if 'initial_value' in fields:
fields.setdefault('PINI', 'YES')
fields.setdefault('DISP', 1)
return getattr(PythonDevice, record)(name, **fields)
def aIn(name, LOPR=None, HOPR=None, **fields):
return _in_record(
'ai', name, LOPR = LOPR, HOPR = HOPR, **fields)
def aOut(name, LOPR=None, HOPR=None, **fields):
fields.setdefault('DRVL', LOPR)
fields.setdefault('DRVH', HOPR)
return PythonDevice.ao(
name, LOPR = LOPR, HOPR = HOPR, **fields)
def boolIn(name, ZNAM=None, ONAM=None, **fields):
return _in_record('bi', name, ZNAM = ZNAM, ONAM = ONAM, **fields)
def boolOut(name, ZNAM=None, ONAM=None, **fields):
return PythonDevice.bo(
name, OMSL = 'supervisory', ZNAM = ZNAM, ONAM = ONAM, **fields)
def longIn(name, LOPR=None, HOPR=None, EGU=None, **fields):
fields.setdefault('MDEL', -1)
return _in_record(
'longin', name, EGU = EGU, LOPR = LOPR, HOPR = HOPR, **fields)
def longOut(name, DRVL=None, DRVH=None, EGU=None, **fields):
return PythonDevice.longout(
name, OMSL = 'supervisory', DRVL = DRVL, DRVH = DRVH, EGU = EGU,
**fields)
# Field name prefixes for mbbi/mbbo records.
_mbbPrefixes = [
'ZR', 'ON', 'TW', 'TH', 'FR', 'FV', 'SX', 'SV', # 0-7
'EI', 'NI', 'TE', 'EL', 'TV', 'TT', 'FT', 'FF'] # 8-15
# All the severity strings supported by <prefix>SV
_severityStrings = ["NO_ALARM", "MINOR", "MAJOR", "INVALID"]
# Converts a list of (option [,severity]) values or tuples into field settings
# suitable for mbbi and mbbo records.
def _process_mbb_values(options, fields):
def process_value(prefix, value, option, severity=None):
fields[prefix + 'ST'] = option
fields[prefix + 'VL'] = value
if severity:
if isinstance(severity, int):
# Map alarm.MINOR_ALARM -> "MINOR"
severity = _severityStrings[severity]
fields[prefix + 'SV'] = severity
# zip() silently ignores extra values in options, so explicitly check length
assert len(options) <= 16, "May not specify more than 16 enum values"
for prefix, (value, option) in zip(_mbbPrefixes, enumerate(options)):
if isinstance(option, tuple):
# The option is tuple consisting of the option name and an optional
# alarm severity.
process_value(prefix, value, *option)
else:
# The option is a simple string naming the option. Assign the
# default numerical value (and no severity setting).
process_value(prefix, value, option)
def mbbIn(name, *options, **fields):
_process_mbb_values(options, fields)
return _in_record('mbbi', name, **fields)
def mbbOut(name, *options, **fields):
_process_mbb_values(options, fields)
return PythonDevice.mbbo(name, OMSL = 'supervisory', **fields)
def stringIn(name, **fields):
return _in_record('stringin', name, **fields)
def stringOut(name, **fields):
return PythonDevice.stringout(name, **fields)
def Action(name, **fields):
return boolOut(name, always_update = True, **fields)
# Converts numpy character code to FTVL value.
NumpyCharCodeToFtvl = {
# The following type codes are supported directly:
'B': 'UCHAR', # ubyte
'b': 'CHAR', # byte
'H': 'USHORT', # ushort
'h': 'SHORT', # short
'i': 'LONG', # intc
'L': 'ULONG', # uint
'l': 'LONG', # int_
'f': 'FLOAT', # single
'd': 'DOUBLE', # float_
'S': 'STRING', # str_
# The following type codes are weakly supported by pretending that
# they're related types.
'?': 'CHAR', # bool_
'p': 'LONG', # intp
'I': 'ULONG', # uintc
'P': 'ULONG', # uintp
# The following type codes are not supported at all:
# q longlong Q ulonglong g longfloat
# F csingle D complex_ G clongfloat
# O object_ U unicode_ V void
}
def _waveform(value, fields):
'''Helper routine for waveform construction. If a value is given it is
interpreted as an initial value and used to configure length and datatype
(unless these are overridden), otherwise length and datatype must be
specified.'''
if 'initial_value' in fields:
assert not value, 'Can\'t specify initial value twice!'
value = (fields.pop('initial_value'),)
if value:
# If a value is specified it should be the *only* non keyword
# argument.
value, = value
value = numpy.array(value)
fields['initial_value'] = value
# Pick up default length and datatype from initial value
length = len(value)
FTVL = NumpyCharCodeToFtvl[value.dtype.char]
else:
# No value specified, so require length and datatype to be specified.
length = fields.pop('length')
FTVL = 'FLOAT'
datatype = fields.pop('datatype', None)
if datatype is not None:
assert 'FTVL' not in fields, \
'Can\'t specify FTVL and datatype together'
FTVL = NumpyCharCodeToFtvl[numpy.dtype(datatype).char]
fields['NELM'] = length
fields.setdefault('FTVL', FTVL)
def Waveform(name, *value, **fields):
_waveform(value, fields)
return _in_record('waveform', name, **fields)
WaveformIn = Waveform
def WaveformOut(name, *value, **fields):
_waveform(value, fields)
return PythonDevice.waveform_out(name, **fields)
# ----------------------------------------------------------------------------
# Support routines for builder
_DatabaseWritten = False
def LoadDatabase():
'''This should be called after all the builder records have been created,
but before calling iocInit(). The database is loaded into EPICS memory,
ready for operation.'''
from tempfile import mkstemp
fd, database = mkstemp('.db')
os.close(fd)
WriteRecords(database)
dbLoadDatabase(database)
os.unlink(database)
pythonSoftIoc.RecordWrapper.reset_builder()
# ----------------------------------------------------------------------------
# Record name configuration. A device name prefix must be specified.
SetSimpleRecordNames(None, ':')
SetDeviceName = SetPrefix
def SetDeviceName(name):
SetPrefix(name)
def UnsetDevice():
SetPrefix(None)
__all__ = [
# Re-exports from epicsdbbuilder
'records',
'PP', 'CP', 'MS', 'NP',
# Wrappers for PythonDevice
'aIn', 'aOut',
'boolIn', 'boolOut',
'longIn', 'longOut',
'stringIn', 'stringOut',
'mbbIn', 'mbbOut',
'Waveform', 'WaveformOut',
'Action',
# Other builder support functions
'LoadDatabase',
'SetDeviceName', 'UnsetDevice'
]