Monday, 16 April 2012

Getting data from the InputShield to the TouchShield


Writing an application for the Game Pack will generally consist of 2 programs. One that runs on the Arduino to send data from the InputShield to the TouchShield, and another that runs on the TouchShield that will perform the main game processing and handle the data from the InputShield.

The program to send data from the InputShield is relatively simple. It polls the pins connected to the various buttons for signals and then sends these signals to the TouchShield through another pin using serial communications. The data is sent in a group containing a value for each component.

e.g. the data sent for one poll, will contain the following set of information

JoyStick Lateral = 100
JoyStick Vertical = 0
JoyStick Button = 1
Button A = 0
Button B = 1

The program to upload to the Arduino can be created using the SimpleArduinoLibrary and adding a main source file (InputShieldForwarder.cpp) containing the code to poll the pins and send the data out through another pin. You can download InputShieldForwarder.cpp here

Create a new project called InputShieldForwarder for the Arduino Uno (follow the steps from the last post). The InputShieldForwarder.cpp source file is copied from the original code created by Liquidware (You can find it here) but required a few tweaks to remove the dependencies on the processing libraries and use the SimpleArduinoLibrary instead.

Your new project should look like this:



So now you can upload the program to the Arduino but it won't look very impressive because the TouchShield is not listening and will not react to data sent from the InputShield. To remedy this, we will upload a little program for the TouchShield which will poll for data and display it on the screen.

Creating a new project for the TouchShield


A TouchShield project is created in the same way as we did for the Arduino except some different configuration is required:

Under AVR Target Hardware properties:

    MCU Type → Atmega2560

In the AVRDude configuration:

    New Programmer configuration:

    Name → TouchShield
    Programmer Hardware → Atmel Butterfly Development Board
    Override default baud rate → 57600

The Antipasto IDE contains loads of useful libraries for manipulating the graphics on the TouchShield. We will reuse these libraries in our Eclipse project (they're super easy to use and there's no point re-inventing the wheel). We will also need the 'utils' folder from the last example to provide methods for constructing and destructing objects (not included in the avr-gcc linker) and a new class to handle the InputShield.

I have packaged up the source files required for this program here. Simply unzip them into your empty project.

Your new project should look like this



Included in the source files is a class called InputShield. This encapsulates the data format away from the main code and interprets the data into a more useful format (well, I have found the format more useful anyway). The analogue readings from the joystick are translated into discrete Left/Right/Up/Down values and the buttons values are also translated into booleans (note: when a button is pressed, the value is 0, not 1). This code is based on the InputShieldDisplay code written by Liquidware which is available here.

The code uses the graphics function: 

    text(textToOutput, xPos, yPos)

to display the data from the InputShield (Coordinates are referenced from the top left corner of the screen). For more information on what graphics functions are available for the TouchShield check out out the wiki page here.

Build the projects (in Release mode) and then we are ready to upload our programs.

  1. Connect the USB cable to the Arduino (connect it back into the Game Pack set up).

  2. Upload the InputShieldForwarder by selecting the project in the Project Explorer and selecting the 'AVR' option from the top menu and 'Upload Project to target device'.

  3. Before we upload to the TouchShield we need to prepare it for receiving the new code. Do this by pressing the teeny tiny button on the left side of the TouchShield. This will make the screen go blank (assuming it was displaying anything before)

    reset button on the TouchShield


  4. Select the InputShieldDisplay project in the Project Explorer and upload it in the same manner as step 2. This takes a little longer than uploading to the Arduino.

If all goes well you should now see some output on the TouchShield that changes when you interact with the InputShield.


No comments:

Post a Comment