Cameron Allotey / Mbed OS 4180miniBball

Dependencies:   wave_player 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library

Committer:
callotey3
Date:
Sun Apr 26 09:47:43 2020 +0000
Revision:
76:2d5afdb54c5f
Parent:
72:47ae7d8d7321
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:17bd84fc5087 1 #include "mbed.h"
callotey3 76:2d5afdb54c5f 2 #include <string>
callotey3 76:2d5afdb54c5f 3 #include "ultrasonic.h"
callotey3 76:2d5afdb54c5f 4 #include "uLCD_4DGL.h"
callotey3 76:2d5afdb54c5f 5 #include "wave_player.h"
callotey3 76:2d5afdb54c5f 6
callotey3 76:2d5afdb54c5f 7 Serial pc(USBTX, USBRX);
mbed_official 0:17bd84fc5087 8
mbed_official 5:3e952c60d705 9 // Network interface
mbed_official 49:1923a727df5b 10 NetworkInterface *net;
mbed_official 0:17bd84fc5087 11
callotey3 76:2d5afdb54c5f 12 Thread threadLED, threadGame; //thread to update game and LED
callotey3 76:2d5afdb54c5f 13 Mutex mtx; //mutex lock
callotey3 76:2d5afdb54c5f 14 Timer t, delay;
callotey3 76:2d5afdb54c5f 15 //AnalogOut DACout(p18); // Speaker
callotey3 76:2d5afdb54c5f 16 PwmOut speaker(p26);
callotey3 76:2d5afdb54c5f 17 //wave_player waver(&DACout);
callotey3 76:2d5afdb54c5f 18
callotey3 76:2d5afdb54c5f 19
callotey3 76:2d5afdb54c5f 20 PwmOut RGBLed_r(p21);
callotey3 76:2d5afdb54c5f 21 PwmOut RGBLed_b(p24);
callotey3 76:2d5afdb54c5f 22 PwmOut RGBLed_g(p25);
callotey3 76:2d5afdb54c5f 23
callotey3 76:2d5afdb54c5f 24 uLCD_4DGL uLCD(p28,p27,p30);
callotey3 76:2d5afdb54c5f 25 void dist(int distance);
callotey3 76:2d5afdb54c5f 26 ultrasonic mu(p9, p10, .1, 1, &dist); //call dist when distance changes
callotey3 76:2d5afdb54c5f 27
callotey3 76:2d5afdb54c5f 28 //Global thread variables and flags
callotey3 76:2d5afdb54c5f 29 volatile int points = 0;
callotey3 76:2d5afdb54c5f 30 volatile int prevPoints = -1;
callotey3 76:2d5afdb54c5f 31 volatile int game = 1; //game flag
callotey3 76:2d5afdb54c5f 32 volatile int endGame = 0;
callotey3 76:2d5afdb54c5f 33 std::string url = "99a86ea0.ngrok.io";
callotey3 76:2d5afdb54c5f 34
callotey3 76:2d5afdb54c5f 35 //method to get sonar distance
callotey3 76:2d5afdb54c5f 36 void dist(int distance)
callotey3 76:2d5afdb54c5f 37 {
callotey3 76:2d5afdb54c5f 38 delay.start();
callotey3 76:2d5afdb54c5f 39 //printf("Game: %d", game);
callotey3 76:2d5afdb54c5f 40 mtx.lock();
callotey3 76:2d5afdb54c5f 41 if(distance < 70 && game == 1 && delay.read() > 1) { //range to be within and add point
callotey3 76:2d5afdb54c5f 42 delay.reset();
callotey3 76:2d5afdb54c5f 43 prevPoints = points;
callotey3 76:2d5afdb54c5f 44 ++points; //incrememnt points
callotey3 76:2d5afdb54c5f 45 speaker.period(1.0/550.0); // 500hz period
callotey3 76:2d5afdb54c5f 46 speaker =0.1; //50% duty cycle - max volume
callotey3 76:2d5afdb54c5f 47 wait(0.25);
callotey3 76:2d5afdb54c5f 48 speaker=0.0;
callotey3 76:2d5afdb54c5f 49 speaker.period(1.0/850.0); // 500hz period
callotey3 76:2d5afdb54c5f 50 speaker =0.1; //50% duty cycle - max volume
callotey3 76:2d5afdb54c5f 51 wait(0.25);
callotey3 76:2d5afdb54c5f 52 speaker=0.0;
callotey3 76:2d5afdb54c5f 53 }
callotey3 76:2d5afdb54c5f 54
callotey3 76:2d5afdb54c5f 55 mtx.unlock();
callotey3 76:2d5afdb54c5f 56
callotey3 76:2d5afdb54c5f 57 }
callotey3 76:2d5afdb54c5f 58
callotey3 76:2d5afdb54c5f 59 /*
callotey3 76:2d5afdb54c5f 60 void rgbLED(void) - thread function
callotey3 76:2d5afdb54c5f 61 Inputs: Uses global Game flag to set the correct color to indicate if
callotey3 76:2d5afdb54c5f 62 the game is happening or not
callotey3 76:2d5afdb54c5f 63 Outputs: LED color, green for game, purple for game over
callotey3 76:2d5afdb54c5f 64 */
callotey3 76:2d5afdb54c5f 65 void rgbLED(void)
callotey3 76:2d5afdb54c5f 66 {
callotey3 76:2d5afdb54c5f 67 while(1) {
callotey3 76:2d5afdb54c5f 68 //printf("In RGB\n");
callotey3 76:2d5afdb54c5f 69 mtx.lock(); //mtx lock for global variables
callotey3 76:2d5afdb54c5f 70 if(game == 1) {
callotey3 76:2d5afdb54c5f 71 //printf("In if");
callotey3 76:2d5afdb54c5f 72 RGBLed_r = 0; //set color to green for game start
callotey3 76:2d5afdb54c5f 73 RGBLed_g = 255;
callotey3 76:2d5afdb54c5f 74 RGBLed_b = 0;
callotey3 76:2d5afdb54c5f 75 } else {
callotey3 76:2d5afdb54c5f 76 //printf("Not in If\n");
callotey3 76:2d5afdb54c5f 77 RGBLed_r = 200; //set color to purple for game over
callotey3 76:2d5afdb54c5f 78 RGBLed_g = 0;
callotey3 76:2d5afdb54c5f 79 RGBLed_b = 255;
callotey3 76:2d5afdb54c5f 80 }
callotey3 76:2d5afdb54c5f 81 //game = !game;
callotey3 76:2d5afdb54c5f 82 mtx.unlock();
callotey3 76:2d5afdb54c5f 83 Thread::wait(500);
callotey3 76:2d5afdb54c5f 84 }
callotey3 76:2d5afdb54c5f 85 }
callotey3 76:2d5afdb54c5f 86
callotey3 76:2d5afdb54c5f 87 /*
callotey3 76:2d5afdb54c5f 88 void updateScore(void) - thread function
callotey3 76:2d5afdb54c5f 89 Inputs: Global game flag, int prevPoints, int points
callotey3 76:2d5afdb54c5f 90 Outputs: Calls distance sensor function if the game is active (1)
callotey3 76:2d5afdb54c5f 91 Adjusts the uLCD screen to display the number of points.
callotey3 76:2d5afdb54c5f 92 Timer checks for if 30 seconds have passed to end the game (game = 0).
callotey3 76:2d5afdb54c5f 93 When the game ends, prints out Game over and score.
callotey3 76:2d5afdb54c5f 94 */
callotey3 76:2d5afdb54c5f 95 void updateScore(void)
callotey3 76:2d5afdb54c5f 96 {
callotey3 76:2d5afdb54c5f 97 while(1) {
callotey3 76:2d5afdb54c5f 98 mtx.lock();
callotey3 76:2d5afdb54c5f 99 if(game == 1) {
callotey3 76:2d5afdb54c5f 100 t.start();
callotey3 76:2d5afdb54c5f 101 mu.checkDistance();
callotey3 76:2d5afdb54c5f 102 //wait(100);
callotey3 76:2d5afdb54c5f 103 if(prevPoints != points) {
callotey3 76:2d5afdb54c5f 104 uLCD.locate(0,0);
callotey3 76:2d5afdb54c5f 105 uLCD.text_width(5);
callotey3 76:2d5afdb54c5f 106 uLCD.text_height(5);
callotey3 76:2d5afdb54c5f 107 uLCD.printf("%d", points);
callotey3 76:2d5afdb54c5f 108
callotey3 76:2d5afdb54c5f 109 }
callotey3 76:2d5afdb54c5f 110 if(t.read() > 30) {
callotey3 76:2d5afdb54c5f 111 game = 0;
callotey3 76:2d5afdb54c5f 112 uLCD.locate(0,0);
callotey3 76:2d5afdb54c5f 113 uLCD.text_width(3);
callotey3 76:2d5afdb54c5f 114 uLCD.text_height(3);
callotey3 76:2d5afdb54c5f 115 uLCD.printf("Game\n over.\nScore: %d", points);
callotey3 76:2d5afdb54c5f 116 t.stop();
callotey3 76:2d5afdb54c5f 117 t.reset();
callotey3 76:2d5afdb54c5f 118 }
callotey3 76:2d5afdb54c5f 119 //printf("Game: %d, Time: %f \n", game, t.read());
callotey3 76:2d5afdb54c5f 120 }
callotey3 76:2d5afdb54c5f 121 mtx.unlock();
callotey3 76:2d5afdb54c5f 122 //Thread::wait(500);
callotey3 76:2d5afdb54c5f 123 }
callotey3 76:2d5afdb54c5f 124 }
callotey3 76:2d5afdb54c5f 125
callotey3 76:2d5afdb54c5f 126 // Socket + wait for game trigger
mbed_official 5:3e952c60d705 127 int main() {
callotey3 76:2d5afdb54c5f 128 //run repeatedly
callotey3 76:2d5afdb54c5f 129 while(1) {
callotey3 76:2d5afdb54c5f 130
mbed_official 47:08787ef063cb 131 int remaining;
mbed_official 47:08787ef063cb 132 int rcount;
mbed_official 47:08787ef063cb 133 char *p;
mbed_official 47:08787ef063cb 134 char *buffer = new char[256];
mbed_official 64:a3dc04daaa2a 135 nsapi_size_or_error_t result;
mbed_official 47:08787ef063cb 136
mbed_official 5:3e952c60d705 137 // Bring up the ethernet interface
callotey3 76:2d5afdb54c5f 138 //printf("Mbed OS Socket via ethernet\n");
mbed_official 48:f2739ac5cb01 139
mbed_official 48:f2739ac5cb01 140 #ifdef MBED_MAJOR_VERSION
mbed_official 49:1923a727df5b 141 printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
mbed_official 48:f2739ac5cb01 142 #endif
callotey3 76:2d5afdb54c5f 143
mbed_official 49:1923a727df5b 144 net = NetworkInterface::get_default_instance();
callotey3 76:2d5afdb54c5f 145
mbed_official 49:1923a727df5b 146 if (!net) {
mbed_official 49:1923a727df5b 147 printf("Error! No network inteface found.\n");
mbed_official 49:1923a727df5b 148 return 0;
mbed_official 49:1923a727df5b 149 }
mbed_official 49:1923a727df5b 150
mbed_official 64:a3dc04daaa2a 151 result = net->connect();
mbed_official 64:a3dc04daaa2a 152 if (result != 0) {
mbed_official 64:a3dc04daaa2a 153 printf("Error! net->connect() returned: %d\n", result);
mbed_official 64:a3dc04daaa2a 154 return result;
mbed_official 47:08787ef063cb 155 }
mbed_official 0:17bd84fc5087 156
mbed_official 0:17bd84fc5087 157 // Show the network address
callotey3 76:2d5afdb54c5f 158 //printf("before hang\n");
mbed_official 49:1923a727df5b 159 const char *ip = net->get_ip_address();
mbed_official 49:1923a727df5b 160 const char *netmask = net->get_netmask();
mbed_official 49:1923a727df5b 161 const char *gateway = net->get_gateway();
callotey3 76:2d5afdb54c5f 162 //printf("after hang\n");
mbed_official 13:ed9e4aa00044 163 printf("IP address: %s\n", ip ? ip : "None");
mbed_official 13:ed9e4aa00044 164 printf("Netmask: %s\n", netmask ? netmask : "None");
mbed_official 13:ed9e4aa00044 165 printf("Gateway: %s\n", gateway ? gateway : "None");
mbed_official 0:17bd84fc5087 166
mbed_official 72:47ae7d8d7321 167 // Open a socket on the network interface, and create a TCP connection to ifconfig.io
mbed_official 5:3e952c60d705 168 TCPSocket socket;
callotey3 76:2d5afdb54c5f 169
callotey3 76:2d5afdb54c5f 170
mbed_official 56:02a6401ec508 171 // Send a simple http request
callotey3 76:2d5afdb54c5f 172 std::string strbuffer;
callotey3 76:2d5afdb54c5f 173 const char * sbuffer;
callotey3 76:2d5afdb54c5f 174 if (endGame) {
callotey3 76:2d5afdb54c5f 175 char s[21];
callotey3 76:2d5afdb54c5f 176 sprintf(s, "%d", points);
callotey3 76:2d5afdb54c5f 177 std::string temp1 = s;
callotey3 76:2d5afdb54c5f 178 char x[21];
callotey3 76:2d5afdb54c5f 179 sprintf(x, "%d", strlen(s));
callotey3 76:2d5afdb54c5f 180 std::string temp2 = x;
callotey3 76:2d5afdb54c5f 181 //sprintf(sbuffer, "POST /game?score=%d HTTP/1.1\r\nHost: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\nConnection: close\r\n\r\n", points, url, strlen(s));
callotey3 76:2d5afdb54c5f 182 strbuffer = "POST /game?score=" + temp1 + " HTTP/1.1\r\nHost: " + url + "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: "+ temp2 +"\r\nConnection: close\r\n\r\n";
callotey3 76:2d5afdb54c5f 183 } else {
callotey3 76:2d5afdb54c5f 184 strbuffer = "GET / HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";
callotey3 76:2d5afdb54c5f 185 }
callotey3 76:2d5afdb54c5f 186 sbuffer = strbuffer.c_str();
mbed_official 56:02a6401ec508 187 nsapi_size_t size = strlen(sbuffer);
mbed_official 56:02a6401ec508 188
mbed_official 64:a3dc04daaa2a 189 result = socket.open(net);
mbed_official 64:a3dc04daaa2a 190 if (result != 0) {
mbed_official 64:a3dc04daaa2a 191 printf("Error! socket.open() returned: %d\n", result);
mbed_official 47:08787ef063cb 192 }
mbed_official 0:17bd84fc5087 193
callotey3 76:2d5afdb54c5f 194 result = socket.connect(url.c_str(), 80);
mbed_official 64:a3dc04daaa2a 195 if (result != 0) {
mbed_official 64:a3dc04daaa2a 196 printf("Error! socket.connect() returned: %d\n", result);
mbed_official 56:02a6401ec508 197 goto DISCONNECT;
mbed_official 47:08787ef063cb 198 }
callotey3 76:2d5afdb54c5f 199
mbed_official 64:a3dc04daaa2a 200 // Loop until whole request sent
mbed_official 47:08787ef063cb 201 while(size) {
mbed_official 64:a3dc04daaa2a 202 result = socket.send(sbuffer+result, size);
mbed_official 64:a3dc04daaa2a 203 if (result < 0) {
mbed_official 64:a3dc04daaa2a 204 printf("Error! socket.send() returned: %d\n", result);
mbed_official 56:02a6401ec508 205 goto DISCONNECT;
mbed_official 47:08787ef063cb 206 }
mbed_official 64:a3dc04daaa2a 207 size -= result;
mbed_official 64:a3dc04daaa2a 208 printf("sent %d [%.*s]\n", result, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 40:afef93b6d854 209 }
callotey3 76:2d5afdb54c5f 210 if (endGame) break;
mbed_official 47:08787ef063cb 211 // Receieve an HTTP response and print out the response line
mbed_official 47:08787ef063cb 212 remaining = 256;
mbed_official 47:08787ef063cb 213 rcount = 0;
mbed_official 47:08787ef063cb 214 p = buffer;
mbed_official 72:47ae7d8d7321 215 while (remaining > 0 && 0 < (result = socket.recv(p, remaining))) {
mbed_official 64:a3dc04daaa2a 216 p += result;
mbed_official 64:a3dc04daaa2a 217 rcount += result;
mbed_official 64:a3dc04daaa2a 218 remaining -= result;
mbed_official 47:08787ef063cb 219 }
mbed_official 64:a3dc04daaa2a 220 if (result < 0) {
mbed_official 64:a3dc04daaa2a 221 printf("Error! socket.recv() returned: %d\n", result);
mbed_official 56:02a6401ec508 222 goto DISCONNECT;
mbed_official 47:08787ef063cb 223 }
mbed_official 64:a3dc04daaa2a 224 // the HTTP response code
mbed_official 13:ed9e4aa00044 225 printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
callotey3 76:2d5afdb54c5f 226 int resp_body = buffer[199] - '0';
callotey3 76:2d5afdb54c5f 227 printf("\nResponse Body: %d\n", resp_body);
callotey3 76:2d5afdb54c5f 228 //start game if value was 1
callotey3 76:2d5afdb54c5f 229 if (resp_body == 1) {
callotey3 76:2d5afdb54c5f 230 mu.startUpdates();
callotey3 76:2d5afdb54c5f 231
callotey3 76:2d5afdb54c5f 232 threadGame.start(updateScore); //start updating the score
callotey3 76:2d5afdb54c5f 233 threadLED.start(rgbLED); //start checking LED lights
callotey3 76:2d5afdb54c5f 234 wait(30); //wati for game to run
callotey3 76:2d5afdb54c5f 235 //strbuffer = "POST /game?score=11 HTTP/1.1\r\nHost: " + url + "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
callotey3 76:2d5afdb54c5f 236 endGame = 1;
callotey3 76:2d5afdb54c5f 237 //break;
callotey3 76:2d5afdb54c5f 238
callotey3 76:2d5afdb54c5f 239 }
mbed_official 47:08787ef063cb 240 delete[] buffer;
mbed_official 0:17bd84fc5087 241
mbed_official 56:02a6401ec508 242 DISCONNECT:
mbed_official 0:17bd84fc5087 243 // Close the socket to return its memory and bring down the network interface
mbed_official 0:17bd84fc5087 244 socket.close();
mbed_official 0:17bd84fc5087 245
mbed_official 5:3e952c60d705 246 // Bring down the ethernet interface
mbed_official 49:1923a727df5b 247 net->disconnect();
mbed_official 0:17bd84fc5087 248 printf("Done\n");
callotey3 76:2d5afdb54c5f 249 wait(5.0);
callotey3 76:2d5afdb54c5f 250 }
callotey3 76:2d5afdb54c5f 251 wait(5.0);
callotey3 76:2d5afdb54c5f 252 //send score before restarting
callotey3 76:2d5afdb54c5f 253 //sendScore(points);
callotey3 76:2d5afdb54c5f 254 //restart system
callotey3 76:2d5afdb54c5f 255 NVIC_SystemReset();
callotey3 76:2d5afdb54c5f 256 }