Contents
The Gameduino Python package contains tools for Gameduino developers. To install it on Linux or Mac:
sudo easy_install gameduino
and on Windows, assuming you already installed Python 2.7 and setuptools:
cd \Python27\Scripts
easy_install PIL
easy_install PySerial
easy_install gameduino
For graphics work, you will need the Python Imaging Library (PIL). It is quite popular so many systems have it installed. To check if you have it, from Python do:
>>> import Image
>>>
The prep module provides utilities for preparing Gameduino media: images and sound. These utilities can be used, for example, to take image files and encode them so that the Gameduino can display them as backgrounds or sprites.
The module defines these constants for use as the palset argument to ImageRAM.addsprites():
| PALETTE256A | 256-color palette A |
| PALETTE256B | 256-color palette B |
| PALETTE256C | 256-color palette C |
| PALETTE256D | 256-color palette D |
| PALETTE4A | Four-color palette A |
| PALETTE4B | Four-color palette B |
| PALETTE16A | Sixteen-color palette A |
| PALETTE16B | Sixteen-color palette B |
Convert a PIL image to a Gameduino character background image.
| Parameters: |
|
|---|---|
| Return type: | tuple of data for (picture, character, font) all array.array. |
The image must have dimensions that are multiples of 8. If any character cell contains more than four colors, then the cell’s pixel are quantized to four colors before encoding. If the image requires more than 256 unique character cells, this function throws exception OverflowError. The tuple returned contains three pieces of data:
To display the image, load these three arrays into Gameduino memory. For example, to encode a single image and write its data to a header file titlescreen.h:
import gameduino.prep as gdprep
(dpic, dchr, dpal) = gdprep.encode(Image.open("titlescreen.png"))
hdr = open("titlescreen.h", "w")
gdprep.dump(hdr, "titlescreen_pic", dpic)
gdprep.dump(hdr, "titlescreen_chr", dchr)
gdprep.dump(hdr, "titlescreen_pal", dpal)
and to display the image on the screen, an Arduino sketch might do:
#include "titlescreen.h"
void setup()
{
...
GD.copy(RAM_PIC, titlescreen_pic, sizeof(titlescreen_pic));
GD.copy(RAM_CHR, titlescreen_chr, sizeof(titlescreen_chr));
GD.copy(RAM_PAL, titlescreen_pal, sizeof(titlescreen_pal));
Writes data to a header file for use in an Arduino Sketch.
| Parameters: |
|
|---|
Given an input image or list of images, convert to a palettized version using at most ncol colors. This function preserves transparency: if the input(s) have transparency then the returned image(s) have .info['transparency'] set to the transparent color.
If im is a single image, returns a single image. If im is a list of images, returns a list of images.
im is a paletted image. Return its palette as a Gameduino sprite palette in an array.array of type ‘H’. This form can be used directly with dump():
import gameduino.prep as gdprep
...
gdprep.dump(hfile, "paletteA", gdprep.getpal(im))
The ImageRAM object simplifies loading of the Gameduino’s 16K sprite image RAM. A caller adds sprite images to the ImageRAM, and finally obtains a memory image using ImageRAM.used().
Add a sprite image to the ImageRAM
| Parameters: |
|
|---|---|
| Return type: | Returns a tuple (image, pal) |
This method adds the data in page to the ImageRAM, and the returns the assigned location. image is the sprite image 0-63 containing the data, and pal is the palette bit select for the data.
For a 4-color image, pal is 0-3, for 16-color image pal is 0-1 and for a 256-color image pal is 0.
The image and pal values may be used to display the sprite using GD::sprite().
If the data would cause the ImageRAM to increase beyond 16K, this method throws exception OverflowError.
Extract multiple sprite frames from a source image, and generate the code to draw them.
| Parameters: |
|
|---|
Given a sequence of sprite frames in im, this method extracts their data and adds it to the ImageRAM. In addition, it writes the code to draw the sprite to the ImageRAM’s header file. For example:
import gameduino.prep as gdprep
ir = gdprep.ImageRAM(open("hdr.h", "w"))
rock0 = gdprep.palettize(Image.open("rock0r.png"), 16)
ir.addsprites("rock0", (16, 16), rock0, gdprep.PALETTE16A, center = (8,8))
would extract the four 16x16 frames from the rock0r.png image:
and write the following code to hdr.h:
#define ROCK0_FRAMES 4
static void draw_rock0(int x, int y, byte anim, byte rot, byte jk = 0) {
...
}
For more more examples, see the asteroids demo game.
Return the contents of the ImageRAM, as an array.array of type ‘B’. The size of the array depends on the amount of data added, up to a limit of 16K.
Read an Audacity spectrum file and return a list of (frequency, amplitude) pairs, loudest first.
| Parameters: |
|
|---|---|
| Return type: | list of tuples (frequency, amplitude) where frequency is a floating-point frequency in Hz, and amplitude in an integer amplitude. |
This function can be used to create voice profiles for instruments and sounds. For example to load a choir sound, previously saved as choir.txt:
for (i, (f, a)) in enumerate(spectrum("choir.txt")):
gd.voice(i, 0, int(4 * f), a, a)
The Gameduino simulator can simulate some aspects of Gameduino hardware, both video and audio. It can be a useful tool for previewing media before loading on actual hardware.
The Gameduino in this module is similar to the one in gameduino.remote:
import gameduino
import gameduino.prep as gdprep
import gameduino.sim as gdsim
im = Image.open("platformer.png").convert("RGB")
(picd,chrd,pald) = gdprep.encode(im)
gd = gdsim.Gameduino()
gd.wrstr(gameduino.RAM_PIC, picd)
gd.wrstr(gameduino.RAM_CHR, chrd)
gd.wrstr(gameduino.RAM_PAL, pald)
gd.im().save("preview.png")
The simulator can produce screenshots (Gameduino.im()), generate single-note sounds (Gameduino.writewave()), and simulate collision RAM (Gameduino.coll()). It does not currently simulate the coprocessor.
The Gameduino object simulates some aspects of the Gameduino hardware. For example:
>>> import gameduino
>>> import gameduino.sim as gdsim
>>> gd = gdsim.Gameduino()
>>> print hex(gd.rd(gameduino.IDENT))
0x6d
Return the 256 bytes of COLLISION RAM.
| Return type: | list of byte values. |
|---|
Dump l bytes memory starting at address a
Fill c bytes of memory at address a with value v
Return the current screen as a 400x300 RGB PIL Image:
>>> import gameduino.sim
>>> gd = gameduino.sim.Gameduino()
>>> gd.im().save("screenshot.png")
Returns current image of memory as a 32768 byte string
Halt coprocessor, load microprogram, restart coprocessor
| Parameters: |
|
|---|
The string is loaded into the Gameduino’s microprogram area, and can be up to 256 bytes. For example, to load the split-screen scroll microprogram:
splitscreen_code = open("splitscreen.binle", "b").read()
gd.microcode(splitscreen_code)
Write string v at screen position (x,y)
Read byte at address a
Read n bytes starting at address a
| Return type: | string of length n. |
|---|
Switch all voices off
Set the state of a hardware sprite
| Parameters: |
|
|---|
Palette select controls the number of colors used for the sprite, the source palette, and which data bits to use as source.
Set the state of a voice.
| Parameters: |
|---|
Write a single byte v to address a.
Write 16-bit value v at to address a
Write 32-bit value v at to address a
Write the simulated output of the sound system to a wave file
| Parameters: |
|
|---|
The remote interface lets Python scripts read and write Gameduino memory, via the USB connection and a simple client running on the Arduino.
The remote interface can be more convenient than compiling and uploading a Sketch when developing media and coprocessor microprograms:
import gameduino.remote
gd = gameduino.remote.Gameduino("/dev/ttyUSB0", 115200)
gd.ascii()
gd.putstr(0, 5, "Hello from Python")
The Gameduino in this module is similar to the one in gameduino.sim.
Because this module uses USB serial interface to communicate with the Arduino, it requires the PySerial module.
The Arduino runs a simple program memloader that listens for serial commands and accesses Gameduino memory.
Return the 256 bytes of COLLISION RAM.
| Return type: | list of byte values. |
|---|
Dump l bytes memory starting at address a
Fill c bytes of memory at address a with value v
Returns current image of memory as a 32768 byte string
Halt coprocessor, load microprogram, restart coprocessor
| Parameters: |
|
|---|
The string is loaded into the Gameduino’s microprogram area, and can be up to 256 bytes. For example, to load the split-screen scroll microprogram:
splitscreen_code = open("splitscreen.binle", "b").read()
gd.microcode(splitscreen_code)
Write string v at screen position (x,y)
Read byte at address a
Read n bytes starting at address a
| Return type: | string of length n. |
|---|
Switch all voices off
Set the state of a hardware sprite
| Parameters: |
|
|---|
Palette select controls the number of colors used for the sprite, the source palette, and which data bits to use as source.
Set the state of a voice.
| Parameters: |
|---|
Write a single byte v to address a.
Write 16-bit value v at to address a
Write 32-bit value v at to address a
Last modified $Date: 2011-06-11 16:44:02 -0700 (Sat, 11 Jun 2011) $