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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
|
"""
wx utility functions for Epics and wxPython interaction
"""
import wx
import time
import sys
import epics
import wx.lib.buttons as buttons
import wx.lib.agw.floatspin as floatspin
PyDeadObjectError = Exception
from .wxutils import Closure, FloatCtrl, set_float
def EpicsFunction(f):
"""decorator to wrap function in a wx.CallAfter() so that
Epics calls can be made in a separate thread, and asynchronously.
This decorator should be used for all code that mix calls to
wx and epics
"""
def wrapper(*args, **kwargs):
"callafter wrapper"
try:
wx.CallAfter(f, *args, **kwargs)
except PyDeadObjectError:
pass
return wrapper
def DelayedEpicsCallback(fcn):
"""decorator to wrap an Epics callback in a wx.CallAfter,
so that the wx and epics ca threads do not clash
This also checks for dead wxPython objects (say, from a
closed window), and remove callbacks to them.
"""
def wrapper(*args, **kw):
"callafter wrapper"
def cb():
"default callback"
try:
fcn(*args, **kw)
except PyDeadObjectError:
cb_index, pv = kw.get('cb_info', (None, None))
if hasattr(pv, 'remove_callback'):
try:
pv.remove_callback(index=cb_index)
except RuntimeError:
pass
if wx.GetApp() is not None:
return wx.CallAfter(cb)
return wrapper
@EpicsFunction
def finalize_epics():
"""explicitly finalize and cleanup epics so as to
prevent core-dumps on exit.
"""
epics.ca.finalize_libca()
epics.ca.poll()
class EpicsTimer:
""" Epics Event Timer:
combines a wxTimer and epics.ca.pend_event to cause Epics Event Processing
within a wx Application.
>>> my_timer = EpicsTimer(parent, period=100)
period is in milliseconds. At each period, epics.ca.poll() will be run.
"""
def __init__(self, parent, period=100, start=True):
self.parent = parent
self.period = period
self.timer = wx.Timer(parent)
self.parent.Bind(wx.EVT_TIMER, self.pend)
if start:
self.StartTimer()
def StopTimer(self):
"stop timer"
self.timer.Stop()
def StartTimer(self):
"start timer"
self.timer.Start(self.period)
@EpicsFunction
def pend(self, event=None):
"pend/poll"
epics.ca.poll()
event.Skip()
class PVMixin(object):
""" base class mixin for any class that needs PV wx callback
support.
If you're working with wxwidgets controls, see PVCtrlMixin.
If you're working with wx OGL drawing, see ogllib.pvShapeMixin.
Classes deriving directly from PVMixin must override OnPVChange()
"""
def __init__(self, pv=None, pvname=None):
self.pv = None
if pv is None and pvname is not None:
pv = pvname
if pv is not None:
self.SetPV(pv)
self._popupmenu = None
self.Bind(wx.EVT_RIGHT_DOWN, self._onRightDown)
def _onRightDown(self, event=None):
"""right button down: show pop-up"""
if event is None:
return
if self._popupmenu is None:
self.build_popupmenu()
wx.CallAfter(self.PopupMenu, self._popupmenu, event.GetPosition())
event.Skip()
def build_popupmenu(self, event=None):
self._copy_name = wx.NewId()
self._copy_val = wx.NewId()
self._show_info = wx.NewId()
self._popupmenu = wx.Menu()
self._popupmenu.Append(self._copy_name, 'Copy PV Name to Clipboard')
self._popupmenu.Append(self._copy_val, 'Copy PV Value to Clipboard')
self._popupmenu.AppendSeparator()
self._popupmenu.Append(self._show_info, 'Show PV Info')
self.Bind(wx.EVT_MENU, self.copy_name, id=self._copy_name)
self.Bind(wx.EVT_MENU, self.copy_val, id=self._copy_val)
self.Bind(wx.EVT_MENU, self.show_info, id=self._show_info)
@EpicsFunction
def copy_name(self, event=None):
dat = wx.TextDataObject()
dat.SetText(self.pv.pvname)
wx.TheClipboard.Open()
wx.TheClipboard.SetData(dat)
wx.TheClipboard.Close()
@EpicsFunction
def copy_val(self, event=None):
dat = wx.TextDataObject()
dat.SetText(self.pv.get(as_string=True))
wx.TheClipboard.Open()
wx.TheClipboard.SetData(dat)
wx.TheClipboard.Close()
@EpicsFunction
def show_info(self, event=None):
dlg = wx.MessageDialog(self, self.pv.info,
'Info for %s' % self.pv.pvname,
wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
@EpicsFunction
def SetPV(self, pv=None):
"set pv, either an epics.PV object or a pvname"
if pv is None:
return
if isinstance(pv, epics.PV):
self.pv = pv
elif isinstance(pv, str):
form = "ctrl" if len(self._fg_colour_alarms) > 0 or len(self._bg_colour_alarms) > 0 else "native"
self.pv = epics.get_pv(pv, form=form)
self.pv.connect()
epics.poll()
self.pv.connection_callbacks.append(self.OnEpicsConnect)
if self.pv.connected:
self.OnEpicsConnect(pvname=self.pv.pvname, conn=True, pv=self.pv)
self.pv.get_ctrlvars()
self.OnPVChange(self.pv.get(as_string=True))
ncback = len(self.pv.callbacks) + 1
self.pv.add_callback(self._pvEvent, wid=self.GetId(), cb_info=ncback)
# print " PVMixin add callback for ", self.pv, self.pv.chid, len(self.pv.callbacks)
@DelayedEpicsCallback
def OnEpicsConnect(self, pvname=None, conn=None, pv=None):
"""Connect Callback:
Enable/Disable widget on change in connection status
"""
pass
@DelayedEpicsCallback
def _pvEvent(self, pvname=None, value=None, wid=None,
char_value=None, **kws):
"epics PV callback function"
if pvname is None or value is None or wid is None:
return
if char_value is None and value is not None:
prec = kws.get('precision', None)
if prec not in (None, 0):
char_value = ("%%.%if" % prec) % value
else:
char_value = set_float(value)
self.OnPVChange(char_value)
@EpicsFunction
def Update(self, value=None):
"update value"
if value is None and self.pv is not None:
value = self.pv.get(as_string=True)
self.OnPVChange(value)
@EpicsFunction
def GetValue(self, as_string=True):
"return value"
val = self.pv.get(as_string=as_string)
result = self.translations.get(val, val)
return result
def OnPVChange(self, str_value):
"""method is called any time the PV value changes, via
Update() or via a PV callback
"""
self._warn("Must override OnPVChange")
def _warn(self, msg):
"write warning"
sys.stderr.write("%s for pv='%s'\n" % (msg, self.pv.pvname))
@EpicsFunction
def GetEnumStrings(self):
"""try to get list of enum strings,
returns enum strings or None"""
epics.poll()
out = None
if isinstance(self.pv, epics.PV):
self.pv.get_ctrlvars()
if self.pv.type == 'enum':
out =list(self.pv.enum_strs)
return out
class PVCtrlMixin(PVMixin):
"""
mixin for wx Controls with epics PVs: connects to PV,
and manages callback events for the PV
An overriding class must provide a method called _SetValue, which
will set the contents of the corresponding widget.
Optional Features for descendents
=================================
* Set a translation dictionary of PVValue->Python Value to be used
whenever values are received via PV callbacks.
* Set translation tables for setting particular foreground/background
colours when the PV takes certain values.
* Override foreground/background colours - without knowing what
colour is currently set by the user, you can call
OverrideForegroundColour()/OverrideBackGroundColor() to set a
different colour on the control and then call the override again
with None to go back to the original colour.
"""
def __init__(self, pv=None, pvname=None, font=None, fg=None, bg=None):
PVMixin.__init__(self, pv, pvname)
self._translations = {}
self._fg_colour_translations = None
self._bg_colour_translations = None
self._fg_colour_alarms = {}
self._bg_colour_alarms = {}
self._default_fg_colour = None
self._default_bg_colour = None
try:
if font is not None:
self.SetFont(font)
if fg is not None:
self.SetForegroundColour(fg)
if bg is not None:
self.SetBackgroundColour(fg)
except:
pass
self._connect_bgcol = self.GetBackgroundColour()
self._connect_fgcol = self.GetForegroundColour()
@DelayedEpicsCallback
def OnEpicsConnect(self, pvname=None, conn=None, pv=None):
"""Connect Callback:
Enable/Disable widget on change in connection status
"""
PVMixin.OnEpicsConnect(self, pvname, conn, pv)
action = getattr(self, 'Enable', None)
bgcol = self._connect_bgcol
fgcol = self._connect_fgcol
if not conn:
action = getattr(self, 'Disable', None)
self._connect_bgcol = self.GetBackgroundColour()
self._connect_fgcol = self.GetForegroundColour()
bgcol = wx.Colour(240, 240, 210)
fgcol = wx.Colour(200, 100, 100)
if action is not None:
self.SetBackgroundColour(bgcol)
self.SetForegroundColour(fgcol)
action()
def SetTranslations(self, translations):
"""
Pass a dictionary of value->value translations here if you want some P
PV values to automatically appear in the event callback as a different
value.
ie, to override PV value 0.0 to say "Disabled", call this method as
control.SetTranslations({ 0.0 : "Disabled" })
It is recommended that you use this function only when it is not
possible to change the PV value in the database, or set a string
value in the database.
"""
self._translations = translations
def SetForegroundColourTranslations(self, translations):
"""
Pass a dictionary of value->colour translations here if you want the
control to automatically set foreground colour based on PV value.
Values used to lookup colours will be string values if available,
but will otherwise be the raw PV value.
Colour values in the dictionary may be strings or wx.Colour objects.
"""
self._fg_colour_translations = translations
def SetBackgroundColourTranslations(self, translations):
"""
Pass a dictionary of value->colour translations here if you want the
control to automatically set background colour based on PV value.
Values used to lookup colours will be string values if available,
but will otherwise be the raw PV value.
Colour values in the dictionary may be strings or wx.Colour objects.
"""
self._bg_colour_translations = translations
def SetForegroundColour(self, colour):
""" (Internal override) Needed to support OverrideForegroundColour()
"""
if self._default_fg_colour is None:
wx.Window.SetForegroundColour(self, colour)
else:
self._default_fg_colour = colour
def GetForegroundColour(self):
""" (Internal override) Needed to support OverrideForegroundColour()
"""
return self._default_fg_colour if self._default_fg_colour is not None \
else wx.Window.GetForegroundColour(self)
def SetBackgroundColour(self, colour):
""" (Internal override) Needed to support OverrideBackgroundColour()
"""
if self._default_bg_colour is None:
wx.Window.SetBackgroundColour(self, colour)
else:
self._default_bg_colour = colour
def GetBackgroundColour(self):
""" (Internal override) Needed to support OverrideBackgroundColour()
"""
return self._default_bg_colour if self._default_bg_colour is not None \
else wx.Window.GetBackgroundColour(self)
def OverrideForegroundColour(self, colour):
"""
Call this method to override the control's current set foreground
colour. Call with colour=None to disable overriding and go back to
whatever was set.
Overriding allows SetForegroundColour() to still work as expected,
except when the "override" is set.
"""
if colour is None:
if self._default_fg_colour is not None:
wx.Window.SetForegroundColour(self, self._default_fg_colour)
self._default_fg_colour = None
else:
if self._default_fg_colour is None:
self._default_fg_colour = wx.Window.GetForegroundColour(self)
wx.Window.SetForegroundColour(self, colour)
def OverrideBackgroundColour(self, colour):
"""
Call this method to override the control's current set background
colour, Call with colour=None to disable overriding and go back to
whatever was set.
Overriding allows SetForegroundColour() to still work as expected,
except when the "override" is set.
"""
if colour is None:
if self._default_bg_colour is not None:
wx.Window.SetBackgroundColour(self, self._default_bg_colour)
else:
if self._default_bg_colour is None:
self._default_bg_colour = wx.Window.GetBackgroundColour(self)
wx.Window.SetBackgroundColour(self, colour)
def _SetValue(self, value):
"set value -- must override"
self._warn("must override _SetValue")
@EpicsFunction
def OnPVChange(self, raw_value):
"called by PV callback"
try:
if self.pv is None:
return
if self.pv.form == "native" \
and ( len(self._fg_colour_alarms) > 0 or
len(self._bg_colour_alarms) > 0 ):
# native PVs don't update severity on callback,
# so we need to do the same manually
self.pv.get_ctrlvars()
colour = None
if self._fg_colour_translations is not None and \
raw_value in self._fg_colour_translations:
colour = self._fg_colour_translations[raw_value]
elif self.pv.severity in self._fg_colour_alarms:
colour = self._fg_colour_alarms[self.pv.severity]
self.OverrideForegroundColour(colour)
colour = None
if self._bg_colour_translations is not None and \
raw_value in self._bg_colour_translations:
colour = self._bg_colour_translations[raw_value]
elif self.pv.severity in self._bg_colour_alarms:
colour = self._bg_colour_alarms[self.pv.severity]
self.OverrideBackgroundColour(colour)
try:
self._SetValue(self._translations.get(raw_value, raw_value))
except TypeError:
pass
except PyDeadObjectError:
pass
class PVTextCtrl(wx.TextCtrl, PVCtrlMixin):
"""
Text control (ie textbox) for PV display (as normal string),
with callback for automatic updates and option to write value
back on input
"""
def __init__(self, parent, pv=None,
font=None, fg=None, bg=None, dirty_timeout=2500, **kws):
if 'style' not in kws:
kws['style'] = wx.TE_PROCESS_ENTER
else:
kws['style'] |= wx.TE_PROCESS_ENTER
wx.TextCtrl.__init__(self, parent, wx.ID_ANY, value='', **kws)
PVCtrlMixin.__init__(self, pv=pv, font=font, fg=fg, bg=bg)
self.Bind(wx.EVT_CHAR, self.OnChar)
self.dirty_timeout = dirty_timeout
self.dirty_writeback_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnWriteback)
self.Bind(wx.EVT_KILL_FOCUS, self.OnWriteback)
self.Bind(wx.EVT_CHAR, self.OnChar)
@EpicsFunction
def _caput(self, value):
"epics pv.put wrapper"
self.pv.put(str(value))
def SetValue(self, value):
"override all setvalue"
self._caput(value)
def _SetValue(self, value):
"set widget value"
wx.TextCtrl.SetValue(self, value)
def OnChar(self, event):
"char event handler"
if event.KeyCode == wx.WXK_RETURN:
self.OnWriteback()
else:
if self.dirty_timeout is not None:
self.dirty_writeback_timer.Start(self.dirty_timeout)
event.Skip()
def OnWriteback(self, event=None):
""" writeback the currently displayed value to the PV """
self.dirty_writeback_timer.Stop()
entry = str(self.GetValue().strip())
self.SetValue(entry)
class PVText(wx.StaticText, PVCtrlMixin):
""" Static text for displaying a PV value,
with callback for automatic updates
By default the text colour will change on alarm states.
This can be overriden or disabled as constructor
parameters
"""
def __init__(self, parent, pv=None, as_string=True,
font=None, fg=None, bg=None, style=None,
minor_alarm="DARKRED", major_alarm="RED",
invalid_alarm="ORANGERED", auto_units=False, units="", **kw):
"""
Create a new pvText
minor_alarm, major_alarm & invalid_alarm are all text colours
that will be set depending no the alarm state of the target
PV. Set to None if you want no highlighting in that alarm state.
auto_units means the PV value will be displayed with the EGU
"engineering units" as a suffix. Alternately, you can specify
an explicit unit string.
"""
wstyle = wx.ALIGN_LEFT
if style is not None:
wstyle = style
wx.StaticText.__init__(self, parent, wx.ID_ANY, label='',
style=wstyle, **kw)
PVCtrlMixin.__init__(self, pv=pv, font=font, fg=fg, bg=bg)
self.as_string = as_string
self.auto_units = auto_units
self.units = units
self._fg_colour_alarms = {
epics.MINOR_ALARM : minor_alarm,
epics.MAJOR_ALARM : major_alarm,
epics.INVALID_ALARM : invalid_alarm }
def _SetValue(self, value):
"set widget label"
if self.auto_units and self.pv.units:
self.units = " " + self.pv.units
if value is not None:
self.SetLabel("%s%s" % (value, self.units))
class PVStaticText(wx.StaticText, PVMixin):
""" Static text for displaying a PV value,
with callback for automatic updates
By default the text colour will change on alarm states.
This can be overriden or disabled as constructor
parameters
"""
def __init__(self, parent, pv=None, style=None,
minor_alarm="DARKRED", major_alarm="RED",
invalid_alarm="ORANGERED", **kw):
wstyle = wx.ALIGN_LEFT
if style is not None:
wstyle = style
wx.StaticText.__init__(self, parent, wx.ID_ANY, label='',
style=wstyle, **kw)
PVMixin.__init__(self, pv=pv)
self._fg_colour_alarms = {
epics.MINOR_ALARM : minor_alarm,
epics.MAJOR_ALARM : major_alarm,
epics.INVALID_ALARM : invalid_alarm }
def _SetValue(self, value):
"set widget label"
if value is not None:
self.SetLabel("%s" % value)
@EpicsFunction
def OnPVChange(self, value):
"called by PV callback"
try:
if self.pv is None:
return
self.SetLabel(value)
except:
pass
class PVEnumButtons(wx.Panel, PVCtrlMixin):
""" a panel of buttons for Epics ENUM controls """
def __init__(self, parent, pv=None,
orientation=wx.HORIZONTAL, **kw):
self.orientation = orientation
wx.Panel.__init__(self, parent, wx.ID_ANY, **kw)
PVCtrlMixin.__init__(self, pv=pv)
@EpicsFunction
def SetPV(self, pv=None):
"set pv, either an epics.PV object or a pvname"
if pv is None:
return
if isinstance(pv, epics.PV):
self.pv = pv
elif isinstance(pv, str):
self.pv = epics.get_pv(pv)
self.pv.connect()
epics.poll()
self.pv.connection_callbacks.append(self.OnEpicsConnect)
self.pv.get_ctrlvars()
self.OnPVChange(self.pv.get(as_string=True))
ncback = len(self.pv.callbacks) + 1
self.pv.add_callback(self._pvEvent, wid=self.GetId(), cb_info=ncback)
pv_value = pv.get(as_string=True)
enum_strings = pv.enum_strs
sizer = wx.BoxSizer(self.orientation)
self.buttons = []
if enum_strings is not None:
for i, label in enumerate(enum_strings):
b = buttons.GenToggleButton(self, -1, label)
self.buttons.append(b)
b.Bind(wx.EVT_BUTTON, Closure(self._onButton, index=i) )
b.Bind(wx.EVT_RIGHT_DOWN, self._onRightDown)
sizer.Add(b, flag = wx.ALL)
b.SetToggle(label==pv_value)
self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)
@EpicsFunction
def _onButton(self, event=None, index=None, **kw):
"button event handler"
if self.pv is not None and index is not None:
self.pv.put(index)
@DelayedEpicsCallback
def _pvEvent(self, pvname=None, value=None, wid=None, **kw):
"pv event handler"
if pvname is None or value is None:
return
for i, btn in enumerate(self.buttons):
btn.up = (i != value)
btn.Refresh()
def _SetValue(self, value):
"not implemented"
pass
class PVEnumChoice(wx.Choice, PVCtrlMixin):
""" a dropdown choice for Epics ENUM controls """
def __init__(self, parent, pv=None, **kw):
wx.Choice.__init__(self, parent, wx.ID_ANY, **kw)
PVCtrlMixin.__init__(self, pv=pv)
@EpicsFunction
def SetPV(self, pv=None):
"set pv, either an epics.PV object or a pvname"
if pv is None:
return
if isinstance(pv, epics.PV):
self.pv = pv
elif isinstance(pv, str):
self.pv = epics.get_pv(pv)
self.pv.connect()
epics.poll()
self.pv.connection_callbacks.append(self.OnEpicsConnect)
self.pv.get_ctrlvars()
self.OnPVChange(self.pv.get(as_string=True))
ncback = len(self.pv.callbacks) + 1
self.pv.add_callback(self._pvEvent, wid=self.GetId(), cb_info=ncback)
self.Bind(wx.EVT_CHOICE, self._onChoice)
pv_value = self.pv.get(as_string=True)
enum_strings = self.pv.enum_strs
self.Clear()
self.AppendItems(enum_strings)
self.SetStringSelection(pv_value)
def _onChoice(self, event=None, **kws):
"choice event handler"
if self.pv is not None:
index = self.pv.enum_strs.index(event.GetString())
self.pv.put(index)
@DelayedEpicsCallback
def _pvEvent(self, pvname=None, value=None, **kw):
"pv event handler"
if value is not None:
self.SetSelection(value)
def _SetValue(self, value):
"set value"
self.SetStringSelection(value)
class PVAlarm(wx.MessageDialog, PVCtrlMixin):
""" Alarm Message for a PV: a MessageDialog will pop up when a
PV trips some alarm level"""
def __init__(self, parent, pv=None,
font=None, fg=None, bg=None, trip_point=None, **kw):
PVCtrlMixin.__init__(self, pv=pv, font=font, fg=None, bg=None)
def _SetValue(self, value):
"set value -- null op for pvAlarm"
pass
class PVFloatCtrl(FloatCtrl, PVCtrlMixin):
""" Float control for PV display of numerical data,
with callback for automatic updates, and
automatic determination of string/float controls
Options:
parent wx widget of parent
pv epics pv to use for value
precision number of digits past decimal point to display
(default to PV's precision)
font wx font
fg wx foreground colour
bg wx background colour
bell_on_invalid ring bell when input is out of range
"""
def __init__(self, parent, pv=None,
font=None, fg=None, bg=None, precision=None, **kw):
self.pv = None
FloatCtrl.__init__(self, parent, value=0,
precision=precision, **kw)
PVCtrlMixin.__init__(self, pv=pv, font=font, fg=None, bg=None)
def _SetValue(self, value):
"set widget value"
FloatCtrl.SetValue(self, value, act=False)
@EpicsFunction
def SetPV(self, pv=None):
"set pv, either an epics.PV object or a pvname"
if isinstance(pv, epics.PV):
self.pv = pv
elif isinstance(pv, str):
self.pv = epics.get_pv(pv)
if self.pv is None:
return
self.pv.connection_callbacks.append(self.OnEpicsConnect)
self.pv.get()
self.pv.get_ctrlvars()
# be sure to set precision before value!! or PV may be moved!!
prec = self.pv.precision
if prec is None:
prec = 0
self.SetPrecision(prec)
self.SetValue(self.pv.char_value, act=False)
if self.pv.type in ('string', 'char'):
try:
x = float(self.pv.value)
except:
self._warn('pvFloatCtrl needs a double or float PV')
llim = set_float(self.pv.lower_ctrl_limit)
hlim = set_float(self.pv.upper_ctrl_limit)
if hlim is not None and llim is not None and hlim > llim:
self.SetMax(hlim)
self.SetMin(llim)
ncback = len(self.pv.callbacks) + 1
self.pv.add_callback(self._pvEvent, wid=self.GetId(), cb_info=ncback)
self.SetAction(self._onEnter)
@DelayedEpicsCallback
def _FloatpvEvent(self, pvname=None, value=None, wid=None,
char_value=None, **kw):
"PV callback / event handler for pv change"
if pvname is None or value is None or wid is None:
return
if char_value is None and value is not None:
prec = kw.get('precision', None)
if prec not in (None, 0):
char_value = ("%%.%if" % prec) % value
else:
char_value = set_float(value)
self.SetValue(char_value, act=False)
@EpicsFunction
def _onEnter(self, value=None, **kw):
"enter/return event"
if value in (None,'') or self.pv is None:
return
try:
if float(value) != self.pv.get():
self.pv.put(float(value))
except:
pass
class PVBitmap(wx.StaticBitmap, PVCtrlMixin):
"""
Static Bitmap where image is based on PV value,
with callback for automatic updates
"""
def __init__(self, parent, pv=None, bitmaps=None,
defaultBitmap=None, **kw):
"""
bitmaps - a dict of Value->Bitmap mappings, to automatically change
the shown bitmap based on the PV value.
defaultBitmap - the bitmap to show if the PV value doesn't match any
of the values in the bitmaps dict.
"""
wx.StaticBitmap.__init__(self, parent, wx.ID_ANY,
bitmap=defaultBitmap, **kw)
PVCtrlMixin.__init__(self, pv=pv)
self.defaultBitmap = defaultBitmap
if bitmaps is None:
bitmaps = {}
self.bitmaps = bitmaps
def _SetValue(self, value):
"set widget value"
if value in self.bitmaps:
nextBitmap = self.bitmaps[value]
else:
nextBitmap = self.defaultBitmap
if nextBitmap != self.GetBitmap():
self.SetBitmap(nextBitmap)
class PVCheckBox(wx.CheckBox, PVCtrlMixin):
"""
Checkbox based on a binary PV value, both reads/writes the
PV on changes.
There are multiple options for translating PV values to checkbox
settings (from least to most complex):
* Use a PV with values 0 and 1
* Use a PV with values that convert via Python's own bool(x)
* Set on_value and off_value in the constructor
* Use SetTranslations() to set a dictionary for converting various
PV values to booleans.
"""
def __init__(self, parent, pv=None, on_value=1, off_value=0, **kw):
self.pv = None
wx.CheckBox.__init__(self, parent, **kw)
PVCtrlMixin.__init__(self, pv=pv, font="", fg=None, bg=None)
wx.EVT_CHECKBOX(parent, self.GetId(), self.OnClicked)
self.on_value = on_value
self.off_value = off_value
self.OnChange = None
def _SetValue(self, value):
"set widget value"
if value in (self.on_value, self.off_value):
self.Value = (value == self.on_value)
else:
self.Value = bool(self.pv.get())
if callable(self.OnChange):
self.OnChange(self)
@EpicsFunction
def OnClicked(self, event=None):
"checkbox event handler"
if self.pv is not None:
self.pv.put(self.on_value if self.Value else self.off_value)
def SetValue(self, new_value):
"set widget value"
old_value = self.Value
wx.CheckBox.SetValue(self, new_value)
if old_value != new_value:
self.OnClicked(None)
# need to redefine the value Property as the old property refs old SetValue
Value = property(wx.CheckBox.GetValue, SetValue)
class PVFloatSpin(floatspin.FloatSpin, PVCtrlMixin):
"""
A FloatSpin (floating-point-aware SpinCtrl) linked to a PV,
both reads and writes the PV on changes.
"""
def __init__(self, parent, pv=None, deadTime=2500,
min_val=None, max_val=None, increment=1.0, digits=-1, **kw):
"""
Most arguments are common with FloatSpin.
Additional Arguments:
pv = pv to set
deadTime = delay (ms) between user typing a value into the field,
and it being set to the PV
"""
floatspin.FloatSpin.__init__(self, parent, increment=increment,
min_val=min_val, max_val=max_val,
digits=digits, **kw)
PVCtrlMixin.__init__(self, pv=pv, font="", fg=None, bg=None)
floatspin.EVT_FLOATSPIN(parent, self.GetId(), self.OnSpin)
self.deadTimer = wx.Timer(self)
self.deadTime = deadTime
wx.EVT_TIMER(self, self.deadTimer.GetId(), self.OnTimeout)
@EpicsFunction
def _SetValue(self, value):
"set value"
floatspin.FloatSpin.SetValue(self, float(self.pv.get()))
@EpicsFunction
def OnSpin(self, event=None):
"spin event handler"
if self.pv is not None:
value = self.GetValue()
if self.pv.upper_ctrl_limit != 0 or self.pv.lower_ctrl_limit != 0:
# both zero -> not set
if value > self.pv.upper_ctrl_limit:
value = self.pv.upper_ctrl_limit
if value < self.pv.lower_ctrl_limit:
value = self.pv.lower_ctrl_limit
self.SetValue(value)
def OnTimeout(self, event):
"timer event handler"
# save & restore insertion point before syncing control
savePoint = self.GetTextCtrl().InsertionPoint
self.SyncSpinToText()
self.GetTextCtrl().InsertionPoint = savePoint
self.OnSpin(event)
def OnChar(self, event):
"floatspin char event"
floatspin.FloatSpin.OnChar(self, event)
# Timer will restart if it's already running
self.deadTimer.Start(milliseconds=self.deadTime, oneShot=True)
def SetValue(self, value):
floatspin.FloatSpin.SetValue(self, value)
if hasattr(self, "pv"): # can be called before PV is assigned
self.pv.put(value)
class PVSpinCtrl(wx.SpinCtrl, PVCtrlMixin):
"""
A SpinCtrl linked to a PV,
both reads and writes the PV on changes.
"""
def __init__(self, parent, pv=None,
min_val=None, max_val=None, **kws):
"""
Most arguments are common with SpinCtrl
Additional Arguments:
pv = pv to set
deadTime = delay (ms) between user typing a value into the field,
and it being set to the PV
"""
wx.SpinCtrl.__init__(self, parent, **kws)
PVCtrlMixin.__init__(self, pv=pv, font="", fg=None, bg=None)
self.Bind(wx.EVT_SPINCTRL, self.OnSpin)
@EpicsFunction
def _SetValue(self, value):
"set value"
wx.SpinCtrl.SetValue(self, self.pv.get())
if hasattr(self, "pv"): # can be called before PV is assigned
self.pv.put(value)
@EpicsFunction
def OnSpin(self, event=None):
"spin event handler"
self._SetValue(self.GetValue())
class PVButton(wx.Button, PVCtrlMixin):
""" A Button linked to a PV. When the button is pressed, a certain value
is written to the PV (useful for momentary PVs with HIGH= set.)
"""
def __init__(self, parent, pv=None, pushValue=1,
disablePV=None, disableValue=1, **kw):
"""
pv = pv to write back to
pushValue = value to write when button is pressed
disablePV = read this PV in order to disable the button
disableValue = disable the button if/when the disablePV has this value
"""
wx.Button.__init__(self, parent, **kw)
PVCtrlMixin.__init__(self, pv=pv, font="", fg=None, bg=None)
self.pushValue = pushValue
self.Bind(wx.EVT_BUTTON, self.OnPress)
if isinstance(disablePV, str):
disablePV = epics.get_pv(disablePV)
disablePV.connect()
self.disablePV = disablePV
self.disableValue = disableValue
if disablePV is not None:
ncback = len(self.disablePV.callbacks) + 1
self.disablePV.add_callback(self._disableEvent, wid=self.GetId(),
cb_info=ncback)
self.maskedEnabled = True
def Enable(self, value=None):
"enable button"
if value is not None:
self.maskedEnabled = value
self._UpdateEnabled()
@EpicsFunction
def _UpdateEnabled(self):
"epics function, called by event handler"
enableValue = self.maskedEnabled
if self.disablePV is not None and \
(self.disablePV.get() == self.disableValue):
enableValue = False
if self.pv is not None and (self.pv.get() == self.pushValue):
enableValue = False
wx.Button.Enable(self, enableValue)
@DelayedEpicsCallback
def _disableEvent(self, **kw):
"disable event handler"
self._UpdateEnabled()
def _SetValue(self, event):
"set value"
self._UpdateEnabled()
@EpicsFunction
def OnPress(self, event):
"button press event handler"
self.pv.put(self.pushValue)
class PVBitmapButton(wx.BitmapButton, PVCtrlMixin):
""" A Button linked to a PV. When the button is pressed, a certain value
is written to the PV (useful for momentary PVs with HIGH= set.)
"""
def __init__(self, parent, pv=None, pushValue=1,
disablePV=None, disableValue=1, bitmap = None, **kw):
"""
pv = pv to write back to
pushValue = value to write when button is pressed
disablePV = read this PV in order to disable the button
disableValue = disable the button if/when the disablePV has this value
bitmap = in image from the wx.ArtProvider library
"""
wx.BitmapButton.__init__(self, parent, bitmap = bitmap, **kw)
PVCtrlMixin.__init__(self, pv=pv, font="", fg=None, bg=None)
self.pushValue = pushValue
self.Bind(wx.EVT_BUTTON, self.OnPress)
if isinstance(disablePV, str):
disablePV = epics.get_pv(disablePV)
disablePV.connect()
self.disablePV = disablePV
self.disableValue = disableValue
if disablePV is not None:
ncback = len(self.disablePV.callbacks) + 1
self.disablePV.add_callback(self._disableEvent, wid=self.GetId(),
cb_info=ncback)
self.maskedEnabled = True
def Enable(self, value=None):
"enable button"
if value is not None:
self.maskedEnabled = value
self._UpdateEnabled()
@EpicsFunction
def _UpdateEnabled(self):
"epics function, called by event handler"
enableValue = self.maskedEnabled
if self.disablePV is not None and \
(self.disablePV.get() == self.disableValue):
enableValue = False
from numpy import array_equal, ndarray, generic
if isinstance(self.pushValue, (ndarray, generic)):
if self.pv is not None and (array_equal(self.pv.get(),self.pushValue)):
enableValue = False
else:
if self.pv is not None and (self.pv.get() is self.pushValue):
enableValue = False
wx.Button.Enable(self, enableValue)
@DelayedEpicsCallback
def _disableEvent(self, **kw):
"disable event handler"
self._UpdateEnabled()
def _SetValue(self, event):
"set value"
self._UpdateEnabled()
@EpicsFunction
def OnPress(self, event):
"button press event handler"
self.pv.put(self.pushValue)
class PVRadioButton(wx.RadioButton, PVCtrlMixin):
"""A pvRadioButton is a radio button associated with a particular PV
and one particular value.
Suggested for use in a group where all radio buttons are
pvRadioButtons, and they all have a discrete value set.
"""
def __init__(self, parent, pv=None, pvValue=None, **kw):
"""
pvValue = This value will be written to the PV when the radiobutton is
pushed, and the radiobutton will become select if/when the PV is set to
this value.
The value used is raw numeric, not "as string"
"""
wx.RadioButton.__init__(self, parent, **kw)
PVCtrlMixin.__init__(self, pv=pv, font="", fg=None, bg=None)
self.pvValue = pvValue
self.Bind(wx.EVT_RADIOBUTTON, self.OnPress)
@EpicsFunction
def OnPress(self, event):
"button press event handler"
self.pv.put(self.pvValue)
@EpicsFunction
def _SetValue(self, value):
"set value"
# uses raw PV val as is not string-converted
if value == self.pvValue or self.pv.get() == self.pvValue:
self.Value = True
class PVComboBox(wx.ComboBox, PVCtrlMixin):
""" A ComboBox linked to a PV. Both reads/writes the combo value on changes
"""
def __init__(self, parent, pv=None, **kw):
wx.ComboBox.__init__(self, parent, **kw)
PVCtrlMixin.__init__(self, pv=pv, font="", fg=None, bg=None)
self.Bind(wx.EVT_TEXT, self.OnText)
def _SetValue(self, value):
"set value"
if value != self.Value:
self.Value = value
@EpicsFunction
def OnText(self, event):
"text event"
self.pv.put(self.Value)
class PVEnumComboBox(PVComboBox):
""" A dropdown ComboBox linked to an enum PV, allows setting of enum values.
Both reads/writes the combo value on changes
"""
def __init__(self, parent, pv=None, **kw):
PVComboBox.__init__(self, parent, pv, style=wx.CB_DROPDOWN|wx.CB_READONLY, **kw)
@DelayedEpicsCallback
def OnEpicsConnect(self, pvname=None, conn=None, pv=None):
PVComboBox.OnEpicsConnect(self, pvname, conn, pv)
if conn:
self.Clear()
for enum in self.pv.enum_strs:
self.Append(enum)
self.Value = self.pv.get(as_string=True)
class PVToggleButton(wx.ToggleButton, PVCtrlMixin):
"""A ToggleButton that can be attached to a bi or bo Epics record."""
def __init__(self, parent, pv=None, down=1, up_colour=None,
down_colour=None, **kwargs):
"""
Create a ToggleButton and attach it to a bi or bo record.
Toggling the button will toggle the bi/bo record (and vice versa.) The
button label is the ONAM or ZNAM values of the record. Note the label
displays the opposite state of the bi/bo record, i.e., it shows what
will happen if the button is clicked.
parent: Parent window of the ToggleButton.
pv: Process variable attached to the ToggleButton. A bi/bo record.
down: pv.value representing a down button. Default 1.
up_colour: Background colour of button when it is up. Default None.
down_colour: Background colour of button when it is down. Default None.
"""
wx.ToggleButton.__init__(self, parent, wx.ID_ANY, label='', **kwargs)
PVCtrlMixin.__init__(self, pv=pv)
self.down = down
self.up_colour = up_colour
self.down_colour = down_colour
self.Bind(wx.EVT_TOGGLEBUTTON, self._onButton)
@EpicsFunction
def _onButton(self, event=None):
"button event handler"
self.labels = self.pv.enum_strs
if self.GetValue():
self.SetLabel(self.labels[0])
self.pv.put(self.down == 1)
self.SetBackgroundColour(self.down_colour)
else:
self.SetLabel(self.labels[1])
self.pv.put(self.down == 0)
self.SetBackgroundColour(self.up_colour)
@EpicsFunction
def _SetValue(self, value):
"set value"
self.labels = self.pv.enum_strs
if value == self.labels[1]:
self.SetValue(self.down==1)
self.SetBackgroundColour(self.down_colour if self.down==1 \
else self.up_colour)
self.SetLabel(self.labels[0])
else:
self.SetValue(self.down==0)
self.SetBackgroundColour(self.down_colour if self.down==0 \
else self.up_colour)
self.SetLabel(self.labels[1])
class PVStatusBar(wx.StatusBar, PVMixin):
"""A status bar that displays a pv value
To use in a wxFrame:
self.SetStatusBar(pvStatusBar(prent=self, pv=PV(...), style=...)
"""
def __init__(self, parent=None, pv=None, **kwargs):
"""
Create a stsus bar that displays a pv value.
"""
wx.StatusBar.__init__(self, parent, wx.ID_ANY, **kwargs)
PVMixin.__init__(self, pv=pv)
@EpicsFunction
def OnPVChange(self, str_value):
"called by PV callback"
self.SetStatusText(str_value)
class PVCollapsiblePane(wx.CollapsiblePane, PVCtrlMixin):
"""A collapsible pane where the display on the collapsed line is set
from a PV value
"""
def __init__(self, parent, pv=None, minor_alarm="DARKRED", major_alarm="RED",
invalid_alarm="ORANGERED", **kw):
wx.CollapsiblePane.__init__(self, parent, **kw)
PVCtrlMixin.__init__(self, pv=pv, font=None, fg=None, bg=None)
self._fg_colour_alarms = {
epics.MINOR_ALARM : minor_alarm,
epics.MAJOR_ALARM : major_alarm,
epics.INVALID_ALARM : invalid_alarm }
if self.pv:
_SetValue(self.pv.value)
def _SetValue(self, value):
if value:
self.SetLabel(value)
|