Your own tethered CodeBug programs
You can write your own tethered CodeBug programs using Python and a few simple commands to control your CodeBug. In the next steps you will start an interactive Python session and enter commands to interact with your tethered CodeBug. Don’t forget you need the tethered mode project installed on your CodeBug for them to work.
Open a Terminal and type:
sudo python3
You will see the python prompt appear >>> Now type:
import codebug_tether
import codebug_tether.colourtail
cb = codebug_tether.CodeBug()
cb.set_pixel(2, 2, 1)
You will see the center LED - at position (2, 2) - light up on CodeBug.
NB: On some computers, or if you have multiple CodeBugs you will have to put the address of your CodeBug in brackets. More details are in the section Addressing Your CodeBug.
To set up the colour tail
colourtail = codebug_tether.colourtail.CodeBugColourTail(cb)
colourtail.init()
Now try setting a Colour tail pixel:
colourtail.set_pixel(0, 255, 0, 0)
colourtail.update()
You will see the first pixel on your colour tail light up red. You passed this function 4 numbers. The first number was the number of the pixel, and the other 3 were red, green and blue. Note: The colour numbers need to be between 0 and 255. The update() function pushes any changes to pixel colours you have made, to the physical colour tail, so you can run multiple set_pixel() commands and then run update() so the pixels all change at the same time.
Try setting multiple Colour tail pixels:
colourtail.set_pixel(0, 0, 0, 255)
colourtail.set_pixel(1, 0, 100, 200)
colourtail.set_pixel(3, 255, 255, 0)
colourtail.update()
All 3 pixels change at once right?! Try different combinations of colours and pixel numbers to get comfortable using this function.
You can use variables and for loops to create amazing patterns with colour tail.
for red in range(0,255):
for green in reversed(range(0,255):
for pixel in range(0,9):
colourtail.set_pixel(pixel, red, green, 0)
colourtail.update()
Get a full list of the commands available by typing:
help(colourtail) or
help(cb)
You can write longer programs for tethered mode CodeBug by writing commands in your text editor and then saving and running the file in the way you did with the examples earlier.
Tethered mode gives your CodeBug access to the full processing power, functionality and network connectivity of your computer! You can use variety of powerful yet easy to use Python modules allow your CodeBug to generate random numbers, react to emails or even respond to Twitter activity.