Guest User

Untitled

a guest
Nov 14th, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. import ui
  2. import time
  3. import json
  4. import speech
  5. import console
  6. from objc_util import *
  7.  
  8. '''
  9. 1.7
  10. #################################################
  11. # Written by Stephen Childs 01/11/2018 #
  12. # Copyright SRHCI Ltd #
  13. # IP and Format rights retained by Steve Childs #
  14. #################################################
  15. #. *********************
  16. #. SRHC Entertainment *
  17. #. CQL QUIZ TIMER *
  18. #. iPhone Version 1.1 *
  19. #. *********************
  20.  
  21. # This app was commisioned by the Calderdale Quiz League, UK.
  22. they required timers on iPhones for league team games that restricted
  23. thelength of time a question took to answer. Two minutes for the team
  24. whose question it was and then if they got the answer wrong; the other
  25. team was allowed 30 seconds to give an answer. this has speeded up the
  26. quiz and provided extra tension in having to beat the clock as well
  27.  
  28. # So in order to make it suitable for use by other Quiz leagues around
  29. the world the iPhone version has been made cutomisable to allow any time
  30. to be set.
  31.  
  32. it also will Disable the idle timer (which puts the device to sleep after
  33. a certain period of inactivity).
  34.  
  35. My thanks also to to JonB of the pythonista community for providing a neat soltion for a ticklish problem we encountered.
  36.  
  37. '''
  38.  
  39. # initialize timers with default values
  40. state = []
  41. transfer=[]
  42. # load fallback values in case file error
  43. stdtime = [120, 30, 10, 5]
  44. timer1 = [0, 0, 0, 0]
  45. timer2 = [0, 0, 0, 0]
  46. seconds = [0, 0, 0]
  47. start = time.clock()
  48. seconds[0] = (int(start))
  49. textchange_called = [0,0,""]
  50. swchange_called = [0,""]
  51. button=[0,0,0,""]
  52. setaw=[1,1]
  53.  
  54.  
  55. class textlook (object):
  56. def textfield_should_begin_editing(self, textfield):
  57. console.hud_alert('shouldbegin called {}'.format(textfield.name))
  58. return True
  59. def textfield_did_begin_editing(self, textfield):
  60. console.hud_alert('didbegin called {}'.format(textfield.name))
  61. def textfield_did_end_editing(self, textfield):
  62. console.hud_alert('didend called {}'.format(textfield.name))
  63. def textfield_should_return(self, textfield):
  64. textfield.end_editing()
  65. return True
  66. def textfield_should_change(self, textfield, range, replacement):
  67. console.hud_alert('shouldchange called {}'.format(textfield.name))
  68. return True
  69. def textfield_did_change(self, textfield):
  70. console.hud_alert('didchange called {}'.format(textfield.name))
  71.  
  72.  
  73. # new countdown value written from display input
  74. @on_main_thread
  75. def textchange(r1):
  76. name=r1.name
  77. textchange_called[1] = 1
  78. textchange_called[2] = name
  79. if name == 'textfield1':
  80. timer1[1] = int(r1.text)
  81. timer1[2] = timer1[1]
  82. if name == 'textfield3':
  83. timer1[3] = int(r1.text)
  84. if name == 'textfield2':
  85. timer2[1] = int(r1.text)
  86. timer2[2] = timer2[1]
  87. if name == 'textfield4':
  88. timer2[3] = int(r1.text)
  89.  
  90.  
  91.  
  92. def switch(sender):
  93. sx = sender.value
  94. if sx is False:
  95. swchange_called[0] = 1
  96. if sx is True:
  97. swchange_called[0] = 2
  98. return
  99.  
  100. def setswitch(sender):
  101. setname = sender.name
  102. val = sender.value
  103. if setname == 'switch2':
  104. if val is True:
  105. setaw[0] = 1
  106. if val is False:
  107. setaw[0] = 0
  108. if setname == 'switch3':
  109. if val is True:
  110. setaw[1] = 1
  111. if val is False:
  112. setaw[1] = 0
  113.  
  114.  
  115. def action(sender):
  116. title = sender.title
  117. button[3]=title
  118. # have they pressed confer button
  119. if title == 'Confer':
  120. if timer1[0] == 0:
  121. timer1[0] = 1
  122. #timer1[2] += 1
  123. button[0]=1
  124. # have they pressed pass button
  125. if title == 'Pass':
  126. if timer2[0] == 0:
  127. timer2[0] = 1
  128. #timer2[2] += 1
  129. timer1[0] = 2
  130. button[1]=1
  131. # have they pressed reset button
  132. if title == 'Reset':
  133. timer1[0] = 0
  134. timer1[2] = timer1[1]
  135. timer2[0] = 0
  136. timer2[2] = timer2[1]
  137. button[2]=1
  138.  
  139.  
  140.  
  141.  
  142.  
  143. # timerx [ on, setval, curval, warn]
  144. def load_timers(timer1, timer2, stdtime):
  145. timer1[1] = stdtime[0]
  146. timer1[2] = stdtime[0]
  147. timer2[1] = stdtime[1]
  148. timer2[2] = stdtime[1]
  149. timer1[3] = stdtime[2]
  150. timer2[3] = stdtime[3]
  151.  
  152. def store_timers(timer1, timer2, stdtime):
  153. stdtime[0] = timer1[1]
  154. stdtime[1] = timer2[1]
  155. stdtime[2] = timer1[3]
  156. stdtime[3] = timer2[3]
  157. state = stdtime
  158. save_state('setup.json', state)
  159.  
  160.  
  161. # update_display()
  162. #@on_main_thread
  163. def how_long(seconds):
  164. seconds[2] = seconds[1]
  165. delta = time.clock()
  166. seconds[1] = int(delta)
  167.  
  168. def buttonaction(button,cb,pb,rb,label4,label5):
  169. # have they pressed confer button
  170. if button[0] == 1:
  171. label4.text = str(timer1[2])
  172. cb.enabled = False
  173. label4.background_color = '#00ff00'
  174. button[0] = 0
  175. # have they pressed pass button
  176. if button[1] == 1:
  177. label5.text = str(timer2[2])
  178. pb.enabled = False
  179. label5.background_color = '#00ff00'
  180. # halt the confer timer and not allow restart
  181. cb.enabled = False
  182. button[1] = 0
  183. # have they pressed reset button
  184. if button[2] == 1:
  185. timer1[0] = 0
  186. timer1[2] = timer1[1]
  187. timer2[0] = 0
  188. timer2[2] = timer2[1]
  189. label4.text = str(timer1[2])
  190. label5.text = str(timer2[2])
  191. cb.enabled = True
  192. pb.enabled = True
  193. label4.background_color = '#00ffff'
  194. label4.text_color = '#000000'
  195. label5.background_color = '#00ffff'
  196. label5.text_color = '#000000'
  197. button[2] = 0
  198.  
  199.  
  200. def switchoff(tf1, tf2, tf3, tf4, sx1, sx2, sx3, cb, pb):
  201. tf1.enabled = False
  202. tf2.enabled = False
  203. tf3.enabled = False
  204. tf4.enabled = False
  205. sx2.enabled = False
  206. sx3.enabled = False
  207. textchange_called[0] = 0
  208. swchange_called[0] = 0
  209. store_timers(timer1, timer2, stdtime)
  210. cb.enabled = True
  211. pb.enabled = True
  212.  
  213.  
  214. def switchon(tf1,tf2,tf3,tf4,sx1,sx2,sx3,cb,pb):
  215. tf1.enabled = True
  216. tf2.enabled = True
  217. tf3.enabled = True
  218. tf4.enabled = True
  219. sx2.enabled = True
  220. sx3.enabled = True
  221. textchange_called[0] = 1
  222. swchange_called[0] = 0
  223. cb.enabled = False
  224. pb.enabled = False
  225.  
  226.  
  227. def label4_update(label4):
  228. label4_change = label4
  229. label4_change.text = str(timer1[2])
  230. if timer1[2] == timer1[3]:
  231. label4_change.text_color = '#ffffff'
  232. label4_change.background_color = '#ff0000'
  233.  
  234.  
  235. def label5_update(label5):
  236. label5_change = label5
  237. label5_change.text = str(timer2[2])
  238. if timer2[2] == timer2[3]:
  239. label5_change.text_color = '#ffffff'
  240. label5_change.background_color = '#ff0000'
  241.  
  242.  
  243. def run_timer1():
  244. if timer1[0] != 2:
  245. timer1[2] -= 1
  246. # print (timer1[2])
  247. if timer1[2] == timer1[3] and setaw[0]==1:
  248. speech.say(str(timer1[3]) + 'Seconds remaining', 'en_GB')
  249. if timer1[2] <= 0:
  250. timer1[0] = 2
  251. if setaw[1]==1:
  252. speech.say('Answer Please', 'en_GB')
  253.  
  254.  
  255. def run_timer2():
  256. if timer2[0] != 2:
  257. timer2[2] -= 1
  258. # print (timer2[2])
  259. if timer2[2] == timer2[3]and setaw[0]==1:
  260. speech.say(str(timer2[3]) + 'Seconds remaining', 'en_US')
  261. if timer2[2] <= 0:
  262. timer2[0] = 2
  263. if setaw[1]==1:
  264. speech.say('Answer Please', 'en_US')
  265.  
  266.  
  267. def timer_update():
  268. if timer1[0] == 1:
  269. run_timer1()
  270. if timer2[0] == 1:
  271. run_timer2()
  272.  
  273.  
  274. def setvaluesonscreen(tf1,tf2,tf3,tf4,label4,label5):
  275. #print "here"
  276. #print textchange_called
  277. if textchange_called[2]==('textfield1'):
  278. #print 'checking t1 set time'
  279. if timer1[1] <= 1 or timer1[1] >= 1801:
  280. timer1[1] = stdtime[0]
  281. tf1.text = str(timer1[1])
  282. #print "looping1"
  283. label4.text = str(timer1[1])
  284. #print 'looping2'
  285. if textchange_called[2]==('textfield3'):
  286. #print 'checking t1 warning'
  287. if timer1[3] <= 0 or timer1[3] >= timer1[1]:
  288. timer1[3] = int(round(timer1[1] / 5))
  289. tf3.text = str(timer1[3])
  290. #print 'looping3'
  291. if textchange_called[2]==('textfield2'):
  292. #print 'checking t2 set time'
  293. if timer2[1] <= 0 or timer2[1] >= 1801:
  294. timer2[1] = stdtime[1]
  295. tf2.text = str(timer2[1])
  296. #print "looping4"
  297. label5.text = str(timer2[1])
  298. #print 'looping5'
  299. if textchange_called[2]==('textfield4'):
  300. if timer2[3] <= 0 or timer2[3] >= timer2[1]:
  301. timer2[3] = int(round(timer2[1] / 5))
  302. tf4.text = str(timer2[3])
  303. #print 'end of update'
  304. return
  305.  
  306. # file handlers
  307. def save_state(filename,state):
  308. with open(filename, 'w') as f:
  309. json.dump(state, f)
  310.  
  311. def load_state(filename):
  312. with open(filename) as f:
  313. return json.load(f)
  314.  
  315. try:
  316. state=load_state('setup.json')
  317. aa= "stdtime"
  318. stdtime = state
  319.  
  320. except IOError:
  321. print "console.hud_alert('setup file not called')"
  322. state = stdtime
  323. save_state('setup.json')
  324.  
  325.  
  326. def main():
  327. state=load_state('setup.json')
  328. stdtime = state
  329. # print stdtime, "loaded values"
  330. #time.sleep(1)
  331. load_timers(timer1,timer2,stdtime)
  332. # print timer1,timer2
  333. v = ui.load_view('cql')
  334. v.background_color = '#00ffff'
  335. label4 = v['label4']
  336. label5 = v['label5']
  337. cb =v['button1']
  338. pb = v['button2']
  339. rb = v['button3']
  340. sx1 = v['switch1']
  341. sx2 = v['switch2']
  342. sx3 = v['switch3']
  343. tf1 = v['textfield1']
  344. tf2 = v['textfield2']
  345. tf3 = v['textfield3']
  346. tf4 = v['textfield4']
  347. tf1.enabled = False
  348. tf2.enabled = False
  349. tf3.enabled = False
  350. tf4.enabled = False
  351. sx2.enabled = False
  352. sx3.enabled = False
  353. delegate=textlook()
  354. x=0
  355. for tf in [tf1, tf2, tf3, tf4]:
  356. tf.action = textchange
  357. tf.keyboard_type= ui.KEYBOARD_NUMBER_PAD
  358. tf.text = str(stdtime[x])
  359. x = x+ 1
  360. label4_update(label4)
  361. label5_update(label5)
  362. v.present('sheet')
  363. # timing loops engaged and ready to run
  364. #console.set_idle_timer_disabled(True)
  365. while True:
  366. how_long(seconds)
  367. if button[0] == 1 or button[1] == 1 or button[2] == 1:
  368. buttonaction(button,cb,pb,rb,label4,label5)
  369. if seconds[1] != seconds[2] and (timer1[0] == 1 or timer2[0] == 1):
  370. timer_update()
  371. label4_update(label4)
  372. label5_update(label5)
  373. if textchange_called[0] == 1 and textchange_called[1] == 1:
  374. setvaluesonscreen(tf1,tf2,tf3,tf4,label4,label5)
  375. textchange_called[1] = 0
  376. if swchange_called[0] == 1:
  377. switchoff(tf1,tf2,tf3,tf4,sx1,sx2,sx3,cb,pb)
  378. if swchange_called[0] == 2:
  379. switchon(tf1,tf2,tf3,tf4,sx1,sx2,sx3,cb,pb)
  380. # check if still on screen
  381. onscreen = v.on_screen
  382. if onscreen is False:
  383. #console.set_idle_timer_disabled(False)
  384. return False
  385.  
  386.  
  387. if __name__ == '__main__':
  388. main()
Add Comment
Please, Sign In to add comment