There are many excellent things to be found in last week’s release of Raspbian Jessie and we’ve been keeping one of the best ones tucked under our big Raspberry Pi-shaped hat. In the Programming menu on the desktop you’ll find a new version of Scratch, our favourite programming language for beginners.
Connect buttons, sensors, cameras, LEDS, goblin sticks and other gubbins to your Pi using Scratch
Tim Rowledge, who has been “vigorously wrangling Scratch into shape over the last couple of years” (thanks Eben), tells us what’s new:
Along with the new Raspbian release we are including the latest Scratch system update. It might have seemed a bit quiet on the Scratch front since March, but lots has happened here in the rainforests of Vancouver Island. There are two primary changes you will notice:
- a significant increase in speed
- the addition of a built-in GPIO server.
One of the big projects last year was to modernize the Scratch code to run in a current Squeak Smalltalk system rather than the very old original version; this improved performance a fair bit all on its own, since the newer Squeak benefited from a lot of work over the years. [The Scratch world is created using Squeak, a dialect of the Smalltalk programming language, and Squeak itself runs on a software emulation of a computer system called a virtual machine -Ed.] It also built us a Scratch that could run on the very latest Squeak virtual machines that have dynamic code translation, generating machine code at run-time. Along with work to improve the Squeak code that implements Scratch, we then had a noticeably faster system.
A major project this year has been building such a virtual machine for the Pi; until now, only x86 machines have been able to run this version. With a very generous amount of support from Eliot Miranda – the original author of the Cog virtual machine and all-round software deity – the ARM Cog VM has been whirring away since around June.
Benchmarks are always a nastily slippery subject, but we feel that Squeak performance is typically between 3 and 10 times faster, obviously depending on what exactly one is measuring. Things will get even faster in the future as we iron out wrinkles in the code generation, and soon we hope to start benefiting from another project that does code optimization on the fly; early hints suggest at least a doubling of performance. Since Scratch uses a lot of graphics and UI code it doesn’t always speed up so much; but we already did a lot of graphics speed improvements for prior releases.
Our favourite “scary big demo” is Andrew Oliver’s implementation of Pac-Man. The original release of Scratch on the Raspberry Pi Model B could manage almost one frame per second, at best. The same Model B with the latest Scratch system can manage about 12-15 frames per second, and on a Raspberry Pi 2 we can get a bit over 30, making a very playable Pac-Man.
The new GPIO server for Pi Scratch is a first pass at a new and hopefully simpler way for users to connect Scratch to the Raspberry Pi’s GPIO pins or to add-on boards plugged into them. It is modelled on the mesh/network server and uses the same internal API so that either or both can be used at any time – indeed, you can have both working and use a Pi as a sort of GPIO server or data source. We have not introduced any new blocks at this point.
The server also allows access to the Pi camera, IP address and date and time and allows complex functionality. For example, the following scripts (along with a suitably configured breadboard) provide the ability to turn LEDs on and off according to a button, to take a photo with a countdown provided by a progressively brightening LED, and ways to check the time etc.
Examples of using Scratch to control the camera module, control LEDs connected to the Raspberry Pi’s GPIO pins, and check the time
We can also plug in Pi add-on cards such as the Sense HAT, Pibrella, Explorer HAT, PiFace, PiLite and Ryanteck motor board.
Each card has its own set of commands layered on top of the basic GPIO facilities described above.
Demo project scripts
In the Scratch Examples directory (found via the
File-->Open dialogue and then the
Examples shortcut) you will find a Sensors and Motors directory; several new GPIO scripts are included, including the one above.
Closing notes from Clive
We’re really pleased that GPIO is now built in to the Pi version of Scratch. It means that users can use access the GPIO pins “out of the box,” and so get into physical computing that much more easily. We’ve also introduced the GPIO pin numbering system also known as BCM numbering for consistency across our resources, and having our own version of GPIO support gives us finer control over functionality and support for add-on boards in future.
All of our resources using Scratch will use this version from now on, and existing resources will be rewritten. Tim’s reference guide details all of the commands and functionality, and there will be a simplified beginner’s tutorial along this week.
Last of all, there’s no way I can end this post without taking the opportunity to thank our community who have supported (and continue to support) GPIO in Scratch on the Pi. In particular, a big thanks to Simon Walters, aka @cymplecy, for all of his work on Scratch GPIO over the last few years.
The post A new version of Scratch for Raspberry Pi: now with added GPIO appeared first on Raspberry Pi.