1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2011-12-20 22:14:28 +0000 (Tue, 20 Dec 2011) $ |
---|
5 | # $Author: jemian $ |
---|
6 | # $Revision: 730 $ |
---|
7 | # $URL$ |
---|
8 | # $Id: vc_pv.py 730 2011-12-20 22:14:28Z jemian $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | |
---|
11 | ''' |
---|
12 | demo view for EPICS PV connection class: m_pv.PvConnection() |
---|
13 | |
---|
14 | Copyright (c) 2009 - 2011, UChicago Argonne, LLC. |
---|
15 | See LICENSE file for details. |
---|
16 | ''' |
---|
17 | |
---|
18 | |
---|
19 | # - - - - - - - - - - - - - - - - - - Imports |
---|
20 | |
---|
21 | |
---|
22 | import wx |
---|
23 | import wx.lib.buttons |
---|
24 | from m_pv import PvConnection |
---|
25 | |
---|
26 | |
---|
27 | # - - - - - - - - - - - - - - - - - - Global |
---|
28 | |
---|
29 | |
---|
30 | __svnid__ = "$Id: vc_pv.py 730 2011-12-20 22:14:28Z jemian $" |
---|
31 | COLOR_MOVING = wx.Colour(179, 250, 142) # pale green |
---|
32 | COLOR_NOT_MOVING = wx.Colour(200, 191, 140) # not exactly background color |
---|
33 | COLOR_PALE_PURPLE = wx.Colour(200, 200, 255) # my name for this |
---|
34 | COLOR_LIGHTGREEN = wx.Colour(200, 255, 200) # my name for this |
---|
35 | COLOR_LIGHTRED = wx.Colour(255, 200, 200) # my name for this |
---|
36 | |
---|
37 | |
---|
38 | # - - - - - - - - - - - - - - - - - - class |
---|
39 | |
---|
40 | |
---|
41 | class DemoView(wx.Frame): |
---|
42 | ''' |
---|
43 | Show an EPICS PV connection. |
---|
44 | Allow it to connect and disconnect. |
---|
45 | (This is like CA Probe.) |
---|
46 | ''' |
---|
47 | |
---|
48 | _cb_counter = 0 |
---|
49 | |
---|
50 | def __init__(self, pvname = ''): |
---|
51 | self.pvname = pvname |
---|
52 | self.value = '' |
---|
53 | self.state = 'not connected' |
---|
54 | self.remarks = '' |
---|
55 | self.pv = None |
---|
56 | |
---|
57 | wx.Frame.__init__(self, id=wx.ID_ANY, name=u'Frame1', |
---|
58 | parent=None, title=u'PV Connection',) |
---|
59 | #self.SetClientSize(wx.Size(750, 440)) |
---|
60 | self.SetToolTipString(u'PV Connection') |
---|
61 | |
---|
62 | self._init_widgets() |
---|
63 | |
---|
64 | def _init_widgets(self): |
---|
65 | self.panel = wx.Panel(id=wx.ID_ANY, name='panel1', parent=self) |
---|
66 | self.panelSizer = wx.BoxSizer(orient=wx.VERTICAL) |
---|
67 | self.panelSizer.AddSizer(self._init_upper_part(self.panel), 1, border=0, flag=wx.EXPAND) |
---|
68 | self.panelSizer.AddSizer(self._init_lower_part(self.panel), 1, border=0, flag=wx.EXPAND) |
---|
69 | self.panelSizer.AddSizer(self._init_buttons(self.panel), 0, border=0, flag=wx.EXPAND) |
---|
70 | self.panel.SetSizer(self.panelSizer) |
---|
71 | |
---|
72 | def _init_upper_part(self, parent): |
---|
73 | ''' |
---|
74 | edit the PV name string if not connected |
---|
75 | ''' |
---|
76 | sbox = wx.StaticBox(parent, id=wx.ID_ANY, |
---|
77 | label='PV name and value', style=0) |
---|
78 | sbs = wx.StaticBoxSizer(sbox, wx.VERTICAL) |
---|
79 | fgs = wx.FlexGridSizer(rows=2, cols=2, hgap=4, vgap=4) |
---|
80 | fgs.AddGrowableCol(1) |
---|
81 | sbs.Add(fgs, 0, wx.EXPAND|wx.ALIGN_CENTRE|wx.ALL, 5) |
---|
82 | |
---|
83 | self.w_pvname = wx.TextCtrl(parent=parent, id=wx.ID_ANY) |
---|
84 | self.w_pvname.SetEditable(True) |
---|
85 | self.w_value = wx.TextCtrl(parent=parent, id=wx.ID_ANY) |
---|
86 | self.w_value.SetEditable(False) |
---|
87 | |
---|
88 | self.SetPV(self.pvname) |
---|
89 | self.SetValue(self.value) |
---|
90 | |
---|
91 | fgs.Add(wx.StaticText(parent, wx.ID_ANY, 'one'), 0, flag=wx.EXPAND) |
---|
92 | fgs.Add(self.w_pvname, 0, flag=wx.EXPAND) |
---|
93 | fgs.Add(wx.StaticText(parent, wx.ID_ANY, 'three'), 0, flag=wx.EXPAND) |
---|
94 | fgs.Add(self.w_value, 0, flag=wx.EXPAND) |
---|
95 | |
---|
96 | return sbs |
---|
97 | |
---|
98 | def _init_lower_part(self, parent): |
---|
99 | ''' |
---|
100 | show connection state and something else useful |
---|
101 | ''' |
---|
102 | sbox = wx.StaticBox(parent, id=wx.ID_ANY, |
---|
103 | label='connection status', style=0) |
---|
104 | sbs = wx.StaticBoxSizer(sbox, wx.VERTICAL) |
---|
105 | fgs = wx.FlexGridSizer(rows=2, cols=1, hgap=4, vgap=4) |
---|
106 | fgs.AddGrowableCol(0) |
---|
107 | sbs.Add(fgs, 0, wx.EXPAND|wx.ALIGN_CENTRE|wx.ALL, 5) |
---|
108 | |
---|
109 | self.w_state = wx.TextCtrl(parent=parent, id=wx.ID_ANY) |
---|
110 | self.w_state.SetEditable(False) |
---|
111 | self.w_remarks = wx.TextCtrl(parent=parent, id=wx.ID_ANY) |
---|
112 | self.w_remarks.SetEditable(False) |
---|
113 | |
---|
114 | self.SetState(self.state) |
---|
115 | self.SetRemarks(self.remarks) |
---|
116 | |
---|
117 | fgs.Add(self.w_state, 0, flag=wx.EXPAND) |
---|
118 | fgs.Add(self.w_remarks, 0, flag=wx.EXPAND) |
---|
119 | |
---|
120 | return sbs |
---|
121 | |
---|
122 | def _init_buttons(self, parent): |
---|
123 | ''' |
---|
124 | build the buttons row and place it in a sizer, return the sizer |
---|
125 | ''' |
---|
126 | self.connectButton = self._my_button(parent, |
---|
127 | label=u'Connect', name='connectButton', |
---|
128 | tip=u'Connect with EPICS PV', |
---|
129 | binding=self.doConnectButton) |
---|
130 | self.disconnectButton = self._my_button(parent, |
---|
131 | label=u'Disconnect', name='disconnectButton', |
---|
132 | tip=u'Disconnect from EPICS PV', |
---|
133 | binding=self.doDisconnectButton) |
---|
134 | |
---|
135 | sizer = wx.BoxSizer(orient=wx.HORIZONTAL) |
---|
136 | self.panelSizer.AddSizer(sizer, 0, border=0, flag=wx.EXPAND) |
---|
137 | sizer.AddWindow(self.connectButton, proportion=1, border=0, flag=wx.EXPAND) |
---|
138 | sizer.AddWindow(self.disconnectButton, proportion=1, border=0, flag=wx.EXPAND) |
---|
139 | return sizer |
---|
140 | |
---|
141 | def _my_button(self, parent, label, name, tip, binding): |
---|
142 | ''' |
---|
143 | Create a button and bind it to a method. |
---|
144 | Return the button object |
---|
145 | ''' |
---|
146 | button = wx.lib.buttons.GenButton(id=wx.ID_ANY, |
---|
147 | label=label, name=name, parent=parent,) |
---|
148 | button.SetToolTipString(tip) |
---|
149 | button.Bind(wx.EVT_BUTTON, binding) |
---|
150 | return button |
---|
151 | |
---|
152 | def doConnectButton(self, event): |
---|
153 | event.Skip() |
---|
154 | print "Connect button pressed" |
---|
155 | self.w_pvname.SetEditable(False) |
---|
156 | pvname = self.w_pvname.GetValue() |
---|
157 | if len(pvname.strip()) > 0: |
---|
158 | self.pvname = pvname |
---|
159 | self.pv = PvConnection(pvname) |
---|
160 | kw = {'vc_pv': True} |
---|
161 | # TODO: we want a PV connection callback, as well |
---|
162 | self.pv.connect(self.callback, **kw) |
---|
163 | |
---|
164 | def doDisconnectButton(self, event): |
---|
165 | event.Skip() |
---|
166 | print "Disconnect button pressed" |
---|
167 | self.w_pvname.SetEditable(True) |
---|
168 | self.pv = None # throw away the PV connection |
---|
169 | |
---|
170 | def SetPV(self, value): |
---|
171 | self.pvname = str(value) |
---|
172 | self.w_pvname.SetValue( self.pvname ) |
---|
173 | |
---|
174 | def SetRemarks(self, value): |
---|
175 | self.remarks = str(value) |
---|
176 | self.w_remarks.SetValue( self.remarks ) |
---|
177 | |
---|
178 | def SetState(self, value): |
---|
179 | self.state = str(value) |
---|
180 | self.w_state.SetValue( self.state ) |
---|
181 | |
---|
182 | def SetValue(self, value): |
---|
183 | self.value = str(value) |
---|
184 | self.w_value.SetValue( self.value ) |
---|
185 | |
---|
186 | def callback(self, **kw): |
---|
187 | "generic monitor callback" |
---|
188 | print kw |
---|
189 | self.SetRemarks(kw['cb_info']) |
---|
190 | self.SetValue(kw['value']) |
---|
191 | self._cb_counter += 1 |
---|
192 | color = self.ColorSelector(self._cb_counter) |
---|
193 | print self._cb_counter, color |
---|
194 | self.w_value.SetBackgroundColour(color) |
---|
195 | |
---|
196 | def ColorSelector(self, index): |
---|
197 | clut = [ COLOR_NOT_MOVING, COLOR_MOVING, COLOR_PALE_PURPLE, COLOR_LIGHTGREEN, COLOR_LIGHTRED, ] |
---|
198 | return clut[index % len(clut)] |
---|
199 | |
---|
200 | |
---|
201 | # - - - - - - - - - - - - - - - - - - methods |
---|
202 | |
---|
203 | |
---|
204 | # - - - - - - - - - - - - - - - - - - main |
---|
205 | |
---|
206 | |
---|
207 | if __name__ == '__main__': |
---|
208 | app = wx.PySimpleApp() |
---|
209 | frame = DemoView('Godzilla') |
---|
210 | frame.SetPV('pj:test') |
---|
211 | frame.SetValue( 'loves Tokyo' ) |
---|
212 | frame.SetState( 'just started' ) |
---|
213 | frame.Show() |
---|
214 | |
---|
215 | app.MainLoop() |
---|