change to final_test
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient mbed-rtos mbed
Fork of Fianl_test by
Diff: main.cpp
- Revision:
- 1:4e629f9ac9e8
- Parent:
- 0:28f8ba171e86
- Child:
- 3:b834ab4a53d1
--- a/main.cpp Mon Apr 20 23:05:36 2015 +0000 +++ b/main.cpp Wed Apr 22 01:46:14 2015 +0000 @@ -4,9 +4,12 @@ #include "rtos.h" #include "EthernetInterface.h" #include "HTTPClient.h" +#include <sstream> +#include <string> +#include <iostream> uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin; - +Serial pc(USBTX, USBRX); DigitalIn in0(p11); DigitalIn in1(p12); DigitalIn in2(p13); @@ -34,35 +37,208 @@ lcd.line(0, 84 , 127, 84 , 0xFF0000); lcd.line(42, 0 , 42, 127, 0xFF0000); lcd.line(84, 0, 84, 127, 0xFF0000); - for(int i=0; i<3; i++) - { - for(int j=0; j<3; j++) - { - if(board[i][j] == 1) - { + for(int i=0; i<3; i++) { + for(int j=0; j<3; j++) { + if(board[i][j] == 0) { lcd.line( j*42+2 , i*42 + 2 ,(j+1)*42-2 , (i+1)*42-2 , GREEN); lcd.line( j*42+2 , (i+1)*42 - 2 ,(j+1)*42-2 , (i)*42+2 , GREEN); - } - else if(board[i][j] == 2) - { + } else if(board[i][j] == 1) { lcd.circle(42*j+21, 42*i+21, 19, BLUE); } } } } +typedef enum GAME_STATE { + STARTING, PLAYING, OVER, RETURN_ERROR = -1 +} GAME_STATE; +HTTPClient http; +EthernetInterface eth; +char str[4096]; +int StartNewGame() +{ + HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/New", str, 4096); + if (ret > 0) { + pc.printf("HTTP Error return in StartNewGame\n"); + return -1; + } + return atoi(str); +} +int JoinGame() +{ + HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/Join", str, 4096); + if (ret > 0) { + pc.printf("HTTP Error return in JoinGame\n"); + return -1; + } + return atoi(str); +} +ostringstream host; +string s; +const char* cstr; +GAME_STATE GetGameState(int gameId) +{ + //ostringstream host; + host.clear(); + host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=GameState"; + s = host.str(); + cstr = s.c_str(); + printf("**** HOST IS %s\n", cstr); + HTTPResult ret = http.get(cstr, str, 4096); + if (ret > 0) { + pc.printf("HTTP Error return in GetGameState\n"); + return (GAME_STATE)-1; + } + s = string(str); + if (s.find("\"GameState\":\"STARTING\"") != string::npos) + return STARTING; + else if(s.find("\"GameState\":\"PLAYING\"") != string::npos) + return PLAYING; + else if(s.find("\"GameState\":\"OVER\"") != string::npos) + return OVER; + else + return RETURN_ERROR; -HTTPClient http; -char str[2048]; -int main() { + return (GAME_STATE)atoi(str); +} +void ParseBoard(string str, int** board) +{ + + // printf("Entering ParseBoard, got %s\n", str.c_str()); + for (int i = 0; i < str.length(); i++){ + if (str[i] == '[' || str[i] == ']' || str[i] == ',' || str[i] == '"') + str[i] = ' '; + } + string temp; + for (int i = 0; i < str.length() - 1; i++){ + if (!(str[i] == ' ' && str[i+1] == ' ')){ + temp += str[i]; + } + + } + + str = temp; + // printf("With changed delimiters %s\n", str.c_str()); + stringstream stream(str); + int t = 0; + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + while(t < str.length() && str[t] == ' ') + t++; + board[i][j] = (int)(str[t] - '0'); + //stream >> board[i][j]; + } + } - EthernetInterface eth; + /* char c; + stream >> c; // eat [ + for (int i = 0; i < 3; i++) { + stream >> c; // eat [ + for (int j = 0; j < 2; j++) { + stream >> board[i][j]; + stream >> c ;// eat , + } + stream >> board[i][2]; + stream >> c ;// eat ] + if (i != 2) + stream >> c; // eat , + }*/ + /* for (int i = 0; i < 3; i++) { + printf("\n["); + for (int j = 0; j < 3; j++) + printf("%d ", board[i][j]); + printf("]"); + }*/ +} +int GetBoardState(int gameId, int** board) +{ + ostringstream host; + host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=BoardState"; + string s = host.str(); + HTTPResult ret = http.get(s.c_str(), str, 4096); + if (ret > 0) { + pc.printf("HTTP Error return in GetBoardState\n"); + return -1; + } + s = string(str); + int pos = s.find("\"BoardState\":"); + if (pos == string::npos) + return -1; + + ParseBoard(s.substr(pos +13, string::npos), board); + return 0; +} +string GameStateToString(GAME_STATE state) +{ + switch(state) { + case STARTING: + return "STARTING"; + case PLAYING: + return "PLAYING"; + case OVER: + return "OVER"; + default: + case RETURN_ERROR: + return "ERROR"; + } +} +int main() +{ + pc.printf("Ethernet connecting...\n"); eth.init(); //Use DHCP eth.connect(); - printf("IP Address is %s\n", eth.getIPAddress()); - printf("%d\n", sizeof(str)); - int ret = http.get("http://ece4180mbedonlinegaming.azurewebsites.net/odata/TicTacToeGames(1)?$select=BoardState", str, 2048); - printf("%d\n",ret); - printf("Result: %s\n", str); + pc.printf("IP Address is %s\n", eth.getIPAddress()); + pc.printf("Trying Start New Game...\n"); + int game = StartNewGame(); + wait(1); + pc.printf("Got: %d\n\n", game); + pc.printf("Trying GetGameState for Game %d...\n", game); + pc.printf("Got: %s\n\n", GameStateToString(GetGameState(game)).c_str()); + pc.printf("Trying Join Game...\n"); + game = JoinGame(); + wait(1); + pc.printf("Got: %d\n\n", game); + //pc.printf("Trying GetGameState for Game %d...\n", game); + //pc.printf("Got: %s\n\n", GameStateToString(GetGameState(game)).c_str()); + pc.printf("Trying GetBoardState for Game %d...\n", game); + Board b; + wait(1); + pc.printf("Got: %d\n\n", GetBoardState(game, b.get_board())); + pc.printf("Testing over****\n"); + /*pc.printf("%d\n", sizeof(str)); + HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/New", str, 4096);//http.get("http://4180.azurewebsites.net/", str, 4096); + if (ret > 0) + pc.printf("Return error\n"); + for (int i = 0; i < 4096;i++){ + if (str[i] == '1'|| str[i] == '2' || str[i] == '3' || str[i] == '4') + pc.printf("HERE %d\n", i); + pc.putc(str[i]); + pc.printf("\n"); + }*/ + return 0; + TCPSocketConnection socket; + + + int r = socket.connect("4180.azurewebsites.net", 80); + if (r != 0) + pc.printf("Failed to connect to 4180.azurewebsites.net:80!\r\n"); + char http_cmd[] = "GET / HTTP/1.0\n\n"; + socket.send_all(http_cmd, sizeof(http_cmd)-1); + + char buff[300]; + int rtn; + while (true) { + rtn = socket.receive(buff, sizeof(buff)-1); + if (rtn <= 0) + break; + buff[rtn] = '\0'; + pc.printf("Received %d chars from server:\n%s\n", rtn, buff); + } + pc.printf("Closing socket...\n"); + socket.close(); + eth.disconnect(); + /* int ret = http.get("http://ece4180mbedonlinegaming.azurewebsites.net/odata/TicTacToeGames(1)?$select=BoardState", str, 2048); + printf("%d\n",ret); + printf("Result: %s\n", str);*/ /* in0.mode(PullUp); in1.mode(PullUp); @@ -73,17 +249,17 @@ in6.mode(PullUp); in7.mode(PullUp); in8.mode(PullUp); - + Board board; int ** output = board.get_board(); printBoard(output); lcd.baudrate(3000000); - + lcd.line(0, 42 , 127, 42 , 0xFF0000); lcd.line(0, 84 , 127, 84 , 0xFF0000); lcd.line(42, 0 , 42, 127, 0xFF0000); lcd.line(84, 0, 84, 127, 0xFF0000); - + while (true) { if(!in0) printout = 1; @@ -105,8 +281,8 @@ printout = 9; else printout = 0; - - + + if(printout>0) { bool valid = board.updateBoard(turn, printout-1); @@ -136,8 +312,8 @@ } } Thread::wait(400); - } + } */ - + }