More complicated test program that generates text, coloured sprites and sound.

Dependencies:   Gameduino mbed CommonTypes

Committer:
RichardE
Date:
Sun Nov 18 15:13:53 2012 +0000
Revision:
5:562a094df892
Parent:
2:5604e16b78bd
New version of Gameduino library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RichardE 0:13f69384ff8a 1 /*
RichardE 0:13f69384ff8a 2 * SOURCE FILE : GameduinoTest.cpp
RichardE 0:13f69384ff8a 3 *
RichardE 0:13f69384ff8a 4 * Definition of class GameduinoTest.
RichardE 0:13f69384ff8a 5 * Tests a Gameduino object.
RichardE 0:13f69384ff8a 6 *
RichardE 0:13f69384ff8a 7 */
RichardE 0:13f69384ff8a 8
RichardE 0:13f69384ff8a 9 #include "GameduinoTest.h" // this module's prototypes
RichardE 0:13f69384ff8a 10 #include "mbed.h" // mbed library
RichardE 1:c8ec5f958f3c 11 #include "sprite.h" // sprite data
RichardE 0:13f69384ff8a 12
RichardE 0:13f69384ff8a 13 /***************/
RichardE 0:13f69384ff8a 14 /* CONSTRUCTOR */
RichardE 0:13f69384ff8a 15 /***************/
RichardE 0:13f69384ff8a 16 GameduinoTest::GameduinoTest() {
RichardE 0:13f69384ff8a 17 }
RichardE 0:13f69384ff8a 18
RichardE 0:13f69384ff8a 19 /**************/
RichardE 0:13f69384ff8a 20 /* DESTRUCTOR */
RichardE 0:13f69384ff8a 21 /**************/
RichardE 0:13f69384ff8a 22 GameduinoTest::~GameduinoTest() {
RichardE 0:13f69384ff8a 23 }
RichardE 0:13f69384ff8a 24
RichardE 1:c8ec5f958f3c 25 /*********************/
RichardE 1:c8ec5f958f3c 26 /* DO ANIMATION TEST */
RichardE 1:c8ec5f958f3c 27 /*********************/
RichardE 1:c8ec5f958f3c 28 // Pass pointer to Gameduino to use in gd.
RichardE 1:c8ec5f958f3c 29 void GameduinoTest::AnimationTest( Gameduino *gd ) {
RichardE 1:c8ec5f958f3c 30 Int16 x = 0;
RichardE 1:c8ec5f958f3c 31 Int8 dir = 1;
RichardE 1:c8ec5f958f3c 32 for( UInt16 i = 0; i < 1000; ++i ) {
RichardE 1:c8ec5f958f3c 33 gd->waitvblank();
RichardE 1:c8ec5f958f3c 34 gd->sprite( 100, x, 100, 0, 0 );
RichardE 1:c8ec5f958f3c 35 x += dir;
RichardE 1:c8ec5f958f3c 36 if( x > 200 ) {
RichardE 1:c8ec5f958f3c 37 dir = -1;
RichardE 1:c8ec5f958f3c 38 }
RichardE 1:c8ec5f958f3c 39 else if( x < 0 ) {
RichardE 1:c8ec5f958f3c 40 dir = 1;
RichardE 1:c8ec5f958f3c 41 }
RichardE 1:c8ec5f958f3c 42 }
RichardE 1:c8ec5f958f3c 43 }
RichardE 1:c8ec5f958f3c 44
RichardE 2:5604e16b78bd 45 /*********************/
RichardE 2:5604e16b78bd 46 /* TEST PLOTS METHOD */
RichardE 2:5604e16b78bd 47 /*********************/
RichardE 2:5604e16b78bd 48 // Pass pointer to Gameduino to use in gd.
RichardE 2:5604e16b78bd 49 void GameduinoTest::TestPlots( Gameduino *gd ) {
RichardE 2:5604e16b78bd 50 #define PLOTLEN 3
RichardE 2:5604e16b78bd 51 Gameduino::sprplot records[ PLOTLEN ] = {
RichardE 2:5604e16b78bd 52 { 0, 0, 26, 0 },
RichardE 2:5604e16b78bd 53 { 16, 1, 27, 0 },
RichardE 2:5604e16b78bd 54 { 32, 2, 28, 0 },
RichardE 2:5604e16b78bd 55 };
RichardE 2:5604e16b78bd 56 gd->__wstartspr( 110 );
RichardE 2:5604e16b78bd 57 gd->plots( 170, 0, records, PLOTLEN );
RichardE 2:5604e16b78bd 58 gd->__end();
RichardE 2:5604e16b78bd 59 }
RichardE 2:5604e16b78bd 60
RichardE 0:13f69384ff8a 61 /****************/
RichardE 0:13f69384ff8a 62 /* RUN THE TEST */
RichardE 0:13f69384ff8a 63 /****************/
RichardE 0:13f69384ff8a 64 void GameduinoTest::Run( void ) {
RichardE 0:13f69384ff8a 65 // Make a serial port for communicating with PC.
RichardE 0:13f69384ff8a 66 Serial pc( USBTX, USBRX );
RichardE 0:13f69384ff8a 67 // Sign on message.
RichardE 0:13f69384ff8a 68 pc.printf( "Up and running!\r\n" );
RichardE 0:13f69384ff8a 69 // Make a digital output for use with Gameduino.
RichardE 0:13f69384ff8a 70 DigitalOut cs( p8 );
RichardE 0:13f69384ff8a 71 // Initialise an SPI link for communications with Gameduino.
RichardE 0:13f69384ff8a 72 // Use pin 5 for MOSI.
RichardE 0:13f69384ff8a 73 // Use pin 6 for MISO.
RichardE 0:13f69384ff8a 74 // Use pin 7 for SCK.
RichardE 0:13f69384ff8a 75 SPI spi( p5, p6, p7 );
RichardE 2:5604e16b78bd 76 // 8MHz clock should be OK.
RichardE 2:5604e16b78bd 77 spi.frequency( 8000000 );
RichardE 0:13f69384ff8a 78 // Set SPI format to use.
RichardE 0:13f69384ff8a 79 // Use 8 bits per SPI frame.
RichardE 0:13f69384ff8a 80 // Use SPI mode 0.
RichardE 0:13f69384ff8a 81 spi.format( 8, 0 );
RichardE 0:13f69384ff8a 82 // Make a Gameduino and pass SPI link and digital output for chip select.
RichardE 0:13f69384ff8a 83 Gameduino gd( &spi, &cs );
RichardE 0:13f69384ff8a 84 // Reset the Gameduino.
RichardE 0:13f69384ff8a 85 gd.begin();
RichardE 0:13f69384ff8a 86 // Lets have a default ASCII character set.
RichardE 0:13f69384ff8a 87 gd.ascii();
RichardE 1:c8ec5f958f3c 88 // Fill sprite image and palette memory.
RichardE 1:c8ec5f958f3c 89 gd.copy( Gameduino::RAM_SPRIMG, sprite_sprimg, sizeof( sprite_sprimg ) );
RichardE 1:c8ec5f958f3c 90 gd.copy( Gameduino::RAM_SPRPAL, sprite_sprpal, sizeof( sprite_sprpal ) );
RichardE 0:13f69384ff8a 91 // Read from ident register.
RichardE 0:13f69384ff8a 92 UInt8 id = gd.rd( Gameduino::IDENT );
RichardE 0:13f69384ff8a 93 // Report back to PC.
RichardE 0:13f69384ff8a 94 pc.printf( "Gameduino ID is 0x%02X.\r\n", (int)id );
RichardE 0:13f69384ff8a 95 // Write something to character memory.
RichardE 0:13f69384ff8a 96 gd.__wstart( Gameduino::RAM_PIC );
RichardE 0:13f69384ff8a 97 for( UInt8 c = 'A'; c <= 'Z'; ++c ) {
RichardE 0:13f69384ff8a 98 gd.__tr8( c );
RichardE 0:13f69384ff8a 99 }
RichardE 0:13f69384ff8a 100 gd.__end();
RichardE 1:c8ec5f958f3c 101 // Test copy method.
RichardE 1:c8ec5f958f3c 102 UInt8 copyData[] = "HELLO";
RichardE 1:c8ec5f958f3c 103 gd.copy( Gameduino::RAM_PIC + 64, copyData, 5 );
RichardE 1:c8ec5f958f3c 104 // Test putstr method.
RichardE 1:c8ec5f958f3c 105 gd.putstr( 3, 10, "Ambidextrous!" );
RichardE 1:c8ec5f958f3c 106 // Show some sprites.
RichardE 1:c8ec5f958f3c 107 UInt16 y = 140;
RichardE 1:c8ec5f958f3c 108 UInt8 spriteNum = 0;
RichardE 1:c8ec5f958f3c 109 for( UInt8 rot = 0; rot < 8; ++rot ) {
RichardE 1:c8ec5f958f3c 110 UInt16 x = 0;
RichardE 1:c8ec5f958f3c 111 for( UInt8 spriteImage = 0; spriteImage < 10; ++spriteImage ) {
RichardE 1:c8ec5f958f3c 112 gd.sprite( spriteNum++, x, y, spriteImage, 0, (Gameduino::Rotation)rot );
RichardE 1:c8ec5f958f3c 113 x += 18;
RichardE 1:c8ec5f958f3c 114 }
RichardE 1:c8ec5f958f3c 115 y += 18;
RichardE 1:c8ec5f958f3c 116 }
RichardE 2:5604e16b78bd 117 // Test out sprite2x2 method.
RichardE 2:5604e16b78bd 118 gd.sprite2x2( 90, 150, 16, 38, 0, Gameduino::None, 0 );
RichardE 2:5604e16b78bd 119 // Test out __wstartspr method.
RichardE 2:5604e16b78bd 120 gd.__wstartspr( 95 );
RichardE 2:5604e16b78bd 121 y = 32;
RichardE 2:5604e16b78bd 122 for( UInt8 imageNum = 15; imageNum < 19; ++imageNum ) {
RichardE 2:5604e16b78bd 123 gd.TransferSprite( 150, y, imageNum, 0 );
RichardE 2:5604e16b78bd 124 y += 16;
RichardE 2:5604e16b78bd 125 }
RichardE 2:5604e16b78bd 126 gd.__end();
RichardE 2:5604e16b78bd 127 // Test out plots method.
RichardE 2:5604e16b78bd 128 TestPlots( &gd );
RichardE 1:c8ec5f958f3c 129 // Do some sprite animation.
RichardE 1:c8ec5f958f3c 130 AnimationTest( &gd );
RichardE 2:5604e16b78bd 131 // Make a noise.
RichardE 2:5604e16b78bd 132 gd.voice( 25, Gameduino::SineWave, 8000, 127, 127 );
RichardE 2:5604e16b78bd 133 wait_ms( 1000 );
RichardE 2:5604e16b78bd 134 gd.voice( 25, Gameduino::SineWave, 0, 0, 0 );
RichardE 2:5604e16b78bd 135 // Make another noise.
RichardE 2:5604e16b78bd 136 gd.voice( 25, Gameduino::WhiteNoise, 8000, 127, 127 );
RichardE 2:5604e16b78bd 137 wait_ms( 1000 );
RichardE 2:5604e16b78bd 138 gd.voice( 25, Gameduino::WhiteNoise, 0, 0, 0 );
RichardE 1:c8ec5f958f3c 139 // Finished with Gameduino.
RichardE 1:c8ec5f958f3c 140 gd.end();
RichardE 0:13f69384ff8a 141 }