Joystick test program for the Gameduino

Dependencies:   Gameduino mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "GD.h"
00003 #include "shield.h"
00004 
00005 SPI spi(ARD_MOSI, ARD_MISO, ARD_SCK);
00006 GDClass GD(ARD_MOSI, ARD_MISO, ARD_SCK, ARD_D9, USBTX, USBRX) ;
00007 
00008 
00009 DigitalIn down(ARD_A2);
00010 DigitalIn up(ARD_A3);
00011 DigitalIn left(ARD_A1);
00012 DigitalIn right(ARD_A0);
00013 DigitalIn a(ARD_D7);
00014 DigitalIn b(ARD_D4);
00015 DigitalIn c(ARD_D5);
00016 DigitalIn start(ARD_D6);
00017 AnalogIn Ain1(ARD_A4);
00018 AnalogIn Ain2(ARD_A5);
00019 
00020 
00021 void setup()
00022 {
00023     down.mode(PullUp);
00024     up.mode(PullUp);
00025     left.mode(PullUp);
00026     right.mode(PullUp);
00027     a.mode(PullUp);
00028     b.mode(PullUp);
00029     c.mode(PullUp);
00030     start.mode(PullUp);
00031 
00032 
00033     GD.begin();
00034     GD.ascii();
00035 
00036     GD.wr16(RAM_SPRPAL + 2 * 255, TRANSPARENT);
00037     byte i;
00038     // draw 32 circles into 32 sprite images
00039     for (i = 0; i < 32; i++) {
00040         GD.wr16(RAM_SPRPAL + 2 * i, RGB(8 * i, 64, 255 - 8 * i));
00041         int dst = RAM_SPRIMG + 256 * i;
00042         GD.__wstart(dst);
00043         byte x, y;
00044         int r2 = min(i * i, 256);
00045         for (y = 0; y < 16; y++) {
00046             for (x = 0; x < 16; x++) {
00047                 byte pixel;
00048                 if ((x * x + y * y) <= r2)
00049                     pixel = i;    // use color above
00050                 else
00051                     pixel = 0xff; // transparent
00052                 spi.write(pixel);
00053             }
00054         }
00055         GD.__end();
00056     }
00057 }
00058 
00059 void circle(int xpos, int ypos, byte a)
00060 {
00061     byte sprnum = 0;
00062     GD.sprite(sprnum++, xpos + 16, ypos + 16, a, 0, 0);
00063     GD.sprite(sprnum++, xpos +  0, ypos + 16, a, 0, 2);
00064     GD.sprite(sprnum++, xpos + 16, ypos +  0, a, 0, 4);
00065     GD.sprite(sprnum++, xpos +  0, ypos +  0, a, 0, 6);
00066 }
00067 
00068 static int bbits()
00069 {
00070     int r = 0;
00071     r |= (up.read() << 0);
00072     r |= (down.read() << 1);
00073     r |= (left.read() << 2);
00074     r |= (right.read() << 3);
00075     r |= (a.read() << 4);
00076     r |= (b.read() << 5);
00077     r |= (c.read() << 6);
00078     r |= (start.read() << 7);
00079     return r;
00080 }
00081 static int xcoord = 0;
00082 static int ycoord = 0;
00083 static int ands = 0x00ff, ors = 0x0000;
00084 
00085 void loop()
00086 {
00087     int bb = bbits();
00088 
00089     GD.putstr(20, 10, (bb & 0x0001) ? "-" : "U");
00090     GD.putstr(20, 20, (bb & 0x0002) ? "-" : "D");
00091     GD.putstr(15, 15, (bb & 0x0004) ? "-" : "L");
00092     GD.putstr(25, 15, (bb & 0x0008) ? "-" : "R");
00093 
00094     GD.putstr(35, 25, (bb & 0x0010) ? "-" : "A");
00095     GD.putstr(40, 25, (bb & 0x0020) ? "-" : "B");
00096     GD.putstr(45, 25, (bb & 0x0040) ? "-" : "C");
00097     GD.putstr(35, 15, (bb & 0x0080) ? "-    " : "Start");
00098 
00099     ands &= bb;
00100     ors |= bb;
00101 
00102     if (ands == 0 && ors == 0x00ff)
00103         GD.putstr(20, 24, "BUTTONS OK");
00104         
00105     xcoord = Ain1 * 255;
00106     ycoord = Ain2 * 255;
00107     char msg[20];
00108     sprintf(msg, "X=%4d, Y=%4d", xcoord, ycoord);
00109     GD.putstr(0, 36, msg);
00110 
00111     circle(xcoord / 4, 255 - ycoord / 4, (bb & 0x10) ? 15 : 31);
00112 
00113 }
00114 
00115 
00116 int main()
00117 {
00118     setup();
00119     while(1) {
00120         loop();
00121     }
00122 }
00123 
00124