From 955cbca83a84b0c1ea06f8c33dfec8dd0f850ed1 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 30 Oct 2013 17:24:11 +0000 Subject: comvert custom blocks to new prim style -- args is passed as a tuple --- (limited to 'pysamples') diff --git a/pysamples/brain.py b/pysamples/brain.py index 33ee320..3510e03 100644 --- a/pysamples/brain.py +++ b/pysamples/brain.py @@ -21,7 +21,7 @@ # . -def myblock(tw, text): +def myblock(tw, args): ''' Dialog with AIML library: Usage: Load this code into a Python Block. Pass text as an argument and the robot's response will be pushed to the stack. Use a Pop Block to pop the response @@ -104,6 +104,7 @@ Close other activities and try once more.')) return kernel + text = args[0] if not hasattr(tw, 'aiml_kernel'): tw.aiml_kernel = brain_load(tw, get_default_voice()) response_text = brain_respond(tw.aiml_kernel, text) diff --git a/pysamples/csound.py b/pysamples/csound.py index a935674..ec1b014 100644 --- a/pysamples/csound.py +++ b/pysamples/csound.py @@ -17,16 +17,24 @@ def myblock(tw, sound): import os dirs = [os.path.join( - os.environ['HOME'], - 'Activities/TamTamMini.activity/common/Resources/Sounds/')] + os.environ['HOME'], + 'Activities/TamTamMini.activity/common/Resources/Sounds/'), + os.path.join( + os.environ['HOME'], + 'Activities/TamTamJam.activity/common/Resources/Sounds/'), + os.path.join( + os.environ['HOME'], + 'Activities/TamTamEdit.activity/common/Resources/Sounds/')] orchlines = [] scorelines = [] instrlist = [] def finddir(): + print dirs for d in dirs: if os.path.isdir(d): return d + return '.' def playSine(pitch=1000, amplitude=5000, duration=1, starttime=0, pitch_envelope='default', amplitude_envelope='default'): @@ -128,18 +136,17 @@ def myblock(tw, sound): csd.write("\n") csd.close() - if type(sound) == float: - playSine(pitch=float(sound)) - elif type(sound) == list: # Create a score by computing a sinewave. - if len(sound) == 1: + if len(sound) == 1: + if isinstance(sound[0], float) or isinstance(sound[0], int): playSine(pitch=float(sound[0])) - elif len(sound) == 2: + else: # Create a score from a prerecorded Wave file. + playWave(sound[0]) + else: + if len(sound) == 2: playSine(pitch=float(sound[0]), amplitude=float(sound[1])) else: playSine(pitch=float(sound[0]), amplitude=float(sound[1]), duration=float(sound[2])) - else: # Create a score from a prerecorded Wave file. - playWave(sound) if tw.running_sugar: path = os.path.join(get_path(tw.activity, 'instance'), 'tmp.csd') else: diff --git a/pysamples/dotted_line.py b/pysamples/dotted_line.py index c0d3008..098e0a4 100644 --- a/pysamples/dotted_line.py +++ b/pysamples/dotted_line.py @@ -124,11 +124,11 @@ # of the numeric argument block docked to the Python block. -def myblock(tw, line_length): +def myblock(tw, args): ''' Draw a dotted line of length line_length. ''' try: # make sure line_length is a number - line_length = float(line_length) + line_length = float(args[0]) except ValueError: return if tw.turtles.get_active_turtle().get_pen_state(): diff --git a/pysamples/forward_push.py b/pysamples/forward_push.py index ca527db..80fe818 100644 --- a/pysamples/forward_push.py +++ b/pysamples/forward_push.py @@ -10,7 +10,7 @@ # destination to the FILO. -def myblock(tw, name): +def myblock(tw, args): ''' ''' def _prim_forward_push(tw, line_length): @@ -42,7 +42,7 @@ def myblock(tw, name): # Create a new block prototype. palette.add_block('forwardpush', style='basic-style-1arg', - label=name, + label=args[0], default=100, prim_name='forwardpush', help_string=_('push destination rgb value to heap')) diff --git a/pysamples/grecord.py b/pysamples/grecord.py index a28b82c..486fdb7 100644 --- a/pysamples/grecord.py +++ b/pysamples/grecord.py @@ -10,7 +10,7 @@ # Sugar Journal. -def myblock(tw, arg): +def myblock(tw, args): ''' Record and playback a sound (Sugar only) ''' import os import gst @@ -204,12 +204,9 @@ def myblock(tw, arg): # Sometime we need to parse multiple arguments, e.g., save, savename save_name = '%s_%s' % (tw.activity.name, _('sound')) - if isinstance(arg, list): - cmd = arg[0].lower() - if len(arg) > 1: - save_name = str(arg[1]) - else: - cmd = arg.lower() + cmd = args[0].lower() + if len(args) > 1: + save_name = str(arg[1]) if cmd == 'start' or cmd == _('start').lower(): tw.grecord.start_recording_audio() diff --git a/pysamples/serial.py b/pysamples/serial.py index 84772ab..d15011c 100644 --- a/pysamples/serial.py +++ b/pysamples/serial.py @@ -9,13 +9,13 @@ # (3) use a Pop Block to retrieve any strings input from serial device. -def myblock(tw, x): # x is the string to transmit +def myblock(tw, args): # x is the string to transmit import serial # you may need to install this library # serial device on USB, 9600 baud ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) - ser.write(str(x)) # send string x + ser.write(str(args[0])) # send string x st = ser.read(1000) # read up to 1000 bytes tw.lc.heap.append(st) # append to heap ser.close() diff --git a/pysamples/sinewave.py b/pysamples/sinewave.py index 4f14c4c..a060f34 100644 --- a/pysamples/sinewave.py +++ b/pysamples/sinewave.py @@ -8,8 +8,9 @@ # the speaker at the specified frequency. -def myblock(tw, frequency): +def myblock(tw, args): ''' Plays a sound at frequency frequency ''' import os + frequency = args[0] os.system('speaker-test -t sine -l 1 -f %d' % (int(frequency))) diff --git a/pysamples/speak.py b/pysamples/speak.py index 30762a9..13215e8 100644 --- a/pysamples/speak.py +++ b/pysamples/speak.py @@ -28,7 +28,7 @@ def myblock(tw, arg): import os pitch = None - if type(arg) == type([]): + if len(arg) > 1: text = arg[0] if len(arg) > 1: pitch = int(arg[1]) @@ -37,7 +37,7 @@ def myblock(tw, arg): elif pitch < 0: pitch = 0 else: - text = arg + text = arg[0] # Turtle Art numbers are passed as float, # but they may be integer values. diff --git a/pysamples/uturn.py b/pysamples/uturn.py index edd2bcd..f6ff2e0 100644 --- a/pysamples/uturn.py +++ b/pysamples/uturn.py @@ -8,7 +8,7 @@ # can use the u-turn block as you would any other block. -def myblock(tw, arg): +def myblock(tw, args): ''' Add a uturn block to the 'turtle' palette ''' # def_prim takes 3 arguments: the primitive name, the number of -- cgit v0.9.1