final, working project

Dependencies:   C12832 EthernetInterface HTTPClient Speaker USBDevice mbed-rtos mbed

Committer:
RiiQQe
Date:
Fri Feb 12 13:12:58 2016 +0000
Revision:
3:2f7b22ffcf23
Parent:
1:795e03f7afa8
mergefix;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RiiQQe 0:b2126965ca21 1 #include "mbed.h"
RiiQQe 0:b2126965ca21 2 #include "HTTPClient.h"
RiiQQe 0:b2126965ca21 3 #include "C12832.h"
RiiQQe 0:b2126965ca21 4 #include "EthernetInterface.h"
RiiQQe 0:b2126965ca21 5 #include <string>
RiiQQe 0:b2126965ca21 6 #include <stdlib.h> /* srand, rand */
RiiQQe 0:b2126965ca21 7 #include "rtos.h"
RiiQQe 0:b2126965ca21 8 #include <sstream>
RiiQQe 0:b2126965ca21 9 #include "USBAudio.h"
RiiQQe 0:b2126965ca21 10 #include "Speaker.h"
RiiQQe 0:b2126965ca21 11
RiiQQe 0:b2126965ca21 12 /*
RiiQQe 0:b2126965ca21 13 Account: mbedTokyoTech
RiiQQe 0:b2126965ca21 14 Psswd: kalle123
RiiQQe 0:b2126965ca21 15 Email: ricli877@student.liu.se
RiiQQe 0:b2126965ca21 16
RiiQQe 0:b2126965ca21 17 $consumerKey = 'XHO3e2fiS44d5i7xc6mvtvDdZ';
RiiQQe 0:b2126965ca21 18 $consumerSecret = '9U4WOjRnpM5Pj0Qv4f6406jTRfN65YY5GeE5PRXHuhPfSiWhEN';
RiiQQe 0:b2126965ca21 19 $accessToken = '4761221959-n4ojXEYR4DZSA4eeqt1WetVHHrZWTUNLJJ9ql3o';
RiiQQe 0:b2126965ca21 20 $accessTokenSecret = 'SXM05FPIW9SUgTYYgpk4buPcypwXIsLDIY3TRns01WRJP';
RiiQQe 0:b2126965ca21 21 */
RiiQQe 0:b2126965ca21 22
RiiQQe 0:b2126965ca21 23 Serial pc(USBTX, USBRX);
RiiQQe 0:b2126965ca21 24
RiiQQe 0:b2126965ca21 25 DigitalIn button(p14);
RiiQQe 0:b2126965ca21 26
RiiQQe 0:b2126965ca21 27 EthernetInterface eth;
RiiQQe 0:b2126965ca21 28
RiiQQe 0:b2126965ca21 29 C12832 lcd(p5, p7, p6, p8, p11);
RiiQQe 0:b2126965ca21 30
RiiQQe 0:b2126965ca21 31 HTTPClient http;
RiiQQe 0:b2126965ca21 32
RiiQQe 0:b2126965ca21 33 BusIn joy(p15,p12,p13,p16);
RiiQQe 0:b2126965ca21 34
RiiQQe 0:b2126965ca21 35 Mutex locker;
RiiQQe 0:b2126965ca21 36
RiiQQe 0:b2126965ca21 37 Speaker speaker2(p26);
RiiQQe 0:b2126965ca21 38
RiiQQe 0:b2126965ca21 39 // sting buffers for tweeting
RiiQQe 0:b2126965ca21 40 char str[512];
RiiQQe 0:b2126965ca21 41 char buf[512];
RiiQQe 0:b2126965ca21 42 //Enemy positions
RiiQQe 0:b2126965ca21 43 int enX[17] = {75,37,19,93,59,21,80,65,27,45,47,49,54,94,62,104,28};
RiiQQe 0:b2126965ca21 44 int enY[19] = {30,16,25,5,15,22,13,7,6,23,24,2,28,21,20,14,17,19,3};
RiiQQe 0:b2126965ca21 45 //Food positions
RiiQQe 0:b2126965ca21 46 int foodX[23] = {2,38,105,109,99,30,97,26,5,40,121,24,101,69,84,48,34,9,118,29,25,70,1};
RiiQQe 0:b2126965ca21 47 int foodY[13] = {11,29,2,23,6,24,28,5,1,27,9,7,13};
RiiQQe 0:b2126965ca21 48
RiiQQe 0:b2126965ca21 49 //Stuff for the snakegame + joystick
RiiQQe 0:b2126965ca21 50 int score = 0;
RiiQQe 0:b2126965ca21 51 int s2 = 0;
RiiQQe 0:b2126965ca21 52 int xPos = 50, yPos = 10;
RiiQQe 0:b2126965ca21 53 int xDim = 128, yDim = 32;
RiiQQe 0:b2126965ca21 54 int step = 4;
RiiQQe 0:b2126965ca21 55 int xFood = foodX[0], yFood = foodY[0], xKiller=enX[0], yKiller=enY[0];
RiiQQe 0:b2126965ca21 56 int foodIndex = 1;
RiiQQe 0:b2126965ca21 57 int enemyIndex = 1;
RiiQQe 0:b2126965ca21 58
RiiQQe 0:b2126965ca21 59 bool gameOver = false;
RiiQQe 0:b2126965ca21 60 bool gameOverScreen = false;
RiiQQe 0:b2126965ca21 61
RiiQQe 0:b2126965ca21 62 int k = 0;
RiiQQe 0:b2126965ca21 63
RiiQQe 0:b2126965ca21 64 int i = 0, j = 0;
RiiQQe 0:b2126965ca21 65
RiiQQe 0:b2126965ca21 66 bool hit2 = false;
RiiQQe 0:b2126965ca21 67
RiiQQe 0:b2126965ca21 68 //Function that checks if the food was eaten, returns true if it was.
RiiQQe 0:b2126965ca21 69 bool hitFood(){
RiiQQe 0:b2126965ca21 70 if(abs(xFood - i) < 5 && abs(yFood - j) < 5) {
RiiQQe 0:b2126965ca21 71 score = score + 1;
RiiQQe 0:b2126965ca21 72 return true;
RiiQQe 0:b2126965ca21 73 }
RiiQQe 0:b2126965ca21 74 return false;
RiiQQe 0:b2126965ca21 75 }
RiiQQe 0:b2126965ca21 76
RiiQQe 0:b2126965ca21 77 //Function that checks if the enemy was hit, return true if it was.
RiiQQe 0:b2126965ca21 78 bool hitEnemy(){
RiiQQe 0:b2126965ca21 79 if(abs(xKiller - i) < 5 && abs(yKiller - j) < 5) {
RiiQQe 0:b2126965ca21 80 return true;
RiiQQe 0:b2126965ca21 81 }
RiiQQe 0:b2126965ca21 82 return false;
RiiQQe 0:b2126965ca21 83 }
RiiQQe 0:b2126965ca21 84 //Function that creates new food
RiiQQe 0:b2126965ca21 85 void createNewFood(){
RiiQQe 0:b2126965ca21 86 xFood = foodX [foodIndex % sizeof(foodX)];
RiiQQe 0:b2126965ca21 87 yFood = foodY [foodIndex % sizeof(foodY)];
RiiQQe 0:b2126965ca21 88
RiiQQe 0:b2126965ca21 89 xKiller = enX[enemyIndex % sizeof(enX)];
RiiQQe 0:b2126965ca21 90 yKiller = enY[enemyIndex % sizeof(enY)];
RiiQQe 0:b2126965ca21 91
RiiQQe 0:b2126965ca21 92 while(abs(xKiller - xFood) < 5 || abs(yKiller - yFood) < 5){
RiiQQe 0:b2126965ca21 93 enemyIndex = enemyIndex + 1;
RiiQQe 0:b2126965ca21 94 xKiller = enX[enemyIndex % sizeof(enX)];
RiiQQe 0:b2126965ca21 95 yKiller = enY[enemyIndex % sizeof(enY)];
RiiQQe 0:b2126965ca21 96
RiiQQe 0:b2126965ca21 97 }
RiiQQe 0:b2126965ca21 98
RiiQQe 0:b2126965ca21 99 foodIndex = foodIndex + 1;
RiiQQe 0:b2126965ca21 100 enemyIndex = enemyIndex + 1;
RiiQQe 0:b2126965ca21 101
RiiQQe 0:b2126965ca21 102 if(foodIndex > 9 || enemyIndex > 9) {
RiiQQe 0:b2126965ca21 103 foodIndex = 0;
RiiQQe 0:b2126965ca21 104 enemyIndex = 0;
RiiQQe 0:b2126965ca21 105 }
RiiQQe 0:b2126965ca21 106 }
RiiQQe 0:b2126965ca21 107
RiiQQe 0:b2126965ca21 108 //Set up game
RiiQQe 0:b2126965ca21 109 void setUpGame() {
RiiQQe 0:b2126965ca21 110 score = 0;
RiiQQe 0:b2126965ca21 111 xPos = 50, yPos = 10;
RiiQQe 0:b2126965ca21 112 xDim = 128, yDim = 32;
RiiQQe 0:b2126965ca21 113 step = 4;
RiiQQe 0:b2126965ca21 114 xFood = foodX[0], yFood = foodY[0], xKiller=enX[0], yKiller=enY[0];
RiiQQe 0:b2126965ca21 115 foodIndex = 1;
RiiQQe 0:b2126965ca21 116 enemyIndex = 1;
RiiQQe 0:b2126965ca21 117 }
RiiQQe 0:b2126965ca21 118
RiiQQe 0:b2126965ca21 119 //Function that shows gameover screen and plays end note
RiiQQe 0:b2126965ca21 120 void gameOverFun(){
RiiQQe 0:b2126965ca21 121 speaker2.PlayNote(969.0, 0.5, 1.0);
RiiQQe 0:b2126965ca21 122 speaker2.PlayNote(800.0, 0.5, 1.0);
RiiQQe 0:b2126965ca21 123 lcd.cls();
RiiQQe 0:b2126965ca21 124 lcd.locate(xDim / 2 - 10, yDim / 2);
RiiQQe 0:b2126965ca21 125 lcd.printf("Game Over");
RiiQQe 0:b2126965ca21 126 }
RiiQQe 0:b2126965ca21 127
RiiQQe 0:b2126965ca21 128 //Function that actually draws the snake and handles the user input
RiiQQe 0:b2126965ca21 129 void playSnake(void const *args)
RiiQQe 0:b2126965ca21 130 {
RiiQQe 0:b2126965ca21 131 while(true) {
RiiQQe 0:b2126965ca21 132 if(gameOver){
RiiQQe 0:b2126965ca21 133 Thread::wait(10000);
RiiQQe 0:b2126965ca21 134 }
RiiQQe 0:b2126965ca21 135
RiiQQe 0:b2126965ca21 136 if (!gameOver && !gameOverScreen) {
RiiQQe 0:b2126965ca21 137
RiiQQe 0:b2126965ca21 138 setUpGame();
RiiQQe 0:b2126965ca21 139
RiiQQe 0:b2126965ca21 140 while(!gameOverScreen) {
RiiQQe 0:b2126965ca21 141
RiiQQe 0:b2126965ca21 142 //Handles joystick-input
RiiQQe 0:b2126965ca21 143 if(joy) {
RiiQQe 0:b2126965ca21 144 if(joy == 0x4) { //Right Left
RiiQQe 0:b2126965ca21 145 k=0;
RiiQQe 0:b2126965ca21 146 i = i - step;
RiiQQe 0:b2126965ca21 147 if(i < 0) i = xDim + i;
RiiQQe 0:b2126965ca21 148 } else if(joy == 0x8) { //Right Left
RiiQQe 0:b2126965ca21 149 k=1;
RiiQQe 0:b2126965ca21 150 i = (i + step) % xDim;
RiiQQe 0:b2126965ca21 151 } else if(joy == 0x1) { //Up Down
RiiQQe 0:b2126965ca21 152 k=2;
RiiQQe 0:b2126965ca21 153 j = j - step;
RiiQQe 0:b2126965ca21 154 if(j < 0) j = yDim + j;
RiiQQe 0:b2126965ca21 155 } else if(joy == 0x2) { //Up Down
RiiQQe 0:b2126965ca21 156 k=3;
RiiQQe 0:b2126965ca21 157 j = (j + step) % yDim;
RiiQQe 0:b2126965ca21 158 }
RiiQQe 0:b2126965ca21 159 }
RiiQQe 0:b2126965ca21 160 //Handles previous inputs if joystick used
RiiQQe 0:b2126965ca21 161 else {
RiiQQe 0:b2126965ca21 162 if(k == 0) {
RiiQQe 0:b2126965ca21 163 i = i - step;
RiiQQe 0:b2126965ca21 164 if(i < 0) i = xDim + i;
RiiQQe 0:b2126965ca21 165 } else if(k == 1) {
RiiQQe 0:b2126965ca21 166 i = (i + step) % xDim;
RiiQQe 0:b2126965ca21 167 } else if(k == 2) {
RiiQQe 0:b2126965ca21 168 j = j - step;
RiiQQe 0:b2126965ca21 169 if(j < 0) j = yDim + j;
RiiQQe 0:b2126965ca21 170 } else if(k == 3) {
RiiQQe 0:b2126965ca21 171 j = (j + step) % yDim;
RiiQQe 0:b2126965ca21 172 }
RiiQQe 0:b2126965ca21 173 }
RiiQQe 0:b2126965ca21 174 lcd.cls();
RiiQQe 0:b2126965ca21 175 lcd.locate(i,j);
RiiQQe 0:b2126965ca21 176
RiiQQe 0:b2126965ca21 177 int r = 3;
RiiQQe 0:b2126965ca21 178 //To move the score to not interfere with game
RiiQQe 0:b2126965ca21 179 if(i < xDim / 2) lcd.locate(xDim-10, 10);
RiiQQe 0:b2126965ca21 180 else lcd.locate(10,10);
RiiQQe 0:b2126965ca21 181 lcd.printf("%i", score);
RiiQQe 0:b2126965ca21 182
RiiQQe 0:b2126965ca21 183 //Draw snake
RiiQQe 0:b2126965ca21 184 lcd.fillcircle(i, j, r, 1);
RiiQQe 0:b2126965ca21 185
RiiQQe 0:b2126965ca21 186 //Draw enemy
RiiQQe 0:b2126965ca21 187 lcd.locate(xKiller,yKiller);
RiiQQe 0:b2126965ca21 188 lcd.printf("X");
RiiQQe 0:b2126965ca21 189 bool hitE = hitEnemy();
RiiQQe 0:b2126965ca21 190
RiiQQe 0:b2126965ca21 191 //Draw food
RiiQQe 0:b2126965ca21 192 lcd.fillcircle(xFood, yFood, 2, 1);
RiiQQe 0:b2126965ca21 193 hit2 = hitFood();
RiiQQe 0:b2126965ca21 194
RiiQQe 0:b2126965ca21 195 //To see if food was eaten
RiiQQe 0:b2126965ca21 196 if(hit2)
RiiQQe 0:b2126965ca21 197 createNewFood();
RiiQQe 0:b2126965ca21 198 else if(hitE){
RiiQQe 0:b2126965ca21 199
RiiQQe 0:b2126965ca21 200 gameOver = true;
RiiQQe 0:b2126965ca21 201 gameOverScreen = true;
RiiQQe 0:b2126965ca21 202 gameOverFun();
RiiQQe 0:b2126965ca21 203 }
RiiQQe 0:b2126965ca21 204 Thread::wait(50); // Used to control the speed of the game
RiiQQe 0:b2126965ca21 205 }
RiiQQe 0:b2126965ca21 206 }
RiiQQe 0:b2126965ca21 207 }
RiiQQe 0:b2126965ca21 208 }
RiiQQe 0:b2126965ca21 209
RiiQQe 0:b2126965ca21 210 // Send msg to proxy script
RiiQQe 0:b2126965ca21 211 void tweet(std::string msg) {
RiiQQe 0:b2126965ca21 212 std::string url ("http://www.rickardlindstedt.com/proxy.php?msg=");
RiiQQe 0:b2126965ca21 213
RiiQQe 0:b2126965ca21 214 url = url + msg;
RiiQQe 0:b2126965ca21 215
RiiQQe 0:b2126965ca21 216 // send HTTP request
RiiQQe 0:b2126965ca21 217 int ret = http.get(url.c_str(), str, 128);
RiiQQe 0:b2126965ca21 218
RiiQQe 0:b2126965ca21 219 if (ret == HTTP_OK)
RiiQQe 0:b2126965ca21 220 {
RiiQQe 0:b2126965ca21 221 pc.printf("Success!\n");
RiiQQe 0:b2126965ca21 222 }
RiiQQe 0:b2126965ca21 223 else
RiiQQe 0:b2126965ca21 224 {
RiiQQe 0:b2126965ca21 225 pc.printf("Error #%d\n", ret);
RiiQQe 0:b2126965ca21 226 }
RiiQQe 0:b2126965ca21 227
RiiQQe 0:b2126965ca21 228 //Used to restart game
RiiQQe 0:b2126965ca21 229 setUpGame();
RiiQQe 0:b2126965ca21 230 }
RiiQQe 0:b2126965ca21 231
RiiQQe 0:b2126965ca21 232 // Set up ethernet network interface
RiiQQe 0:b2126965ca21 233 void setUpNetwork() {
RiiQQe 0:b2126965ca21 234 eth.init();
RiiQQe 0:b2126965ca21 235 eth.connect();
RiiQQe 0:b2126965ca21 236 }
RiiQQe 0:b2126965ca21 237
RiiQQe 0:b2126965ca21 238 //Thread for Tweet
RiiQQe 0:b2126965ca21 239 void tweetHighscore(void const *args){
RiiQQe 0:b2126965ca21 240 setUpNetwork();
RiiQQe 0:b2126965ca21 241 while(true) {
RiiQQe 0:b2126965ca21 242
RiiQQe 0:b2126965ca21 243 if (gameOver) {
RiiQQe 0:b2126965ca21 244
RiiQQe 0:b2126965ca21 245 std::string msg;
RiiQQe 0:b2126965ca21 246 ostringstream convert;
RiiQQe 0:b2126965ca21 247 convert << score;
RiiQQe 0:b2126965ca21 248 msg = convert.str();
RiiQQe 0:b2126965ca21 249 tweet(msg);
RiiQQe 0:b2126965ca21 250 locker.lock();
RiiQQe 0:b2126965ca21 251 gameOver = false;
RiiQQe 0:b2126965ca21 252
RiiQQe 0:b2126965ca21 253 bool restart = true;
RiiQQe 0:b2126965ca21 254 while(restart){
RiiQQe 0:b2126965ca21 255 if(joy) restart = false;
RiiQQe 0:b2126965ca21 256 }
RiiQQe 0:b2126965ca21 257 gameOverScreen = false;
RiiQQe 0:b2126965ca21 258 locker.unlock();
RiiQQe 0:b2126965ca21 259 }
RiiQQe 0:b2126965ca21 260
RiiQQe 0:b2126965ca21 261 Thread::wait(1000);
RiiQQe 0:b2126965ca21 262 }
RiiQQe 0:b2126965ca21 263 }
RiiQQe 0:b2126965ca21 264
RiiQQe 0:b2126965ca21 265 int main()
RiiQQe 0:b2126965ca21 266 {
RiiQQe 0:b2126965ca21 267 createNewFood();
RiiQQe 0:b2126965ca21 268
RiiQQe 0:b2126965ca21 269 Thread threadSnake(playSnake);
RiiQQe 0:b2126965ca21 270 Thread threadTweet(tweetHighscore);
RiiQQe 0:b2126965ca21 271
RiiQQe 0:b2126965ca21 272 while(true) {
RiiQQe 0:b2126965ca21 273 Thread::wait(500);
RiiQQe 0:b2126965ca21 274 }
RiiQQe 0:b2126965ca21 275 }