AP mode

Dependencies:   NetworkSocketAPI WizFi310Interface mbed

Fork of WizFi310_TCP_Echo_Server_Example by WIZnet

Committer:
maru536
Date:
Mon Oct 02 21:29:51 2017 +0000
Revision:
3:4b4eb90875bd
Parent:
2:8d119e9b8f5a
Child:
7:e5500683781b
Comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maru536 2:8d119e9b8f5a 1 #include "tts.h"
maru536 2:8d119e9b8f5a 2
maru536 2:8d119e9b8f5a 3
maru536 2:8d119e9b8f5a 4 void playTTS(VS1053 *player, TCPSocket *socket, char* buf) {
maru536 2:8d119e9b8f5a 5 int content_length;
maru536 2:8d119e9b8f5a 6 char text[200] = "";
maru536 2:8d119e9b8f5a 7 char c;
maru536 2:8d119e9b8f5a 8 socket->connect("api.voicerss.org", 80);
maru536 2:8d119e9b8f5a 9 url_encode(text, buf);
maru536 3:4b4eb90875bd 10 sprintf(buf, "GET /?key=fb82492d22b541ce8983bbea7d0d984b&hl=en-us&src=%s&c=mp3&f=8khz_8bit_mono&r=1 HTTP/1.1\nHost: api.voicerss.org\n\n", text);
maru536 2:8d119e9b8f5a 11 socket->send(buf, MAX_BUF_SIZE);
maru536 2:8d119e9b8f5a 12 extractVoiceData(socket, content_length);
maru536 2:8d119e9b8f5a 13 printf("play start! : %d\n", content_length);
maru536 2:8d119e9b8f5a 14 //player->sendDataBlock(socket, content_length);
maru536 2:8d119e9b8f5a 15 int sizeSent = 0;
maru536 2:8d119e9b8f5a 16 while (socket->recv(&c, sizeof(c)) >= 0 && sizeSent < content_length) {
maru536 2:8d119e9b8f5a 17 //printf("%c", c);
maru536 2:8d119e9b8f5a 18 sizeSent++;
maru536 2:8d119e9b8f5a 19 }
maru536 2:8d119e9b8f5a 20 printf("\nplay size: %d\n", sizeSent);
maru536 2:8d119e9b8f5a 21 }
maru536 2:8d119e9b8f5a 22
maru536 2:8d119e9b8f5a 23 void playTTS(TCPSocket *socket, char* buf) {
maru536 2:8d119e9b8f5a 24 int content_length;
maru536 2:8d119e9b8f5a 25 char text[200] = "";
maru536 2:8d119e9b8f5a 26 char c;
maru536 2:8d119e9b8f5a 27 socket->connect("api.voicerss.org", 80);
maru536 2:8d119e9b8f5a 28 url_encode(text, buf);
maru536 3:4b4eb90875bd 29 sprintf(buf, "GET /?key=fb82492d22b541ce8983bbea7d0d984b&hl=en-us&src=%s&c=mp3&f=8khz_8bit_mono&r=1 HTTP/1.1\nHost: api.voicerss.org\n\n", text);
maru536 2:8d119e9b8f5a 30 socket->send(buf, MAX_BUF_SIZE);
maru536 2:8d119e9b8f5a 31 extractVoiceData(socket, content_length);
maru536 2:8d119e9b8f5a 32 printf("play start! : %d\n", content_length);
maru536 2:8d119e9b8f5a 33 int sizeSent = 0;
maru536 2:8d119e9b8f5a 34 while (sizeSent < content_length) {
maru536 2:8d119e9b8f5a 35 if (socket->recv(&c, sizeof(c)) >= 0) {
maru536 2:8d119e9b8f5a 36 //printf("%d: %c\n", sizeSent, c);
maru536 2:8d119e9b8f5a 37 sizeSent++;
maru536 2:8d119e9b8f5a 38 }
maru536 2:8d119e9b8f5a 39 }
maru536 2:8d119e9b8f5a 40 printf("\nplay size: %d\n", sizeSent);
maru536 2:8d119e9b8f5a 41 }
maru536 2:8d119e9b8f5a 42
maru536 2:8d119e9b8f5a 43 void url_encode(char *dst, const char* src ) {
maru536 2:8d119e9b8f5a 44 int i, j = 0, len;
maru536 2:8d119e9b8f5a 45 char* tmp;
maru536 2:8d119e9b8f5a 46
maru536 2:8d119e9b8f5a 47 len = strlen( src );
maru536 2:8d119e9b8f5a 48 tmp = (char*) malloc( (sizeof(char) * 3 * len) +1 );
maru536 2:8d119e9b8f5a 49
maru536 2:8d119e9b8f5a 50 for( i = 0 ; i < len ; i++ ){
maru536 2:8d119e9b8f5a 51 if( IS_ALNUM( src[i] ) ) {
maru536 2:8d119e9b8f5a 52 tmp[j] = src[i];
maru536 2:8d119e9b8f5a 53 }
maru536 2:8d119e9b8f5a 54 else{
maru536 2:8d119e9b8f5a 55 snprintf( &tmp[j], 4, "%%%02X\n", (unsigned char)src[i] );
maru536 2:8d119e9b8f5a 56 j += 2;
maru536 2:8d119e9b8f5a 57 }
maru536 2:8d119e9b8f5a 58 j++;
maru536 2:8d119e9b8f5a 59 }
maru536 2:8d119e9b8f5a 60
maru536 2:8d119e9b8f5a 61 tmp[j] = 0;
maru536 2:8d119e9b8f5a 62 strcpy(dst, tmp);
maru536 2:8d119e9b8f5a 63 }
maru536 2:8d119e9b8f5a 64
maru536 2:8d119e9b8f5a 65 uint16_t strToUint16(char* str, int size)
maru536 2:8d119e9b8f5a 66 {
maru536 2:8d119e9b8f5a 67 uint16_t result = 0;
maru536 2:8d119e9b8f5a 68 for (int i = 0; i < size; i++) {
maru536 2:8d119e9b8f5a 69 if (str[i] > '9' || str[i] < '0') {
maru536 2:8d119e9b8f5a 70 break;
maru536 2:8d119e9b8f5a 71 }
maru536 2:8d119e9b8f5a 72
maru536 2:8d119e9b8f5a 73 result *= 10;
maru536 2:8d119e9b8f5a 74 result += str[i] - '0';
maru536 2:8d119e9b8f5a 75 }
maru536 2:8d119e9b8f5a 76
maru536 2:8d119e9b8f5a 77 return result;
maru536 2:8d119e9b8f5a 78 }
maru536 2:8d119e9b8f5a 79
maru536 2:8d119e9b8f5a 80 bool extractVoiceData(TCPSocket *socket, int &content_length) {
maru536 2:8d119e9b8f5a 81 bool isGetLength = false;
maru536 2:8d119e9b8f5a 82 char c;
maru536 2:8d119e9b8f5a 83 content_length = 0;
maru536 2:8d119e9b8f5a 84 char buf[MAX_NUMBER_SIZE] = "";
maru536 2:8d119e9b8f5a 85 while (socket->recv(&c, sizeof(c)) >= 0) {
maru536 2:8d119e9b8f5a 86 if (isGetLength == false && c == '\n') {
maru536 2:8d119e9b8f5a 87 for (int i = 0; i < MAX_HEADER_SIZE; i++) {
maru536 2:8d119e9b8f5a 88 if (socket->recv(&c, sizeof(c)) >= 0 && c == 'C' && socket->recv(&c, sizeof(c)) >= 0 && c == 'o' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' &&
maru536 2:8d119e9b8f5a 89 socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == 'e' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' &&
maru536 2:8d119e9b8f5a 90 socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == '-' && socket->recv(&c, sizeof(c)) >= 0 && c == 'L' &&
maru536 2:8d119e9b8f5a 91 socket->recv(&c, sizeof(c)) >= 0 && c == 'e' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' && socket->recv(&c, sizeof(c)) >= 0 && c == 'g' &&
maru536 2:8d119e9b8f5a 92 socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == 'h' && socket->recv(&c, sizeof(c)) >= 0 && c == ':' &&
maru536 2:8d119e9b8f5a 93 socket->recv(&c, sizeof(c)) >= 0 && c == ' ') {
maru536 2:8d119e9b8f5a 94 for(int j = 0; j < MAX_NUMBER_SIZE; j++) {
maru536 2:8d119e9b8f5a 95 if (socket->recv(&c, sizeof(c)) >= 0 && c == '\n') {
maru536 2:8d119e9b8f5a 96 buf[j] = 0;
maru536 2:8d119e9b8f5a 97 content_length = strToUint16(buf, sizeof(buf));
maru536 2:8d119e9b8f5a 98 isGetLength = true;
maru536 2:8d119e9b8f5a 99 break;
maru536 2:8d119e9b8f5a 100 }
maru536 2:8d119e9b8f5a 101 buf[j] = c;
maru536 2:8d119e9b8f5a 102 }
maru536 2:8d119e9b8f5a 103 }
maru536 2:8d119e9b8f5a 104 else {
maru536 2:8d119e9b8f5a 105 break;
maru536 2:8d119e9b8f5a 106 }
maru536 2:8d119e9b8f5a 107 }
maru536 2:8d119e9b8f5a 108 }
maru536 2:8d119e9b8f5a 109
maru536 2:8d119e9b8f5a 110 if (isGetLength == true && c == '\n') {
maru536 2:8d119e9b8f5a 111 for (int i = 0; i < 3; i++) {
maru536 2:8d119e9b8f5a 112 if (socket->recv(&c, sizeof(c)) >= 0 && c == '\n') {
maru536 2:8d119e9b8f5a 113 printf("content_length: %d\n", content_length);
maru536 2:8d119e9b8f5a 114 return true;
maru536 2:8d119e9b8f5a 115 }
maru536 2:8d119e9b8f5a 116 }
maru536 2:8d119e9b8f5a 117 }
maru536 2:8d119e9b8f5a 118 }
maru536 2:8d119e9b8f5a 119
maru536 2:8d119e9b8f5a 120 return false;
maru536 2:8d119e9b8f5a 121 }
maru536 2:8d119e9b8f5a 122
maru536 2:8d119e9b8f5a 123 void strcpy(unsigned char *dst, unsigned char *src)
maru536 2:8d119e9b8f5a 124 {
maru536 2:8d119e9b8f5a 125 int idx = 0;
maru536 2:8d119e9b8f5a 126 for (idx = 0; src[idx] != '\0'; idx++) {
maru536 2:8d119e9b8f5a 127 dst[idx] = src[idx];
maru536 2:8d119e9b8f5a 128 }
maru536 2:8d119e9b8f5a 129 dst[idx] = src[idx];
maru536 2:8d119e9b8f5a 130 }