Buy the book
Mu Editor
PyGame Zero
Python

PAD - Code in Python

Code in Python — time to move on... how do we program a controller?

Thanks to the book 'Code in Python — let's create an adventure game', each of you has learned the basics of programming using Python, whether a student, teacher or parent. Now it's time for the next step...

We're placing a Code in Python PAD in your hands, which we hope will encourage you to deepen your knowledge of the Python language. We expect you'll have fun along the way. Not so bad, right? :)

At first glance, our PAD looks like some kind of weird controller for a game console. In fact, it can serve this role. If it's plugged into a computer, you can use it to control characters in games just as you would with a standard controller pad. Our controller has one analogue joystick and five buttons, which, for many games, is more than enough. However, the Code in Python PAD has many additional features not present in standard controllers. This is what makes it different from other, seemingly similar devices.

First of all, the large matrix of LEDs may catch the eye. You can write short programs in Python to draw and animate various shapes on the matrix. The controller also has a small speaker that can produce basic but fun 'old-school' sounds.

On the left side of the controller, you'll find a discreet and mysterious connector. This is the controller's expansion port, to which you can connect various modules. We'll be able to communicate with attached modules by writing simple Python programs.

You're probably wondering what modules are available; here are a few: tilt sensor/gyroscope, light/colour sensor, input/output expansion, servos, stepper motors, DC motors, distance sensors, motion sensors, RGB LEDs, temperature/humidity/pressure sensors... as you can see, there is quite a range!

The Code in Python PAD requires no drivers or additional power source. It comes equipped with a USB cable for connecting to your computer. The following steps will show you how it works.

So we can start working with the PAD quickly, without complication, we have chosen a simple and friendly working environment — the Mu editor. Install it, and we're ready to go!

INSTALLATION AND CONFIGURATION

Press the button and download the application that installs the Mu editor and the entire development environment needed to create the adventure game. Download the file appropriate for your operating system (Windows, OSX, Linux) and then run the installation program. Below is a detailed installation guide for the system of your choice. After installation, run the Mu editor and then configure it according to the instructions below.

If you have gone through the installation and started the editor, it's time for the next step

Step 2 - decive connection

EDITOR INSTALLATION INSTRUCTIONS

Windows Installation Guide

Step 1 Download Mu Installer

Download the appropriate installer for your operating system: 32-bit or 64-bit. The buttons to download the corresponding version of the editor are located above. To find out which installer is right for your system, right-click on 'This Computer' and select the properties option. Find the operating system type in the System Type tab.

Step 2 - Start the installer

Find the Mu editor installation program you downloaded and run it by double-clicking on the installer icon.

Step 3 - License Agreement

Read the license, and then if you want to continue the installation click the 'Agree' button .

Step 4 - Select users

Choose whether you want to install the Mu editor for all users or just for yourself and click 'Next'. Please note that if your user account does not have administrative rights, this step may not appear during the installation process. In that case, the installer can only work if you select 'Install just for me'.

Step 5 - Choose where you want to install the editor

We suggest that you do not change the automatically proposed path. This will make it easier for you to complete the setup.

Step 6 - Installation

Wait for the Mu editor to install on your computer.

Step 7 - Completion of installation

The installation is complete, click the 'Finish' button to close the installer. You can start the Mu editor for example by clicking icon on the Start menu.

Mac OSX Installation Guide

Step 1 Download Mu Installer

Download the Mac OSX installation program by clicking the download button above.

Step 2 – Open the installation program

Open the Downloads folder – click ‘Go’ and then ‘Downloads’ in the Finder menu.

Double-click the Mu installer disk image (.dmg file). When you open the installer, you will be asked to accept the software license agreement, and then your computer will check if the package is not damaged (it may take a few minutes).

Step 3 - Mu Installation

Click on the ‘mu-editor’ icon, hold it down, and drag it to the Applications folder.

Step 4 – Installation

The Mu editor will install in the Applications folder.

Step 5 - Start Mu

Open the Applications folder – click 'Go' and then 'Applications’ in the 'Finder' menu. Press CTRL and click on the Mu icon. A dialog box appears where you can check if you want to open Mu. Click on 'Open':

Linux Installation Guide

Step 1 – Start the installation process by entering the following command::

pip3 -V

This way you'll check if you have pip for Python3 installed. If you don't have one, install the package that's right for your distribution, i.e. for Debian/Ubuntu/Mint systems this could be a package called python3-pip.

Step 2 - Install the editor by typing the following command::

sudo -H pip3 install mu-editor

Step 3 – Check if the editor is installed by typing the following command::

/usr/local/bin/mu-editor


Step 4 - Add an icon to your desktop. Here's an example for Mint:


EDITOR CONFIGURATION

When you start the Mu editor, press the ‘Mode’ key in the upper-left corner of the editor's main menu. Then, in the window that appears, select ‘Pygame Zero’ and press the ‘OK’ key. The editor will switch to ‘Pygame Zero’ mode. Our environment is ready to go!

Device connection

To use the PAD, you need to connect it to your computer using the USB cable. Any operating system (MS Windows, Mac OS, Linux) will automatically detect the connected device; we don't need to install any additional drivers. Immediately after connecting the controller, it should be ready for use. See how to import programs.

Download files
Step 3 - example programs

Technical Description:

5V power supply with 400mA via USB cable connected to a computer. Software controlled at the Python programming language level.
Power supply protected against short-circuiting. Information about short circuits is read at the Python programming language level.
Independently controlled expansion pins. Short-circuit proof.
Pin input and output voltage of 3.3V with tolerance up to 5V.
Pins can be set and controlled in the following modes:
Digital input with pull-up resistor,
Digital input with pull-down resistor,
Digital output 0–3.3V,
Digital output with open drain,
Analogue input 0–3.3V,
Analogue output 0–3.3V,
I2C protocol,
SPI protocol,
1-Wire protocol,
Digital diode protocol,
PWM output with set frequency and filling,
Serial / UART transmission (0–3.3V)

Example programs

The PAD can be programmed in many different ways. Below we present some suggestions which will deepen your knowledge of Python in a fun and user-friendly manner.