fsfs
Dependencies: 4DGL-uLCD-SE LSM303D mbed
Fork of LSM303D_SPI by
main.cpp@0:684294feee71, 2014-10-21 (annotated)
- Committer:
- shurjo_1234
- Date:
- Tue Oct 21 16:49:53 2014 +0000
- Revision:
- 0:684294feee71
- Child:
- 1:bad5121a35c7
LSM303D Game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shurjo_1234 | 0:684294feee71 | 1 | #include "mbed.h" |
shurjo_1234 | 0:684294feee71 | 2 | #include "LSM303D.h" |
shurjo_1234 | 0:684294feee71 | 3 | |
shurjo_1234 | 0:684294feee71 | 4 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
shurjo_1234 | 0:684294feee71 | 5 | |
shurjo_1234 | 0:684294feee71 | 6 | LSM303D sensor1(spi,p15); |
shurjo_1234 | 0:684294feee71 | 7 | LSM303D sensor2(spi,p16); |
shurjo_1234 | 0:684294feee71 | 8 | LSM303D sensor3(spi,p17); |
shurjo_1234 | 0:684294feee71 | 9 | LSM303D sensor4(spi,p18); |
shurjo_1234 | 0:684294feee71 | 10 | |
shurjo_1234 | 0:684294feee71 | 11 | #include "uLCD_4DGL.h" |
shurjo_1234 | 0:684294feee71 | 12 | |
shurjo_1234 | 0:684294feee71 | 13 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
shurjo_1234 | 0:684294feee71 | 14 | |
shurjo_1234 | 0:684294feee71 | 15 | int main() { |
shurjo_1234 | 0:684294feee71 | 16 | // Chip must be deselected |
shurjo_1234 | 0:684294feee71 | 17 | sensor1.initialize(); |
shurjo_1234 | 0:684294feee71 | 18 | float vx_old = sensor1.magnitometer(2); |
shurjo_1234 | 0:684294feee71 | 19 | |
shurjo_1234 | 0:684294feee71 | 20 | float fx=63.0,fy=2.0,vx= 0.0,vy=0.4; |
shurjo_1234 | 0:684294feee71 | 21 | int x=63,y=2,radius=2; |
shurjo_1234 | 0:684294feee71 | 22 | uLCD.background_color(BLACK); |
shurjo_1234 | 0:684294feee71 | 23 | uLCD.cls(); |
shurjo_1234 | 0:684294feee71 | 24 | |
shurjo_1234 | 0:684294feee71 | 25 | int pts[20]; |
shurjo_1234 | 0:684294feee71 | 26 | for (int level=1; level<=5; level++) |
shurjo_1234 | 0:684294feee71 | 27 | { |
shurjo_1234 | 0:684294feee71 | 28 | if (level == 1) |
shurjo_1234 | 0:684294feee71 | 29 | { |
shurjo_1234 | 0:684294feee71 | 30 | pts[0] = 30; pts[1] = 63; |
shurjo_1234 | 0:684294feee71 | 31 | pts[2] = 96; pts[3] = 63; |
shurjo_1234 | 0:684294feee71 | 32 | } |
shurjo_1234 | 0:684294feee71 | 33 | else if (level == 2) |
shurjo_1234 | 0:684294feee71 | 34 | { |
shurjo_1234 | 0:684294feee71 | 35 | pts[0] = 0; pts[1] = 40; |
shurjo_1234 | 0:684294feee71 | 36 | pts[2] = 50; pts[3] = 40; |
shurjo_1234 | 0:684294feee71 | 37 | |
shurjo_1234 | 0:684294feee71 | 38 | pts[4] = 76; pts[5] = 80; |
shurjo_1234 | 0:684294feee71 | 39 | pts[6] = 126; pts[7] = 80; |
shurjo_1234 | 0:684294feee71 | 40 | |
shurjo_1234 | 0:684294feee71 | 41 | } |
shurjo_1234 | 0:684294feee71 | 42 | else if (level == 3) |
shurjo_1234 | 0:684294feee71 | 43 | { |
shurjo_1234 | 0:684294feee71 | 44 | pts[0] = 0; pts[1] = 30; |
shurjo_1234 | 0:684294feee71 | 45 | pts[2] = 50; pts[3] = 30; |
shurjo_1234 | 0:684294feee71 | 46 | |
shurjo_1234 | 0:684294feee71 | 47 | pts[4] = 76; pts[5] = 60; |
shurjo_1234 | 0:684294feee71 | 48 | pts[6] = 126; pts[7] = 60; |
shurjo_1234 | 0:684294feee71 | 49 | |
shurjo_1234 | 0:684294feee71 | 50 | pts[8] = 76; pts[9] = 90; |
shurjo_1234 | 0:684294feee71 | 51 | pts[10] = 126; pts[11] = 90; |
shurjo_1234 | 0:684294feee71 | 52 | |
shurjo_1234 | 0:684294feee71 | 53 | } |
shurjo_1234 | 0:684294feee71 | 54 | else if (level == 4) |
shurjo_1234 | 0:684294feee71 | 55 | { |
shurjo_1234 | 0:684294feee71 | 56 | pts[0] = 0; pts[1] = 28; |
shurjo_1234 | 0:684294feee71 | 57 | pts[2] = 50; pts[3] = 28; |
shurjo_1234 | 0:684294feee71 | 58 | |
shurjo_1234 | 0:684294feee71 | 59 | pts[4] = 76; pts[5] = 56; |
shurjo_1234 | 0:684294feee71 | 60 | pts[6] = 126; pts[7] = 56; |
shurjo_1234 | 0:684294feee71 | 61 | |
shurjo_1234 | 0:684294feee71 | 62 | pts[8] = 76; pts[9] = 84; |
shurjo_1234 | 0:684294feee71 | 63 | pts[10] = 126; pts[11] = 84; |
shurjo_1234 | 0:684294feee71 | 64 | |
shurjo_1234 | 0:684294feee71 | 65 | pts[12] = 76; pts[13] = 112; |
shurjo_1234 | 0:684294feee71 | 66 | pts[14] = 126; pts[15] = 112; |
shurjo_1234 | 0:684294feee71 | 67 | } |
shurjo_1234 | 0:684294feee71 | 68 | else |
shurjo_1234 | 0:684294feee71 | 69 | { |
shurjo_1234 | 0:684294feee71 | 70 | pts[0] = 0; pts[1] = 20; |
shurjo_1234 | 0:684294feee71 | 71 | pts[2] = 50; pts[3] = 20; |
shurjo_1234 | 0:684294feee71 | 72 | |
shurjo_1234 | 0:684294feee71 | 73 | pts[4] = 76; pts[5] = 40; |
shurjo_1234 | 0:684294feee71 | 74 | pts[6] = 126; pts[7] = 40; |
shurjo_1234 | 0:684294feee71 | 75 | |
shurjo_1234 | 0:684294feee71 | 76 | pts[8] = 76; pts[9] = 60; |
shurjo_1234 | 0:684294feee71 | 77 | pts[10] = 126; pts[11] = 60; |
shurjo_1234 | 0:684294feee71 | 78 | |
shurjo_1234 | 0:684294feee71 | 79 | pts[12] = 76; pts[13] = 80; |
shurjo_1234 | 0:684294feee71 | 80 | pts[14] = 126; pts[15] = 80; |
shurjo_1234 | 0:684294feee71 | 81 | |
shurjo_1234 | 0:684294feee71 | 82 | pts[16] = 76; pts[17] = 100; |
shurjo_1234 | 0:684294feee71 | 83 | pts[18] = 126; pts[19] = 100; |
shurjo_1234 | 0:684294feee71 | 84 | } |
shurjo_1234 | 0:684294feee71 | 85 | |
shurjo_1234 | 0:684294feee71 | 86 | uLCD.cls(); |
shurjo_1234 | 0:684294feee71 | 87 | for (int i=0; i<level; i++) |
shurjo_1234 | 0:684294feee71 | 88 | { |
shurjo_1234 | 0:684294feee71 | 89 | uLCD.line(pts[4*i], pts[4*i+1], pts[4*i+2], pts[4*i+3], WHITE); |
shurjo_1234 | 0:684294feee71 | 90 | } |
shurjo_1234 | 0:684294feee71 | 91 | |
shurjo_1234 | 0:684294feee71 | 92 | y = 2; |
shurjo_1234 | 0:684294feee71 | 93 | fy = 2; |
shurjo_1234 | 0:684294feee71 | 94 | |
shurjo_1234 | 0:684294feee71 | 95 | while (y<126) |
shurjo_1234 | 0:684294feee71 | 96 | { |
shurjo_1234 | 0:684294feee71 | 97 | vx = (sensor1.magnitometer(2)-vx_old)/100; |
shurjo_1234 | 0:684294feee71 | 98 | //draw ball |
shurjo_1234 | 0:684294feee71 | 99 | uLCD.filled_circle(x, y, radius, RED); |
shurjo_1234 | 0:684294feee71 | 100 | //bounce off edge walls and slow down a bit? |
shurjo_1234 | 0:684294feee71 | 101 | |
shurjo_1234 | 0:684294feee71 | 102 | for (int i=0; i<level; i++) |
shurjo_1234 | 0:684294feee71 | 103 | { |
shurjo_1234 | 0:684294feee71 | 104 | if (x>pts[4*i] && x<pts[4*i+2] && y>pts[4*i+1]-1 && y<pts[4*i+3]+1) |
shurjo_1234 | 0:684294feee71 | 105 | { |
shurjo_1234 | 0:684294feee71 | 106 | uLCD.cls(); |
shurjo_1234 | 0:684294feee71 | 107 | uLCD.text_width(4); //4X size text |
shurjo_1234 | 0:684294feee71 | 108 | uLCD.text_height(4); |
shurjo_1234 | 0:684294feee71 | 109 | uLCD.color(RED); |
shurjo_1234 | 0:684294feee71 | 110 | uLCD.locate(0,0); |
shurjo_1234 | 0:684294feee71 | 111 | uLCD.printf("GAME OVER"); |
shurjo_1234 | 0:684294feee71 | 112 | return 0; |
shurjo_1234 | 0:684294feee71 | 113 | } |
shurjo_1234 | 0:684294feee71 | 114 | } |
shurjo_1234 | 0:684294feee71 | 115 | |
shurjo_1234 | 0:684294feee71 | 116 | uLCD.filled_circle(x, y, radius, BLACK); |
shurjo_1234 | 0:684294feee71 | 117 | //move ball |
shurjo_1234 | 0:684294feee71 | 118 | fx=fx+vx; |
shurjo_1234 | 0:684294feee71 | 119 | fy=fy+vy; |
shurjo_1234 | 0:684294feee71 | 120 | x=(int)fx; |
shurjo_1234 | 0:684294feee71 | 121 | y=(int)fy; |
shurjo_1234 | 0:684294feee71 | 122 | if (x<0) |
shurjo_1234 | 0:684294feee71 | 123 | { |
shurjo_1234 | 0:684294feee71 | 124 | x += 126; |
shurjo_1234 | 0:684294feee71 | 125 | fx = x; |
shurjo_1234 | 0:684294feee71 | 126 | } |
shurjo_1234 | 0:684294feee71 | 127 | if (x>126) |
shurjo_1234 | 0:684294feee71 | 128 | { |
shurjo_1234 | 0:684294feee71 | 129 | x -= 126; |
shurjo_1234 | 0:684294feee71 | 130 | fx = x; |
shurjo_1234 | 0:684294feee71 | 131 | } |
shurjo_1234 | 0:684294feee71 | 132 | |
shurjo_1234 | 0:684294feee71 | 133 | } |
shurjo_1234 | 0:684294feee71 | 134 | uLCD.cls(); |
shurjo_1234 | 0:684294feee71 | 135 | uLCD.text_width(4); //4X size text |
shurjo_1234 | 0:684294feee71 | 136 | uLCD.text_height(4); |
shurjo_1234 | 0:684294feee71 | 137 | uLCD.color(RED); |
shurjo_1234 | 0:684294feee71 | 138 | uLCD.locate(1,1); |
shurjo_1234 | 0:684294feee71 | 139 | |
shurjo_1234 | 0:684294feee71 | 140 | if (level <= 4) |
shurjo_1234 | 0:684294feee71 | 141 | uLCD.printf("%2D",level+1); |
shurjo_1234 | 0:684294feee71 | 142 | else |
shurjo_1234 | 0:684294feee71 | 143 | { |
shurjo_1234 | 0:684294feee71 | 144 | uLCD.locate(1,1); |
shurjo_1234 | 0:684294feee71 | 145 | uLCD.printf("YOU WIN"); |
shurjo_1234 | 0:684294feee71 | 146 | } |
shurjo_1234 | 0:684294feee71 | 147 | wait(1); |
shurjo_1234 | 0:684294feee71 | 148 | |
shurjo_1234 | 0:684294feee71 | 149 | } |
shurjo_1234 | 0:684294feee71 | 150 | |
shurjo_1234 | 0:684294feee71 | 151 | |
shurjo_1234 | 0:684294feee71 | 152 | |
shurjo_1234 | 0:684294feee71 | 153 | } |