Script demo_FunctionCyclePlan_py
[hide private]
[frames] | no frames]

Source Code for Script script-demo_FunctionCyclePlan_py

 1  from joy import * 
 2  from math import sin,pi 
 3   
4 -class FunctionCyclePlanApp( JoyApp ):
5 - def __init__(self,*arg,**kw):
6 JoyApp.__init__(self,*arg,**kw)
7
8 - def onStart( self ):
9 def fun( phase ): 10 s = sin(phase*2*pi) 11 progress( " "*(int((s+1)*30)) + "#" )
12 self.plan = FunctionCyclePlan(self, fun, 24) 13 self.plan.onStart = curry(progress,">>> START") 14 self.plan.onStop = curry(progress,">>> STOP") 15 self.freq = 1.0 16 self.rate = 0.05 17 self.limit = 1/0.45
18
19 - def onEvent(self, evt):
20 if evt.type==KEYDOWN: 21 if evt.key in [ord('q'),27]: # 'q' and [esc] stop program 22 self.stop() 23 # 24 elif evt.key==ord(' '): # [space] stops cycles 25 self.plan.setPeriod(0) 26 progress('Period changed to %s' % str(self.plan.period)) 27 # 28 elif evt.key in (ord(','),ord('.')): 29 f = self.freq 30 # Change frequency up/down in range -limit..limit hz 31 if evt.key==ord(','): 32 f = (1-self.rate)*f - self.rate*self.limit 33 else: 34 f = (1-self.rate)*f + self.rate*self.limit 35 if abs(f) < 1.0/self.limit: 36 self.plan.setPeriod( 0 ) 37 else: 38 self.plan.setPeriod( 1/f ) 39 self.freq = f 40 progress('Period changed to %g, %.2f Hz' % (self.plan.period,f)) 41 # 42 elif evt.key==ord('h'): 43 progress( "HELP: ',' to decrease/reverse period; '.' opposite; ' ' stop; 'q' end program; 'h' this help; other keys start Plan" ) 44 # 45 else: # any other key 46 progress( "Starting cycles...." ) 47 self.plan.start() 48 if evt.type!=TIMEREVENT: 49 JoyApp.onEvent(self,evt)
50 51 if __name__=="__main__": 52 print """ 53 Demo of FunctionCyclePlan class 54 ------------------------------- 55 56 When any key is pressed, starts a FunctionCyclePlan that prints 57 an ASCII art sine wave under keyboard control. 58 59 h -- help 60 q -- quit 61 , and . -- change rate 62 SPACE -- pause / resume 63 any other key -- start 64 65 The application can be terminated with 'q' or [esc] 66 """ 67 import joy 68 joy.DEBUG[:]=[] 69 app=FunctionCyclePlanApp() 70 app.run() 71