Chris Dick
/
Gameduino_Joystick_test
Joystick test program for the Gameduino
main.cpp@0:7d943f226caa, 2012-12-20 (annotated)
- Committer:
- TheChrisyd
- Date:
- Thu Dec 20 21:53:30 2012 +0000
- Revision:
- 0:7d943f226caa
working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TheChrisyd | 0:7d943f226caa | 1 | #include "mbed.h" |
TheChrisyd | 0:7d943f226caa | 2 | #include "GD.h" |
TheChrisyd | 0:7d943f226caa | 3 | #include "shield.h" |
TheChrisyd | 0:7d943f226caa | 4 | |
TheChrisyd | 0:7d943f226caa | 5 | SPI spi(ARD_MOSI, ARD_MISO, ARD_SCK); |
TheChrisyd | 0:7d943f226caa | 6 | GDClass GD(ARD_MOSI, ARD_MISO, ARD_SCK, ARD_D9, USBTX, USBRX) ; |
TheChrisyd | 0:7d943f226caa | 7 | |
TheChrisyd | 0:7d943f226caa | 8 | |
TheChrisyd | 0:7d943f226caa | 9 | DigitalIn down(ARD_A2); |
TheChrisyd | 0:7d943f226caa | 10 | DigitalIn up(ARD_A3); |
TheChrisyd | 0:7d943f226caa | 11 | DigitalIn left(ARD_A1); |
TheChrisyd | 0:7d943f226caa | 12 | DigitalIn right(ARD_A0); |
TheChrisyd | 0:7d943f226caa | 13 | DigitalIn a(ARD_D7); |
TheChrisyd | 0:7d943f226caa | 14 | DigitalIn b(ARD_D4); |
TheChrisyd | 0:7d943f226caa | 15 | DigitalIn c(ARD_D5); |
TheChrisyd | 0:7d943f226caa | 16 | DigitalIn start(ARD_D6); |
TheChrisyd | 0:7d943f226caa | 17 | AnalogIn Ain1(ARD_A4); |
TheChrisyd | 0:7d943f226caa | 18 | AnalogIn Ain2(ARD_A5); |
TheChrisyd | 0:7d943f226caa | 19 | |
TheChrisyd | 0:7d943f226caa | 20 | |
TheChrisyd | 0:7d943f226caa | 21 | void setup() |
TheChrisyd | 0:7d943f226caa | 22 | { |
TheChrisyd | 0:7d943f226caa | 23 | down.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 24 | up.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 25 | left.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 26 | right.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 27 | a.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 28 | b.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 29 | c.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 30 | start.mode(PullUp); |
TheChrisyd | 0:7d943f226caa | 31 | |
TheChrisyd | 0:7d943f226caa | 32 | |
TheChrisyd | 0:7d943f226caa | 33 | GD.begin(); |
TheChrisyd | 0:7d943f226caa | 34 | GD.ascii(); |
TheChrisyd | 0:7d943f226caa | 35 | |
TheChrisyd | 0:7d943f226caa | 36 | GD.wr16(RAM_SPRPAL + 2 * 255, TRANSPARENT); |
TheChrisyd | 0:7d943f226caa | 37 | byte i; |
TheChrisyd | 0:7d943f226caa | 38 | // draw 32 circles into 32 sprite images |
TheChrisyd | 0:7d943f226caa | 39 | for (i = 0; i < 32; i++) { |
TheChrisyd | 0:7d943f226caa | 40 | GD.wr16(RAM_SPRPAL + 2 * i, RGB(8 * i, 64, 255 - 8 * i)); |
TheChrisyd | 0:7d943f226caa | 41 | int dst = RAM_SPRIMG + 256 * i; |
TheChrisyd | 0:7d943f226caa | 42 | GD.__wstart(dst); |
TheChrisyd | 0:7d943f226caa | 43 | byte x, y; |
TheChrisyd | 0:7d943f226caa | 44 | int r2 = min(i * i, 256); |
TheChrisyd | 0:7d943f226caa | 45 | for (y = 0; y < 16; y++) { |
TheChrisyd | 0:7d943f226caa | 46 | for (x = 0; x < 16; x++) { |
TheChrisyd | 0:7d943f226caa | 47 | byte pixel; |
TheChrisyd | 0:7d943f226caa | 48 | if ((x * x + y * y) <= r2) |
TheChrisyd | 0:7d943f226caa | 49 | pixel = i; // use color above |
TheChrisyd | 0:7d943f226caa | 50 | else |
TheChrisyd | 0:7d943f226caa | 51 | pixel = 0xff; // transparent |
TheChrisyd | 0:7d943f226caa | 52 | spi.write(pixel); |
TheChrisyd | 0:7d943f226caa | 53 | } |
TheChrisyd | 0:7d943f226caa | 54 | } |
TheChrisyd | 0:7d943f226caa | 55 | GD.__end(); |
TheChrisyd | 0:7d943f226caa | 56 | } |
TheChrisyd | 0:7d943f226caa | 57 | } |
TheChrisyd | 0:7d943f226caa | 58 | |
TheChrisyd | 0:7d943f226caa | 59 | void circle(int xpos, int ypos, byte a) |
TheChrisyd | 0:7d943f226caa | 60 | { |
TheChrisyd | 0:7d943f226caa | 61 | byte sprnum = 0; |
TheChrisyd | 0:7d943f226caa | 62 | GD.sprite(sprnum++, xpos + 16, ypos + 16, a, 0, 0); |
TheChrisyd | 0:7d943f226caa | 63 | GD.sprite(sprnum++, xpos + 0, ypos + 16, a, 0, 2); |
TheChrisyd | 0:7d943f226caa | 64 | GD.sprite(sprnum++, xpos + 16, ypos + 0, a, 0, 4); |
TheChrisyd | 0:7d943f226caa | 65 | GD.sprite(sprnum++, xpos + 0, ypos + 0, a, 0, 6); |
TheChrisyd | 0:7d943f226caa | 66 | } |
TheChrisyd | 0:7d943f226caa | 67 | |
TheChrisyd | 0:7d943f226caa | 68 | static int bbits() |
TheChrisyd | 0:7d943f226caa | 69 | { |
TheChrisyd | 0:7d943f226caa | 70 | int r = 0; |
TheChrisyd | 0:7d943f226caa | 71 | r |= (up.read() << 0); |
TheChrisyd | 0:7d943f226caa | 72 | r |= (down.read() << 1); |
TheChrisyd | 0:7d943f226caa | 73 | r |= (left.read() << 2); |
TheChrisyd | 0:7d943f226caa | 74 | r |= (right.read() << 3); |
TheChrisyd | 0:7d943f226caa | 75 | r |= (a.read() << 4); |
TheChrisyd | 0:7d943f226caa | 76 | r |= (b.read() << 5); |
TheChrisyd | 0:7d943f226caa | 77 | r |= (c.read() << 6); |
TheChrisyd | 0:7d943f226caa | 78 | r |= (start.read() << 7); |
TheChrisyd | 0:7d943f226caa | 79 | return r; |
TheChrisyd | 0:7d943f226caa | 80 | } |
TheChrisyd | 0:7d943f226caa | 81 | static int xcoord = 0; |
TheChrisyd | 0:7d943f226caa | 82 | static int ycoord = 0; |
TheChrisyd | 0:7d943f226caa | 83 | static int ands = 0x00ff, ors = 0x0000; |
TheChrisyd | 0:7d943f226caa | 84 | |
TheChrisyd | 0:7d943f226caa | 85 | void loop() |
TheChrisyd | 0:7d943f226caa | 86 | { |
TheChrisyd | 0:7d943f226caa | 87 | int bb = bbits(); |
TheChrisyd | 0:7d943f226caa | 88 | |
TheChrisyd | 0:7d943f226caa | 89 | GD.putstr(20, 10, (bb & 0x0001) ? "-" : "U"); |
TheChrisyd | 0:7d943f226caa | 90 | GD.putstr(20, 20, (bb & 0x0002) ? "-" : "D"); |
TheChrisyd | 0:7d943f226caa | 91 | GD.putstr(15, 15, (bb & 0x0004) ? "-" : "L"); |
TheChrisyd | 0:7d943f226caa | 92 | GD.putstr(25, 15, (bb & 0x0008) ? "-" : "R"); |
TheChrisyd | 0:7d943f226caa | 93 | |
TheChrisyd | 0:7d943f226caa | 94 | GD.putstr(35, 25, (bb & 0x0010) ? "-" : "A"); |
TheChrisyd | 0:7d943f226caa | 95 | GD.putstr(40, 25, (bb & 0x0020) ? "-" : "B"); |
TheChrisyd | 0:7d943f226caa | 96 | GD.putstr(45, 25, (bb & 0x0040) ? "-" : "C"); |
TheChrisyd | 0:7d943f226caa | 97 | GD.putstr(35, 15, (bb & 0x0080) ? "- " : "Start"); |
TheChrisyd | 0:7d943f226caa | 98 | |
TheChrisyd | 0:7d943f226caa | 99 | ands &= bb; |
TheChrisyd | 0:7d943f226caa | 100 | ors |= bb; |
TheChrisyd | 0:7d943f226caa | 101 | |
TheChrisyd | 0:7d943f226caa | 102 | if (ands == 0 && ors == 0x00ff) |
TheChrisyd | 0:7d943f226caa | 103 | GD.putstr(20, 24, "BUTTONS OK"); |
TheChrisyd | 0:7d943f226caa | 104 | |
TheChrisyd | 0:7d943f226caa | 105 | xcoord = Ain1 * 255; |
TheChrisyd | 0:7d943f226caa | 106 | ycoord = Ain2 * 255; |
TheChrisyd | 0:7d943f226caa | 107 | char msg[20]; |
TheChrisyd | 0:7d943f226caa | 108 | sprintf(msg, "X=%4d, Y=%4d", xcoord, ycoord); |
TheChrisyd | 0:7d943f226caa | 109 | GD.putstr(0, 36, msg); |
TheChrisyd | 0:7d943f226caa | 110 | |
TheChrisyd | 0:7d943f226caa | 111 | circle(xcoord / 4, 255 - ycoord / 4, (bb & 0x10) ? 15 : 31); |
TheChrisyd | 0:7d943f226caa | 112 | |
TheChrisyd | 0:7d943f226caa | 113 | } |
TheChrisyd | 0:7d943f226caa | 114 | |
TheChrisyd | 0:7d943f226caa | 115 | |
TheChrisyd | 0:7d943f226caa | 116 | int main() |
TheChrisyd | 0:7d943f226caa | 117 | { |
TheChrisyd | 0:7d943f226caa | 118 | setup(); |
TheChrisyd | 0:7d943f226caa | 119 | while(1) { |
TheChrisyd | 0:7d943f226caa | 120 | loop(); |
TheChrisyd | 0:7d943f226caa | 121 | } |
TheChrisyd | 0:7d943f226caa | 122 | } |
TheChrisyd | 0:7d943f226caa | 123 | |
TheChrisyd | 0:7d943f226caa | 124 |