A Library for the Gameduino shield

Dependencies:   Arduino

Dependents:   Gameduino_Asteroids_game Gameduino_Ball_demo Gameduino_Bitmap_demo Gameduino_chessboard_demo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GD.h Source File

GD.h

00001 /*
00002  *  LICENSE
00003  *  Copyright (c) 2012 James Bowman, Chris Dick
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  *
00023  *  DESCRIPTION
00024  * Copyright (c) 2011 by James Bowman <jamesb@excamera.com>
00025  * Gameduino library for mbed. Ported from Arduino by Chris Dick
00026  *
00027  *
00028  */
00029 
00030 
00031 #ifndef _GD_H_INCLUDED
00032 #define _GD_H_INCLUDED
00033 #include "mbed.h"
00034 #include "arduino.h" 
00035 
00036 struct sprplot
00037 {
00038   signed char x, y;
00039   byte image, palette;
00040 };
00041 
00042 /** Gameduino library ported from the arduino
00043  *
00044  * Example:
00045  * @code
00046  * // 
00047  * #include "mbed.h"
00048  * #include "GD.h"
00049  * 
00050  * 
00051  * GDClass GD(p5, p6, p7, p8, USBTX, USBRX) ;
00052  * 
00053  * int main() {
00054  *        // initialise Gameduino
00055  *        GD.begin();
00056  *        // load ascii characters 
00057  *        GD.ascii();
00058  * }
00059  * @endcode
00060  */
00061 class GDClass {
00062 private:
00063   SPI gameduino_spi;
00064   DigitalOut gameduino_cs;
00065   Serial pc;
00066 public:
00067 /** Create a Gameduino Object with the specified spi pins and uart pins.
00068  *
00069  * @param mosi name of pin for spi mosi connection.
00070  * @param miso name of pin for spi moso connection.
00071  * @param sclk name of pin for spi sclk connection.
00072  * @param cs name of pin for spi cs connection.
00073  * @param utx name of pin for uart tx.
00074  * @param urx name of pin for uart rx.
00075  */
00076   GDClass(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName utx, PinName urx);
00077 /** Initialize the connection to the adapter, and reset the adapter.
00078  *
00079  * @param void.
00080  */
00081   void begin();
00082 /** end the spi transaction.
00083  *
00084  * @param void.
00085  */
00086   void end();
00087 /** start an spi transaction to addr.
00088  *
00089  * @param addr: address of memory location.
00090  */
00091   void __start(unsigned int addr);
00092 /** start an spi write transaction to addr.
00093  *
00094  * @param addr: address of memory location. 
00095  */
00096   void __wstart(unsigned int addr);
00097 /** end the spi transaction.
00098  *
00099  * @param void.
00100  */
00101   void __end(void);
00102 /** Read a byte from memory location addr.
00103  *
00104  * @param addr: address of memory location.
00105  */
00106   byte rd(unsigned int addr);
00107 /** Write byte v to memory location addr.
00108  *
00109  * @param addr: address of memory location.
00110  * @param v: data to write.
00111  */
00112   void wr(unsigned int addr, byte v);
00113 /** Read a 16-bit word from memory locations addr and addr+1.
00114  *
00115  * @param addr: address of first memory location.
00116  */
00117   unsigned int rd16(unsigned int addr);
00118 /** Write 16-bit word v to memory locations addr and addr+1.
00119  *
00120  * @param addr: address of first memory location.
00121  * @param v: data to write.
00122  */
00123   void wr16(unsigned int addr, unsigned int v);
00124 /** Fill count memory locations starting at addr with value v.
00125  *
00126  * @param addr: address of first memory location.
00127  * @param v: data to write.
00128  * @param count: number of memory location to fill.
00129  */
00130   void fill(int addr, byte v, unsigned int count);
00131 /** Copy count bytes from AVR flash memory location src to addr.
00132  *
00133  * @param addr: address of first destination memory location.
00134  * @param *src: address of first flash memory.
00135  * @param count: number of memory location to copy.
00136  */
00137   void copy(unsigned int addr, PROGMEM prog_uchar *src, int count);
00138 /** Set character palette entry pal to color rgb.
00139  *
00140  * @param pal: address of palatte.
00141  * @param rgb: colour value.
00142  */
00143   void setpal(int pal, unsigned int rgb);
00144 /** Set attributes of sprite spr, can also use RGB(r,g,b) Macro.
00145  *
00146  * @param spr: sprite number, 0-255.
00147  * @param x: x coordinate, 0-511. 0 is left edge of screen.
00148  * @param y: y coordinate, 0-511. 0 is top edge of screen.
00149  * @param image: image number, 0-63.
00150  * @param palette: palette control 0-15.
00151  * @param rot: rotate/flip control 0-7.
00152  * @param jk: J/K collision select, 0-1.
00153  */
00154   void sprite(int spr, int x, int y, byte image, byte palette, byte rot = 0, byte jk = 0);
00155 /** Set attributes of four sprites in a 2x2 configuration. (x,y) specifies the center of the group.
00156  *
00157  * @param spr: sprite number, 0-255.
00158  * @param x: x center coordinate , 0-511. 0 is left edge of screen.
00159  * @param y: y center coordinate, 0-511. 0 is top edge of screen.
00160  * @param image: image number, 0-63.
00161  * @param palette: palette control 0-15.
00162  * @param rot: rotate/flip control 0-7.
00163  * @param jk: J/K collision select, 0-1.
00164  */
00165   void sprite2x2(int spr, int x, int y, byte image, byte palette, byte rot = 0, byte jk = 0);
00166 /** Waits for the start of the vertical blanking period. By waiting for vertical blanking: any animation is guaranteed to be smooth, the sprite collision RAM is valid.
00167  *
00168  * @param void
00169  */
00170   void waitvblank();
00171 /** Copy count bytes from AVR flash memory location src into the coprocessor’s instruction RAM, and restart the coprocessor.
00172  *
00173  * @param *src: address of first flash memory.
00174  * @param count: number of memory location to copy.
00175  */
00176   void microcode(PROGMEM prog_uchar *src, int count);
00177 /** uncompression algorithm for use with online tools
00178  *
00179  * @param addr first address of gameduino memory to store to.
00180  * @param *src pointer to compressed data in program memory.
00181  */
00182   void uncompress(unsigned int addr, PROGMEM prog_uchar *src);
00183 /** Set audio voice waveform, frequency and amplitude. The 64 audio voices are summed for final output. So to guarantee no audio clipping, the sum of all voices’ amplitudes should be 255 or less.
00184  *
00185  * @param voice: hardware voice number, 0-63.
00186  * @param wave: waveform, 0 is sine wave, 1 is noise.
00187  * @param freq: frequency in quarter-Hz, e.g. 100 Hz is 400.
00188  * @param lamp: left amplitude, 0-255.
00189  * @param ramp: right amplitude, 0-255.
00190  */
00191   void voice(int v, byte wave, unsigned int freq, byte lamp, byte ramp);
00192 /** Load a standard 8x8 ASCII font into character RAM. This is useful for debugging and status display. 
00193  *
00194  * @param void
00195  *
00196  */
00197   void ascii();
00198 /** Draw string s at character screen position (x,y). Assumes that a standard character set has been loaded, e.g. by ascii().
00199  *
00200  * @param x: x coordinate, 0-511. 0 is left edge of screen.
00201  * @param y: y coordinate, 0-511. 0 is top edge of screen.
00202  * @param *s: pointer to string.
00203  */
00204   void putstr(int x, int y, const char *s);
00205 /** grab a screen shot and dump to uart. Python script availiable from http://excamera.com/sphinx/gameduino/samples/screenshot/index.html to save recieved data.
00206  *
00207  * @param frame number to added to saved filename.
00208  */
00209   void screenshot(unsigned int frame);
00210 /** start an spi write transaction to sprite.
00211  *
00212  * @param spr: sprite number to write to.
00213  */
00214   void __wstartspr(unsigned int spr = 0);
00215 /** controls sprite chosen with __wstartspr with position (x,y), offset (ox,oy), rotation rot, collision class jk and palette and image.
00216  *
00217  * @param ox: x offset.
00218  * @param oy: y offset.
00219  * @param x: x coordinate, 0-511. 0 is left edge of screen.
00220  * @param y: y coordinate, 0-511. 0 is top edge of screen.
00221  * @param image: SpriteImageSelect. 0-63. slects which source image to use.
00222  * @param palette: sprite palette select, 4 bits Controls how pixel data is converted to colour.
00223  * @param rot: rotation of sprite, 3 bits rotates sprites by quarter turns.
00224  * @param jk: collision class. 0 is J, 1 is K.
00225  */
00226   void xsprite(int ox, int oy, signed char x, signed char y, byte image, byte palette, byte rot = 0, byte jk = 0);
00227 /** hide current sprite by moving it off visible screen area
00228  *
00229  * @param void
00230  */
00231   void xhide();
00232 /** plots count sprites at using xsprite method 
00233  *
00234  * @param void
00235  */
00236   void plots(int ox, int oy, PROGMEM sprplot *psp, byte count, byte rot, byte jk);
00237   
00238   byte spr;   // Current sprite, incremented by xsprite/xhide above
00239 };
00240 
00241 #define GD_HAS_PLOTS    1     // have the 'GD.plots' method
00242 
00243 //extern GDClass GD;
00244 
00245 #define RGB(r,g,b) ((((r) >> 3) << 10) | (((g) >> 3) << 5) | ((b) >> 3))
00246 #define TRANSPARENT (1 << 15) // transparent for chars and sprites
00247 
00248 #define RAM_PIC       0x0000    // Screen Picture, 64 x 64 = 4096 bytes
00249 #define RAM_CHR       0x1000    // Screen Characters, 256 x 16 = 4096 bytes
00250 #define RAM_PAL       0x2000    // Screen Character Palette, 256 x 8 = 2048 bytes
00251 
00252 #define IDENT         0x2800
00253 #define REV           0x2801
00254 #define FRAME         0x2802
00255 #define VBLANK        0x2803
00256 #define SCROLL_X      0x2804
00257 #define SCROLL_Y      0x2806
00258 #define JK_MODE       0x2808
00259 #define J1_RESET      0x2809
00260 #define SPR_DISABLE   0x280a
00261 #define SPR_PAGE      0x280b
00262 #define IOMODE        0x280c
00263 
00264 #define BG_COLOR      0x280e
00265 #define SAMPLE_L      0x2810
00266 #define SAMPLE_R      0x2812
00267 
00268 #define MODULATOR     0x2814
00269 
00270 #define SCREENSHOT_Y  0x281e
00271 
00272 #define PALETTE16A    0x2840   // 16-color palette RAM A, 32 bytes
00273 #define PALETTE16B    0x2860   // 16-color palette RAM B, 32 bytes
00274 #define PALETTE4A     0x2880   // 4-color palette RAM A, 8 bytes
00275 #define PALETTE4B     0x2888   // 4-color palette RAM A, 8 bytes
00276 #define COMM          0x2890   // Communication buffer
00277 #define COLLISION     0x2900   // Collision detection RAM, 256 bytes
00278 #define VOICES        0x2a00   // Voice controls
00279 #define J1_CODE       0x2b00   // J1 coprocessor microcode RAM
00280 #define SCREENSHOT    0x2c00   // screenshot line RAM
00281 #define RAM_SPR       0x3000    // Sprite Control, 512 x 4 = 2048 bytes
00282 #define RAM_SPRPAL    0x3800    // Sprite Palettes, 4 x 256 = 2048 bytes
00283 #define RAM_SPRIMG    0x4000    // Sprite Image, 64 x 256 = 16384 bytes
00284 
00285 #define GET_FAR_ADDRESS(var) (var)
00286 #endif //_GD_H_INCLUDED