root / python / .svn / text-base / launch_box.py.svn-base @ 0:3c15de202295
History | View | Annotate | Download (13.7 KB)
1 | 0:3c15de202295 | ee11d037 | #!/usr/bin/python |
---|---|---|---|
2 | ##Import Necessary Modules |
||
3 | try: |
||
4 | import wx |
||
5 | import time |
||
6 | import os |
||
7 | import subprocess |
||
8 | import pickle |
||
9 | import sched |
||
10 | import platform |
||
11 | except ImportError: |
||
12 | raise ImportError,"Please check whether all necessary modules are installed" |
||
13 | class Launch_box(wx.Frame): |
||
14 | #global GENDER,wav_path |
||
15 | #GENDER = 'Female' |
||
16 | scan_settings = -1 |
||
17 | #conf_dict =[] |
||
18 | def __init__(self, parent, id, title): #initalisation method |
||
19 | #frame method with frame parameters passed |
||
20 | wx.Frame.__init__(self, parent, id, title, size=(1024, 600)) |
||
21 | self.panel = wx.Panel(self, -1) # creates frame panel |
||
22 | self.panel.SetBackgroundColour('#C0C0C0') #Panel Backgnd color |
||
23 | #font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT) # font settings |
||
24 | font = wx.Font(16, wx.ROMAN, wx.NORMAL, wx.BOLD) |
||
25 | font.SetPointSize(18) # font size settings |
||
26 | if platform.machine()=='armv7l':#beagle board volume and gpio settings |
||
27 | #subprocess.call(['./gpio.sh','133','low']) |
||
28 | os.system("sudo i2cset -y 2 0x4d 0x45 >/dev/null") |
||
29 | os.system("sudo i2cset -y 2 0x4d 0x65 >/dev/null") |
||
30 | os.system("sudo i2cset -y 2 0x4d 0x89 >/dev/null") |
||
31 | time.sleep(1) |
||
32 | os.system("sudo i2cset -y 2 0x4d 0xb3 >/dev/null") |
||
33 | os.system("amixer -c omap3beagle -- sset Carkit playback 67% >/dev/null")##set the carkit volume |
||
34 | self.conf_dict = {1:'SPEED' ,2:'VOLUME',3:'GENDER',4:'LEVEL'} |
||
35 | config_file = open('configuration.pkl', 'rb') |
||
36 | config_settin = pickle.load(config_file)##load the configuration details |
||
37 | config_file.close() |
||
38 | global GENDER,wav_path |
||
39 | os.chdir("../") |
||
40 | if not os.path.exists("Voices"): |
||
41 | os.mkdir("Voices") |
||
42 | os.mkdir("Voices/Male") |
||
43 | os.mkdir("Voices/Female") |
||
44 | os.chdir("./launch") |
||
45 | wav_path = "../Voices/" |
||
46 | gender_dict = {1:'Female',2:'Male'} |
||
47 | GENDER = gender_dict[config_settin['GENDER']] |
||
48 | self.set_volume(config_settin['VOLUME'])#settin the DAC2 volume |
||
49 | vbox = wx.BoxSizer(wx.VERTICAL)#boxsizer for placement of widgets |
||
50 | self.settin_button = wx.Button(self.panel,-1, 'Settings',size=(140,65))#button creation |
||
51 | self.settin_button.SetFont(font) |
||
52 | self.hope_button = wx.Button(self.panel,-1, 'HOPE',size=(140,65))#button creation |
||
53 | self.hope_button.SetFont(font) |
||
54 | |||
55 | self.Title_txt = wx.StaticText(self.panel,-1," LAUNCH-BOX ",(5,5)) |
||
56 | self.Title_txt.SetFont(font) |
||
57 | speed_txt = wx.Button(self.panel, -1, 'SPEED', size=(140,65)) |
||
58 | self.rb1 = wx.RadioButton(self.panel, -1, 'Fast', (5, 5),style = wx.RB_GROUP) |
||
59 | self.rb2 = wx.RadioButton(self.panel, -1, 'Medium', (5, 5)) |
||
60 | self.rb3 = wx.RadioButton(self.panel, -1, 'Slow', (5, 5)) |
||
61 | volume_txt = wx.Button(self.panel, -1, 'VOLUME', size=(140,65)) |
||
62 | self.rb4 = wx.RadioButton(self.panel, -1, 'Low', (5, 5),style = wx.RB_GROUP) |
||
63 | self.rb5 = wx.RadioButton(self.panel, -1, 'Medium', (5, 5)) |
||
64 | self.rb6 = wx.RadioButton(self.panel, -1, 'High', (5, 5)) |
||
65 | gender_txt = wx.Button(self.panel, -1, 'GENDER', size=(140,65)) |
||
66 | self.rb7 = wx.RadioButton(self.panel, -1, 'Female', (5, 5),style = wx.RB_GROUP) |
||
67 | self.rb8 = wx.RadioButton(self.panel, -1, 'Male', (5, 5)) |
||
68 | User_level_txt = wx.Button(self.panel, -1, 'LEVEL',size=(140,65)) |
||
69 | self.rb9 = wx.RadioButton(self.panel, -1, 'Beginner', (5, 5),style = wx.RB_GROUP) |
||
70 | self.rb10 = wx.RadioButton(self.panel, -1, 'Advanced', (5, 5)) |
||
71 | self.shutdown_button = wx.Button(self.panel, -1, 'Shutdown',size=(145,65)) |
||
72 | self.sanyog_button = wx.Button(self.panel,-1, 'SANYOG',size=(140,65))#button creation |
||
73 | self.sanyog_button.SetFont(font) |
||
74 | |||
75 | vbox.Add(self.Title_txt, 0,wx.TOP|wx.ALIGN_CENTER , 10)#|wx.EXPAND |
||
76 | vbox.AddSpacer(30) |
||
77 | speed_list =[speed_txt,self.rb1,self.rb2,self.rb3] |
||
78 | volume_list = [volume_txt,self.rb4,self.rb5,self.rb6] |
||
79 | gender_list = [gender_txt,self.rb7,self.rb8] |
||
80 | User_level_list = [User_level_txt,self.rb9,self.rb10] |
||
81 | #shutdown_list = [shutdown_txt] |
||
82 | Button_list = [self.settin_button,self.hope_button,self.sanyog_button,self.shutdown_button] |
||
83 | self.wdgt_list = [Button_list,speed_list,volume_list,gender_list,User_level_list]#,shutdown_list |
||
84 | speed_grid = wx.GridSizer(1,len(speed_list)) |
||
85 | volume_grid = wx.GridSizer(1,len(volume_list)) |
||
86 | gender_grid = wx.GridSizer(1,len(gender_list)) |
||
87 | User_level_grid = wx.GridSizer(1,len(User_level_list)) |
||
88 | Button_grid = wx.GridSizer(1,len(Button_list)) |
||
89 | #shutdown_grid = wx.GridSizer(1,len(shutdown_list)) |
||
90 | grid_list = [Button_grid,speed_grid,volume_grid,gender_grid,User_level_grid ]#,shutdown_grid |
||
91 | for k in range(1,len(self.wdgt_list)): |
||
92 | self.wdgt_list[k][config_settin[self.conf_dict[k]]].SetValue(True) |
||
93 | for grid_indx in xrange(len(grid_list)): |
||
94 | if grid_indx>0 : |
||
95 | self.wdgt_list[grid_indx][0].Bind(wx.EVT_KEY_DOWN, self.select_option) |
||
96 | for list_indx in xrange(len(self.wdgt_list[grid_indx])): |
||
97 | grid_list[grid_indx].Add(self.wdgt_list[grid_indx][list_indx],0,wx.CENTRE) |
||
98 | self.wdgt_list[grid_indx][list_indx].SetFont(font) |
||
99 | self.wdgt_list[grid_indx][list_indx].SetBackgroundColour('#f0f8ff') |
||
100 | |||
101 | vbox.Add(Button_grid, 0,wx.BOTTOM, 10)#|wx.EXPAND |
||
102 | vbox.Add(speed_grid, 0, wx.BOTTOM | wx.TOP) |
||
103 | vbox.AddSpacer(20) |
||
104 | vbox.Add(volume_grid, 0, wx.BOTTOM | wx.TOP) |
||
105 | vbox.AddSpacer(20) |
||
106 | vbox.Add(gender_grid, 0, wx.BOTTOM | wx.TOP) |
||
107 | vbox.AddSpacer(20) |
||
108 | vbox.Add(User_level_grid, 0, wx.BOTTOM | wx.TOP) |
||
109 | vbox.AddSpacer(20) |
||
110 | #vbox.Add(User_level_grid, 0, wx.BOTTOM | wx.TOP) |
||
111 | #vbox.AddSpacer(20) |
||
112 | |||
113 | self.panel.SetSizer(vbox) # to display the elements in the frame |
||
114 | self.hope_button.Bind(wx.EVT_BUTTON, self.launch_hope) |
||
115 | self.hope_button.Bind(wx.EVT_KEY_DOWN, self.launch_hope) |
||
116 | self.hope_button.Bind(wx.EVT_LEFT_DOWN, self.launch_hope) |
||
117 | self.shutdown_button.Bind(wx.EVT_BUTTON, self.shutdown) |
||
118 | self.shutdown_button.Bind(wx.EVT_KEY_DOWN, self.shutdown) |
||
119 | self.Centre() ##frame opened at the center of the screen |
||
120 | self.Show(True) ##Displays the screen |
||
121 | self.label = 'Settings' |
||
122 | #""" TIMER INCLUSION """# |
||
123 | self.settings = [self.settin_button,self.hope_button,self.sanyog_button,self.shutdown_button,self.rb1,self.rb2,self.rb3,self.rb4,\ |
||
124 | self.rb5,self.rb6,self.rb7,self.rb8,self.rb9,self.rb10] |
||
125 | self.settings[0].Bind(wx.EVT_KEY_DOWN, self.select) |
||
126 | self.settings[0].Bind(wx.EVT_LEFT_DOWN, self.select) |
||
127 | for i in range(4,len(self.settings)): |
||
128 | self.settings[i].Bind(wx.EVT_KEY_DOWN, self.select_value) |
||
129 | self.settings[i].Bind(wx.EVT_LEFT_DOWN, self.select_value) |
||
130 | self.panel.Bind(wx.EVT_LEFT_DOWN,self.mouse_click) |
||
131 | TIMER_ID = 200 # pick a number to set timer speed_listid |
||
132 | wx.EVT_TIMER(self.panel, TIMER_ID, self.set_focus) # call the on_timer function |
||
133 | self.timer_strt_scr = wx.Timer(self.panel, TIMER_ID) # message will be sent to the panel |
||
134 | self.timer_strt_scr.Start(1500) # x100 milliseconds |
||
135 | self.grid_indx = 0 |
||
136 | self.list_indx = 0 |
||
137 | self.option = 0 |
||
138 | self.wdgt_list[self.grid_indx][self.list_indx].SetFocus() # to set focus |
||
139 | self.wdgt_list[self.grid_indx][self.list_indx].SetBackgroundColour('#fffacd') |
||
140 | self.read_aloud(self.wdgt_list[self.grid_indx][self.list_indx].GetLabel()) |
||
141 | |||
142 | def mouse_click(self,event): |
||
143 | if self.hope_button == self.wdgt_list[self.grid_indx][self.list_indx]: |
||
144 | self.launch_hope(event) |
||
145 | elif self.settin_button == self.wdgt_list[self.grid_indx][self.list_indx]: |
||
146 | self.select(event) |
||
147 | elif self.shutdown_button == self.wdgt_list[self.grid_indx][self.list_indx]: |
||
148 | self.shutdown(event) |
||
149 | elif self.wdgt_list[1][0] == self.wdgt_list[self.grid_indx][self.list_indx] or self.wdgt_list[2][0] == self.wdgt_list[self.grid_indx][self.list_indx] or self.wdgt_list[3][0] == self.wdgt_list[self.grid_indx][self.list_indx] or self.wdgt_list[4][0] == self.wdgt_list[self.grid_indx][self.list_indx]: |
||
150 | self.select_option(event) |
||
151 | else: |
||
152 | self.select_value(event) |
||
153 | def select(self,event): |
||
154 | self.label = self.settin_button.GetLabel() |
||
155 | global GENDER |
||
156 | #print self.label |
||
157 | if self.label == 'Settings': |
||
158 | self.settin_button.SetLabel('SAVE') |
||
159 | self.list_indx = 0 |
||
160 | |||
161 | else: |
||
162 | self.settin_button.SetLabel('Settings') |
||
163 | self.list_indx = 0 |
||
164 | data = [] |
||
165 | for j in range(1,len(self.wdgt_list)): |
||
166 | for k in range(1,len(self.wdgt_list[j])): |
||
167 | if self.wdgt_list[j][k].GetValue(): |
||
168 | data.append(k) |
||
169 | config = {'SPEED' : data[0],'VOLUME': data[1],'GENDER': data[2],'LEVEL' : data[3]} |
||
170 | output = open('configuration.pkl', 'wb') |
||
171 | pickle.dump(config,output) |
||
172 | output.close() |
||
173 | gender_dict = {1:'Female',2:'Male'} |
||
174 | #print "test",str(gender_dict[config['GENDER']]) |
||
175 | GENDER = str(gender_dict[config['GENDER']]) |
||
176 | def select_option(self,event): #to choose the various setting options |
||
177 | self.button_label = self.wdgt_list[self.grid_indx][self.list_indx].GetLabel() |
||
178 | if self.button_label == 'BACK': |
||
179 | self.option = 0 |
||
180 | self.wdgt_list[self.grid_indx][self.list_indx].SetLabel(self.prev) |
||
181 | |||
182 | else : |
||
183 | self.option = 1 |
||
184 | self.prev = self.button_label |
||
185 | self.wdgt_list[self.grid_indx][self.list_indx].SetLabel('BACK') |
||
186 | def select_value(self,event): |
||
187 | self.option = 0 |
||
188 | self.wdgt_list[self.grid_indx][self.list_indx].SetValue(True) |
||
189 | if self.conf_dict[self.grid_indx]== 'VOLUME': |
||
190 | self.set_volume(self.list_indx) |
||
191 | self.list_indx = 0 |
||
192 | self.wdgt_list[self.grid_indx][self.list_indx].SetLabel(self.prev) |
||
193 | def set_focus(self,event): |
||
194 | #print "focus" |
||
195 | self.wdgt_list[self.grid_indx][self.list_indx].SetBackgroundColour('#f0f8ff') |
||
196 | self.label=self.settin_button.GetLabel() |
||
197 | if self.grid_indx == 0 and self.label == 'Settings': |
||
198 | self.list_indx = (self.list_indx + 1) % len(self.wdgt_list[self.grid_indx]) |
||
199 | if self.label=='SAVE'and self.option==0: |
||
200 | self.grid_indx = (self.grid_indx + 1) % len(self.wdgt_list) |
||
201 | if self.option == 1 : |
||
202 | if self.list_indx == len(self.wdgt_list[self.grid_indx]): |
||
203 | self.list_indx = (self.list_indx + 1) |
||
204 | self.list_indx = (self.list_indx + 1) % len(self.wdgt_list[self.grid_indx]) |
||
205 | self.wdgt_list[self.grid_indx][self.list_indx].SetFocus() # to set focus |
||
206 | self.wdgt_list[self.grid_indx][self.list_indx].SetBackgroundColour('#fffacd') |
||
207 | self.read_aloud(self.wdgt_list[self.grid_indx][self.list_indx].GetLabel()) |
||
208 | #print "Test",GENDER |
||
209 | def set_volume(self,vol_tres): |
||
210 | if platform.machine()=='armv7l': |
||
211 | if vol_tres == 3: |
||
212 | |||
213 | os.system("sudo i2cset -y 2 0x4d 0x47 >/dev/null") |
||
214 | os.system("sudo i2cset -y 2 0x4d 0x67 >/dev/null") |
||
215 | os.system("amixer -c omap3beagle -- cset numid=6 70% >/dev/null") |
||
216 | elif vol_tres == 2: |
||
217 | |||
218 | os.system("sudo i2cset -y 2 0x4d 0x45 >/dev/null") |
||
219 | os.system("sudo i2cset -y 2 0x4d 0x65 >/dev/null") |
||
220 | os.system("amixer -c omap3beagle -- cset numid=6 40% >/dev/null") |
||
221 | else : |
||
222 | os.system("amixer -c omap3beagle -- cset numid=6 20% >/dev/null") |
||
223 | os.system("sudo i2cset -y 2 0x4d 0x43 >/dev/null") |
||
224 | os.system("sudo i2cset -y 2 0x4d 0x63 >/dev/null") |
||
225 | def launch_hope(self,event): |
||
226 | #self.Destroy() |
||
227 | if platform.system() == 'Windows': |
||
228 | os.system("python ../Hope/hope.py") |
||
229 | if platform.system() == 'Linux': |
||
230 | os.system("sudo python ../Hope/hope.py") |
||
231 | def shutdown(self,event): |
||
232 | if platform.system() == 'Linux': |
||
233 | os.system("sudo halt") |
||
234 | if platform.system() == 'Windows': |
||
235 | os.system("shutdown -s") |
||
236 | def read_aloud(self,sound): |
||
237 | global GENDER |
||
238 | #print GENDER |
||
239 | resume_time = self.timer_strt_scr.GetInterval() |
||
240 | self.timer_strt_scr.Stop() |
||
241 | #subprocess.call(['./playit.sh',GENDER,sound ]) |
||
242 | if os.path.isfile(wav_path+GENDER+'/'+sound+'.wav'): |
||
243 | if platform.machine()=='armv7l': |
||
244 | #print "IN File",GENDER |
||
245 | play_temp = wav_path+GENDER+'/'+sound+'.wav' |
||
246 | subprocess.call(['aplay', play_temp]) |
||
247 | else : |
||
248 | play = wx.Sound(wav_path+GENDER+'/'+sound+'.wav') |
||
249 | play.Play() |
||
250 | else : |
||
251 | |||
252 | #print GENDER |
||
253 | fname = wav_path+GENDER+"/input.txt" |
||
254 | myfile = open(fname, "w") |
||
255 | myfile.write(sound) |
||
256 | myfile.close() |
||
257 | fpath =sound+".wav" |
||
258 | |||
259 | if GENDER=='Male': |
||
260 | os.chdir('../Voices/Male/') |
||
261 | subprocess.call(['espeak','-f', 'input.txt', '-ven+m3','-w',fpath ]) |
||
262 | else: |
||
263 | os.chdir('../Voices/Female/') |
||
264 | subprocess.call(['espeak','-f', 'input.txt', '-ven+f3','-w',fpath ]) |
||
265 | |||
266 | if platform.machine()=='armv7l': |
||
267 | |||
268 | subprocess.call(['aplay', fpath]) |
||
269 | else : |
||
270 | play = wx.Sound(fpath) |
||
271 | play.Play() |
||
272 | os.chdir('../') |
||
273 | os.chdir('../launch') |
||
274 | self.timer_strt_scr.Start(resume_time) |
||
275 | ## Main Loop: |
||
276 | if __name__ == "__main__": |
||
277 | |||
278 | app = wx.App() |
||
279 | Launch_box(None, -1, 'LAUNCH') ##main class |
||
280 | app.MainLoop() |