Library for httpserver
Fork of httpServer_with_Ethernt by
Handler/FsHandler.cpp@5:62b15cc96f1b, 2017-11-28 (annotated)
- Committer:
- wiznetw7500
- Date:
- Tue Nov 28 08:54:11 2017 +0000
- Revision:
- 5:62b15cc96f1b
- Parent:
- 4:2903435e3811
Internet Piano
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hjjeon | 0:e59cc54df17c | 1 | /* FsHandler.cpp */ |
hjjeon | 0:e59cc54df17c | 2 | #include "mbed.h" |
hjjeon | 0:e59cc54df17c | 3 | #include "FsHandler.h" |
hjjeon | 0:e59cc54df17c | 4 | //#define DEBUG |
hjjeon | 0:e59cc54df17c | 5 | #include "hl_debug.h" |
hjjeon | 0:e59cc54df17c | 6 | |
wiznetw7500 | 5:62b15cc96f1b | 7 | #define Do3 131 //C octave3 |
wiznetw7500 | 5:62b15cc96f1b | 8 | #define Do3s 139 //C# |
wiznetw7500 | 5:62b15cc96f1b | 9 | #define Re3 147 //D |
wiznetw7500 | 5:62b15cc96f1b | 10 | #define Re3s 156//D# |
wiznetw7500 | 5:62b15cc96f1b | 11 | #define Mi3 165 //E |
wiznetw7500 | 5:62b15cc96f1b | 12 | #define Fa3 175 //F |
wiznetw7500 | 5:62b15cc96f1b | 13 | #define Fa3s 185 //F# |
wiznetw7500 | 5:62b15cc96f1b | 14 | #define So3 196 //G |
wiznetw7500 | 5:62b15cc96f1b | 15 | #define So3s 208 //G# |
wiznetw7500 | 5:62b15cc96f1b | 16 | #define La3 220 //A |
wiznetw7500 | 5:62b15cc96f1b | 17 | #define La3s 233 //A# |
wiznetw7500 | 5:62b15cc96f1b | 18 | #define Ti3 247 //B |
wiznetw7500 | 5:62b15cc96f1b | 19 | #define Do4 262 //C octave4 |
wiznetw7500 | 5:62b15cc96f1b | 20 | #define Do4s 277 //C# |
wiznetw7500 | 5:62b15cc96f1b | 21 | #define Re4 294 //D |
wiznetw7500 | 5:62b15cc96f1b | 22 | #define Re4s 311//D# |
wiznetw7500 | 5:62b15cc96f1b | 23 | #define Mi4 330 //E |
wiznetw7500 | 5:62b15cc96f1b | 24 | #define Fa4 349 //F |
wiznetw7500 | 5:62b15cc96f1b | 25 | #define Fa4s 370 //F# |
wiznetw7500 | 5:62b15cc96f1b | 26 | #define So4 392 //G |
wiznetw7500 | 5:62b15cc96f1b | 27 | #define So4s 415 //G# |
wiznetw7500 | 5:62b15cc96f1b | 28 | #define La4 440 //A |
wiznetw7500 | 5:62b15cc96f1b | 29 | #define La4s 466 //A# |
wiznetw7500 | 5:62b15cc96f1b | 30 | #define Ti4 494 //B |
wiznetw7500 | 5:62b15cc96f1b | 31 | #define Do5 523 //C octave5 |
wiznetw7500 | 5:62b15cc96f1b | 32 | #define Do5s 554 //C# |
wiznetw7500 | 5:62b15cc96f1b | 33 | #define Re5 587 //D |
wiznetw7500 | 5:62b15cc96f1b | 34 | #define Re5s 622//D# |
wiznetw7500 | 5:62b15cc96f1b | 35 | #define Mi5 659 //E |
wiznetw7500 | 5:62b15cc96f1b | 36 | #define Fa5 699 //F |
wiznetw7500 | 5:62b15cc96f1b | 37 | #define Fa5s 740 //F# |
wiznetw7500 | 5:62b15cc96f1b | 38 | #define So5 784 //G |
wiznetw7500 | 5:62b15cc96f1b | 39 | #define So5s 831 //G# |
wiznetw7500 | 5:62b15cc96f1b | 40 | #define La5 880 //A |
wiznetw7500 | 5:62b15cc96f1b | 41 | #define La5s 932 //A# |
wiznetw7500 | 5:62b15cc96f1b | 42 | #define Ti5 988 //B |
wiznetw7500 | 5:62b15cc96f1b | 43 | |
wiznetw7500 | 5:62b15cc96f1b | 44 | PwmOut Buzzer(D3); |
wiznetw7500 | 5:62b15cc96f1b | 45 | |
wiznetw7500 | 5:62b15cc96f1b | 46 | float C_3 = 1000000/Do3, |
wiznetw7500 | 5:62b15cc96f1b | 47 | Cs_3 = 1000000/Do3s, |
wiznetw7500 | 5:62b15cc96f1b | 48 | D_3 = 1000000/Re3, |
wiznetw7500 | 5:62b15cc96f1b | 49 | Ds_3 = 1000000/Re3s, |
wiznetw7500 | 5:62b15cc96f1b | 50 | E_3 = 1000000/Mi3, |
wiznetw7500 | 5:62b15cc96f1b | 51 | F_3 = 1000000/Fa3, |
wiznetw7500 | 5:62b15cc96f1b | 52 | Fs_3 = 1000000/Fa3s, |
wiznetw7500 | 5:62b15cc96f1b | 53 | G_3 = 1000000/So3, |
wiznetw7500 | 5:62b15cc96f1b | 54 | Gs_3 = 1000000/So3s, |
wiznetw7500 | 5:62b15cc96f1b | 55 | A_3 = 1000000/La3, |
wiznetw7500 | 5:62b15cc96f1b | 56 | As_3 = 1000000/La3s, |
wiznetw7500 | 5:62b15cc96f1b | 57 | B_3 = 1000000/Ti3, |
wiznetw7500 | 5:62b15cc96f1b | 58 | C_4 = 1000000/Do4, |
wiznetw7500 | 5:62b15cc96f1b | 59 | Cs_4 = 1000000/Do4s, |
wiznetw7500 | 5:62b15cc96f1b | 60 | D_4 = 1000000/Re4, |
wiznetw7500 | 5:62b15cc96f1b | 61 | Ds_4 = 1000000/Re4s, |
wiznetw7500 | 5:62b15cc96f1b | 62 | E_4 = 1000000/Mi4, |
wiznetw7500 | 5:62b15cc96f1b | 63 | F_4 = 1000000/Fa4, |
wiznetw7500 | 5:62b15cc96f1b | 64 | Fs_4 = 1000000/Fa4s, |
wiznetw7500 | 5:62b15cc96f1b | 65 | G_4 = 1000000/So4, |
wiznetw7500 | 5:62b15cc96f1b | 66 | Gs_4 = 1000000/So4s, |
wiznetw7500 | 5:62b15cc96f1b | 67 | A_4 = 1000000/La4, |
wiznetw7500 | 5:62b15cc96f1b | 68 | As_4 = 1000000/La4s, |
wiznetw7500 | 5:62b15cc96f1b | 69 | B_4 = 1000000/Ti4, |
wiznetw7500 | 5:62b15cc96f1b | 70 | C_5 = 1000000/Do5, |
wiznetw7500 | 5:62b15cc96f1b | 71 | Cs_5 = 1000000/Do5s, |
wiznetw7500 | 5:62b15cc96f1b | 72 | D_5 = 1000000/Re5, |
wiznetw7500 | 5:62b15cc96f1b | 73 | Ds_5 = 1000000/Re5s, |
wiznetw7500 | 5:62b15cc96f1b | 74 | E_5 = 1000000/Mi5, |
wiznetw7500 | 5:62b15cc96f1b | 75 | F_5 = 1000000/Fa5, |
wiznetw7500 | 5:62b15cc96f1b | 76 | Fs_5 = 1000000/Fa5s, |
wiznetw7500 | 5:62b15cc96f1b | 77 | G_5 = 1000000/So5, |
wiznetw7500 | 5:62b15cc96f1b | 78 | Gs_5 = 1000000/So5s, |
wiznetw7500 | 5:62b15cc96f1b | 79 | A_5 = 1000000/La5, |
wiznetw7500 | 5:62b15cc96f1b | 80 | As_5 = 1000000/La5s, |
wiznetw7500 | 5:62b15cc96f1b | 81 | B_5 = 1000000/Ti5; |
wiznetw7500 | 5:62b15cc96f1b | 82 | |
wiznetw7500 | 5:62b15cc96f1b | 83 | int tones[] = {E_4, D_4, C_4, D_4, E_4, E_4, E_4, 0, D_4, D_4, D_4, 0, E_4, G_4, G_4, 0, |
wiznetw7500 | 5:62b15cc96f1b | 84 | E_4, D_4, C_4, D_4, E_4, E_4, E_4, 0, D_4, D_4, E_4, D_4, C_4, 0, 0, 0}; |
wiznetw7500 | 5:62b15cc96f1b | 85 | int tones_num = 32; |
wiznetw7500 | 5:62b15cc96f1b | 86 | |
wiznetw7500 | 5:62b15cc96f1b | 87 | void Tune(PwmOut name, int period); |
wiznetw7500 | 5:62b15cc96f1b | 88 | void Auto_tunes(PwmOut name, int period); |
wiznetw7500 | 5:62b15cc96f1b | 89 | void Stop_tunes(PwmOut name); |
wiznetw7500 | 5:62b15cc96f1b | 90 | |
hjjeon | 0:e59cc54df17c | 91 | DigitalOut led_red(LED1); |
hjjeon | 0:e59cc54df17c | 92 | DigitalOut led_green(LED2); |
hjjeon | 0:e59cc54df17c | 93 | DigitalOut led_blue(LED3); |
hjjeon | 0:e59cc54df17c | 94 | |
hjjeon | 0:e59cc54df17c | 95 | DigitalIn din(PC_14); |
hjjeon | 0:e59cc54df17c | 96 | |
hjjeon | 0:e59cc54df17c | 97 | static int matchstrings(const char* one, const char* two) |
hjjeon | 0:e59cc54df17c | 98 | { |
hjjeon | 0:e59cc54df17c | 99 | int m = 0; |
hjjeon | 0:e59cc54df17c | 100 | |
hjjeon | 0:e59cc54df17c | 101 | for (m = 0; m < min(strlen(one), strlen(two)) ; m++) { |
hjjeon | 0:e59cc54df17c | 102 | if (one[m] != two[m]) |
hjjeon | 0:e59cc54df17c | 103 | return m; |
hjjeon | 0:e59cc54df17c | 104 | } |
hjjeon | 0:e59cc54df17c | 105 | return m; |
hjjeon | 0:e59cc54df17c | 106 | } |
hjjeon | 0:e59cc54df17c | 107 | |
hjjeon | 0:e59cc54df17c | 108 | std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; |
hjjeon | 0:e59cc54df17c | 109 | |
hjjeon | 0:e59cc54df17c | 110 | HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp) |
hjjeon | 0:e59cc54df17c | 111 | : HTTPRequestHandler(Msg, Tcp) |
hjjeon | 0:e59cc54df17c | 112 | { |
hjjeon | 0:e59cc54df17c | 113 | m_rootPath = rootPath; |
hjjeon | 0:e59cc54df17c | 114 | m_localPath = localPath; |
hjjeon | 0:e59cc54df17c | 115 | |
hjjeon | 0:e59cc54df17c | 116 | string myPath = m_rootPath + m_localPath; |
hjjeon | 0:e59cc54df17c | 117 | |
hjjeon | 0:e59cc54df17c | 118 | // Now replace the virtual root path with a mounted device path |
hjjeon | 0:e59cc54df17c | 119 | std::map<const char*, const char*>::iterator it; |
hjjeon | 0:e59cc54df17c | 120 | const char *bestMatch = NULL; |
hjjeon | 0:e59cc54df17c | 121 | const char *bestMatchExchange = NULL; |
hjjeon | 0:e59cc54df17c | 122 | int match_ind = -1; |
hjjeon | 0:e59cc54df17c | 123 | for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) { |
hjjeon | 0:e59cc54df17c | 124 | // find best match (if the given logical path is containted in the root |
hjjeon | 0:e59cc54df17c | 125 | int s = matchstrings(myPath.c_str(), it->second); |
hjjeon | 0:e59cc54df17c | 126 | INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s); |
hjjeon | 0:e59cc54df17c | 127 | if ((s == strlen(it->second)) && (s > match_ind)) { |
hjjeon | 0:e59cc54df17c | 128 | match_ind = s; |
hjjeon | 0:e59cc54df17c | 129 | bestMatch = it->first; |
hjjeon | 0:e59cc54df17c | 130 | bestMatchExchange = it->second; |
hjjeon | 0:e59cc54df17c | 131 | } |
hjjeon | 0:e59cc54df17c | 132 | } |
hjjeon | 0:e59cc54df17c | 133 | |
hjjeon | 0:e59cc54df17c | 134 | if (bestMatch != NULL) { |
hjjeon | 0:e59cc54df17c | 135 | m_rootPath = bestMatch; |
hjjeon | 0:e59cc54df17c | 136 | m_localPath = string(myPath).substr(strlen(bestMatchExchange)); |
hjjeon | 0:e59cc54df17c | 137 | } |
hjjeon | 0:e59cc54df17c | 138 | |
hjjeon | 0:e59cc54df17c | 139 | handleRequest(); |
hjjeon | 0:e59cc54df17c | 140 | } |
hjjeon | 0:e59cc54df17c | 141 | |
hjjeon | 0:e59cc54df17c | 142 | HTTPFsRequestHandler::~HTTPFsRequestHandler() |
hjjeon | 0:e59cc54df17c | 143 | { |
hjjeon | 0:e59cc54df17c | 144 | } |
hjjeon | 0:e59cc54df17c | 145 | |
hjjeon | 0:e59cc54df17c | 146 | int HTTPFsRequestHandler::handleGetRequest() |
hjjeon | 0:e59cc54df17c | 147 | { |
hjjeon | 0:e59cc54df17c | 148 | HTTPHeaders headers; |
hjjeon | 0:e59cc54df17c | 149 | int retval = 0; //success |
wiznetw7500 | 5:62b15cc96f1b | 150 | uint8_t pin_state; |
hjjeon | 0:e59cc54df17c | 151 | |
wiznetw7500 | 5:62b15cc96f1b | 152 | if( std::string::npos != msg.uri.find("get_dio14.cgi") ) |
hjjeon | 0:e59cc54df17c | 153 | { |
wiznetw7500 | 5:62b15cc96f1b | 154 | if(din) |
wiznetw7500 | 5:62b15cc96f1b | 155 | pin_state = 1; |
wiznetw7500 | 5:62b15cc96f1b | 156 | else |
wiznetw7500 | 5:62b15cc96f1b | 157 | pin_state = 0; |
wiznetw7500 | 5:62b15cc96f1b | 158 | |
wiznetw7500 | 5:62b15cc96f1b | 159 | /* |
wiznetw7500 | 5:62b15cc96f1b | 160 | *len = sprintf((char *)buf, "DioCallback({\"dio_p\":\"14\",\ |
wiznetw7500 | 5:62b15cc96f1b | 161 | \"dio_s\":\"%d\"\ |
wiznetw7500 | 5:62b15cc96f1b | 162 | });", |
wiznetw7500 | 5:62b15cc96f1b | 163 | pin_state // Digital io status |
wiznetw7500 | 5:62b15cc96f1b | 164 | ); |
wiznetw7500 | 5:62b15cc96f1b | 165 | |
wiznetw7500 | 5:62b15cc96f1b | 166 | |
wiznetw7500 | 5:62b15cc96f1b | 167 | Tcp. |
wiznetw7500 | 5:62b15cc96f1b | 168 | */ |
justinkim | 2:dd293a10a772 | 169 | } |
hjjeon | 0:e59cc54df17c | 170 | else //read html pages |
hjjeon | 0:e59cc54df17c | 171 | { |
hjjeon | 0:e59cc54df17c | 172 | if (m_localPath.length() > 4) |
hjjeon | 0:e59cc54df17c | 173 | getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); |
hjjeon | 0:e59cc54df17c | 174 | else |
hjjeon | 0:e59cc54df17c | 175 | getStandardHeaders(headers); |
hjjeon | 0:e59cc54df17c | 176 | |
hjjeon | 0:e59cc54df17c | 177 | INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); |
hjjeon | 0:e59cc54df17c | 178 | |
hjjeon | 0:e59cc54df17c | 179 | std::string reqPath; |
hjjeon | 0:e59cc54df17c | 180 | |
hjjeon | 0:e59cc54df17c | 181 | // Check if we received a directory with the local bath |
hjjeon | 0:e59cc54df17c | 182 | if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { |
hjjeon | 0:e59cc54df17c | 183 | // yes, we shall append the default page name |
hjjeon | 0:e59cc54df17c | 184 | m_localPath += "index.html"; |
hjjeon | 0:e59cc54df17c | 185 | } |
hjjeon | 0:e59cc54df17c | 186 | |
hjjeon | 0:e59cc54df17c | 187 | reqPath = m_rootPath + m_localPath; |
hjjeon | 0:e59cc54df17c | 188 | |
hjjeon | 0:e59cc54df17c | 189 | INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); |
hjjeon | 0:e59cc54df17c | 190 | |
hjjeon | 0:e59cc54df17c | 191 | FILE *fp = fopen(reqPath.c_str(), "r"); |
hjjeon | 0:e59cc54df17c | 192 | if (fp != NULL) { |
hjjeon | 0:e59cc54df17c | 193 | char * pBuffer = NULL; |
hjjeon | 0:e59cc54df17c | 194 | int sz = 8192; |
hjjeon | 0:e59cc54df17c | 195 | while( pBuffer == NULL) { |
hjjeon | 0:e59cc54df17c | 196 | sz /= 2; |
hjjeon | 0:e59cc54df17c | 197 | pBuffer = (char*)malloc(sz); |
hjjeon | 0:e59cc54df17c | 198 | if (sz < 128) |
hjjeon | 0:e59cc54df17c | 199 | error ("OutOfMemory"); |
hjjeon | 0:e59cc54df17c | 200 | } |
hjjeon | 0:e59cc54df17c | 201 | |
hjjeon | 0:e59cc54df17c | 202 | // File was found and can be returned |
hjjeon | 0:e59cc54df17c | 203 | |
hjjeon | 0:e59cc54df17c | 204 | // first determine the size |
hjjeon | 0:e59cc54df17c | 205 | fseek(fp, 0, SEEK_END); |
hjjeon | 0:e59cc54df17c | 206 | long size = ftell(fp); |
hjjeon | 0:e59cc54df17c | 207 | fseek(fp, 0, SEEK_SET); |
hjjeon | 0:e59cc54df17c | 208 | |
hjjeon | 0:e59cc54df17c | 209 | startResponse(200, size); |
hjjeon | 0:e59cc54df17c | 210 | while(!feof(fp) && !ferror(fp)) { |
hjjeon | 0:e59cc54df17c | 211 | int cnt = fread(pBuffer, 1, sz , fp); |
hjjeon | 0:e59cc54df17c | 212 | if (cnt < 0) |
hjjeon | 0:e59cc54df17c | 213 | cnt = 0; |
hjjeon | 0:e59cc54df17c | 214 | processResponse(cnt, pBuffer); |
hjjeon | 0:e59cc54df17c | 215 | } |
hjjeon | 0:e59cc54df17c | 216 | delete pBuffer; |
hjjeon | 0:e59cc54df17c | 217 | endResponse(); |
hjjeon | 0:e59cc54df17c | 218 | fclose(fp); |
hjjeon | 0:e59cc54df17c | 219 | } |
hjjeon | 0:e59cc54df17c | 220 | else { |
hjjeon | 0:e59cc54df17c | 221 | retval = 404; |
hjjeon | 0:e59cc54df17c | 222 | ERR("Requested file was not found !"); |
hjjeon | 0:e59cc54df17c | 223 | } |
hjjeon | 0:e59cc54df17c | 224 | } |
hjjeon | 0:e59cc54df17c | 225 | |
hjjeon | 0:e59cc54df17c | 226 | return retval; |
hjjeon | 0:e59cc54df17c | 227 | } |
hjjeon | 0:e59cc54df17c | 228 | |
hjjeon | 0:e59cc54df17c | 229 | int HTTPFsRequestHandler::handlePostRequest() |
hjjeon | 0:e59cc54df17c | 230 | { |
wiznetw7500 | 5:62b15cc96f1b | 231 | int i = 0; |
hjjeon | 0:e59cc54df17c | 232 | int pin = 0; |
hjjeon | 0:e59cc54df17c | 233 | |
wiznetw7500 | 5:62b15cc96f1b | 234 | if( std::string::npos != msg.uri.find("set_LED.cgi") ) |
hjjeon | 0:e59cc54df17c | 235 | { |
hjjeon | 0:e59cc54df17c | 236 | pin = get_http_param_value("pin"); |
wiznetw7500 | 5:62b15cc96f1b | 237 | if(pin==0) Tune(Buzzer, C_4); |
wiznetw7500 | 5:62b15cc96f1b | 238 | else if(pin==1) Tune(Buzzer, Cs_4); |
wiznetw7500 | 5:62b15cc96f1b | 239 | else if(pin==2) Tune(Buzzer, D_4); |
wiznetw7500 | 5:62b15cc96f1b | 240 | else if(pin==3) Tune(Buzzer, Ds_4); |
wiznetw7500 | 5:62b15cc96f1b | 241 | else if(pin==4) Tune(Buzzer, E_4); |
wiznetw7500 | 5:62b15cc96f1b | 242 | else if(pin==5) Tune(Buzzer, F_4); |
wiznetw7500 | 5:62b15cc96f1b | 243 | else if(pin==6) Tune(Buzzer, Fs_4); |
wiznetw7500 | 5:62b15cc96f1b | 244 | else if(pin==7) Tune(Buzzer, G_4); |
wiznetw7500 | 5:62b15cc96f1b | 245 | else if(pin==8) Tune(Buzzer, Gs_4); |
wiznetw7500 | 5:62b15cc96f1b | 246 | else if(pin==9) Tune(Buzzer, A_4); |
wiznetw7500 | 5:62b15cc96f1b | 247 | else if(pin==10) Tune(Buzzer, As_4); |
wiznetw7500 | 5:62b15cc96f1b | 248 | else if(pin==11) Tune(Buzzer, B_4); |
wiznetw7500 | 5:62b15cc96f1b | 249 | else if(pin==12) Tune(Buzzer, C_5); |
wiznetw7500 | 5:62b15cc96f1b | 250 | else if(pin==13) Tune(Buzzer, Cs_5); |
wiznetw7500 | 5:62b15cc96f1b | 251 | else if(pin==14) Tune(Buzzer, D_5); |
wiznetw7500 | 5:62b15cc96f1b | 252 | else if(pin==15) Tune(Buzzer, Ds_5); |
wiznetw7500 | 5:62b15cc96f1b | 253 | else if(pin==16) Tune(Buzzer, E_5); |
wiznetw7500 | 5:62b15cc96f1b | 254 | else if(pin==17) Tune(Buzzer, F_5); |
wiznetw7500 | 5:62b15cc96f1b | 255 | else if(pin==18) Tune(Buzzer, Fs_5); |
wiznetw7500 | 5:62b15cc96f1b | 256 | else if(pin==19) Tune(Buzzer, G_5); |
wiznetw7500 | 5:62b15cc96f1b | 257 | else if(pin==20) Tune(Buzzer, Gs_5); |
wiznetw7500 | 5:62b15cc96f1b | 258 | else if(pin==21) Tune(Buzzer, A_5); |
wiznetw7500 | 5:62b15cc96f1b | 259 | else if(pin==22) Tune(Buzzer, As_5); |
wiznetw7500 | 5:62b15cc96f1b | 260 | else if(pin==23) Tune(Buzzer, B_5); |
hjjeon | 0:e59cc54df17c | 261 | else |
hjjeon | 0:e59cc54df17c | 262 | { |
hjjeon | 0:e59cc54df17c | 263 | WARN("Wrong pin number"); |
hjjeon | 0:e59cc54df17c | 264 | } |
hjjeon | 0:e59cc54df17c | 265 | |
hjjeon | 0:e59cc54df17c | 266 | return 0; |
wiznetw7500 | 5:62b15cc96f1b | 267 | } |
wiznetw7500 | 5:62b15cc96f1b | 268 | else if( std::string::npos != msg.uri.find("set_AUTO.cgi") ) |
wiznetw7500 | 5:62b15cc96f1b | 269 | { |
wiznetw7500 | 5:62b15cc96f1b | 270 | pin = get_http_param_value("pin"); |
wiznetw7500 | 5:62b15cc96f1b | 271 | if(pin==0) |
wiznetw7500 | 5:62b15cc96f1b | 272 | { |
wiznetw7500 | 5:62b15cc96f1b | 273 | for(i=0; i<tones_num; i++) |
wiznetw7500 | 5:62b15cc96f1b | 274 | { |
wiznetw7500 | 5:62b15cc96f1b | 275 | Auto_tunes(Buzzer, tones[i]); |
wiznetw7500 | 5:62b15cc96f1b | 276 | wait_ms(124); |
wiznetw7500 | 5:62b15cc96f1b | 277 | } |
wiznetw7500 | 5:62b15cc96f1b | 278 | Stop_tunes(Buzzer); |
wiznetw7500 | 5:62b15cc96f1b | 279 | } |
wiznetw7500 | 5:62b15cc96f1b | 280 | else if(pin == 99) |
wiznetw7500 | 5:62b15cc96f1b | 281 | { |
wiznetw7500 | 5:62b15cc96f1b | 282 | Stop_tunes(Buzzer); |
wiznetw7500 | 5:62b15cc96f1b | 283 | } |
wiznetw7500 | 5:62b15cc96f1b | 284 | else |
wiznetw7500 | 5:62b15cc96f1b | 285 | { |
wiznetw7500 | 5:62b15cc96f1b | 286 | WARN("Wrong pin number"); |
wiznetw7500 | 5:62b15cc96f1b | 287 | } |
wiznetw7500 | 5:62b15cc96f1b | 288 | |
wiznetw7500 | 5:62b15cc96f1b | 289 | return 0; |
wiznetw7500 | 5:62b15cc96f1b | 290 | } |
hjjeon | 0:e59cc54df17c | 291 | else |
hjjeon | 0:e59cc54df17c | 292 | { |
hjjeon | 0:e59cc54df17c | 293 | return 404; |
hjjeon | 0:e59cc54df17c | 294 | } |
hjjeon | 0:e59cc54df17c | 295 | } |
hjjeon | 0:e59cc54df17c | 296 | |
hjjeon | 0:e59cc54df17c | 297 | int HTTPFsRequestHandler::handlePutRequest() |
hjjeon | 0:e59cc54df17c | 298 | { |
hjjeon | 0:e59cc54df17c | 299 | return 404; |
hjjeon | 0:e59cc54df17c | 300 | } |
hjjeon | 0:e59cc54df17c | 301 | |
hjjeon | 0:e59cc54df17c | 302 | uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name) |
hjjeon | 0:e59cc54df17c | 303 | { |
hjjeon | 0:e59cc54df17c | 304 | uint8_t * name = 0; |
hjjeon | 0:e59cc54df17c | 305 | uint8_t * pos2; |
hjjeon | 0:e59cc54df17c | 306 | uint16_t len = 0; |
hjjeon | 0:e59cc54df17c | 307 | char value[10]; |
hjjeon | 0:e59cc54df17c | 308 | uint32_t ret = 0; |
hjjeon | 0:e59cc54df17c | 309 | |
hjjeon | 0:e59cc54df17c | 310 | //msg.attri |
hjjeon | 0:e59cc54df17c | 311 | if((name = (uint8_t *)strstr(msg.attri, param_name))) |
hjjeon | 0:e59cc54df17c | 312 | { |
hjjeon | 0:e59cc54df17c | 313 | name += strlen(param_name) + 1; |
hjjeon | 0:e59cc54df17c | 314 | pos2 = (uint8_t*)strstr((char*)name, "&"); |
hjjeon | 0:e59cc54df17c | 315 | if(!pos2) |
hjjeon | 0:e59cc54df17c | 316 | { |
hjjeon | 0:e59cc54df17c | 317 | pos2 = name + strlen((char*)name); |
hjjeon | 0:e59cc54df17c | 318 | } |
hjjeon | 0:e59cc54df17c | 319 | len = pos2 - name; |
hjjeon | 0:e59cc54df17c | 320 | |
hjjeon | 0:e59cc54df17c | 321 | if(len) |
hjjeon | 0:e59cc54df17c | 322 | { |
hjjeon | 0:e59cc54df17c | 323 | strncpy(value, (char*)name, len); |
hjjeon | 0:e59cc54df17c | 324 | ret = atoi(value); |
hjjeon | 0:e59cc54df17c | 325 | } |
hjjeon | 0:e59cc54df17c | 326 | } |
hjjeon | 0:e59cc54df17c | 327 | return ret; |
hjjeon | 0:e59cc54df17c | 328 | } |
hjjeon | 0:e59cc54df17c | 329 | |
wiznetw7500 | 5:62b15cc96f1b | 330 | /** |
wiznetw7500 | 5:62b15cc96f1b | 331 | * @brief Tune Function |
wiznetw7500 | 5:62b15cc96f1b | 332 | * @param name : Choose the PwmOut |
wiznetw7500 | 5:62b15cc96f1b | 333 | period : this param is tune value. (C_3...B_5) |
wiznetw7500 | 5:62b15cc96f1b | 334 | * @retval None |
wiznetw7500 | 5:62b15cc96f1b | 335 | */ |
wiznetw7500 | 5:62b15cc96f1b | 336 | void Tune(PwmOut name, int period) |
wiznetw7500 | 5:62b15cc96f1b | 337 | { |
wiznetw7500 | 5:62b15cc96f1b | 338 | name.period_us(period); |
wiznetw7500 | 5:62b15cc96f1b | 339 | name.write(0.50f); // 50% duty cycle |
wiznetw7500 | 5:62b15cc96f1b | 340 | wait_ms(250); // 1/4 beat |
wiznetw7500 | 5:62b15cc96f1b | 341 | name.period_us(0); // Sound off |
wiznetw7500 | 5:62b15cc96f1b | 342 | } |
justinkim | 2:dd293a10a772 | 343 | |
wiznetw7500 | 5:62b15cc96f1b | 344 | /** |
wiznetw7500 | 5:62b15cc96f1b | 345 | * @brief Auto tunes Function |
wiznetw7500 | 5:62b15cc96f1b | 346 | * @param name : Choose the PwmOut |
wiznetw7500 | 5:62b15cc96f1b | 347 | period : this param is tune value. (C_3...B_5) |
wiznetw7500 | 5:62b15cc96f1b | 348 | * @retval None |
wiznetw7500 | 5:62b15cc96f1b | 349 | */ |
wiznetw7500 | 5:62b15cc96f1b | 350 | void Auto_tunes(PwmOut name, int period) |
wiznetw7500 | 5:62b15cc96f1b | 351 | { |
wiznetw7500 | 5:62b15cc96f1b | 352 | name.period_us(period); |
wiznetw7500 | 5:62b15cc96f1b | 353 | name.write(0.50f); // 50% duty cycle |
wiznetw7500 | 5:62b15cc96f1b | 354 | wait_ms(250); // 1/4 beat |
wiznetw7500 | 5:62b15cc96f1b | 355 | } |
wiznetw7500 | 5:62b15cc96f1b | 356 | |
wiznetw7500 | 5:62b15cc96f1b | 357 | void Stop_tunes(PwmOut name) |
wiznetw7500 | 5:62b15cc96f1b | 358 | { |
wiznetw7500 | 5:62b15cc96f1b | 359 | name.period_us(0); |
wiznetw7500 | 5:62b15cc96f1b | 360 | } |