mbed tokyotech
Dependencies: C12832 EthernetInterface HTTPClient USBDevice mbed-rtos mbed Speaker
Diff: main.cpp
- Revision:
- 1:bc8574ad00b3
- Parent:
- 0:d5e6ab0403f0
- Child:
- 2:d26f6e124f1a
--- a/main.cpp Wed Jan 27 11:29:18 2016 +0000 +++ b/main.cpp Thu Feb 04 07:32:42 2016 +0000 @@ -35,8 +35,7 @@ // USBAudio USBAudio audio(FREQ, NB_CHA, 8000, 1, 0x7180, 0x7500); -// Speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker -AnalogOut speaker(p18); + // Ticker to send data to the speaker at the required frequency Ticker tic; @@ -52,6 +51,9 @@ // Previous value sent to the speaker uint16_t p_val = 0; + +// Speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker +AnalogOut speaker(p18); DigitalIn button(p14); @@ -141,13 +143,17 @@ void playSnake(void const *args) { while(true) { - //pc.printf("Playsnake"); + if(gameOver){ + Thread::wait(10000); + } + if (!gameOver) { //locker.lock(); setUpGame(); //pc.printf("PlayLock"); while(true) { //Handles joystick-input + pc.printf("Playsnake"); if(joy) { if(joy == 0x4) { //Right Left k=0; @@ -202,9 +208,10 @@ if(hit2) createNewFood(); else if(hitE){ - //locker.unlock(); + gameOver = true; - break; + break; + } Thread::wait(50); // Used to control the speed of the game } @@ -227,21 +234,22 @@ // Tweet msg void tweet(std::string msg) { std::string url ("http://www.rickardlindstedt.com/proxy.php?msg="); - - pc.printf("Message: %s\n", msg.c_str()); - pc.printf("\nTrying to post data...\n"); + //pc.printf("Message: %s\n", msg.c_str()); + //pc.printf("\nTrying to post data...\n"); // replace special characters with URL-safe ones - rep(msg, " ", "%20"); + /*rep(msg, " ", "%20"); rep(msg, "#", "%23"); - rep(msg, ":", "%3A"); + rep(msg, ":", "%3A");*/ + msg = "heeej"; url = url + msg; - pc.printf("URL: %s\n", url.c_str()); - + //pc.printf("URL: %s\n", url.c_str()); + pc.printf("i tweet3"); // send HTTP request int ret = http.get(url.c_str(), str, 128); + pc.printf("i tweet4"); if (ret == HTTP_OK) { pc.printf("Success!\n"); @@ -252,26 +260,33 @@ } pc.printf("Response code: %d\n", http.getHTTPResponseCode()); + } // Set up ethernet network interface void setUpNetwork() { eth.init(); eth.connect(); - pc.printf("IP: %s", eth.getIPAddress()); + //pc.printf("IP: %s", eth.getIPAddress()); } void tweetHighscore(void const *args){ + //tic.detach(); setUpNetwork(); while(true) { //locker.lock(); if (gameOver) { + pc.printf("I game over"); + //tic.detach(); std::string msg; ostringstream convert; convert << "Score: " << score << ", " << xKiller; msg = convert.str(); tweet(msg); + pc.printf("efter tweet(msg)"); + locker.lock(); gameOver = false; + locker.unlock(); } //locker.unlock(); Thread::wait(1000); @@ -310,59 +325,31 @@ // Send value to the speaker speaker.write_u16((uint16_t)speaker_value); } - + int main() { // Attach a function executed each 1/FREQ s - tic.attach_us(tic_handler, 1000000.0/(float)FREQ); + // tic.attach_us(tic_handler, 1000000.0/(float)FREQ); - pc.printf("hej"); + //pc.printf("hej"); createNewFood(); - pc.printf("after create food"); + //pc.printf("after create food"); Thread threadSnake(playSnake); Thread threadTweet(tweetHighscore); - pc.printf("after threads"); - - while (1) { + //pc.printf("after threads"); + + + /*while (1) { // Read an audio packet - audio.read((uint8_t *)buf2); - available = true; - } - /*while(true) { - Thread::wait(500); + if(gameOver){ + + Thread::wait(10000); + } + audio.read((uint8_t *)buf2); + available = true; + }*/ -} - -/* -// Read string from serial USB -std::string readStringFromUSB() { - // Read message from USB - pc.printf("Input message: "); - - int i = 0; - char current = pc.getc(); - while(current != '\n' && current != '\r') { - buf[i] = current; - current = pc.getc(); - i++; + while(true) { + Thread::wait(500); } - buf[i] = '\0'; - - std::string str (buf); - return str; -} -*/ - -/* -int oldMain() { - // Set up ehternet network interface - setUpNetwork(); - - // Read message from USB - std::string msg = readStringFromUSB(); - - // Tweet message - tweet(msg); - return 0; -} -*/ \ No newline at end of file +} \ No newline at end of file