/
MainWindow.py
236 lines (213 loc) · 12.3 KB
/
MainWindow.py
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
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Tue Jul 12 13:27:06 2011
import wx
import serial
from arduino import Arduino
# begin wxGlade: dependencies
# end wxGlade
# begin wxGlade: extracode
# end wxGlade
class MainWindow(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MainWindow.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
# Menu Bar
self.frame_Primary_menubar = wx.MenuBar()
self.file = wx.Menu()
self.menuItemAbout = wx.MenuItem(self.file, wx.NewId(), "About", "Information about this program", wx.ITEM_NORMAL)
self.file.AppendItem(self.menuItemAbout)
self.menuItemChangeSerialPort = wx.MenuItem(self.file, wx.NewId(), "Change Serial Port", "Change Port Connection", wx.ITEM_NORMAL)
self.file.AppendItem(self.menuItemChangeSerialPort)
self.menuItemExit = wx.MenuItem(self.file, wx.NewId(), "Exit", "Terminate the program", wx.ITEM_NORMAL)
self.file.AppendItem(self.menuItemExit)
self.frame_Primary_menubar.Append(self.file, "File")
self.help = wx.Menu()
self.menuItemContent = wx.MenuItem(self.help, wx.NewId(), "Content", "Information about this program", wx.ITEM_NORMAL)
self.help.AppendItem(self.menuItemContent)
self.frame_Primary_menubar.Append(self.help, "Help")
self.SetMenuBar(self.frame_Primary_menubar)
# Menu Bar end
self.statusBar = self.CreateStatusBar(1, 0)
self.choice_Switch_1 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1 = wx.StaticText(self, -1, "Switch 1\n", style=wx.ALIGN_CENTRE)
self.choice_Switch_2 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1_copy = wx.StaticText(self, -1, "Switch 2\n", style=wx.ALIGN_CENTRE)
self.choice_Switch_3 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1_copy_1 = wx.StaticText(self, -1, "Switch 3\n", style=wx.ALIGN_CENTRE)
self.choice_Switch_4 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1_copy_2 = wx.StaticText(self, -1, "Switch 4", style=wx.ALIGN_CENTRE)
self.choice_Switch_5 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1_copy_3 = wx.StaticText(self, -1, "Switch 5\n", style=wx.ALIGN_CENTRE)
self.choice_Switch_6 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1_copy_4 = wx.StaticText(self, -1, "Switch 6\n", style=wx.ALIGN_CENTRE)
self.choice_Switch_7 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1_copy_5 = wx.StaticText(self, -1, "Switch 7\n", style=wx.ALIGN_CENTRE)
self.choice_Switch_8 = wx.Choice(self, -1, choices=["Off", "On"])
self.label_1_copy_6 = wx.StaticText(self, -1, "Switch 8", style=wx.ALIGN_CENTRE)
self.static_line_1 = wx.StaticLine(self, -1)
self.text_ctrl_Status = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_WORDWRAP)
self.button_ReadConfig = wx.Button(self, -1, "Read Configuration")
self.button_ApplyConfig = wx.Button(self, -1, "Apply Configuration")
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_MENU, self.OnAbout, self.menuItemAbout)
self.Bind(wx.EVT_MENU, self.OnChangeSerialPort, self.menuItemChangeSerialPort)
self.Bind(wx.EVT_MENU, self.OnExit, self.menuItemExit)
self.Bind(wx.EVT_MENU, self.OnContent, self.menuItemContent)
self.Bind(wx.EVT_BUTTON, self.OnReadConfig, self.button_ReadConfig)
self.Bind(wx.EVT_BUTTON, self.OnApplyConfig, self.button_ApplyConfig)
# end wxGlade
# print instructions
self.windowPrintLn("Welcome to the picogrid switch control utility.")
self.windowPrintLn("For help, see Help->Content or contact webbhorn@mit.edu")
# basic configuration
self.arduino = None
self.arduinoPinMap = [6,7,8,9,10,11,12,13] # Pin 1 controls switch 1, pin 2 controls switch 2, etc...
self.switchList = [self.choice_Switch_1, self.choice_Switch_2, self.choice_Switch_3, self.choice_Switch_4,
self.choice_Switch_5, self.choice_Switch_6, self.choice_Switch_7, self.choice_Switch_8]
self.serialPort = 'COM0'
def __set_properties(self):
# begin wxGlade: MainWindow.__set_properties
self.SetTitle("Picogrid Load Controller")
self.statusBar.SetStatusWidths([-1])
# statusbar fields
statusBar_fields = ["Load Control Utility"]
for i in range(len(statusBar_fields)):
self.statusBar.SetStatusText(statusBar_fields[i], i)
self.choice_Switch_1.SetSelection(0)
self.choice_Switch_2.SetSelection(0)
self.choice_Switch_3.SetSelection(0)
self.choice_Switch_4.SetSelection(0)
self.choice_Switch_5.SetSelection(0)
self.choice_Switch_6.SetSelection(0)
self.choice_Switch_7.SetSelection(0)
self.choice_Switch_8.SetSelection(0)
self.text_ctrl_Status.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "Ubuntu"))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MainWindow.__do_layout
sizer_Main = wx.BoxSizer(wx.VERTICAL)
sizer_ButtonBar = wx.BoxSizer(wx.HORIZONTAL)
sizer_SwitchBar = wx.BoxSizer(wx.HORIZONTAL)
sizer_Switch_8_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Switch_7_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Switch_6_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Switch_5_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Switch_4_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Switch_3_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Switch_2_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Switch_1_Slot = wx.BoxSizer(wx.VERTICAL)
sizer_Main.Add((20, 10), 0, wx.EXPAND, 0)
sizer_Switch_1_Slot.Add(self.choice_Switch_1, 0, wx.EXPAND, 0)
sizer_Switch_1_Slot.Add(self.label_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_1_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Switch_2_Slot.Add(self.choice_Switch_2, 0, wx.EXPAND, 0)
sizer_Switch_2_Slot.Add(self.label_1_copy, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_2_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Switch_3_Slot.Add(self.choice_Switch_3, 0, wx.EXPAND, 0)
sizer_Switch_3_Slot.Add(self.label_1_copy_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_3_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Switch_4_Slot.Add(self.choice_Switch_4, 0, wx.EXPAND, 0)
sizer_Switch_4_Slot.Add(self.label_1_copy_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_4_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Switch_5_Slot.Add(self.choice_Switch_5, 0, wx.EXPAND, 0)
sizer_Switch_5_Slot.Add(self.label_1_copy_3, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_5_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Switch_6_Slot.Add(self.choice_Switch_6, 0, wx.EXPAND, 0)
sizer_Switch_6_Slot.Add(self.label_1_copy_4, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_6_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Switch_7_Slot.Add(self.choice_Switch_7, 0, wx.EXPAND, 0)
sizer_Switch_7_Slot.Add(self.label_1_copy_5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_7_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Switch_8_Slot.Add(self.choice_Switch_8, 0, wx.EXPAND, 0)
sizer_Switch_8_Slot.Add(self.label_1_copy_6, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_SwitchBar.Add(sizer_Switch_8_Slot, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 12)
sizer_Main.Add(sizer_SwitchBar, 1, wx.ALL|wx.EXPAND, 3)
sizer_Main.Add(self.static_line_1, 0, wx.TOP|wx.BOTTOM|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 5)
sizer_Main.Add(self.text_ctrl_Status, 2, wx.LEFT|wx.RIGHT|wx.EXPAND, 20)
sizer_ButtonBar.Add(self.button_ReadConfig, 0, wx.LEFT|wx.RIGHT, 10)
sizer_ButtonBar.Add(self.button_ApplyConfig, 0, wx.LEFT|wx.RIGHT, 10)
sizer_Main.Add(sizer_ButtonBar, 1, wx.LEFT|wx.RIGHT|wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)
sizer_Main.Add((20, 10), 0, wx.EXPAND, 0)
self.SetSizer(sizer_Main)
sizer_Main.Fit(self)
sizer_Main.SetSizeHints(self)
self.Layout()
# end wxGlade
def windowPrintLn(self, text):
"""Print a string to the terminal within the main window and a newline character"""
self.text_ctrl_Status.WriteText(text + "\n")
def OnAbout(self, event): # wxGlade: MainWindow.<event_handler>
#self.windowPrintLn('Event handler "OnAbout" not implemented!')
event.Skip()
def OnExit(self, event): # wxGlade: MainWindow.<event_handler>
if self.arduino is not None:
self.arduino.close()
dlg = wx.MessageDialog(self,
"Do you really want to close this application?",
"Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
self.Destroy()
def OnContent(self, event): # wxGlade: MainWindow.<event_handler>
#self.windowPrintLn("Event handler `OnContent' not implemented!")
event.Skip()
def OnReadConfig(self, event): # wxGlade: MainWindow.<event_handler>
self.windowPrintLn('')
if self.arduino is None:
self.windowPrintLn("Must connect to the arduino first! (File->Change Serial Port)")
return
for pin, value in zip(self.arduinoPinMap, self.switchList):
if self.arduino.getState(pin) is False:
self.windowPrintLn("Pin " + str(pin) + " is LOW")
value.SetSelection(0)
else:
self.windowPrintLn("Pin " + str(pin) + " is HIGH")
value.SetSelection(1)
def OnApplyConfig(self, event): # wxGlade: MainWindow.<event_handler>
self.windowPrintLn('')
if self.arduino is None:
self.windowPrintLn("Must connect to the arduino first! (File->Change Serial Port)")
return
for pin, value in zip(self.arduinoPinMap, self.switchList):
if value.GetSelection() is 0:
self.windowPrintLn(''.join(['Set pin ', str(pin), ' to LOW']))
self.arduino.setLow(pin)
else:
self.windowPrintLn(''.join(['Set pin ', str(pin), ' to HIGH']))
self.arduino.setHigh(pin)
def OnChangeSerialPort(self, event): # wxGlade: MainWindow.<event_handler>
self.windowPrintLn('')
self.windowPrintLn("Searching for available serial ports...")
#scan for available ports. return a list of tuples (num, name)
available = []
for i in range(256):
try:
s = serial.Serial(i)
available.append( (i, s.portstr))
s.close() # explicit close because of delayed GC in java
except serial.SerialException:
pass
self.windowPrintLn("Found the following avilable ports:")
for nu, na in available: self.windowPrintLn(na)
dlg = wx.SingleChoiceDialog(self, "Select the correct serial port", 'Serial Port Selection',
[na for nu, na in available], wx.CHOICEDLG_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.serialPort = dlg.GetStringSelection()
self.windowPrintLn('Selected ' + self.serialPort)
self.windowPrintLn("Attempting to connect to arduino over " + self.serialPort + ".....")
try:
self.arduino = Arduino(self.serialPort)
self.arduino.output(self.arduinoPinMap)
self.arduino.turnOff()
self.windowPrintLn(str(self.arduino))
except serial.serialutil.SerialException:
self.windowPrintLn("Failed to connect to port " + self.serialPort + "!")
self.arduino = None
except TimeoutException:
self.windowPrintLn("5 second timeout: Failed to communicate with arduino over " + self.serialPort)
self.windowPrintLn("Reset arduino and try again, or try different port.")
self.arduino = None
# end of class MainWindow