From 6acdbc3db543f2692ee336a99722f5ab0b46c77e Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 13 Nov 2013 22:42:18 +0000 Subject: convert to new primitive type --- (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 febd409..098e0a4 100644 --- a/pysamples/dotted_line.py +++ b/pysamples/dotted_line.py @@ -1,4 +1,4 @@ -#Copyright (c) 2009-11, Walter Bender +#Copyright (c) 2009-13, Walter Bender # This procedure is invoked when the user-definable block on the "extras" # palette is selected. Some examples of how to use this block are included @@ -52,43 +52,57 @@ # clearscreen(self) tw.canvas.clearscreen() # Note: Clears the screen and resets all turtle and # pen attributes to default values -# setpen(self, flag) tw.canvas.setpen(True) +# +# +# Class Turtles -- useful properties and methods (from within +# tamyblock.py, tw.turtles is the class instance) +# +# self.set_turtle(name) tw.turltes.set_turtle(1) +# Note: Set the current turtle to turtle '1' +# self.get_active_turtle() tw.turltes.get_active_turtle() +# Note: Returns active turtle (See Class Turtle below) +# +# Class Turtle -- useful properties and methods (from within +# tamyblock.py, tw.turtles.get_active_turtle() is the class instance) +# +# set_pen_state(self, flag) +# tw.turtles.get_active_turtle().set_pen_state(True) # Note: True will set the pen "down", enabling drawing; # False will set the pen "up" -# forward(self, n) tw.canvas.forward(100) +# forward(self, n) tw.turtles.get_active_turtle().forward(100) # Note: Move the turtle forward 100 units -# arc(self, a, r) tw.canvas.arc(120, 50) +# arc(self, a, r) tw.turtles.get_active_turtle().arc(120, 50) # Note: Move the turtle along an arc of 120 degrees # (clockwise) and radius of 50 units -# setheading(self, a) tw.canvas.setheading(180) +# set_heading(self, a) tw.turtles.get_active_turtle().set_heading(180) # Note: Set the turtle heading to 180 # (towards the bottom of the screen) -# self.heading tw.canvas.heading +# self.get_heading() tw.turtles.get_active_turtle().get_heading() # Note: The current heading -# setpensize(self, n) tw.canvas.setpensize(25) +# set_pen_size(self, n) tw.turtles.get_active_turtle().set_pen_size(25) # Note: Set the turtle pensize to 25 units -# self.pensize tw.canvas.pensize -# Note: The current pensize -# setcolor(self, c) tw.canvas.color(70) +# self.get_pen_size() tw.turtles.get_active_turtle().get_pen_size() +# Note: The current pen size +# self.set_color(self, c) tw.turtles.get_active_turtle().set_color(70) # Note: Set the pen color to 70 (blue) -# self.color tw.canvas.color +# self.color() tw.turtles.get_active_turtle().get_color() # Note: The current pen color -# setshade(self, s) tw.canvas.shade(50) +# self.set_shade(self, s) tw.turtles.get_active_turtle().set_shade(50) # Note: Set the pen shade to 50 -# self.shade tw.canvas.shade +# self.get_shade() tw.turtles.get_active_turtle().get_shade() # Note: The current pen shade -# fillscreen(self, c, s) tw.canvas.fillscreen(70, 90) +# fillscreen(self, c, s) +# tw.turtles.get_active_turtle().fillscreen(70, 90) # Note: Fill the screen with color 70, shade 90 (light blue) -# setxy(self, x, y) tw.canvas.setxy(100,100) +# self.set_xy(self, x, y) +# tw.turtles.get_active_turtle().set_xy(100,100) # Note: Move the turtle to position (100, 100) -# self.xcor tw.canvas.xcor +# self.get_xy tw.turtles.get_active_turtle().get_xy()[0] # Note: The current x coordinate of the turtle # (scaled to current units) -# self.ycor tw.canvas.ycor +# self.get_xy tw.turtles.get_active_turtle().get_xy()[1] # Note: The current y coordinate of the turtle # (scaled to current units) -# self.set_turtle(name) tw.canvas.set_turtle(1) -# Note: Set the current turtle to turtle '1' # # # Other useful Python functions @@ -110,24 +124,25 @@ # 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.canvas.pendown: + if tw.turtles.get_active_turtle().get_pen_state(): dist = 0 - while dist + tw.canvas.pensize < line_length: # repeat drawing dots - tw.canvas.setpen(True) - tw.canvas.forward(1) - tw.canvas.setpen(False) - tw.canvas.forward((tw.canvas.pensize * 2) - 1) - dist += (tw.canvas.pensize * 2) + pen_size = tw.turtles.get_active_turtle().get_pen_size() + while dist + pen_size < line_length: # repeat drawing dots + tw.turtles.get_active_turtle().set_pen_state(True) + tw.turtles.get_active_turtle().forward(1) + tw.turtles.get_active_turtle().set_pen_state(False) + tw.turtles.get_active_turtle().forward(pen_size * 2 - 1) + dist += pen_size * 2 # make sure we have moved exactly line_length - tw.canvas.forward(line_length - dist) - tw.canvas.setpen(True) + tw.turtles.get_active_turtle().forward(line_length - dist) + tw.turtles.get_active_turtle().set_pen_state(True) else: - tw.canvas.forward(line_length) + tw.turtles.get_active_turtle().forward(line_length) return diff --git a/pysamples/forward_push.py b/pysamples/forward_push.py index b98c726..80fe818 100644 --- a/pysamples/forward_push.py +++ b/pysamples/forward_push.py @@ -1,13 +1,16 @@ -#Copyright (c) 2012, Walter Bender +#Copyright (c) 2012-2013, Walter Bender # Usage: Import this code into a Python (user-definable) block; when -# this code is run, the turtle will draw a line of the length of the -# numeric argument block docked to the Python block. But before -# drawing the line, it pushes the rgb values of the destination to the -# FILO. +# this code is run, a new block will be added to the Turtle Palette. +# This block will be named 'name', the value of the argument block +# docked to the Python block. +# +# The new block will cause the turtle to draw a line of the +# length. But before drawing the line, it pushes the rgb values of the +# destination to the FILO. -def myblock(tw, name): +def myblock(tw, args): ''' ''' def _prim_forward_push(tw, line_length): @@ -15,16 +18,16 @@ def myblock(tw, name): line_length = float(line_length) except ValueError: return - penstatus = tw.canvas.pendown - tw.canvas.setpen(False) - tw.canvas.forward(line_length) - r, g, b, a = tw.canvas.get_pixel() + penstatus = tw.turtles.get_active_turtle().get_pen_status() + tw.turtles.get_active_turtle().set_pen_state(False) + tw.turtles.get_active_turtle().forward(line_length) + r, g, b, a = tw.turtles.get_active_turtle().get_pixel() tw.lc.heap.append(b) tw.lc.heap.append(g) tw.lc.heap.append(r) - tw.canvas.forward(-line_length) - tw.canvas.setpen(penstatus) - tw.canvas.forward(line_length) + tw.turtles.get_active_turtle().forward(-line_length) + tw.turtles.get_active_turtle().set_pen_state(penstatus) + tw.turtles.get_active_turtle().forward(line_length) return from TurtleArt.tapalette import make_palette, palette_name_to_index @@ -39,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 cdeb96d..60683a0 100644 --- a/pysamples/uturn.py +++ b/pysamples/uturn.py @@ -1,4 +1,4 @@ -#Copyright (c) 2011, Walter Bender + #Copyright (c) 2011-2013, Walter Bender # This procedure is invoked when the user-definable block on the # "extras" palette is selected. @@ -8,11 +8,22 @@ # 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 + # arguments -- 0 in this case -- and the function to call -- in this + # case, we define the _prim_uturn function to set heading += 180. + def _prim_uturn(tw): + value = tw.turtles.get_active_turtle().get_heading() + 180 + tw.turtles.get_active_turtle().set_heading(value) + # We also update the label on the heading block to indicate + # the current heading value + if tw.lc.update_values: + tw.lc.update_label_value('heading', value) + from TurtleArt.tapalette import make_palette, palette_name_to_index - from TurtleArt.talogo import primitive_dictionary + from TurtleArt.taprimitive import Primitive, ConstantArg from gettext import gettext as _ # Choose a palette for the new block. @@ -23,12 +34,10 @@ def myblock(tw, arg): style='basic-style-extended-vertical', label=_('uturn'), prim_name='uturn', - help_string=_('make a uturn')) + help_string=_('turns the turtle 180 degrees')) # Add its primitive to the LogoCode dictionary. - tw.lc.def_prim('uturn', 0, - lambda self: primitive_dictionary['set'] - ('heading', tw.canvas.seth, tw.canvas.heading + 180)) + tw.lc.def_prim('uturn', 0, Primitive(_prim_uturn, arg_descs=[ConstantArg(tw)])) # Regenerate the palette, which will now include the new block. tw.show_toolbar_palette(palette_name_to_index('turtle'), -- cgit v0.9.1