This page will help you get started with the disco add ons with Marty using the python library. Before we get started, make sure you have python installed and have set up your disco add ons.
First you must always import Martypy and establish connection to your Marty. Follow the code below to connect your Marty.
import martypy
# USB connection
# change "COM1" on this next line to specify the USB port you want to use
# comment this line out to use WiFi
my_marty = martypy.Marty("USB")
# WiFi connection
# uncomment this and change the IP address to your Marty's to use WiFi
# my_marty = martypy.Marty("wifi", "192.168.0.5")
You should now be able to play with your disco add ons using the three functions below.
The function disco_named_pattern allows you to set your disco add ons to a built-in pattern of lights. You will need to pass in your choice of pattern and specify the add on you would like to turn on, which is outlined below.
disco_pattern(add_on, pattern):
Try this function using the code below to set all of your disco add ons to pattern show-off.
my_marty.disco_named_pattern(my_marty.Disco.ALL, "show-off")
The function disco_color allows you to turn on and set your disco add ons to the color of your choice.
disco_color(color, add_on, region, api):
Try this function using the code below to set all of your disco add ons to teal.
my_marty.disco_color('teal', my_marty.Disco.ALL, api='led')
You can also get the same color by passing in a hex code or RGB tuple. See the code samples below on how you would pass the color teal as a hex code or RGB tuple, respectively.
my_marty.disco_color('#02d1bc', my_marty.Disco.ALL, api='led')
my_marty.disco_color((2, 209, 188), my_marty.Disco.ALL, api='led')
The function disco_off allows you to turn off the disco add on of your choice. You can specify which specific add on you want to turn off, or turn off multiple at a time.
disco_off(add_on, api):
Try this function using the code below to turn off all of your disco add ons.
my_marty.disco_off(api = 'led')
Now we can use our disco functions to make Marty party! Follow the code below to light up Marty's disco add ons and make Marty dance.
from martypy import Marty
my_marty = Marty("wifi", "192.168.0.42") # Replace 192.168.0.42 by your Marty's IP
my_marty.disco_color('blue', my_marty.Disco.FEET, api='led')
my_marty.disco_named_pattern(my_marty.Disco.EYES, pattern="show-off")
my_marty.disco_named_pattern(my_marty.Disco.ARMS, pattern="pinwheel")
my_marty.dance()
my_marty.disco_off(api='led')
When passing in your add ons, you have two options:
1. You can pass in a string of the name of the add on that you would like to call the function on. You can find the names of all your add ons by visiting the Scratch app and going to Configure –> ADD-ONS. Don't forget to disconnect the app after in order to connect with your Marty in python!
2. You can also pass in groupings of the add ons so you can call your chosen function on multiple add ons at a time! We have groupings for:
Pass in one the variables above in the add_on parameter of your chosen function when you would like to call a pair or all of the add ons.