root / python / hope.py @ 0:3c15de202295
History | View | Annotate | Download (33.5 KB)
1 | 0:3c15de202295 | ee11d037 | #!usr/bin/python
|
---|---|---|---|
2 | """HOPE
|
||
3 | Developed on the Kavi kit.
|
||
4 | STATUS : Working
|
||
5 | FEATURES: Font and background with all the widgets included
|
||
6 | COMMENTS: Frame has appeared with bgcolor and font settings
|
||
7 | * Level1 - Alphabet triple buttons Added
|
||
8 | * Level2 - Alapabet buttons and Back button are Added
|
||
9 | Level3 - Text boxes are added
|
||
10 | - MORE and BACK buttons Added
|
||
11 | - DONE and ERASE buttons are Added
|
||
12 | LevelF - Addition of the text Box used to read out the sentence
|
||
13 |
|
||
14 | * Timer is intiated
|
||
15 | * Methods included : 1] on_timer() # defines the focus action
|
||
16 | 2] on_close () # defines exit action
|
||
17 | * Scaning of the alphabet Triples is done
|
||
18 | * When a button in level one is clicked the values in the aplhabet
|
||
19 | buttons in level 2 change
|
||
20 | * Scanings of LEVEL2 buttons[Alphabet,back,done and erase] is done.
|
||
21 | * Actions performed when level2 buttons are clicked is defined
|
||
22 | * Erase - erases the text in the read out textbox and returns to level three
|
||
23 | * Undo - Clears the text in spell mode and word mode textboxes
|
||
24 | and returns to level one
|
||
25 | * Back - Returns focus to level one
|
||
26 | * # - Copies the spell mode text to the readout box and goes to
|
||
27 | levelone after focussing done and erase buttons
|
||
28 |
|
||
29 | Spell mode function
|
||
30 | Spellmode andWord mode logic Added
|
||
31 | Templete mode is added
|
||
32 | * Done action is added
|
||
33 | * Automatic Button Resize
|
||
34 | * Skips scanning blank textboxes
|
||
35 | * Text to Speech
|
||
36 | -espeak is used[Note:Windows users need to install espeak and copy the espeak
|
||
37 | exe to system32 folder
|
||
38 | -Male and Female Voices are available and are set in the launch_box
|
||
39 |
|
||
40 | """
|
||
41 | ##Import Necessary Modules
|
||
42 | try:
|
||
43 | import wx |
||
44 | import time |
||
45 | import subprocess |
||
46 | import pickle |
||
47 | import sched |
||
48 | import os |
||
49 | import platform |
||
50 | from operator import itemgetter |
||
51 | except ImportError: |
||
52 | raise ImportError,"Please check whether all necessary modules are installed" |
||
53 | |||
54 | #*******************************************************************************#
|
||
55 | |||
56 | ## Main class Definition
|
||
57 | |||
58 | class Class_Frame(wx.Frame): |
||
59 | global SPEED,BG_COLOR,path,GENDER,wav_path
|
||
60 | path = "../Hope/"
|
||
61 | wav_path = "../Voices/"
|
||
62 | scan_L1 = -1
|
||
63 | scan_L2 = -1
|
||
64 | scan_L3 = -1
|
||
65 | Level = 1
|
||
66 | clr = ""
|
||
67 | label_prt = ''
|
||
68 | BG_COLOR = '#C0C0C0'
|
||
69 | btn_color = '#f0f8ff'#wx.WHITE |
||
70 | focus_color = '#fffacd'#(255,192,203) |
||
71 | sm_color = (173,234,234)#blue |
||
72 | sm1_color = (250,128,114)#salmon |
||
73 | sm_word = ''#spell mode |
||
74 | sm_prev = ''
|
||
75 | word_prev = ''
|
||
76 | word = ''
|
||
77 | refresh_word = ''
|
||
78 | temp =0
|
||
79 | n = 1 #word mode |
||
80 | wm_flag = 0
|
||
81 | done = 1
|
||
82 | erase = 0
|
||
83 | loop = 0
|
||
84 | no_of_wm_txtbx = 4
|
||
85 | Lvl = {"Level_1":1,"Level_2":2,"Level_3":3} |
||
86 | temp_button = ["TEMP","UNDO","EXIT"] |
||
87 | Alp_tri_dict = {0:"ABC",1:"DEF",2:"GHI",3:"JKL",4:"MNO",5:"PQR",6:"STU",7:"VWX",8:"YZ#",9:"123"} |
||
88 | |||
89 | speed_dict = {1:1000,2:1500,3:2000} |
||
90 | gender_dict = {1:'Female',2:'Male'} |
||
91 | config_file = open("../launch/configuration.pkl", 'rb') |
||
92 | config_settin = pickle.load(config_file) |
||
93 | config_file.close() |
||
94 | SPEED = speed_dict[config_settin['SPEED']]
|
||
95 | GENDER = gender_dict[config_settin['GENDER']]
|
||
96 | def __init__(self, parent, id, title): #initalisation method |
||
97 | #frame method with frame parameters passed
|
||
98 | wx.Frame.__init__(self, parent, id, title, size=(1024, 600)) |
||
99 | self.panel = wx.Panel(self, -1) # creates frame panel |
||
100 | self.panel.SetBackgroundColour(BG_COLOR) #Panel Backgnd color |
||
101 | #font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
|
||
102 | font = wx.Font(16, wx.ROMAN, wx.NORMAL, wx.BOLD)
|
||
103 | font.SetPointSize(18) # font size settings |
||
104 | if platform.machine()=='armv7l': |
||
105 | # subprocess.call(['./gpio.sh','133','low'])
|
||
106 | os.system("sudo i2cset -y 2 0x4d 0x45 >/dev/null")
|
||
107 | |||
108 | os.system("sudo i2cset -y 2 0x4d 0x65 >/dev/null")
|
||
109 | os.system("sudo i2cset -y 2 0x4d 0x89 >/dev/null")
|
||
110 | ################################################################################
|
||
111 | |||
112 | #""" WIDGETS INCLUSION """#
|
||
113 | |||
114 | ################################################################################
|
||
115 | |||
116 | ### Main BOX - Vbox ###
|
||
117 | |||
118 | vbox = wx.BoxSizer(wx.VERTICAL)#boxsizer for placement of widgets
|
||
119 | |||
120 | vbox.Add((-1, 10))#spacing |
||
121 | |||
122 | ############################ LEVEL ONE ##################################
|
||
123 | |||
124 | |||
125 | Level1 = wx.BoxSizer(wx.HORIZONTAL)#panel for buttons of alphabet triples
|
||
126 | gs1 = wx.GridSizer(1, len(self.Alp_tri_dict), 1, 1) #gridsizer for the alphabet triple buttons |
||
127 | self.b = []
|
||
128 | for j in xrange(len(self.Alp_tri_dict)): #loop to add the buttons to the grid and to set the font |
||
129 | self.b.append(wx.Button(self.panel,-1, self.Alp_tri_dict[j],size=(15,75)))#button creation |
||
130 | gs1.Add(self.b[j], 0, wx.EXPAND) |
||
131 | self.b[j].SetFont(font)
|
||
132 | self.b[j].SetBackgroundColour(self.btn_color) |
||
133 | self.b[j].Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
||
134 | Level1.Add(gs1, 1,wx.EXPAND ) # Add the grid to level one |
||
135 | vbox.Add(Level1, 0,wx.TOP|wx.ALIGN_CENTER|wx.EXPAND , 10) # Add level one to the panel |
||
136 | |||
137 | ######################## LEVEL TWO ###############################
|
||
138 | |||
139 | vbox.Add((-1, 20)) |
||
140 | |||
141 | Level2 = wx.BoxSizer(wx.HORIZONTAL)#Boxsizer for buttons of alphabets , text boxes ,More,back,DONE,Erase
|
||
142 | |||
143 | ### Alphabet buttons and Back Button inclusion
|
||
144 | Alpt = wx.BoxSizer(wx.VERTICAL) #Vertical sizer for the alphabet buttons
|
||
145 | self.b11 = wx.Button(self.panel, -1,'Back',size=(100,50))#BUTTON CREATION |
||
146 | self.b12 = wx.Button(self.panel, -1, 'A') |
||
147 | self.b13 = wx.Button(self.panel, -1, 'B') |
||
148 | self.b14 = wx.Button(self.panel, -1, 'C') |
||
149 | self.alp = [self.b11,self.b12,self.b13,self.b14] #button list |
||
150 | Alpt_gs = wx.GridSizer(4, 1, 3, 3) #Alphabet button grid |
||
151 | #Alpt_gs.AddSpacer(2)
|
||
152 | for j in xrange(4): #loop to add the buttons to the grid and to set the font |
||
153 | Alpt_gs.Add(self.alp[j], 0, wx.EXPAND) |
||
154 | self.alp[j].SetFont(font)
|
||
155 | self.alp[j].SetBackgroundColour(self.btn_color) # colour settings |
||
156 | self.alp[j].Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
||
157 | Alpt.Add(Alpt_gs, 1, wx.LEFT) #Add alphabet grid to box |
||
158 | Level2.Add(Alpt, 1, wx.EXPAND) #Add alphabet box to the Level2 boxsizer |
||
159 | |||
160 | ### Text Boxes buttons inclusion
|
||
161 | |||
162 | text_col = wx.BoxSizer(wx.VERTICAL) # Vertical sizer for text boxes
|
||
163 | self.txt_bx1 = wx.TextCtrl(self.panel, -1,size=(600,15),style = wx.TE_LEFT) #text box for spell mode |
||
164 | self.txt_bx1.SetBackgroundColour(self.sm_color)#SET colour of the spell mode text box |
||
165 | text_col.Add(self.txt_bx1, 1,wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT) #Add spell text box to textbox sizer |
||
166 | #self.txt_bx2.SetSize((200,200))
|
||
167 | text_col.AddSpacer(5)#for spacing between text boxes |
||
168 | self.txt_bx2 = wx.TextCtrl(self.panel, -1,size=(600,15),style=wx.TE_LEFT) #text box for word mode |
||
169 | text_col.Add(self.txt_bx2, 1,wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT ) #Add word text box to textbox sizer |
||
170 | text_col.AddSpacer(5)#for spacing between text boxes |
||
171 | self.txt_bx3 = wx.TextCtrl(self.panel, -1,size=(600,15),style = wx.TE_LEFT) |
||
172 | text_col.Add(self.txt_bx3, 1,wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT ) |
||
173 | text_col.AddSpacer(5)#for spacing between text boxes |
||
174 | self.txt_bx4 = wx.TextCtrl(self.panel, -1,size=(600,15),style = wx.TE_LEFT) |
||
175 | text_col.Add(self.txt_bx4, 1,wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT) |
||
176 | text_col.AddSpacer(5)#for spacing between text boxes |
||
177 | self.txt_bx5 = wx.TextCtrl(self.panel, -1,size=(600,15)) |
||
178 | text_col.Add(self.txt_bx5, 1,wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT) |
||
179 | #text_col.AddSpacer(5)#for spacing between text boxes
|
||
180 | self.txt_bx1.SetFont(font)
|
||
181 | self.txt_bx2.SetFont(font)
|
||
182 | self.txt_bx3.SetFont(font)
|
||
183 | self.txt_bx4.SetFont(font)
|
||
184 | self.txt_bx5.SetFont(font)
|
||
185 | ### INCLUSION of MORE and ERASE buttons
|
||
186 | |||
187 | More_bck= wx.BoxSizer(wx.HORIZONTAL) #sizer to add more and back buttons
|
||
188 | More_bck_gs = wx.GridSizer(1, 2, 1, 1) #grid to add the buttons |
||
189 | #MORE and BACK button Creation
|
||
190 | self.b15 = wx.Button(self.panel, -1, 'MORE',size=(120,40)) |
||
191 | self.b16 = wx.Button(self.panel, -1, 'BACK',size=(120,40)) |
||
192 | More_bck_gs.AddMany( [(self.b15, 0, wx.EXPAND), |
||
193 | (self.b16, 0,wx.EXPAND )]) #Addition of button to the grid |
||
194 | #Font and color settings of the buttons
|
||
195 | self.b15.SetFont(font)
|
||
196 | self.b16.SetFont(font)
|
||
197 | self.b15.SetBackgroundColour(self.btn_color) |
||
198 | self.b16.SetBackgroundColour(self.btn_color) |
||
199 | self.b15.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
||
200 | self.b16.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
||
201 | More_bck.Add(More_bck_gs, 1,wx.CENTER| wx.TOP) # Add the grid to the sizerwx.LEFT | wx.RIGHT| |
||
202 | text_col.AddSpacer(5)
|
||
203 | text_col.Add(More_bck, 1,wx.EXPAND |wx.ALIGN_CENTER) #Add the more_bck sizer to the text_col sizer |
||
204 | |||
205 | Level2.Add(text_col, 1, wx.CENTER|wx.EXPAND) #Add text_col sizer to the level2 sizer |
||
206 | |||
207 | ### INCLUSION of DONE and ERASE buttons
|
||
208 | Done_Ers = wx.BoxSizer(wx.VERTICAL) #sizer to add DONE and ERASE buttons
|
||
209 | Done_Ers_gs = wx.GridSizer(2, 1, 1, 1) #Grid to add the buttons |
||
210 | # Creation of DONE and ERASE buttons
|
||
211 | self.b17 = wx.Button(self.panel, -1, 'DONE',size=(100,40)) # |
||
212 | self.b18 = wx.Button(self.panel, -1, 'ERASE',size=(100,40)) |
||
213 | Done_Ers_gs.AddSpacer(10)
|
||
214 | # Addition of DONE and ERASE buttons to Done_Ers Grid
|
||
215 | Done_Ers_gs.AddMany( [(self.b17, 0,wx.EXPAND),#wx.ALIGN_CENTER_VERTICAL |
||
216 | (self.b18, 0, wx.EXPAND)])#, wx.EXPAND |
||
217 | Done_Ers_gs.AddSpacer(20)
|
||
218 | #Font settings of DONE and ERASE buttons
|
||
219 | self.b17.SetFont(font)
|
||
220 | self.b18.SetFont(font)
|
||
221 | self.b17.SetBackgroundColour(self.btn_color) |
||
222 | self.b18.SetBackgroundColour(self.btn_color) |
||
223 | self.b17.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
||
224 | self.b18.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
||
225 | #Addition of Done_Ers Grid to Done_Ers Sizer
|
||
226 | Done_Ers.Add(Done_Ers_gs, 1, wx.ALIGN_RIGHT | wx.RIGHT)
|
||
227 | #Addition of Done_Ers Sizer to Level2 sizer
|
||
228 | Level2.Add(Done_Ers, 1,wx.EXPAND)
|
||
229 | |||
230 | vbox.Add(Level2, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 10) #Add level2 boxsizer to main box |
||
231 | |||
232 | |||
233 | ###################### LEVELF READOUT TEXT BOX ###########################
|
||
234 | |||
235 | |||
236 | ###INCLUSION of the Readout Textbox
|
||
237 | |||
238 | vbox.Add((-1, 30))#spacing bottom |
||
239 | LevelF = wx.BoxSizer(wx.HORIZONTAL) # boxer to include the text box
|
||
240 | self.senttxt_bx = wx.TextCtrl(self.panel, -1) #Text Box Creation |
||
241 | self.senttxt_bx.SetFont(font)
|
||
242 | LevelF.Add(self.senttxt_bx, 1,wx.EXPAND | wx.LEFT | wx.RIGHT )#Addition of the text box to the sizer |
||
243 | vbox.Add(LevelF, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.LEFT | wx.RIGHT, 10)#Add the sizer to the Main box |
||
244 | vbox.Add((-1, 10)) #spacing |
||
245 | |||
246 | |||
247 | ################# FOR PROPER DISPLAY OF WIDGETS #############
|
||
248 | |||
249 | self.panel.SetSizer(vbox) # to display the elements in the frame |
||
250 | |||
251 | # Bind the button click to the action that is to happenwx.EVT_LEFT_DOWN|
|
||
252 | self.panel.Bind(wx.EVT_LEFT_DOWN, self.action) |
||
253 | self.Bind(wx.EVT_BUTTON, self.action) |
||
254 | self.alp = [self.b11,self.b12,self.b13,self.b14,self.b17,self.b18] |
||
255 | self.txt = [self.txt_bx1,self.txt_bx2,self.txt_bx3,self.txt_bx4,self.txt_bx5,self.b15,self.b16] |
||
256 | for loop in range(0,self.no_of_wm_txtbx+1): |
||
257 | self.txt[loop].Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
||
258 | |||
259 | |||
260 | #""" TIMER INCLUSION """#
|
||
261 | |||
262 | |||
263 | TIMER_ID = 100 # pick a number to set timer id |
||
264 | wx.EVT_TIMER(self.panel, TIMER_ID, self.on_timer) # call the on_timer function |
||
265 | self.timer = wx.Timer(self.panel, TIMER_ID) # message will be sent to the panel |
||
266 | self.timer.Start(SPEED) # x100 milliseconds |
||
267 | |||
268 | |||
269 | |||
270 | #""" DISPLAY THE FRAME """#
|
||
271 | |||
272 | |||
273 | |||
274 | self.Centre() ##frame opened at the center of the screen |
||
275 | self.Show(True) ##Displays the screen |
||
276 | |||
277 | #******************************************************************************#
|
||
278 | |||
279 | ###Method to stop the timer and exit HOPE###
|
||
280 | |||
281 | def on_close(self,event): |
||
282 | self.timer.Stop()
|
||
283 | frame.Destroy() |
||
284 | def OnKeyDown(self, event): |
||
285 | keycode = event.GetKeyCode() |
||
286 | #print keycode
|
||
287 | #if keycode == wx.WXK_SPACE or keycode == 65 or keycode == 66:
|
||
288 | self.action(event)
|
||
289 | def exit_hope(self): |
||
290 | self.timer.Stop()
|
||
291 | self.Destroy()
|
||
292 | |||
293 | #******************************************************************************#
|
||
294 | |||
295 | ### Method to set the focus automatically using timer
|
||
296 | |||
297 | def on_timer(self,event): |
||
298 | |||
299 | if self.Level == self.Lvl["Level_1"]: |
||
300 | # Reset the erase button colour special case!!After a word is selected.
|
||
301 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color) |
||
302 | self.b[self.scan_L1].SetBackgroundColour(self.btn_color) # Reset the button colour |
||
303 | self.scan_L1 = (self.scan_L1 + 1) % len(self.Alp_tri_dict) |
||
304 | self.b[self.scan_L1].SetBackgroundColour(self.focus_color)# Coloured Highlight of the focused button |
||
305 | self.b[self.scan_L1].SetFocus() # to set focus on the alphabet triples |
||
306 | sound = self.b[self.scan_L1].GetLabel() |
||
307 | if sound == 'YZ#': |
||
308 | self.read_aloud("Y Z space") |
||
309 | elif sound == '123': |
||
310 | self.read_aloud("Template") |
||
311 | elif sound == 'DEF': |
||
312 | self.read_aloud("D E F") |
||
313 | elif sound == 'GHI': |
||
314 | self.read_aloud("G H I") |
||
315 | elif sound == 'MNO': |
||
316 | self.read_aloud("M N O") |
||
317 | elif sound == 'STU': |
||
318 | self.read_aloud("S T U") |
||
319 | else :
|
||
320 | self.read_aloud(sound)
|
||
321 | |||
322 | elif self.Level == self.Lvl["Level_2"]:#focus the individual alphabets |
||
323 | |||
324 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color)# Reset the button colour |
||
325 | self.scan_L2 = (self.scan_L2 + 1) % 6 |
||
326 | self.alp[self.scan_L2].SetBackgroundColour(self.focus_color)# Coloured Highlight of the focused button |
||
327 | self.alp[self.scan_L2].SetFocus()# to set focus on the alphabet |
||
328 | if (self.scan_L2 ==5)&(self.label_prt == '#'): |
||
329 | self.label_prt = '' |
||
330 | #Goback to level one
|
||
331 | self.Level = self.Lvl["Level_1"] |
||
332 | #Start scaning the button one of level one
|
||
333 | self.scan_L1 = -1 |
||
334 | if (self.scan_L2 ==5)&(self.label_prt != '#'): |
||
335 | self.Level = self.Lvl["Level_2"] |
||
336 | self.scan_L2 = -1 |
||
337 | |||
338 | if (self.scan_L2 ==0)&(self.wm_flag == 1): |
||
339 | self.wm_flag = 0 #reset Flag |
||
340 | self.Clr_txt_bx()
|
||
341 | |||
342 | sound = self.alp[self.scan_L2].GetLabel() |
||
343 | if sound == 'TEMP': |
||
344 | self.read_aloud("Template") |
||
345 | elif sound == '#': |
||
346 | self.read_aloud("Space") |
||
347 | else :
|
||
348 | self.read_aloud(sound)
|
||
349 | |||
350 | elif self.Level == self.Lvl["Level_3"]:#focus on the textboxes ,more and back buttons |
||
351 | self.erase = 0 |
||
352 | #reset Erase button color
|
||
353 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color) |
||
354 | if self.scan_L3 == 0: # Spell mode text box default color is set to blue |
||
355 | color = self.sm_color
|
||
356 | else :
|
||
357 | color = self.btn_color
|
||
358 | self.txt[self.scan_L3].SetBackgroundColour(color)# Reset the button colour |
||
359 | if self.scan_L3 ==4: |
||
360 | self.txt[self.scan_L3].SetValue(self.refresh_word) |
||
361 | self.scan_L3 = (self.scan_L3 + 1)%len(self.txt) |
||
362 | if (self.temp==1)&(self.scan_L3==0): |
||
363 | self.txt[self.scan_L3].SetBackgroundColour(self.sm_color)# Reset the button |
||
364 | self.scan_L3 = (self.scan_L3 + 1)%len(self.txt) |
||
365 | if self.scan_L3<=4: |
||
366 | sound = self.txt[self.scan_L3].GetValue() |
||
367 | self.refresh_word = sound
|
||
368 | else:
|
||
369 | sound = self.txt[self.scan_L3].GetLabel() |
||
370 | refresh = 1
|
||
371 | while sound == '': # condition to skip empty text boxes |
||
372 | self.refresh_word = self.txt[self.scan_L3-1].GetValue() |
||
373 | if refresh == 1 and self.scan_L3>1: |
||
374 | self.txt[self.scan_L3-1].SetValue(self.refresh_word) |
||
375 | refresh = 0
|
||
376 | self.scan_L3 = (self.scan_L3 + 1)%len(self.txt) |
||
377 | if self.scan_L3<=4: |
||
378 | sound = self.txt[self.scan_L3].GetValue() |
||
379 | else:
|
||
380 | sound = self.txt[self.scan_L3].GetLabel() |
||
381 | |||
382 | self.txt[self.scan_L3].SetBackgroundColour(self.focus_color)# Coloured Highlight of the focused button |
||
383 | self.txt[self.scan_L3].SetFocus()# to set focus on the alphabet |
||
384 | self.read_aloud(sound)
|
||
385 | def read_aloud(self,sound): |
||
386 | resume_time = self.timer.GetInterval()
|
||
387 | self.timer.Stop()
|
||
388 | #subprocess.call(['./playit.sh',GENDER,sound ])
|
||
389 | if os.path.isfile(wav_path+GENDER+'/'+sound+'.wav'): |
||
390 | if platform.machine()=='armv7l': |
||
391 | play_temp = wav_path+GENDER+'/'+sound+'.wav' |
||
392 | subprocess.call(['aplay',play_temp])
|
||
393 | else:
|
||
394 | play = wx.Sound(wav_path+GENDER+'/'+sound+'.wav') |
||
395 | play.Play() |
||
396 | else :
|
||
397 | |||
398 | fname = wav_path+GENDER+"/input.txt"
|
||
399 | myfile = open(fname, "w") |
||
400 | myfile.write(sound) |
||
401 | myfile.close() |
||
402 | fpath =sound+".wav"
|
||
403 | |||
404 | if GENDER == "Male": |
||
405 | os.chdir('../Voices/Male/')
|
||
406 | subprocess.call(['espeak','-f','input.txt', '-ven+m3','-w',fpath ]) |
||
407 | else:
|
||
408 | os.chdir('../Voices/Female/')
|
||
409 | subprocess.call(['espeak','-f','input.txt', '-ven+f3','-w',fpath ]) |
||
410 | if platform.machine()=='armv7l': |
||
411 | subprocess.call(['aplay',fpath])
|
||
412 | else:
|
||
413 | play = wx.Sound(fpath) |
||
414 | play.Play() |
||
415 | os.chdir('../')
|
||
416 | os.chdir('../launch')
|
||
417 | self.timer.Start(resume_time)
|
||
418 | |||
419 | def Clr_txt_bx(self): |
||
420 | self.sm_word = self.clr |
||
421 | self.refresh_word=self.clr |
||
422 | # Clear the Spell mode & Word mode text box
|
||
423 | for j in xrange(self.no_of_wm_txtbx+1): |
||
424 | self.txt[j].SetValue(self.clr) |
||
425 | |||
426 | #******************************************************************************#
|
||
427 | def action(self,event): |
||
428 | ## print "Action Level:",self.Level
|
||
429 | if self.Level == self.Lvl["Level_1"]: # IF level one assign alphabet values to buttons in level two |
||
430 | self.Level = self.Lvl["Level_2"] # A click will take the focus to level two |
||
431 | self.temp = 0 |
||
432 | if (self.b[self.scan_L1] == self.b[-1]): # Check for last button to set as Templete/Undo button |
||
433 | self.label1 = self.temp_button |
||
434 | else:
|
||
435 | # Get The ALPLABET TRIPLES and split them to individual alphabets
|
||
436 | self.label1 = self.b[self.scan_L1].GetLabel() |
||
437 | # Set the individual alphabets to the buttons in level2
|
||
438 | self.b12.SetLabel(self.label1[0]) |
||
439 | self.b13.SetLabel(self.label1[1]) |
||
440 | self.b14.SetLabel(self.label1[2]) |
||
441 | #Reset the clicked button Colour
|
||
442 | self.b[self.scan_L1].SetBackgroundColour(self.btn_color) |
||
443 | self.scan_L2 = -1 |
||
444 | |||
445 | elif self.Level == self.Lvl["Level_2"]: # IF level Two |
||
446 | #self.erase =1
|
||
447 | # Get the label value of the selected button
|
||
448 | self.label = self.alp[self.scan_L2].GetLabel() |
||
449 | if (self.label1!= self.temp_button)&(len(self.sm_word)==0): |
||
450 | File = path+self.label1 + ".txt" |
||
451 | self.Fname = open(File,'r') |
||
452 | self.file_list = self.Fname.readlines() |
||
453 | self.Fname.close()
|
||
454 | if (self.label1 == self.temp_button): |
||
455 | self.Fname = open(path+'temp.txt','r') |
||
456 | self.file_list = self.Fname.readlines() |
||
457 | self.Fname.close()
|
||
458 | if (self.label == 'Back'): |
||
459 | self.temp = 0 |
||
460 | self.Level = self.Lvl["Level_1"] #Goback to level one |
||
461 | #Start scaning the button one of level one
|
||
462 | self.scan_L1=self.scan_L1 -3 |
||
463 | elif (self.label == '#'): # copy the spell mode text to the readout text box |
||
464 | self.spell = self.txt_bx1.GetValue() |
||
465 | self.word_prev = self.word # used to erase the last added word |
||
466 | self.word +=self.spell #append the new word to the sentence |
||
467 | self.word += ' '# add space |
||
468 | self.senttxt_bx.SetValue(self.word) |
||
469 | self.Clr_txt_bx()
|
||
470 | self.label_prt = '#' |
||
471 | self.temp = 0 |
||
472 | # Reset color highlight of the # button
|
||
473 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color) |
||
474 | |||
475 | elif (self.label == 'UNDO'): |
||
476 | self.Clr_txt_bx()
|
||
477 | # Reset color highlight of the undo button
|
||
478 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color) |
||
479 | # Goback to level one
|
||
480 | self.Level = self.Lvl["Level_1"] |
||
481 | # Start scaning the button one of level one
|
||
482 | self.scan_L1 = -1 |
||
483 | elif (self.label == 'TEMP'): |
||
484 | self.word_list = self.file_list |
||
485 | self.wlist=[]
|
||
486 | self.word_list = []
|
||
487 | for i in range(0,len(self.file_list)): |
||
488 | self.wlist.append(str(self.file_list[i][:-1]).split(',')) |
||
489 | self.wlist[i][1] = int(self.wlist[i][1]) |
||
490 | self.wlist.sort(key=itemgetter(1),reverse=True) |
||
491 | for i in range(0,len(self.file_list)): |
||
492 | self.word_list.append(self.wlist[i][0]) |
||
493 | for i in range(1,self.no_of_wm_txtbx+1): |
||
494 | self.txt[i].SetValue(self.word_list[i][:])# |
||
495 | # Reset color highlight of the TEMP button
|
||
496 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color) |
||
497 | self.temp = 1 #template mode |
||
498 | self.scan_L3 = 0 |
||
499 | self.txt[self.scan_L3].SetBackgroundColour(self.sm_color) |
||
500 | self.Level = self.Lvl["Level_3"] #goto level 3 |
||
501 | |||
502 | elif self.label == 'ERASE': |
||
503 | self.senttxt_bx.SetValue(self.word_prev) |
||
504 | self.word = self.word_prev |
||
505 | self.sm_word = self.clr |
||
506 | self.temp = 0 |
||
507 | self.wm_flag = 0 #reset Flag |
||
508 | #Start scaning the button one of level one
|
||
509 | self.scan_L1 = -1 #changed from -1 to 0[starts from wm txt box] so no use |
||
510 | self.Level = self.Lvl["Level_3"] |
||
511 | elif self.label == 'EXIT': |
||
512 | #self.timer.Stop()
|
||
513 | #self.Destroy()
|
||
514 | self.exit_hope()
|
||
515 | elif self.label == 'DONE':# done button clicked |
||
516 | if self.done ==1: # for first click to stop the scanning |
||
517 | self.scan_L2 = 4 # Retain the highlight at Done button |
||
518 | # Highlight the DONE button colour to different color than usual
|
||
519 | self.alp[self.scan_L2].SetBackgroundColour(self.sm_color) |
||
520 | self.txt[0].SetBackgroundColour(self.sm_color) |
||
521 | if platform.machine()=='armv7l': |
||
522 | #subprocess.call(['./gpio.sh','133','high' ])
|
||
523 | os.system("sudo i2cset -y 2 0x4d 0x4d >/dev/null")
|
||
524 | os.system("sudo i2cset -y 2 0x4d 0x6d >/dev/null")
|
||
525 | os.system("sudo i2cset -y 2 0x4d 0x87 >/dev/null")
|
||
526 | self.read_aloud(self.word[:-1]) |
||
527 | if platform.machine()=='armv7l': |
||
528 | #subprocess.call(['./gpio.sh','133','low' ])
|
||
529 | |||
530 | os.system("sudo i2cset -y 2 0x4d 0x45 >/dev/null")
|
||
531 | os.system("sudo i2cset -y 2 0x4d 0x65 >/dev/null")
|
||
532 | os.system("sudo i2cset -y 2 0x4d 0x89 >/dev/null")
|
||
533 | |||
534 | self.timer.Stop()# stopping the timer stops the scanning |
||
535 | self.wm_flag = 0 #reset Flag |
||
536 | self.word = self.word[:-1]+'\n' |
||
537 | str_word = str(self.word) |
||
538 | if self.word != '\n': |
||
539 | if len(str_word.rsplit())>1: |
||
540 | self.read_sent = open(path+'temp.txt','a+') |
||
541 | else:
|
||
542 | i = 0
|
||
543 | while(self.Alp_tri_dict[i].find(self.word[0])==-1): |
||
544 | i = i+1
|
||
545 | self.read_sent = open(path+self.Alp_tri_dict[i]+'.txt','a+') |
||
546 | guess =[] |
||
547 | guess_list= self.read_sent.readlines()
|
||
548 | for i in range(0,len(guess_list)): |
||
549 | guess.append(str(guess_list[i][:-1]).split(',')) |
||
550 | #self.wlist[i][0] = (self.wlist[i][1])
|
||
551 | match = 0
|
||
552 | for i in range(0,len(guess)): |
||
553 | if guess[i][0] == self.word[:-1]: |
||
554 | match =1
|
||
555 | #print "match", self.word[:-1],guess[i][0]
|
||
556 | break
|
||
557 | else:
|
||
558 | match = 0
|
||
559 | if match == 0: |
||
560 | self.read_sent.writelines(self.word[:-1]+','+'1'+'\n') |
||
561 | self.read_sent.close()
|
||
562 | self.Clr_txt_bx()
|
||
563 | self.done *= -1 # invert the logic for second click |
||
564 | else :
|
||
565 | if platform.machine()=='armV7l': |
||
566 | #subprocess.call(['./gpio.sh','133','low'])
|
||
567 | |||
568 | os.system("sudo i2cset -y 2 0x4d 0x45 >/dev/null")
|
||
569 | os.system("sudo i2cset -y 2 0x4d 0x65 >/dev/null")
|
||
570 | os.system("sudo i2cset -y 2 0x4d 0x89 >/dev/null")
|
||
571 | |||
572 | |||
573 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color)#Reset the Done button to the normal color |
||
574 | self.scan_L1 = -1 # set the scanning button as the first button in the level |
||
575 | self.Level = self.Lvl["Level_1"] # Set the active level as one |
||
576 | self.senttxt_bx.SetValue(self.clr) |
||
577 | self.sm_prev = self.clr |
||
578 | self.word = self.clr |
||
579 | self.label_prt = self.clr#loop in LEVEL_2 |
||
580 | self.timer.Start(SPEED) # Resume the Scanning |
||
581 | self.done *= -1 # invert the logic for the next click |
||
582 | |||
583 | else:
|
||
584 | self.temp = 0 |
||
585 | # Reset color highlight of the alphabet button
|
||
586 | self.alp[self.scan_L2].SetBackgroundColour(self.btn_color) |
||
587 | # Copy the clicked alphabet button label to the spell mode textbox
|
||
588 | self.sm_prev = self.sm_word # copy the text to a variable incase to print previous data if back is pressed |
||
589 | self.sm_word += self.label |
||
590 | self.txt_bx1.SetValue(self.sm_word) |
||
591 | if self.label1!= self.temp_button: |
||
592 | self.Update_words()
|
||
593 | #Set LEVEL as three and reset scan to scan the first element
|
||
594 | self.scan_L3 = -1 |
||
595 | self.Level = self.Lvl["Level_3"] |
||
596 | elif self.Level == self.Lvl["Level_3"]: |
||
597 | |||
598 | # Get the label value of the selected button
|
||
599 | self.label_3 = self.txt[self.scan_L3].GetLabel() |
||
600 | if (self.txt[self.scan_L3] == self.txt_bx1): |
||
601 | # color highlight of the spell mode button
|
||
602 | self.txt[self.scan_L3].SetBackgroundColour(self.sm1_color) |
||
603 | #Set LEVEL as one and reset scan to scan the first element
|
||
604 | self.scan_L1 = -1 |
||
605 | self.scan_L2 = -1 |
||
606 | self.Level = self.Lvl["Level_1"] |
||
607 | elif self.label_3 == 'BACK': |
||
608 | # Erase the last include alphabet
|
||
609 | self.sm_word = self.sm_prev |
||
610 | self.txt_bx1.SetValue(self.sm_prev) |
||
611 | self.Update_words()
|
||
612 | # Reset color highlight of the BACK button
|
||
613 | self.txt[self.scan_L3].SetBackgroundColour(self.btn_color) |
||
614 | #Set LEVEL as one and reset scan to scan the first element
|
||
615 | self.scan_L2 = -1 |
||
616 | self.Level = self.Lvl["Level_2"] |
||
617 | elif self.label_3 == 'MORE': |
||
618 | self.n += 1 |
||
619 | i = 1
|
||
620 | W = len(self.word_list) |
||
621 | if W>=((self.n-1)*self.no_of_wm_txtbx ): |
||
622 | self.display = W-((self.n-1)*self.no_of_wm_txtbx) |
||
623 | while ((self.display>0)& (i<=self.no_of_wm_txtbx)): |
||
624 | self.txt[i].SetValue(self.word_list[(i+((self.n-1)*(self.no_of_wm_txtbx)))-1][:])#-1 |
||
625 | i = i+1
|
||
626 | self.display = self.display-1 |
||
627 | if i<(self.no_of_wm_txtbx+1): |
||
628 | self.loop = 1 |
||
629 | else: self.loop = 0 |
||
630 | if ((self.display<self.no_of_wm_txtbx)&(self.display>=0)&(self.loop==1)): |
||
631 | for i in range(i,self.no_of_wm_txtbx+1): |
||
632 | self.txt[i].SetValue(self.clr) |
||
633 | self.loop = 0 |
||
634 | self.n = 0 |
||
635 | if (self.display <=0): |
||
636 | self.n = 0 |
||
637 | self.loop = 0 |
||
638 | |||
639 | else :
|
||
640 | self.wm_word = self.txt[self.scan_L3].GetValue() |
||
641 | str_wm_word = str(self.wm_word) |
||
642 | if len(str_wm_word.rsplit())>1: |
||
643 | Fwrite = open(path+'temp.txt','r') |
||
644 | else:
|
||
645 | i = 0
|
||
646 | while(self.Alp_tri_dict[i].find(self.wm_word[0])==-1): |
||
647 | i = i+1
|
||
648 | #print self.Alp_tri_dict[i]+'.txt'
|
||
649 | Fwrite = open(path+self.Alp_tri_dict[i]+'.txt','r') |
||
650 | flist = Fwrite.readlines() |
||
651 | Fwrite.close() |
||
652 | if len(str_wm_word.rsplit())>1: |
||
653 | Fwrite = open(path+'temp.txt','w') |
||
654 | else:
|
||
655 | i = 0
|
||
656 | while(self.Alp_tri_dict[i].find(self.wm_word[0])==-1): |
||
657 | i = i+1
|
||
658 | #print self.Alp_tri_dict[i]+'.txt'
|
||
659 | Fwrite = open(path+self.Alp_tri_dict[i]+'.txt','w') |
||
660 | wordlist=[] |
||
661 | wlist=[] |
||
662 | for i in range(0,len(flist)): |
||
663 | wlist.append(str(flist[i][:-1]).split(',')) |
||
664 | wlist[i][1] = int(wlist[i][1]) |
||
665 | wlist.sort(key=itemgetter(1),reverse=True) |
||
666 | for i in range(0,len(flist)): |
||
667 | wordlist.append(wlist[i][0])
|
||
668 | match = 0
|
||
669 | for i in range(0,len(flist)): |
||
670 | if wlist[i][0] == self.wm_word: |
||
671 | wlist[i][1] = str((wlist[i][1])+1) |
||
672 | Fwrite.writelines((wlist[i][0])+','+str(wlist[i][1])+'\n') |
||
673 | self.word_prev = self.word # used to erase the last added word |
||
674 | self.word += self.wm_word # append a new word to the sentence |
||
675 | self.word += ' ' # add space |
||
676 | self.senttxt_bx.SetValue(self.word) |
||
677 | # Reset color highlight of the # button
|
||
678 | self.txt[self.scan_L3].SetBackgroundColour(self.btn_color) |
||
679 | self.txt[self.scan_L3].SetValue(self.refresh_word)#refresh after the word is selected |
||
680 | self.scan_L2 = 3 #goto done button |
||
681 | self.Level = self.Lvl["Level_2"] #done is in level 2 |
||
682 | self.wm_flag = 1 |
||
683 | |||
684 | def Update_words(self): |
||
685 | self.n = 1 |
||
686 | #self.word_list = self.file_list
|
||
687 | search_word = self.sm_word
|
||
688 | #print self.sm_word
|
||
689 | self.wlist=[]
|
||
690 | self.word_list = []
|
||
691 | for i in range(0,len(self.file_list)): |
||
692 | self.wlist.append(str(self.file_list[i][:-1]).split(',')) |
||
693 | self.wlist[i][1] = int(self.wlist[i][1]) |
||
694 | self.wlist.sort(key=itemgetter(1),reverse=True) |
||
695 | for i in range(0,len(self.file_list)): |
||
696 | self.word_list.append(self.wlist[i][0]) |
||
697 | update_list = [] |
||
698 | #print len(self.word_list) , len(search_word)
|
||
699 | for letter in range(0,len(search_word)): |
||
700 | for word in range(0,len(self.word_list)): |
||
701 | if len(self.word_list[word])>letter: |
||
702 | if search_word[letter] == self.word_list[word][letter]: |
||
703 | update_list.append(self.word_list[word])
|
||
704 | self.word_list = update_list
|
||
705 | #print len(update_list)
|
||
706 | update_list = [] |
||
707 | self.k=1 |
||
708 | while ((self.k<=len(self.word_list))& (self.k<=self.no_of_wm_txtbx)&(len(search_word)!=0)): |
||
709 | self.txt[self.k].SetValue(self.word_list[self.k-1][:])#-1 |
||
710 | self.k = self.k+1 |
||
711 | if self.k<self.no_of_wm_txtbx: |
||
712 | for i in range(self.k,self.no_of_wm_txtbx+1): |
||
713 | self.txt[i].SetValue(self.clr) |
||
714 | |||
715 | ## Main Loop:
|
||
716 | |||
717 | if __name__ == "__main__": |
||
718 | app = wx.App() |
||
719 | Class_Frame(None, -1, 'HOPE') ##main class |
||
720 | app.MainLoop() |