mbed tokyotech
Dependencies: C12832 EthernetInterface HTTPClient USBDevice mbed-rtos mbed Speaker
main.cpp@0:d5e6ab0403f0, 2016-01-27 (annotated)
- Committer:
- Mickemoj
- Date:
- Wed Jan 27 11:29:18 2016 +0000
- Revision:
- 0:d5e6ab0403f0
- Child:
- 1:bc8574ad00b3
shitshit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mickemoj | 0:d5e6ab0403f0 | 1 | #include "mbed.h" |
Mickemoj | 0:d5e6ab0403f0 | 2 | #include "HTTPClient.h" |
Mickemoj | 0:d5e6ab0403f0 | 3 | #include "C12832.h" |
Mickemoj | 0:d5e6ab0403f0 | 4 | #include "EthernetInterface.h" |
Mickemoj | 0:d5e6ab0403f0 | 5 | #include <string> |
Mickemoj | 0:d5e6ab0403f0 | 6 | #include <stdlib.h> /* srand, rand */ |
Mickemoj | 0:d5e6ab0403f0 | 7 | #include "rtos.h" |
Mickemoj | 0:d5e6ab0403f0 | 8 | #include <sstream> |
Mickemoj | 0:d5e6ab0403f0 | 9 | #include "USBAudio.h" |
Mickemoj | 0:d5e6ab0403f0 | 10 | |
Mickemoj | 0:d5e6ab0403f0 | 11 | /* |
Mickemoj | 0:d5e6ab0403f0 | 12 | Account: mbedTokyoTech |
Mickemoj | 0:d5e6ab0403f0 | 13 | Psswd: kalle123 |
Mickemoj | 0:d5e6ab0403f0 | 14 | Email: ricli877@student.liu.se |
Mickemoj | 0:d5e6ab0403f0 | 15 | |
Mickemoj | 0:d5e6ab0403f0 | 16 | $consumerKey = 'XHO3e2fiS44d5i7xc6mvtvDdZ'; |
Mickemoj | 0:d5e6ab0403f0 | 17 | $consumerSecret = '9U4WOjRnpM5Pj0Qv4f6406jTRfN65YY5GeE5PRXHuhPfSiWhEN'; |
Mickemoj | 0:d5e6ab0403f0 | 18 | $accessToken = '4761221959-n4ojXEYR4DZSA4eeqt1WetVHHrZWTUNLJJ9ql3o'; |
Mickemoj | 0:d5e6ab0403f0 | 19 | $accessTokenSecret = 'SXM05FPIW9SUgTYYgpk4buPcypwXIsLDIY3TRns01WRJP'; |
Mickemoj | 0:d5e6ab0403f0 | 20 | */ |
Mickemoj | 0:d5e6ab0403f0 | 21 | |
Mickemoj | 0:d5e6ab0403f0 | 22 | // frequency: 48 kHz |
Mickemoj | 0:d5e6ab0403f0 | 23 | #define FREQ 48000 |
Mickemoj | 0:d5e6ab0403f0 | 24 | |
Mickemoj | 0:d5e6ab0403f0 | 25 | // 1 channel: mono |
Mickemoj | 0:d5e6ab0403f0 | 26 | #define NB_CHA 1 |
Mickemoj | 0:d5e6ab0403f0 | 27 | |
Mickemoj | 0:d5e6ab0403f0 | 28 | // Length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. As there is one channel, the length will be 48 * 2 * 1 |
Mickemoj | 0:d5e6ab0403f0 | 29 | #define AUDIO_LENGTH_PACKET 48 * 2 * 1 |
Mickemoj | 0:d5e6ab0403f0 | 30 | |
Mickemoj | 0:d5e6ab0403f0 | 31 | Serial pc(USBTX, USBRX); |
Mickemoj | 0:d5e6ab0403f0 | 32 | |
Mickemoj | 0:d5e6ab0403f0 | 33 | //Variables for audio streaming |
Mickemoj | 0:d5e6ab0403f0 | 34 | |
Mickemoj | 0:d5e6ab0403f0 | 35 | // USBAudio |
Mickemoj | 0:d5e6ab0403f0 | 36 | USBAudio audio(FREQ, NB_CHA, 8000, 1, 0x7180, 0x7500); |
Mickemoj | 0:d5e6ab0403f0 | 37 | |
Mickemoj | 0:d5e6ab0403f0 | 38 | // Speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker |
Mickemoj | 0:d5e6ab0403f0 | 39 | AnalogOut speaker(p18); |
Mickemoj | 0:d5e6ab0403f0 | 40 | |
Mickemoj | 0:d5e6ab0403f0 | 41 | // Ticker to send data to the speaker at the required frequency |
Mickemoj | 0:d5e6ab0403f0 | 42 | Ticker tic; |
Mickemoj | 0:d5e6ab0403f0 | 43 | |
Mickemoj | 0:d5e6ab0403f0 | 44 | // Buffer where one audio packet will be stored (LENGTH_AUDIO_PACKET/2 because we are storing int16 and not uint8) |
Mickemoj | 0:d5e6ab0403f0 | 45 | int16_t buf2[AUDIO_LENGTH_PACKET/2]; |
Mickemoj | 0:d5e6ab0403f0 | 46 | |
Mickemoj | 0:d5e6ab0403f0 | 47 | // Show if an audio packet is available |
Mickemoj | 0:d5e6ab0403f0 | 48 | volatile bool available = false; |
Mickemoj | 0:d5e6ab0403f0 | 49 | |
Mickemoj | 0:d5e6ab0403f0 | 50 | // Index of the value which will be send to the speaker |
Mickemoj | 0:d5e6ab0403f0 | 51 | int index_buf = 0; |
Mickemoj | 0:d5e6ab0403f0 | 52 | |
Mickemoj | 0:d5e6ab0403f0 | 53 | // Previous value sent to the speaker |
Mickemoj | 0:d5e6ab0403f0 | 54 | uint16_t p_val = 0; |
Mickemoj | 0:d5e6ab0403f0 | 55 | |
Mickemoj | 0:d5e6ab0403f0 | 56 | DigitalIn button(p14); |
Mickemoj | 0:d5e6ab0403f0 | 57 | |
Mickemoj | 0:d5e6ab0403f0 | 58 | EthernetInterface eth; |
Mickemoj | 0:d5e6ab0403f0 | 59 | |
Mickemoj | 0:d5e6ab0403f0 | 60 | C12832 lcd(p5, p7, p6, p8, p11); |
Mickemoj | 0:d5e6ab0403f0 | 61 | |
Mickemoj | 0:d5e6ab0403f0 | 62 | HTTPClient http; |
Mickemoj | 0:d5e6ab0403f0 | 63 | |
Mickemoj | 0:d5e6ab0403f0 | 64 | BusIn joy(p15,p12,p13,p16); |
Mickemoj | 0:d5e6ab0403f0 | 65 | |
Mickemoj | 0:d5e6ab0403f0 | 66 | Mutex locker; |
Mickemoj | 0:d5e6ab0403f0 | 67 | |
Mickemoj | 0:d5e6ab0403f0 | 68 | // sting buffers for tweeting |
Mickemoj | 0:d5e6ab0403f0 | 69 | char str[512]; |
Mickemoj | 0:d5e6ab0403f0 | 70 | char buf[512]; |
Mickemoj | 0:d5e6ab0403f0 | 71 | |
Mickemoj | 0:d5e6ab0403f0 | 72 | int enX[17] = {75,37,19,93,59,21,80,65,27,45,47,49,54,94,62,104,28}; |
Mickemoj | 0:d5e6ab0403f0 | 73 | int enY[19] = {30,16,25,5,15,22,13,7,6,23,24,2,28,21,20,14,17,19,3}; |
Mickemoj | 0:d5e6ab0403f0 | 74 | |
Mickemoj | 0:d5e6ab0403f0 | 75 | 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}; |
Mickemoj | 0:d5e6ab0403f0 | 76 | int foodY[13] = {11,29,2,23,6,24,28,5,1,27,9,7,13}; |
Mickemoj | 0:d5e6ab0403f0 | 77 | |
Mickemoj | 0:d5e6ab0403f0 | 78 | //Stuff for the snakegame + joystick |
Mickemoj | 0:d5e6ab0403f0 | 79 | int score = 0; |
Mickemoj | 0:d5e6ab0403f0 | 80 | int s2 = 0; |
Mickemoj | 0:d5e6ab0403f0 | 81 | int xPos = 50, yPos = 10; |
Mickemoj | 0:d5e6ab0403f0 | 82 | int xDim = 128, yDim = 32; |
Mickemoj | 0:d5e6ab0403f0 | 83 | int step = 4; |
Mickemoj | 0:d5e6ab0403f0 | 84 | int xFood = foodX[0], yFood = foodY[0], xKiller=enX[0], yKiller=enY[0]; |
Mickemoj | 0:d5e6ab0403f0 | 85 | int foodIndex = 1; |
Mickemoj | 0:d5e6ab0403f0 | 86 | int enemyIndex = 1; |
Mickemoj | 0:d5e6ab0403f0 | 87 | |
Mickemoj | 0:d5e6ab0403f0 | 88 | bool gameOver = false; |
Mickemoj | 0:d5e6ab0403f0 | 89 | |
Mickemoj | 0:d5e6ab0403f0 | 90 | |
Mickemoj | 0:d5e6ab0403f0 | 91 | int k = 0; |
Mickemoj | 0:d5e6ab0403f0 | 92 | |
Mickemoj | 0:d5e6ab0403f0 | 93 | int i = 0, j = 0; |
Mickemoj | 0:d5e6ab0403f0 | 94 | |
Mickemoj | 0:d5e6ab0403f0 | 95 | bool hit2 = false; |
Mickemoj | 0:d5e6ab0403f0 | 96 | //Function that checks if the food was eaten, returns true if it was. |
Mickemoj | 0:d5e6ab0403f0 | 97 | bool hitFood(){ |
Mickemoj | 0:d5e6ab0403f0 | 98 | if(abs(xFood - i) < 5 && abs(yFood - j) < 5) { |
Mickemoj | 0:d5e6ab0403f0 | 99 | score = score + 1; |
Mickemoj | 0:d5e6ab0403f0 | 100 | return true; |
Mickemoj | 0:d5e6ab0403f0 | 101 | } |
Mickemoj | 0:d5e6ab0403f0 | 102 | return false; |
Mickemoj | 0:d5e6ab0403f0 | 103 | } |
Mickemoj | 0:d5e6ab0403f0 | 104 | bool hitEnemy(){ |
Mickemoj | 0:d5e6ab0403f0 | 105 | if(abs(xKiller - i) < 5 && abs(yKiller - j) < 5) { |
Mickemoj | 0:d5e6ab0403f0 | 106 | return true; |
Mickemoj | 0:d5e6ab0403f0 | 107 | } |
Mickemoj | 0:d5e6ab0403f0 | 108 | return false; |
Mickemoj | 0:d5e6ab0403f0 | 109 | } |
Mickemoj | 0:d5e6ab0403f0 | 110 | //Function that creates new food |
Mickemoj | 0:d5e6ab0403f0 | 111 | void createNewFood(){ |
Mickemoj | 0:d5e6ab0403f0 | 112 | xFood = foodX [foodIndex % sizeof(foodX)]; |
Mickemoj | 0:d5e6ab0403f0 | 113 | yFood = foodY [foodIndex % sizeof(foodY)]; |
Mickemoj | 0:d5e6ab0403f0 | 114 | |
Mickemoj | 0:d5e6ab0403f0 | 115 | xKiller = enX[enemyIndex % sizeof(enX)]; |
Mickemoj | 0:d5e6ab0403f0 | 116 | yKiller = enY[enemyIndex % sizeof(enY)]; |
Mickemoj | 0:d5e6ab0403f0 | 117 | |
Mickemoj | 0:d5e6ab0403f0 | 118 | while(abs(xKiller - xFood) < 5 || abs(yKiller - yFood) < 5){ |
Mickemoj | 0:d5e6ab0403f0 | 119 | enemyIndex = enemyIndex + 1; |
Mickemoj | 0:d5e6ab0403f0 | 120 | xKiller = enX[enemyIndex % sizeof(enX)]; |
Mickemoj | 0:d5e6ab0403f0 | 121 | yKiller = enY[enemyIndex % sizeof(enY)]; |
Mickemoj | 0:d5e6ab0403f0 | 122 | } |
Mickemoj | 0:d5e6ab0403f0 | 123 | |
Mickemoj | 0:d5e6ab0403f0 | 124 | foodIndex = foodIndex + 1; |
Mickemoj | 0:d5e6ab0403f0 | 125 | enemyIndex = enemyIndex + 1; |
Mickemoj | 0:d5e6ab0403f0 | 126 | } |
Mickemoj | 0:d5e6ab0403f0 | 127 | |
Mickemoj | 0:d5e6ab0403f0 | 128 | void setUpGame() { |
Mickemoj | 0:d5e6ab0403f0 | 129 | score = 0; |
Mickemoj | 0:d5e6ab0403f0 | 130 | xPos = 50, yPos = 10; |
Mickemoj | 0:d5e6ab0403f0 | 131 | xDim = 128, yDim = 32; |
Mickemoj | 0:d5e6ab0403f0 | 132 | step = 4; |
Mickemoj | 0:d5e6ab0403f0 | 133 | xFood = foodX[0], yFood = foodY[0], xKiller=enX[0], yKiller=enY[0]; |
Mickemoj | 0:d5e6ab0403f0 | 134 | foodIndex = 1; |
Mickemoj | 0:d5e6ab0403f0 | 135 | enemyIndex = 1; |
Mickemoj | 0:d5e6ab0403f0 | 136 | } |
Mickemoj | 0:d5e6ab0403f0 | 137 | |
Mickemoj | 0:d5e6ab0403f0 | 138 | |
Mickemoj | 0:d5e6ab0403f0 | 139 | |
Mickemoj | 0:d5e6ab0403f0 | 140 | //Function that actually draws the snake and handles the user input |
Mickemoj | 0:d5e6ab0403f0 | 141 | void playSnake(void const *args) |
Mickemoj | 0:d5e6ab0403f0 | 142 | { |
Mickemoj | 0:d5e6ab0403f0 | 143 | while(true) { |
Mickemoj | 0:d5e6ab0403f0 | 144 | //pc.printf("Playsnake"); |
Mickemoj | 0:d5e6ab0403f0 | 145 | if (!gameOver) { |
Mickemoj | 0:d5e6ab0403f0 | 146 | //locker.lock(); |
Mickemoj | 0:d5e6ab0403f0 | 147 | setUpGame(); |
Mickemoj | 0:d5e6ab0403f0 | 148 | //pc.printf("PlayLock"); |
Mickemoj | 0:d5e6ab0403f0 | 149 | while(true) { |
Mickemoj | 0:d5e6ab0403f0 | 150 | //Handles joystick-input |
Mickemoj | 0:d5e6ab0403f0 | 151 | if(joy) { |
Mickemoj | 0:d5e6ab0403f0 | 152 | if(joy == 0x4) { //Right Left |
Mickemoj | 0:d5e6ab0403f0 | 153 | k=0; |
Mickemoj | 0:d5e6ab0403f0 | 154 | i = i - step; |
Mickemoj | 0:d5e6ab0403f0 | 155 | if(i < 0) i = xDim + i; |
Mickemoj | 0:d5e6ab0403f0 | 156 | } else if(joy == 0x8) { //Right Left |
Mickemoj | 0:d5e6ab0403f0 | 157 | k=1; |
Mickemoj | 0:d5e6ab0403f0 | 158 | i = (i + step) % xDim; |
Mickemoj | 0:d5e6ab0403f0 | 159 | } else if(joy == 0x1) { //Up Down |
Mickemoj | 0:d5e6ab0403f0 | 160 | k=2; |
Mickemoj | 0:d5e6ab0403f0 | 161 | j = j - step; |
Mickemoj | 0:d5e6ab0403f0 | 162 | if(j < 0) j = yDim + j; |
Mickemoj | 0:d5e6ab0403f0 | 163 | } else if(joy == 0x2) { //Up Down |
Mickemoj | 0:d5e6ab0403f0 | 164 | k=3; |
Mickemoj | 0:d5e6ab0403f0 | 165 | j = (j + step) % yDim; |
Mickemoj | 0:d5e6ab0403f0 | 166 | } |
Mickemoj | 0:d5e6ab0403f0 | 167 | }else { |
Mickemoj | 0:d5e6ab0403f0 | 168 | if(k == 0) { |
Mickemoj | 0:d5e6ab0403f0 | 169 | i = i - step; |
Mickemoj | 0:d5e6ab0403f0 | 170 | if(i < 0) i = xDim + i; |
Mickemoj | 0:d5e6ab0403f0 | 171 | } else if(k == 1) { |
Mickemoj | 0:d5e6ab0403f0 | 172 | i = (i + step) % xDim; |
Mickemoj | 0:d5e6ab0403f0 | 173 | } else if(k == 2) { |
Mickemoj | 0:d5e6ab0403f0 | 174 | j = j - step; |
Mickemoj | 0:d5e6ab0403f0 | 175 | if(j < 0) j = yDim + j; |
Mickemoj | 0:d5e6ab0403f0 | 176 | } else if(k == 3) { |
Mickemoj | 0:d5e6ab0403f0 | 177 | j = (j + step) % yDim; |
Mickemoj | 0:d5e6ab0403f0 | 178 | } |
Mickemoj | 0:d5e6ab0403f0 | 179 | } |
Mickemoj | 0:d5e6ab0403f0 | 180 | lcd.cls(); |
Mickemoj | 0:d5e6ab0403f0 | 181 | lcd.locate(i,j); |
Mickemoj | 0:d5e6ab0403f0 | 182 | |
Mickemoj | 0:d5e6ab0403f0 | 183 | int r = 3; |
Mickemoj | 0:d5e6ab0403f0 | 184 | //To move the score to not interfere with game |
Mickemoj | 0:d5e6ab0403f0 | 185 | if(i < xDim / 2) lcd.locate(xDim-10, 10); |
Mickemoj | 0:d5e6ab0403f0 | 186 | else lcd.locate(10,10); |
Mickemoj | 0:d5e6ab0403f0 | 187 | lcd.printf("%i", score); |
Mickemoj | 0:d5e6ab0403f0 | 188 | |
Mickemoj | 0:d5e6ab0403f0 | 189 | //Draw snake |
Mickemoj | 0:d5e6ab0403f0 | 190 | lcd.fillcircle(i, j, r, 1); |
Mickemoj | 0:d5e6ab0403f0 | 191 | |
Mickemoj | 0:d5e6ab0403f0 | 192 | //Draw enemy |
Mickemoj | 0:d5e6ab0403f0 | 193 | lcd.locate(xKiller,yKiller); |
Mickemoj | 0:d5e6ab0403f0 | 194 | lcd.printf("X"); |
Mickemoj | 0:d5e6ab0403f0 | 195 | bool hitE = hitEnemy(); |
Mickemoj | 0:d5e6ab0403f0 | 196 | |
Mickemoj | 0:d5e6ab0403f0 | 197 | //Draw food |
Mickemoj | 0:d5e6ab0403f0 | 198 | lcd.fillcircle(xFood, yFood, 2, 1); |
Mickemoj | 0:d5e6ab0403f0 | 199 | hit2 = hitFood(); |
Mickemoj | 0:d5e6ab0403f0 | 200 | |
Mickemoj | 0:d5e6ab0403f0 | 201 | //To see if food was eaten |
Mickemoj | 0:d5e6ab0403f0 | 202 | if(hit2) |
Mickemoj | 0:d5e6ab0403f0 | 203 | createNewFood(); |
Mickemoj | 0:d5e6ab0403f0 | 204 | else if(hitE){ |
Mickemoj | 0:d5e6ab0403f0 | 205 | //locker.unlock(); |
Mickemoj | 0:d5e6ab0403f0 | 206 | gameOver = true; |
Mickemoj | 0:d5e6ab0403f0 | 207 | break; |
Mickemoj | 0:d5e6ab0403f0 | 208 | } |
Mickemoj | 0:d5e6ab0403f0 | 209 | Thread::wait(50); // Used to control the speed of the game |
Mickemoj | 0:d5e6ab0403f0 | 210 | } |
Mickemoj | 0:d5e6ab0403f0 | 211 | Thread::wait(1000); |
Mickemoj | 0:d5e6ab0403f0 | 212 | } |
Mickemoj | 0:d5e6ab0403f0 | 213 | } |
Mickemoj | 0:d5e6ab0403f0 | 214 | } |
Mickemoj | 0:d5e6ab0403f0 | 215 | |
Mickemoj | 0:d5e6ab0403f0 | 216 | // Replace all occurrences of s2 with s3 in s1 |
Mickemoj | 0:d5e6ab0403f0 | 217 | void rep(std::string &s1, const char * s2, const char * s3) { |
Mickemoj | 0:d5e6ab0403f0 | 218 | size_t index = 0; |
Mickemoj | 0:d5e6ab0403f0 | 219 | while (index < s1.size()) { |
Mickemoj | 0:d5e6ab0403f0 | 220 | index = s1.find(s2, index); |
Mickemoj | 0:d5e6ab0403f0 | 221 | if (index == std::string::npos) break; |
Mickemoj | 0:d5e6ab0403f0 | 222 | s1.replace(index, strlen(s2), s3); |
Mickemoj | 0:d5e6ab0403f0 | 223 | index += strlen(s3); |
Mickemoj | 0:d5e6ab0403f0 | 224 | } |
Mickemoj | 0:d5e6ab0403f0 | 225 | } |
Mickemoj | 0:d5e6ab0403f0 | 226 | |
Mickemoj | 0:d5e6ab0403f0 | 227 | // Tweet msg |
Mickemoj | 0:d5e6ab0403f0 | 228 | void tweet(std::string msg) { |
Mickemoj | 0:d5e6ab0403f0 | 229 | std::string url ("http://www.rickardlindstedt.com/proxy.php?msg="); |
Mickemoj | 0:d5e6ab0403f0 | 230 | |
Mickemoj | 0:d5e6ab0403f0 | 231 | pc.printf("Message: %s\n", msg.c_str()); |
Mickemoj | 0:d5e6ab0403f0 | 232 | pc.printf("\nTrying to post data...\n"); |
Mickemoj | 0:d5e6ab0403f0 | 233 | |
Mickemoj | 0:d5e6ab0403f0 | 234 | // replace special characters with URL-safe ones |
Mickemoj | 0:d5e6ab0403f0 | 235 | rep(msg, " ", "%20"); |
Mickemoj | 0:d5e6ab0403f0 | 236 | rep(msg, "#", "%23"); |
Mickemoj | 0:d5e6ab0403f0 | 237 | rep(msg, ":", "%3A"); |
Mickemoj | 0:d5e6ab0403f0 | 238 | |
Mickemoj | 0:d5e6ab0403f0 | 239 | url = url + msg; |
Mickemoj | 0:d5e6ab0403f0 | 240 | |
Mickemoj | 0:d5e6ab0403f0 | 241 | pc.printf("URL: %s\n", url.c_str()); |
Mickemoj | 0:d5e6ab0403f0 | 242 | |
Mickemoj | 0:d5e6ab0403f0 | 243 | // send HTTP request |
Mickemoj | 0:d5e6ab0403f0 | 244 | int ret = http.get(url.c_str(), str, 128); |
Mickemoj | 0:d5e6ab0403f0 | 245 | if (ret == HTTP_OK) |
Mickemoj | 0:d5e6ab0403f0 | 246 | { |
Mickemoj | 0:d5e6ab0403f0 | 247 | pc.printf("Success!\n"); |
Mickemoj | 0:d5e6ab0403f0 | 248 | } |
Mickemoj | 0:d5e6ab0403f0 | 249 | else |
Mickemoj | 0:d5e6ab0403f0 | 250 | { |
Mickemoj | 0:d5e6ab0403f0 | 251 | pc.printf("Error #%d\n", ret); |
Mickemoj | 0:d5e6ab0403f0 | 252 | } |
Mickemoj | 0:d5e6ab0403f0 | 253 | |
Mickemoj | 0:d5e6ab0403f0 | 254 | pc.printf("Response code: %d\n", http.getHTTPResponseCode()); |
Mickemoj | 0:d5e6ab0403f0 | 255 | } |
Mickemoj | 0:d5e6ab0403f0 | 256 | |
Mickemoj | 0:d5e6ab0403f0 | 257 | // Set up ethernet network interface |
Mickemoj | 0:d5e6ab0403f0 | 258 | void setUpNetwork() { |
Mickemoj | 0:d5e6ab0403f0 | 259 | eth.init(); |
Mickemoj | 0:d5e6ab0403f0 | 260 | eth.connect(); |
Mickemoj | 0:d5e6ab0403f0 | 261 | pc.printf("IP: %s", eth.getIPAddress()); |
Mickemoj | 0:d5e6ab0403f0 | 262 | } |
Mickemoj | 0:d5e6ab0403f0 | 263 | |
Mickemoj | 0:d5e6ab0403f0 | 264 | void tweetHighscore(void const *args){ |
Mickemoj | 0:d5e6ab0403f0 | 265 | setUpNetwork(); |
Mickemoj | 0:d5e6ab0403f0 | 266 | while(true) { |
Mickemoj | 0:d5e6ab0403f0 | 267 | //locker.lock(); |
Mickemoj | 0:d5e6ab0403f0 | 268 | if (gameOver) { |
Mickemoj | 0:d5e6ab0403f0 | 269 | std::string msg; |
Mickemoj | 0:d5e6ab0403f0 | 270 | ostringstream convert; |
Mickemoj | 0:d5e6ab0403f0 | 271 | convert << "Score: " << score << ", " << xKiller; |
Mickemoj | 0:d5e6ab0403f0 | 272 | msg = convert.str(); |
Mickemoj | 0:d5e6ab0403f0 | 273 | tweet(msg); |
Mickemoj | 0:d5e6ab0403f0 | 274 | gameOver = false; |
Mickemoj | 0:d5e6ab0403f0 | 275 | } |
Mickemoj | 0:d5e6ab0403f0 | 276 | //locker.unlock(); |
Mickemoj | 0:d5e6ab0403f0 | 277 | Thread::wait(1000); |
Mickemoj | 0:d5e6ab0403f0 | 278 | } |
Mickemoj | 0:d5e6ab0403f0 | 279 | } |
Mickemoj | 0:d5e6ab0403f0 | 280 | |
Mickemoj | 0:d5e6ab0403f0 | 281 | // Function executed each 1/FREQ s |
Mickemoj | 0:d5e6ab0403f0 | 282 | void tic_handler() { |
Mickemoj | 0:d5e6ab0403f0 | 283 | float speaker_value; |
Mickemoj | 0:d5e6ab0403f0 | 284 | |
Mickemoj | 0:d5e6ab0403f0 | 285 | if (available) { |
Mickemoj | 0:d5e6ab0403f0 | 286 | // Convert 2 bytes in float |
Mickemoj | 0:d5e6ab0403f0 | 287 | speaker_value = (float)(buf2[index_buf]); |
Mickemoj | 0:d5e6ab0403f0 | 288 | |
Mickemoj | 0:d5e6ab0403f0 | 289 | // Speaker_value between 0 and 65535 |
Mickemoj | 0:d5e6ab0403f0 | 290 | speaker_value += 32768.0; |
Mickemoj | 0:d5e6ab0403f0 | 291 | |
Mickemoj | 0:d5e6ab0403f0 | 292 | // Adjust according to current volume |
Mickemoj | 0:d5e6ab0403f0 | 293 | speaker_value *= audio.getVolume(); |
Mickemoj | 0:d5e6ab0403f0 | 294 | |
Mickemoj | 0:d5e6ab0403f0 | 295 | |
Mickemoj | 0:d5e6ab0403f0 | 296 | // As two bytes has been read, we move the index of two bytes |
Mickemoj | 0:d5e6ab0403f0 | 297 | index_buf++; |
Mickemoj | 0:d5e6ab0403f0 | 298 | |
Mickemoj | 0:d5e6ab0403f0 | 299 | // If we have read all the buffer, no more data available |
Mickemoj | 0:d5e6ab0403f0 | 300 | if (index_buf == AUDIO_LENGTH_PACKET/2) { |
Mickemoj | 0:d5e6ab0403f0 | 301 | index_buf = 0; |
Mickemoj | 0:d5e6ab0403f0 | 302 | available = false; |
Mickemoj | 0:d5e6ab0403f0 | 303 | } |
Mickemoj | 0:d5e6ab0403f0 | 304 | } else { |
Mickemoj | 0:d5e6ab0403f0 | 305 | speaker_value = p_val; |
Mickemoj | 0:d5e6ab0403f0 | 306 | } |
Mickemoj | 0:d5e6ab0403f0 | 307 | |
Mickemoj | 0:d5e6ab0403f0 | 308 | p_val = speaker_value; |
Mickemoj | 0:d5e6ab0403f0 | 309 | |
Mickemoj | 0:d5e6ab0403f0 | 310 | // Send value to the speaker |
Mickemoj | 0:d5e6ab0403f0 | 311 | speaker.write_u16((uint16_t)speaker_value); |
Mickemoj | 0:d5e6ab0403f0 | 312 | } |
Mickemoj | 0:d5e6ab0403f0 | 313 | |
Mickemoj | 0:d5e6ab0403f0 | 314 | int main() |
Mickemoj | 0:d5e6ab0403f0 | 315 | { |
Mickemoj | 0:d5e6ab0403f0 | 316 | // Attach a function executed each 1/FREQ s |
Mickemoj | 0:d5e6ab0403f0 | 317 | tic.attach_us(tic_handler, 1000000.0/(float)FREQ); |
Mickemoj | 0:d5e6ab0403f0 | 318 | |
Mickemoj | 0:d5e6ab0403f0 | 319 | pc.printf("hej"); |
Mickemoj | 0:d5e6ab0403f0 | 320 | createNewFood(); |
Mickemoj | 0:d5e6ab0403f0 | 321 | pc.printf("after create food"); |
Mickemoj | 0:d5e6ab0403f0 | 322 | Thread threadSnake(playSnake); |
Mickemoj | 0:d5e6ab0403f0 | 323 | Thread threadTweet(tweetHighscore); |
Mickemoj | 0:d5e6ab0403f0 | 324 | pc.printf("after threads"); |
Mickemoj | 0:d5e6ab0403f0 | 325 | |
Mickemoj | 0:d5e6ab0403f0 | 326 | while (1) { |
Mickemoj | 0:d5e6ab0403f0 | 327 | // Read an audio packet |
Mickemoj | 0:d5e6ab0403f0 | 328 | audio.read((uint8_t *)buf2); |
Mickemoj | 0:d5e6ab0403f0 | 329 | available = true; |
Mickemoj | 0:d5e6ab0403f0 | 330 | } |
Mickemoj | 0:d5e6ab0403f0 | 331 | /*while(true) { |
Mickemoj | 0:d5e6ab0403f0 | 332 | Thread::wait(500); |
Mickemoj | 0:d5e6ab0403f0 | 333 | }*/ |
Mickemoj | 0:d5e6ab0403f0 | 334 | } |
Mickemoj | 0:d5e6ab0403f0 | 335 | |
Mickemoj | 0:d5e6ab0403f0 | 336 | /* |
Mickemoj | 0:d5e6ab0403f0 | 337 | // Read string from serial USB |
Mickemoj | 0:d5e6ab0403f0 | 338 | std::string readStringFromUSB() { |
Mickemoj | 0:d5e6ab0403f0 | 339 | // Read message from USB |
Mickemoj | 0:d5e6ab0403f0 | 340 | pc.printf("Input message: "); |
Mickemoj | 0:d5e6ab0403f0 | 341 | |
Mickemoj | 0:d5e6ab0403f0 | 342 | int i = 0; |
Mickemoj | 0:d5e6ab0403f0 | 343 | char current = pc.getc(); |
Mickemoj | 0:d5e6ab0403f0 | 344 | while(current != '\n' && current != '\r') { |
Mickemoj | 0:d5e6ab0403f0 | 345 | buf[i] = current; |
Mickemoj | 0:d5e6ab0403f0 | 346 | current = pc.getc(); |
Mickemoj | 0:d5e6ab0403f0 | 347 | i++; |
Mickemoj | 0:d5e6ab0403f0 | 348 | } |
Mickemoj | 0:d5e6ab0403f0 | 349 | buf[i] = '\0'; |
Mickemoj | 0:d5e6ab0403f0 | 350 | |
Mickemoj | 0:d5e6ab0403f0 | 351 | std::string str (buf); |
Mickemoj | 0:d5e6ab0403f0 | 352 | return str; |
Mickemoj | 0:d5e6ab0403f0 | 353 | } |
Mickemoj | 0:d5e6ab0403f0 | 354 | */ |
Mickemoj | 0:d5e6ab0403f0 | 355 | |
Mickemoj | 0:d5e6ab0403f0 | 356 | /* |
Mickemoj | 0:d5e6ab0403f0 | 357 | int oldMain() { |
Mickemoj | 0:d5e6ab0403f0 | 358 | // Set up ehternet network interface |
Mickemoj | 0:d5e6ab0403f0 | 359 | setUpNetwork(); |
Mickemoj | 0:d5e6ab0403f0 | 360 | |
Mickemoj | 0:d5e6ab0403f0 | 361 | // Read message from USB |
Mickemoj | 0:d5e6ab0403f0 | 362 | std::string msg = readStringFromUSB(); |
Mickemoj | 0:d5e6ab0403f0 | 363 | |
Mickemoj | 0:d5e6ab0403f0 | 364 | // Tweet message |
Mickemoj | 0:d5e6ab0403f0 | 365 | tweet(msg); |
Mickemoj | 0:d5e6ab0403f0 | 366 | return 0; |
Mickemoj | 0:d5e6ab0403f0 | 367 | } |
Mickemoj | 0:d5e6ab0403f0 | 368 | */ |