![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Projekat: Labirint Anesa Drakovac Emir Mujačić
Dependencies: MMA8451Q N5110 TSI mbed
main.cpp@0:347c6091fbff, 2014-06-09 (annotated)
- Committer:
- tim008
- Date:
- Mon Jun 09 08:04:06 2014 +0000
- Revision:
- 0:347c6091fbff
Projekat: Labirint; Anesa Drakovac; Emir Muja?i?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim008 | 0:347c6091fbff | 1 | #include "mbed.h" |
tim008 | 0:347c6091fbff | 2 | #include "N5110.h" |
tim008 | 0:347c6091fbff | 3 | #include "MMA8451Q.h" |
tim008 | 0:347c6091fbff | 4 | #include "TSISensor.h" |
tim008 | 0:347c6091fbff | 5 | |
tim008 | 0:347c6091fbff | 6 | #define MAXWIDTH 84 |
tim008 | 0:347c6091fbff | 7 | #define MAXHEIGHT 48 |
tim008 | 0:347c6091fbff | 8 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
tim008 | 0:347c6091fbff | 9 | |
tim008 | 0:347c6091fbff | 10 | Timer deb; |
tim008 | 0:347c6091fbff | 11 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
tim008 | 0:347c6091fbff | 12 | N5110 lcd (PTD0, PTA13, PTA12, PTD5, PTC6, PTC5, PTD4); |
tim008 | 0:347c6091fbff | 13 | // NC u PTC7 u N5110.cpp |
tim008 | 0:347c6091fbff | 14 | // SDA SCL Address |
tim008 | 0:347c6091fbff | 15 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
tim008 | 0:347c6091fbff | 16 | |
tim008 | 0:347c6091fbff | 17 | TSISensor tsi; |
tim008 | 0:347c6091fbff | 18 | Ticker t; |
tim008 | 0:347c6091fbff | 19 | int X=0; |
tim008 | 0:347c6091fbff | 20 | int Y=0; |
tim008 | 0:347c6091fbff | 21 | |
tim008 | 0:347c6091fbff | 22 | bool Sredina=true, Gore=false, Desno=false; |
tim008 | 0:347c6091fbff | 23 | int BrzinaX=0, BrzinaY=0; |
tim008 | 0:347c6091fbff | 24 | |
tim008 | 0:347c6091fbff | 25 | void mapa() |
tim008 | 0:347c6091fbff | 26 | { |
tim008 | 0:347c6091fbff | 27 | for(int i=0; i<84;i++) |
tim008 | 0:347c6091fbff | 28 | for(int j=0; j<40;j++) |
tim008 | 0:347c6091fbff | 29 | { |
tim008 | 0:347c6091fbff | 30 | if(i==0 || i==83) lcd.setPixel(i,j); |
tim008 | 0:347c6091fbff | 31 | if(j==0 || j==39) lcd.setPixel(i,j); |
tim008 | 0:347c6091fbff | 32 | } |
tim008 | 0:347c6091fbff | 33 | int y=5, i=0, x; |
tim008 | 0:347c6091fbff | 34 | for(int i=5; i<79;i++) if(i<67 || i>70) lcd.setPixel(i,y); //1 |
tim008 | 0:347c6091fbff | 35 | y=10; |
tim008 | 0:347c6091fbff | 36 | for(int i=5; i<79;i++) if(i<23 || i>26) lcd.setPixel(i,y); //2 |
tim008 | 0:347c6091fbff | 37 | y=15; |
tim008 | 0:347c6091fbff | 38 | for(int i=5; i<79;i++) lcd.setPixel(i,y); //3 |
tim008 | 0:347c6091fbff | 39 | y=19; |
tim008 | 0:347c6091fbff | 40 | for(int i=5; i<79; i++) if(i<14 || i>17) lcd.setPixel(i,y); //4 |
tim008 | 0:347c6091fbff | 41 | y=24; |
tim008 | 0:347c6091fbff | 42 | for(int i=5; i<79; i++) if(i<32 || i>35) lcd.setPixel(i,y); //5 |
tim008 | 0:347c6091fbff | 43 | y=29; |
tim008 | 0:347c6091fbff | 44 | for(int i=5; i<79; i++) if(i<56 || i>59) lcd.setPixel(i,y); //6 |
tim008 | 0:347c6091fbff | 45 | y=34; |
tim008 | 0:347c6091fbff | 46 | for(int i=5; i<84; i++) if(i<10 || i>13) lcd.setPixel(i,y); //7 |
tim008 | 0:347c6091fbff | 47 | x=5; |
tim008 | 0:347c6091fbff | 48 | for(int i=5; i<34; i++) lcd.setPixel(x,i); |
tim008 | 0:347c6091fbff | 49 | x=78; |
tim008 | 0:347c6091fbff | 50 | for(int i=5; i<34; i++) lcd.setPixel(x,i); |
tim008 | 0:347c6091fbff | 51 | lcd.refresh(); |
tim008 | 0:347c6091fbff | 52 | lcd.printString("Nivo:1 Pauza",0,5); |
tim008 | 0:347c6091fbff | 53 | } |
tim008 | 0:347c6091fbff | 54 | |
tim008 | 0:347c6091fbff | 55 | void cilj() |
tim008 | 0:347c6091fbff | 56 | { |
tim008 | 0:347c6091fbff | 57 | for(int i=24; i<28; i++) |
tim008 | 0:347c6091fbff | 58 | for (int j=16; j<19; j++) |
tim008 | 0:347c6091fbff | 59 | lcd.setPixel(i,j); |
tim008 | 0:347c6091fbff | 60 | |
tim008 | 0:347c6091fbff | 61 | lcd.refresh(); |
tim008 | 0:347c6091fbff | 62 | |
tim008 | 0:347c6091fbff | 63 | } |
tim008 | 0:347c6091fbff | 64 | |
tim008 | 0:347c6091fbff | 65 | void cursor(int x, int y) |
tim008 | 0:347c6091fbff | 66 | { |
tim008 | 0:347c6091fbff | 67 | for(int i=x; i<x+3; i++) |
tim008 | 0:347c6091fbff | 68 | for (int j=y; j<y+3; j++) |
tim008 | 0:347c6091fbff | 69 | lcd.setPixel(i,j); |
tim008 | 0:347c6091fbff | 70 | |
tim008 | 0:347c6091fbff | 71 | lcd.refresh(); |
tim008 | 0:347c6091fbff | 72 | |
tim008 | 0:347c6091fbff | 73 | } |
tim008 | 0:347c6091fbff | 74 | |
tim008 | 0:347c6091fbff | 75 | void cursor_main_menua(int x, int y) |
tim008 | 0:347c6091fbff | 76 | { |
tim008 | 0:347c6091fbff | 77 | for(int i=x; i<x+10; i++) |
tim008 | 0:347c6091fbff | 78 | for(int j=y; j<y+5; j++) |
tim008 | 0:347c6091fbff | 79 | { |
tim008 | 0:347c6091fbff | 80 | lcd.setPixel(i,j); |
tim008 | 0:347c6091fbff | 81 | |
tim008 | 0:347c6091fbff | 82 | } |
tim008 | 0:347c6091fbff | 83 | lcd.refresh(); |
tim008 | 0:347c6091fbff | 84 | } |
tim008 | 0:347c6091fbff | 85 | |
tim008 | 0:347c6091fbff | 86 | void cursor_nivoi(int x, int y) |
tim008 | 0:347c6091fbff | 87 | { |
tim008 | 0:347c6091fbff | 88 | for(int i=x; i<x+14; i++) |
tim008 | 0:347c6091fbff | 89 | for(int j=y; j<y+5; j++) |
tim008 | 0:347c6091fbff | 90 | { |
tim008 | 0:347c6091fbff | 91 | if(lcd.getPixel(i,j)==0) lcd.setPixel(i,j); |
tim008 | 0:347c6091fbff | 92 | if(lcd.getPixel(i,j)==1) lcd.clearPixel(i,j); |
tim008 | 0:347c6091fbff | 93 | } |
tim008 | 0:347c6091fbff | 94 | lcd.refresh(); |
tim008 | 0:347c6091fbff | 95 | } |
tim008 | 0:347c6091fbff | 96 | |
tim008 | 0:347c6091fbff | 97 | void brisi(int x, int y) |
tim008 | 0:347c6091fbff | 98 | { |
tim008 | 0:347c6091fbff | 99 | for(int i=x; i<x+3; i++) |
tim008 | 0:347c6091fbff | 100 | for (int j=y; j<y+3; j++) |
tim008 | 0:347c6091fbff | 101 | lcd.clearPixel(i,j); |
tim008 | 0:347c6091fbff | 102 | lcd.refresh(); |
tim008 | 0:347c6091fbff | 103 | } |
tim008 | 0:347c6091fbff | 104 | |
tim008 | 0:347c6091fbff | 105 | void brzina(int n) |
tim008 | 0:347c6091fbff | 106 | { |
tim008 | 0:347c6091fbff | 107 | switch(n) |
tim008 | 0:347c6091fbff | 108 | { |
tim008 | 0:347c6091fbff | 109 | case 0:{ wait(0.07); break;} |
tim008 | 0:347c6091fbff | 110 | case 1:{ wait(0.06); break;} |
tim008 | 0:347c6091fbff | 111 | case 2:{ wait(0.05); break;} |
tim008 | 0:347c6091fbff | 112 | case 3:{ wait(0.04); break;} |
tim008 | 0:347c6091fbff | 113 | case 4:{ wait(0.03); break;} |
tim008 | 0:347c6091fbff | 114 | case 5:{ wait(0.02); break;} |
tim008 | 0:347c6091fbff | 115 | } |
tim008 | 0:347c6091fbff | 116 | } |
tim008 | 0:347c6091fbff | 117 | |
tim008 | 0:347c6091fbff | 118 | void KLijevo(int &x, int &y) |
tim008 | 0:347c6091fbff | 119 | { |
tim008 | 0:347c6091fbff | 120 | if(x>1 && lcd.getPixel(x-1,y)==0 && lcd.getPixel(x-1,y+1)==0 && lcd.getPixel(x-1,y+2)==0 ){ |
tim008 | 0:347c6091fbff | 121 | brisi(x,y); |
tim008 | 0:347c6091fbff | 122 | x=x-1; |
tim008 | 0:347c6091fbff | 123 | cursor(x,y); |
tim008 | 0:347c6091fbff | 124 | brzina(BrzinaX); |
tim008 | 0:347c6091fbff | 125 | } |
tim008 | 0:347c6091fbff | 126 | } |
tim008 | 0:347c6091fbff | 127 | |
tim008 | 0:347c6091fbff | 128 | void KDesno(int &x, int &y) |
tim008 | 0:347c6091fbff | 129 | { |
tim008 | 0:347c6091fbff | 130 | if(x<80 && lcd.getPixel(x+3,y) ==0 && lcd.getPixel(x+3,y+1) ==0 && lcd.getPixel(x+3,y+2) ==0 || (x+3)==24 || (x+3)==25 || (x+3)==26){ |
tim008 | 0:347c6091fbff | 131 | brisi(x,y); |
tim008 | 0:347c6091fbff | 132 | x=x+1; |
tim008 | 0:347c6091fbff | 133 | cursor(x,y); |
tim008 | 0:347c6091fbff | 134 | brzina(BrzinaX); |
tim008 | 0:347c6091fbff | 135 | } |
tim008 | 0:347c6091fbff | 136 | } |
tim008 | 0:347c6091fbff | 137 | |
tim008 | 0:347c6091fbff | 138 | void KGore(int &x, int &y) |
tim008 | 0:347c6091fbff | 139 | { |
tim008 | 0:347c6091fbff | 140 | if(y>1 && lcd.getPixel(x,y-1)==0 && lcd.getPixel(x+1,y-1)==0 && lcd.getPixel(x+2,y-1)==0){ |
tim008 | 0:347c6091fbff | 141 | brisi(x,y); |
tim008 | 0:347c6091fbff | 142 | y=y-1; |
tim008 | 0:347c6091fbff | 143 | cursor(x,y); |
tim008 | 0:347c6091fbff | 144 | brzina(BrzinaY); |
tim008 | 0:347c6091fbff | 145 | } |
tim008 | 0:347c6091fbff | 146 | } |
tim008 | 0:347c6091fbff | 147 | |
tim008 | 0:347c6091fbff | 148 | void KDolje(int &x, int &y) |
tim008 | 0:347c6091fbff | 149 | { |
tim008 | 0:347c6091fbff | 150 | if(y<36 && lcd.getPixel(x,y+3)==0 && lcd.getPixel(x+1,y+3)==0 && lcd.getPixel(x+2,y+3)==0){ |
tim008 | 0:347c6091fbff | 151 | brisi(x,y); |
tim008 | 0:347c6091fbff | 152 | y=y+1; |
tim008 | 0:347c6091fbff | 153 | cursor(x,y); |
tim008 | 0:347c6091fbff | 154 | brzina(BrzinaY); |
tim008 | 0:347c6091fbff | 155 | } |
tim008 | 0:347c6091fbff | 156 | } |
tim008 | 0:347c6091fbff | 157 | |
tim008 | 0:347c6091fbff | 158 | void SDesno() |
tim008 | 0:347c6091fbff | 159 | { |
tim008 | 0:347c6091fbff | 160 | if(Sredina || Desno) |
tim008 | 0:347c6091fbff | 161 | { |
tim008 | 0:347c6091fbff | 162 | Desno = true ; Sredina=false; |
tim008 | 0:347c6091fbff | 163 | if(BrzinaX<5) BrzinaX++; |
tim008 | 0:347c6091fbff | 164 | KDesno(X,Y); |
tim008 | 0:347c6091fbff | 165 | } |
tim008 | 0:347c6091fbff | 166 | if(!Sredina && !Desno) |
tim008 | 0:347c6091fbff | 167 | { |
tim008 | 0:347c6091fbff | 168 | if(BrzinaX>0) BrzinaX--; |
tim008 | 0:347c6091fbff | 169 | if(BrzinaX==0) Sredina=true; |
tim008 | 0:347c6091fbff | 170 | KLijevo(X,Y); |
tim008 | 0:347c6091fbff | 171 | } |
tim008 | 0:347c6091fbff | 172 | } |
tim008 | 0:347c6091fbff | 173 | |
tim008 | 0:347c6091fbff | 174 | void SLijevo() |
tim008 | 0:347c6091fbff | 175 | { |
tim008 | 0:347c6091fbff | 176 | if(Sredina || !Desno) {Desno = false ; Sredina=false; if(BrzinaX<5) BrzinaX++;KLijevo(X,Y); } |
tim008 | 0:347c6091fbff | 177 | if(!Sredina && Desno) |
tim008 | 0:347c6091fbff | 178 | { |
tim008 | 0:347c6091fbff | 179 | if(BrzinaX>0) BrzinaX--; |
tim008 | 0:347c6091fbff | 180 | if(BrzinaX==0) Sredina=true; |
tim008 | 0:347c6091fbff | 181 | KDesno(X,Y); |
tim008 | 0:347c6091fbff | 182 | } |
tim008 | 0:347c6091fbff | 183 | } |
tim008 | 0:347c6091fbff | 184 | |
tim008 | 0:347c6091fbff | 185 | void SGore() |
tim008 | 0:347c6091fbff | 186 | { |
tim008 | 0:347c6091fbff | 187 | if(Sredina || Gore) {Gore = true ; Sredina=false; if(BrzinaY<5) BrzinaY++; KGore(X,Y);} |
tim008 | 0:347c6091fbff | 188 | if(!Sredina && !Gore) |
tim008 | 0:347c6091fbff | 189 | { |
tim008 | 0:347c6091fbff | 190 | if(BrzinaY>0) BrzinaY--; |
tim008 | 0:347c6091fbff | 191 | if(BrzinaY==0) Sredina=true; |
tim008 | 0:347c6091fbff | 192 | KDolje(X,Y); |
tim008 | 0:347c6091fbff | 193 | } |
tim008 | 0:347c6091fbff | 194 | } |
tim008 | 0:347c6091fbff | 195 | |
tim008 | 0:347c6091fbff | 196 | void SDole() |
tim008 | 0:347c6091fbff | 197 | { |
tim008 | 0:347c6091fbff | 198 | if(Sredina || !Gore) {Gore = false ; Sredina=false; if(BrzinaY<5) BrzinaY++; KDolje(X,Y);} |
tim008 | 0:347c6091fbff | 199 | if(!Sredina && Gore) |
tim008 | 0:347c6091fbff | 200 | |
tim008 | 0:347c6091fbff | 201 | { |
tim008 | 0:347c6091fbff | 202 | if(BrzinaY>0) BrzinaY--; |
tim008 | 0:347c6091fbff | 203 | if(BrzinaY==0) Sredina=true; |
tim008 | 0:347c6091fbff | 204 | KGore(X,Y); |
tim008 | 0:347c6091fbff | 205 | } |
tim008 | 0:347c6091fbff | 206 | } |
tim008 | 0:347c6091fbff | 207 | |
tim008 | 0:347c6091fbff | 208 | void kretanje() |
tim008 | 0:347c6091fbff | 209 | { |
tim008 | 0:347c6091fbff | 210 | |
tim008 | 0:347c6091fbff | 211 | float x, y; |
tim008 | 0:347c6091fbff | 212 | x = (acc.getAccX()); |
tim008 | 0:347c6091fbff | 213 | y = (acc.getAccY()); |
tim008 | 0:347c6091fbff | 214 | if(y<-0.5) SLijevo(); |
tim008 | 0:347c6091fbff | 215 | if(y>0.5) SDesno(); |
tim008 | 0:347c6091fbff | 216 | if(x<-0.5) SGore(); |
tim008 | 0:347c6091fbff | 217 | if(x>0.5) SDole(); |
tim008 | 0:347c6091fbff | 218 | if((x<0.5 && x>-0.5) || (y<0.5 && y>-0.5)) Sredina=true; |
tim008 | 0:347c6091fbff | 219 | |
tim008 | 0:347c6091fbff | 220 | } |
tim008 | 0:347c6091fbff | 221 | |
tim008 | 0:347c6091fbff | 222 | void kreni() |
tim008 | 0:347c6091fbff | 223 | { |
tim008 | 0:347c6091fbff | 224 | int n=0; |
tim008 | 0:347c6091fbff | 225 | lcd.clear(); |
tim008 | 0:347c6091fbff | 226 | X=1; Y=1; |
tim008 | 0:347c6091fbff | 227 | cursor(X,Y); |
tim008 | 0:347c6091fbff | 228 | mapa(); |
tim008 | 0:347c6091fbff | 229 | cilj(); |
tim008 | 0:347c6091fbff | 230 | int a=0; |
tim008 | 0:347c6091fbff | 231 | while(a==0) |
tim008 | 0:347c6091fbff | 232 | { |
tim008 | 0:347c6091fbff | 233 | kretanje(); |
tim008 | 0:347c6091fbff | 234 | if(X==24 && Y==16) |
tim008 | 0:347c6091fbff | 235 | { |
tim008 | 0:347c6091fbff | 236 | lcd.clear(); |
tim008 | 0:347c6091fbff | 237 | lcd.printString("Pobjeda", 20,3); |
tim008 | 0:347c6091fbff | 238 | wait(3); |
tim008 | 0:347c6091fbff | 239 | lcd.clear(); |
tim008 | 0:347c6091fbff | 240 | a=1; |
tim008 | 0:347c6091fbff | 241 | } |
tim008 | 0:347c6091fbff | 242 | if(tsi.readPercentage()>0.6) n=1; |
tim008 | 0:347c6091fbff | 243 | while(n==1) |
tim008 | 0:347c6091fbff | 244 | { |
tim008 | 0:347c6091fbff | 245 | wait(0.001); |
tim008 | 0:347c6091fbff | 246 | if(tsi.readPercentage()>0.4) n=2; |
tim008 | 0:347c6091fbff | 247 | } |
tim008 | 0:347c6091fbff | 248 | } |
tim008 | 0:347c6091fbff | 249 | } |
tim008 | 0:347c6091fbff | 250 | |
tim008 | 0:347c6091fbff | 251 | void menu() |
tim008 | 0:347c6091fbff | 252 | { |
tim008 | 0:347c6091fbff | 253 | int n=1; |
tim008 | 0:347c6091fbff | 254 | lcd.printString("New Game", 5,1); |
tim008 | 0:347c6091fbff | 255 | lcd.printString("Level",5,2); |
tim008 | 0:347c6091fbff | 256 | cursor_main_menua(54, 10); |
tim008 | 0:347c6091fbff | 257 | lcd.refresh(); |
tim008 | 0:347c6091fbff | 258 | while(1) |
tim008 | 0:347c6091fbff | 259 | { |
tim008 | 0:347c6091fbff | 260 | |
tim008 | 0:347c6091fbff | 261 | float x; |
tim008 | 0:347c6091fbff | 262 | x = (acc.getAccX()); |
tim008 | 0:347c6091fbff | 263 | if(x<-0.5) n=1; |
tim008 | 0:347c6091fbff | 264 | if(x>0.5) n=2; |
tim008 | 0:347c6091fbff | 265 | if(n==2) |
tim008 | 0:347c6091fbff | 266 | { |
tim008 | 0:347c6091fbff | 267 | lcd.clear(); |
tim008 | 0:347c6091fbff | 268 | lcd.printString("New Game", 5,1); |
tim008 | 0:347c6091fbff | 269 | lcd.printString("Level",5,2); |
tim008 | 0:347c6091fbff | 270 | cursor_main_menua(54, 17); |
tim008 | 0:347c6091fbff | 271 | |
tim008 | 0:347c6091fbff | 272 | } |
tim008 | 0:347c6091fbff | 273 | if(n==1) |
tim008 | 0:347c6091fbff | 274 | { |
tim008 | 0:347c6091fbff | 275 | lcd.clear(); |
tim008 | 0:347c6091fbff | 276 | lcd.printString("New Game", 5,1); |
tim008 | 0:347c6091fbff | 277 | lcd.printString("Level",5,2); |
tim008 | 0:347c6091fbff | 278 | cursor_main_menua(54, 10); |
tim008 | 0:347c6091fbff | 279 | |
tim008 | 0:347c6091fbff | 280 | } |
tim008 | 0:347c6091fbff | 281 | if(tsi.readPercentage()>0.0) |
tim008 | 0:347c6091fbff | 282 | { |
tim008 | 0:347c6091fbff | 283 | if(n==1) |
tim008 | 0:347c6091fbff | 284 | { |
tim008 | 0:347c6091fbff | 285 | kreni(); |
tim008 | 0:347c6091fbff | 286 | } |
tim008 | 0:347c6091fbff | 287 | if(n==2) |
tim008 | 0:347c6091fbff | 288 | { |
tim008 | 0:347c6091fbff | 289 | |
tim008 | 0:347c6091fbff | 290 | lcd.clear(); |
tim008 | 0:347c6091fbff | 291 | lcd.printString("Level 1", 5,1); |
tim008 | 0:347c6091fbff | 292 | lcd.printString("Touch to", 5,3); |
tim008 | 0:347c6091fbff | 293 | lcd.printString("START", 5,4); |
tim008 | 0:347c6091fbff | 294 | wait(1); |
tim008 | 0:347c6091fbff | 295 | while(1) |
tim008 | 0:347c6091fbff | 296 | if(tsi.readPercentage()>0.0) kreni(); |
tim008 | 0:347c6091fbff | 297 | } |
tim008 | 0:347c6091fbff | 298 | } |
tim008 | 0:347c6091fbff | 299 | } |
tim008 | 0:347c6091fbff | 300 | } |
tim008 | 0:347c6091fbff | 301 | |
tim008 | 0:347c6091fbff | 302 | int main(){ |
tim008 | 0:347c6091fbff | 303 | deb.start(); |
tim008 | 0:347c6091fbff | 304 | lcd.init(); |
tim008 | 0:347c6091fbff | 305 | lcd.setXYAddress(X,Y); |
tim008 | 0:347c6091fbff | 306 | lcd.setBrightness(0.0); |
tim008 | 0:347c6091fbff | 307 | |
tim008 | 0:347c6091fbff | 308 | menu(); |
tim008 | 0:347c6091fbff | 309 | while(1){ |
tim008 | 0:347c6091fbff | 310 | } |
tim008 | 0:347c6091fbff | 311 | } |