epicsdev 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epicsdev/__init__.py +0 -0
- epicsdev/epicsdev.py +244 -0
- epicsdev-0.0.0.dist-info/METADATA +17 -0
- epicsdev-0.0.0.dist-info/RECORD +6 -0
- epicsdev-0.0.0.dist-info/WHEEL +4 -0
- epicsdev-0.0.0.dist-info/licenses/LICENSE +21 -0
epicsdev/__init__.py
ADDED
|
File without changes
|
epicsdev/epicsdev.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"""Skeleton and helper functions for creating EPICS PVAccess server"""
|
|
2
|
+
# pylint: disable=invalid-name
|
|
3
|
+
__version__= 'v0.0.0 26-01-14'# Created
|
|
4
|
+
#TODO: Do not start if another device is already running
|
|
5
|
+
#TODO: NTEnums do not have structure display
|
|
6
|
+
#TODO: Find a way to indicate that a PV is writable.
|
|
7
|
+
# Options:
|
|
8
|
+
# 1) add structure control with (0,0) limits as indication of Writable.
|
|
9
|
+
# 2) use an extra field of the NTScalar.
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import time
|
|
13
|
+
from p4p.nt import NTScalar, NTEnum
|
|
14
|
+
from p4p.nt.enum import ntenum
|
|
15
|
+
from p4p.server import Server
|
|
16
|
+
from p4p.server.thread import SharedPV
|
|
17
|
+
|
|
18
|
+
#``````````````````Module Storage`````````````````````````````````````````````
|
|
19
|
+
class C_():
|
|
20
|
+
"""Storage for module members"""
|
|
21
|
+
AppName = 'epicsDevLecroyScope'
|
|
22
|
+
cycle = 0
|
|
23
|
+
lastRareUpdate = 0.
|
|
24
|
+
server = None
|
|
25
|
+
serverState = ''
|
|
26
|
+
PVs = {}
|
|
27
|
+
PVDefs = []
|
|
28
|
+
#```````````````````Helper methods````````````````````````````````````````````
|
|
29
|
+
def printTime(): return time.strftime("%m%d:%H%M%S")
|
|
30
|
+
def printi(msg): print(f'inf_@{printTime()}: {msg}')
|
|
31
|
+
def printw(msg):
|
|
32
|
+
txt = f'WAR_@{printTime()}: {msg}'
|
|
33
|
+
print(txt)
|
|
34
|
+
#publish('status',txt)
|
|
35
|
+
def printe(msg):
|
|
36
|
+
txt = f'ERR_{printTime()}: {msg}'
|
|
37
|
+
print(txt)
|
|
38
|
+
#publish('status',txt)
|
|
39
|
+
def _printv(msg, level):
|
|
40
|
+
if pargs.verbose >= level: print(f'DBG{level}: {msg}')
|
|
41
|
+
def printv(msg): _printv(msg, 1)
|
|
42
|
+
def printvv(msg): _printv(msg, 2)
|
|
43
|
+
def printv3(msg): _printv(msg, 3)
|
|
44
|
+
|
|
45
|
+
def pvobj(pvname):
|
|
46
|
+
"""Return PV with given name"""
|
|
47
|
+
return C_.PVs[pargs.prefix+pvname]
|
|
48
|
+
|
|
49
|
+
def pvv(pvname:str):
|
|
50
|
+
"""Return PV value"""
|
|
51
|
+
return pvobj(pvname).current()
|
|
52
|
+
|
|
53
|
+
def publish(pvname:str, value, ifChanged=False, t=None):
|
|
54
|
+
"""Post PV with new value"""
|
|
55
|
+
try:
|
|
56
|
+
pv = pvobj(pvname)
|
|
57
|
+
except KeyError:
|
|
58
|
+
return
|
|
59
|
+
if t is None:
|
|
60
|
+
t = time.time()
|
|
61
|
+
if not ifChanged or pv.current() != value:
|
|
62
|
+
pv.post(value, timestamp=t)
|
|
63
|
+
|
|
64
|
+
def SPV(initial, vtype=None):
|
|
65
|
+
"""Construct SharedPV, vtype should be one of typeCode keys,
|
|
66
|
+
if vtype is None then the nominal type will be determined automatically
|
|
67
|
+
"""
|
|
68
|
+
typeCode = {
|
|
69
|
+
'F64':'d', 'F32':'f', 'I64':'l', 'I8':'b', 'U8':'B', 'I16':'h',
|
|
70
|
+
'U16':'H', 'I32':'i', 'U32':'I', str:'s', 'enum':'enum',
|
|
71
|
+
}
|
|
72
|
+
iterable = type(initial) not in (int,float,str)
|
|
73
|
+
if vtype is None:
|
|
74
|
+
firstItem = initial[0] if iterable else initial
|
|
75
|
+
itype = type(firstItem)
|
|
76
|
+
vtype = {int: 'I32', float: 'F32'}.get(itype,itype)
|
|
77
|
+
tcode = typeCode[vtype]
|
|
78
|
+
if tcode == 'enum':
|
|
79
|
+
initial = {'choices': initial, 'index': 0}
|
|
80
|
+
nt = NTEnum(display=True)#TODO: that does not work
|
|
81
|
+
else:
|
|
82
|
+
prefix = 'a' if iterable else ''
|
|
83
|
+
nt = NTScalar(prefix+tcode, display=True, control=True, valueAlarm=True)
|
|
84
|
+
return SharedPV(nt=nt, initial=initial)
|
|
85
|
+
|
|
86
|
+
#``````````````````Definition of PVs``````````````````````````````````````````
|
|
87
|
+
def _define_PVs():
|
|
88
|
+
"""Example of PV definitions"""
|
|
89
|
+
R,W,SET,U,ENUM,LL,LH = 'R','W','setter','units','enum','limitLow','limitHigh'
|
|
90
|
+
alarm = {'valueAlarm':{'lowAlarmLimit':0, 'highAlarmLimit':100}}
|
|
91
|
+
return [
|
|
92
|
+
# device-specific PVs
|
|
93
|
+
['VoltOffset', 'Offset', SPV(0.), W, {U:'V'}],
|
|
94
|
+
['VoltPerDiv', 'Vertical scale', SPV(0.), W, {U:'V/du'}],
|
|
95
|
+
['TimePerDiv', 'Horizontal scale', SPV('0.01 0.02 0.05 0.1 0.2 0.5 1 2 5'.split(),ENUM), W, {U:'S/du'}],
|
|
96
|
+
['trigDelay', 'Trigger delay', SPV(0.), W, {U:'S'}],
|
|
97
|
+
['Waveform', 'Waveform array', SPV([0.]), R, {}],
|
|
98
|
+
['tAxis', 'Full scale of horizontal axis', SPV([0.]), R, {}],
|
|
99
|
+
['recordLength','Max number of points', SPV(100,'U32'), W, {}],
|
|
100
|
+
['peak2peak', 'Peak-to-peak amplitude', SPV(0.), R, {}],
|
|
101
|
+
['alarm', 'PV with alarm', SPV(0), 'WA', alarm],
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
#``````````````````create_PVs()```````````````````````````````````````````````
|
|
105
|
+
def _create_PVs():
|
|
106
|
+
"""Create PVs"""
|
|
107
|
+
ts = time.time()
|
|
108
|
+
for defs in C_.PVDefs:
|
|
109
|
+
pname,desc,spv,features,extra = defs
|
|
110
|
+
pv = spv
|
|
111
|
+
ivalue = pv.current()
|
|
112
|
+
printv(f'created pv {pname}, initial: {type(ivalue),ivalue}, extra: {extra}')
|
|
113
|
+
C_.PVs[pargs.prefix+pname] = pv
|
|
114
|
+
#if isinstance(ivalue,dict):# NTEnum
|
|
115
|
+
if 'ntenum' in str(type(ivalue)):
|
|
116
|
+
pv.post(ivalue, timestamp=ts)
|
|
117
|
+
else:
|
|
118
|
+
v = pv._wrap(ivalue, timestamp=ts)
|
|
119
|
+
v['display.description'] = desc
|
|
120
|
+
for field in extra.keys():
|
|
121
|
+
if field in ['limitLow','limitHigh','format','units']:
|
|
122
|
+
v[f'display.{field}'] = extra[field]
|
|
123
|
+
if field.startswith('limit'):
|
|
124
|
+
v[f'control.{field}'] = extra[field]
|
|
125
|
+
if field == 'valueAlarm':
|
|
126
|
+
for key,value in extra[field].items():
|
|
127
|
+
v[f'valueAlarm.{key}'] = value
|
|
128
|
+
pv.post(v)
|
|
129
|
+
|
|
130
|
+
# add new attributes. To my surprise that works!
|
|
131
|
+
pv.name = pname
|
|
132
|
+
pv.setter = extra.get('setter')
|
|
133
|
+
|
|
134
|
+
writable = 'W' in features
|
|
135
|
+
if writable:
|
|
136
|
+
@pv.put
|
|
137
|
+
def handle(pv, op):
|
|
138
|
+
ct = time.time()
|
|
139
|
+
vv = op.value()
|
|
140
|
+
vr = vv.raw.value
|
|
141
|
+
if isinstance(vv, ntenum):
|
|
142
|
+
vr = vv
|
|
143
|
+
if pv.setter:
|
|
144
|
+
pv.setter(vr)
|
|
145
|
+
# value could change by the setter
|
|
146
|
+
vr = pvv(pv.name)
|
|
147
|
+
printv(f'putting {pv.name} = {vr}')
|
|
148
|
+
pv.post(vr, timestamp=ct) # update subscribers
|
|
149
|
+
op.done()
|
|
150
|
+
#print(f'PV {pv.name} created: {pv}')
|
|
151
|
+
#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
|
|
152
|
+
#``````````````````Setters
|
|
153
|
+
def set_verbosity(level):
|
|
154
|
+
"""Set verbosity level for debugging"""
|
|
155
|
+
pargs.verbose = level
|
|
156
|
+
publish('verbosity',level)
|
|
157
|
+
|
|
158
|
+
def set_server(state=None):
|
|
159
|
+
"""Example of the setter for the server PV."""
|
|
160
|
+
#printv(f'>set_server({state}), {type(state)}')
|
|
161
|
+
if state is None:
|
|
162
|
+
state = pvv('server')
|
|
163
|
+
printi(f'Setting server state to {state}')
|
|
164
|
+
state = str(state)
|
|
165
|
+
if state == 'Start':
|
|
166
|
+
printi('Starting the server')
|
|
167
|
+
#configure_scope()
|
|
168
|
+
#adopt_local_setting()
|
|
169
|
+
publish('server','Started')
|
|
170
|
+
elif state == 'Stop':
|
|
171
|
+
printi('server stopped')
|
|
172
|
+
publish('server','Stopped')
|
|
173
|
+
elif state == 'Exit':
|
|
174
|
+
printi('server is exiting')
|
|
175
|
+
publish('server','Exited')
|
|
176
|
+
elif state == 'Clear':
|
|
177
|
+
publish('acqCount', 0)
|
|
178
|
+
#publish('lostTrigs', 0)
|
|
179
|
+
#C_.triggersLost = 0
|
|
180
|
+
publish('status','Cleared')
|
|
181
|
+
# set server to previous state
|
|
182
|
+
set_server(C_.serverState)
|
|
183
|
+
C_.serverState = state
|
|
184
|
+
|
|
185
|
+
def poll():
|
|
186
|
+
"""Example of polling function"""
|
|
187
|
+
C_.cycle += 1
|
|
188
|
+
printv(f'cycle {C_.cycle}')
|
|
189
|
+
publish('cycle', C_.cycle)
|
|
190
|
+
|
|
191
|
+
def create_PVs(pvDefs:list):
|
|
192
|
+
"""Creates manadatory PVs and adds PVs, using definitions from pvDEfs list"""
|
|
193
|
+
U,LL,LH = 'units','limitLow','limitHigh'
|
|
194
|
+
C_.PVDefs = [
|
|
195
|
+
['version', 'Program version', SPV(__version__), 'R', {}],
|
|
196
|
+
['status', 'Server status', SPV('?'), 'W', {}],
|
|
197
|
+
['server', 'Server control',
|
|
198
|
+
SPV('Start Stop Clear Exit Started Stopped Exited'.split(), 'enum'),
|
|
199
|
+
'W', {'setter':set_server}],
|
|
200
|
+
['verbosity', 'Debugging verbosity', SPV(0,'U8'), 'W',
|
|
201
|
+
{'setter':set_verbosity}],
|
|
202
|
+
['polling', 'Polling interval', SPV(1.0), 'W', {U:'S', LL:0.001, LH:10.1}],
|
|
203
|
+
['cycle', 'Cycle number', SPV(0,'U32'), 'R', {}],
|
|
204
|
+
]
|
|
205
|
+
# append application PVs, defined in define_PVs()
|
|
206
|
+
C_.PVDefs += pvDefs
|
|
207
|
+
_create_PVs()
|
|
208
|
+
return C_.PVs
|
|
209
|
+
|
|
210
|
+
#``````````````````Example of the Main() function````````````````````````````
|
|
211
|
+
if __name__ == "__main__":
|
|
212
|
+
# Argument parsing
|
|
213
|
+
parser = argparse.ArgumentParser(description = __doc__,
|
|
214
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
215
|
+
epilog=f'{__version__}')
|
|
216
|
+
parser.add_argument('-c','--channels', type=int, default=4, help=
|
|
217
|
+
'Number of channels in the scope')
|
|
218
|
+
parser.add_argument('-p', '--prefix', default='epicsDev:', help=
|
|
219
|
+
'Prefix to be prepended to all PVs')
|
|
220
|
+
parser.add_argument('-l', '--listPVs', action='store_true', help=\
|
|
221
|
+
'List all generated PVs')
|
|
222
|
+
parser.add_argument('-v', '--verbose', action='count', default=0, help=\
|
|
223
|
+
'Show more log messages (-vv: show even more)')
|
|
224
|
+
pargs = parser.parse_args()
|
|
225
|
+
|
|
226
|
+
PVs = create_PVs(_define_PVs())# Provide your PV definitions instead of _define_PVs()
|
|
227
|
+
|
|
228
|
+
# List the PVs
|
|
229
|
+
if pargs.listPVs:
|
|
230
|
+
print(f'List of PVs:')
|
|
231
|
+
for pvname in PVs:
|
|
232
|
+
print(pvname)
|
|
233
|
+
|
|
234
|
+
# Start the Server. Use your set_server, if needed.
|
|
235
|
+
set_server('Start')
|
|
236
|
+
|
|
237
|
+
# Main loop
|
|
238
|
+
server = Server(providers=[PVs])
|
|
239
|
+
printi(f'Server started with polling interval {repr(pvv("polling"))} S.')
|
|
240
|
+
while not C_.serverState.startswith('Exit'):
|
|
241
|
+
time.sleep(pvv("polling"))
|
|
242
|
+
if not C_.serverState.startswith('Stop'):
|
|
243
|
+
poll()
|
|
244
|
+
printi('Server is exited')
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: epicsdev
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: Helper module for creating EPICS PVAccess servers using p4p
|
|
5
|
+
Project-URL: Homepage, https://github.com/ASukhanov/epicsdev
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/ASukhanov/epicsdev
|
|
7
|
+
Author-email: Andrey Sukhanov <sukhanov@bnl.gov>
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Requires-Dist: p4p
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# epicsdev
|
|
17
|
+
Helper module for creating EPICS PVAccess servers.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
epicsdev/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
epicsdev/epicsdev.py,sha256=BiiRZJw9CsmUO0arADg9ZKiiSv-uzvcoh02T6jQ7y_o,8913
|
|
3
|
+
epicsdev-0.0.0.dist-info/METADATA,sha256=7JnJzpkOMPVVH4mqjv6wQBqTUmA_LHH-Jh6s9AWkRV8,608
|
|
4
|
+
epicsdev-0.0.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
5
|
+
epicsdev-0.0.0.dist-info/licenses/LICENSE,sha256=qj3cUKUrX4oXTb0NwuJQ44ThYDEMUfOeIjw9kkT6Qck,1072
|
|
6
|
+
epicsdev-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrey Sukhanov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|