void Grid::print_points() const { // check indexing: if (!check()) { print_labels(); return; } for (unsigned int i = 0; i < gridSize(); i++) { // get index set: MultiIndexed::IndexSet is = i2x(i); // get Cartesian point: COORD_CART cp = getPoint(is); cout << "i = " << i << " --> "; for (unsigned int j = 0; j < cp.size(); j++) { cout << "x(" << j << ") = " << cp[j] << " "; } cout << endl; } }
int8_t GSwifi::writeHead (int8_t cid, uint16_t status_code) { char *cmd = PB("S0",1); cmd[ 1 ] = i2x(cid); escape( cmd ); serial_->print(P("HTTP/1.0 ")); char *msg; switch (status_code) { case 200: msg = P("200 OK"); break; case 400: msg = P("400 Bad Request"); break; case 404: msg = P("404 Not Found"); break; case 500: default: msg = P("500 Internal Server Error"); break; } serial_->println(msg); serial_->print("Access-Control-Allow-Origin: *\r\nServer: IRKit/"); serial_->println(version); serial_->println(P("Content-Type: text/plain\r\n")); }
// does recurse int8_t GSwifi::close (int8_t cid) { char *cmd = PB("AT+NCLOSE=0", 1); cmd[ 10 ] = i2x(cid); TIMER_STOP( timers_[cid] ); command(cmd, GSCOMMANDMODE_NORMAL); if (did_timeout_) { return -1; } return 0; }
void setPixelsFunction( SDL_Surface* surface, int ix0, int iy0, int ix1, int iy1, Function2d func ){ SDL_LockSurface( surface ); int nx = ix1-ix0; int ny = iy1-iy0; for (int iy=iy0; iy<iy1; iy++){ Uint32 * pixel = ((Uint32*)surface->pixels) + iy*surface->w + ix0; double y = i2y( iy ); for (int ix=ix0; ix<ix1; ix++){ double x = i2x( ix ); double f = func( x, y ); Uint8 fc = ( (Uint8)(255.0*f) ) & 0xFF; Uint32 color = (fc<<16)|(fc<<8)|fc | 0xFF000000; *pixel = color; pixel++; } } SDL_UnlockSurface( surface ); }
void draw(){ //SDL_SetRenderDrawColor(render, 128, 128, 128, 255); //SDL_RenderClear(render); SDL_RenderCopy( render, tempTex, &SrcR, &DestR); int mouseX,mouseY; SDL_GetMouseState( &mouseX, &mouseY ); double dfdx=0,dfdy=0; double x = i2x( mouseX ); double y = i2y( mouseY ); double f = warped_function( x, y, dfdx, dfdy ); SDL_SetRenderDrawColor(render, 255, 0, 0, 255); SDL_RenderDrawLine ( render, x2i( x ), y2i( y ), x2i( x - dfdx ), y2i( y - dfdy ) ); //printf( " %f %f %f %f \n", x, y, dfdx, dfdy ); SDL_RenderPresent( render ); SDL_UpdateWindowSurface(window); }
void Grid::print_indices() const { // check indexing: if (!check()) { print_labels(); return; } for (unsigned int i = 0; i < indexSize(); i++) { // get index set: MultiIndexed::IndexSet is = i2x(i); cout << "i = " << i << " --> "; for (unsigned int j = 0; j < is.size(); j++) { cout << "x(" << j << ") = " << is[j] << " "; } cout << "---> i = " << x2i(is); cout << endl; } }
void Grid::identifyPoints() { // clear lists: filterMap_i2g.clear(); filterMap_g2i.clear(); // prepare: MultiIndexed::IndexSetSet identical; vector<COORD_CART> checked; vector<int> checked_i; vector<COORD_CART> doubles; int duplicates = 0; int actLabel = 0; // loop: for (unsigned int i = 0; i < indexSize(); i++) { // current index set: MultiIndexed::IndexSet is = i2x(i); // get Cartesian point: COORD_CART cp = getPoint(is); // check if this has been found before: vector<COORD_CART>::iterator bef = find(checked.begin(), checked.end(), cp); // yes, this is a duplicate: if (bef != checked.end()) { // the old label: int i_old = 0; // search the original: vector<COORD_CART>::iterator found = find(doubles.begin(), doubles.end(), cp); // yes, found it: if (found != doubles.end()) { // get position: int pos = found - doubles.begin(); i_old = (identical[pos])[0]; // write: identical[pos].push_back(i); // tell: cout << gridName << ": Found duplicate. Identify (" << String(is) << ") with (" << String(i2x(i_old)) << ")." << endl; // this reduces the grid size, so count: duplicates++; } // no, it's new: else { // add to lists: doubles.push_back(cp); i_old = bef - checked.begin(); MultiIndexed::IndexSet newpair(2); newpair[0] = i_old; newpair[1] = i; identical.push_back(newpair); // tell: cout << gridName << ": Found duplicate. Identify (" << String(is) << ") with (" << String(i2x( (identical.back())[0])) << ")" << endl; // this reduces the grid size, so count: duplicates++; } // write original label to filter: filterMap_i2g.push_back(i_old); } // no, this is not a duplicate: else { // give label: filterMap_i2g.push_back(actLabel); filterMap_g2i.push_back(i); actLabel++; // mark as checked: checked.push_back(cp); checked_i.push_back(i); } } // reduce grid size: gSize -= duplicates; cout<<"i2gMap: " << endl; cout<<String(filterMap_i2g)<<endl; cout<<"g2iMap: " << endl; cout<<String(filterMap_g2i)<<endl; cout << gridName << ": Index size = " << indexSize() << ", grid size = " << gridSize() << endl; }
vector<IO::OFILE::Data> Grid::getOData(const string & type) const { vector<IO::OFILE::Data> out; //////////////////////////////////////////////////////////// // // Text file output: // if (type.compare(IO::OFILE::TYPE::TXT) == 0) { // add a piece of data: points IO::OFILE::Data dat; // set pre and post fields: dat.type = type; dat.origin = gridName; // Output Cartesian points: if (flag_output_cart) { dat.name = "grid_points_cartesian"; dat.pre_data = "// " + gridName + ": " + String(gSize) + " points in Cartesian space (" + String(getN()) + ")\n"; for (unsigned int g = 0; g < gridSize(); ++g) { // pick a coordinate: const MultiIndexed::IndexSet coord = g2x(g); // get Cartesian coordinate: COORD_GRID x = getPoint(coord); // write: dat.data += String(x) + " "; } } // grid point output: else { dat.name = "grid_points"; dat.pre_data = "// " + gridName + ": " + String(indexSize()) + " points in grid space (" + String(getN()) + ")\n"; for (unsigned int i = 0; i < indexSize(); i++) { // pick a coordinate: MultiIndexed::IndexSet coord = i2x(i); // get Cartesian coordinate: COORD_GRID x = getGridPoint(coord); // write: dat.data += String(x) + " "; } } // write points: dat.data += "\n"; out.push_back(dat); } //////////////////////////////////////////////////////////// // // VTK ascii file output: // else if (type.compare(IO::OFILE::TYPE::VTK_ASCII) == 0) { // add a piece of data: points IO::OFILE::Data dat; // set pre and post fields: dat.type = type; dat.origin = gridName; // Output Cartesian points: if (flag_output_cart) { // meta data for grid points: vector<int> dims = getN(); dims.resize(3, 1); dat.name = "grid_points_cartesian"; dat.pre_data = String("DATASET UNSTRUCTURED_GRID\n") // + "DIMENSIONS " + IO::vi2s(dims) + "\n" + "POINTS " + String(gridSize()) + " \n"; for (unsigned int g = 0; g < gridSize(); g++) { // pick a coordinate: MultiIndexed::IndexSet coord = g2x(g); // get Cartesian coordinate: COORD_GRID x = getPoint(coord); // vtk expects 3 dimensions: x.resize(3, 0); // write: dat.data += String(x) + " "; } } // grid point output: else { // meta data for grid points: vector<int> dims = getN(); dims.resize(3, 1); dat.name = "grid_points"; dat.pre_data = String("DATASET STRUCTURED_GRID\n") + "DIMENSIONS " + String(dims) + "\n" + "POINTS " + String(indexSize()) + " \n"; for (unsigned int i = 0; i < indexSize(); i++) { // pick a coordinate: MultiIndexed::IndexSet coord = i2x(i); // get Cartesian coordinate: COORD_GRID x = getPoint(coord); // vtk expects 3 dimensions: x.resize(3, 0); // write: dat.data += String(x) + " "; } } // write points: dat.data += "\n"; out.push_back(dat); // add new piece of data: cells dat = IO::OFILE::Data(); // set pre and post fields: dat.type = type; dat.origin = gridName; dat.pre_data = "CELLS " + String(cells()) + " "; dat.data = ""; // Output in Cartesian space: if (flag_output_cart) { dat.name = "grid_cells_cartesian"; // count data output: int dcounter = 0; // cell output: for(int i = 0; i < cells(); i++) { // get cell: MultiIndexed::IndexCell cell = getIndexCell(i); // grid filter: for(unsigned j = 0; j < cell.size(); j++) cell[j] = filter_i2g(cell[j]); reduceDup_v(cell); // write cell: dat.data += String(cell.size()); dcounter++; for(unsigned j = 0; j < cell.size(); j++){ cout<<String(cell[j])<<" -> "<<String(x2g(cell[j]))<<endl; dat.data += " " + String(x2g(cell[j])); dcounter++; } cout<<endl; dat.data += "\n"; } dat.pre_data += String(dcounter) + "\n"; } dat.data += "\n"; out.push_back(dat); } return out; }
int8_t GSwifi::join (GSSECURITY sec, const char *ssid, const char *pass, int dhcp, char *name_) { char *cmd; uint8_t offset; if (joined_) { return -1; } disconnect(); // infrastructure mode command(PB("AT+WM=0",1), GSCOMMANDMODE_NORMAL); // set DHCP name with mac address cmd = PB("AT+NDHCP=1,%",1); strcpy( cmd+11, hostname()); command(cmd, GSCOMMANDMODE_NORMAL); switch (sec) { case GSSECURITY_NONE: case GSSECURITY_OPEN: case GSSECURITY_WEP: command(PB("AT+WSEC=0",1), GSCOMMANDMODE_NORMAL); cmd = PB("AT+WAUTH=%",1); cmd[ 9 ] = i2x(sec); command(cmd, GSCOMMANDMODE_NORMAL); if (sec != GSSECURITY_NONE) { // key are either 10 or 26 hexadecimal digits corresponding to a 40- bit or 104-bit key cmd = PB("AT+WWEP1=%",1); strcpy(cmd+9, pass); command(cmd, GSCOMMANDMODE_NORMAL); } cmd = PB("AT+WA=%",1); strcpy(cmd+6, ssid); command(cmd, GSCOMMANDMODE_DHCP, GS_TIMEOUT_LONG); // normal people don't understand difference between wep open/shared. // so we try shared first, and when it failed, try open if (gs_failure_ && (sec == GSSECURITY_WEP)) { return join(GSSECURITY_OPEN, ssid, pass, dhcp, name_); } break; case GSSECURITY_WPA_PSK: command(PB("AT+WAUTH=0",1), GSCOMMANDMODE_NORMAL); cmd = PB("AT+WWPA=%",1); strcpy( cmd+8, pass); command(cmd, GSCOMMANDMODE_NORMAL, GS_TIMEOUT_LONG); cmd = PB("AT+WA=%",1); strcpy( cmd+6, ssid); command(cmd, GSCOMMANDMODE_DHCP, GS_TIMEOUT_LONG); break; case GSSECURITY_WPA2_PSK: command(PB("AT+WAUTH=0",1), GSCOMMANDMODE_NORMAL); cmd = PB("AT+WPAPSK=%,%",1); offset = 10; strcpy( cmd+offset, ssid ); offset += strlen(ssid); cmd[ offset ] = ','; offset += 1; strcpy( cmd+offset, pass ); command(cmd, GSCOMMANDMODE_NORMAL, GS_TIMEOUT_LONG); cmd = PB("AT+WA=%",1); strcpy( cmd+6, ssid); command(cmd, GSCOMMANDMODE_DHCP, GS_TIMEOUT_LONG); if (gs_failure_) { return join(GSSECURITY_WPA_PSK, ssid, pass, dhcp, name_); } break; default: return -1; } if (did_timeout_) { return -1; } if (gs_failure_) { return -1; } joined_ = true; return 0; }
// returns -1 on error, >0 on success int8_t GSwifi::request(GSwifi::GSMETHOD method, const char *path, const char *body, uint16_t length, GSwifi::GSResponseHandler handler, uint8_t timeout, uint8_t is_binary) { char *cmd; #ifdef TESTER // Tester only requests against limited AP test target // DNSLOOKUP should fail, because it's in limitedAP mode, ignore it. sprintf( ipaddr_, "%s", "192.168.1.1" ); #else cmd = PB("AT+DNSLOOKUP=" DOMAIN,1); command(cmd, GSCOMMANDMODE_DNSLOOKUP); #endif // modified by eqiglii 2015-12-12 if ( strstr (path, "proxy.php") ) { // this means it is my temperature reporting service, which should go via my proxy server sprintf( ipaddr_, "%s", "192.168.0.18" ); // my raspberrypi IP address, it changes depending on connected home devices } // end by eqiglii 2015-12-12 // don't fail fatally on dnslookup failure, // we cache our server's ipaddress anyway // it happens randomly if (ipaddr_[ 0 ] == 0) { // but if it has not been resolved yet, // we must be not connected to internet, // let's show yellow blink on colorled Serial.println ("DOMAIN is not resolved to ipaddr"); // print log, added by eqiglii clear(); on_disconnect_(); return -1; } cmd = PB("AT+NCTCP=",1); strcpy( cmd+9, ipaddr_ ); #ifdef TESTER strcpy( cmd+9+strlen(ipaddr_), ",80"); #else // modified by eqiglii 2015-12-12 if ( strstr (path, "proxy.php") ) { // this means it is my temperature reporting service, which should go via my proxy server strcpy( cmd+9+strlen(ipaddr_), ",80"); } else { // this means it is normal IRKit post data strcpy( cmd+9+strlen(ipaddr_), ",443"); } #endif connected_cid_ = CID_UNDEFINED; command(cmd, GSCOMMANDMODE_CONNECT); int8_t cid = connected_cid_; char xid = i2x(cid); if (did_timeout_) { return -1; } if ((0 <= cid) && (cid <= 16)) { // OK } else { return -1; } handlers_[ cid ] = handler; #ifndef TESTER // modified by eqiglii 2015-12-12 if ( strstr (path, "proxy.php") == NULL ) { // this means it is normal IRKit post data, NOT my temperature reporting service cmd = PB("AT+SSLOPEN=%,cacert",1); cmd[ 11 ] = xid; command(cmd, GSCOMMANDMODE_NORMAL); } #endif // disable TCP_MAXRT and TCP_KEEPALIVE_X, because these commands takes some time to issue, // and GS1011MIPS denies short TCP_KEEPALIVE_INTVL, // and our server (currently Heroku) disconnects connections after 30seconds, // and it's difficult to handle incoming requests while sending these. // TCP_MAXRT = 10 // AT+SETSOCKOPT=0,6,10,10,4 // cmd = PB("AT+SETSOCKOPT=%,6,10,10,4",1); // cmd[ 14 ] = xid; // command(cmd, GSCOMMANDMODE_NORMAL); // Enable TCP_KEEPALIVE on this socket // AT+SETSOCKOPT=0,65535,8,1,4 // cmd = PB("AT+SETSOCKOPT=%,65535,8,1,4",1); // cmd[ 14 ] = xid; // command(cmd, GSCOMMANDMODE_NORMAL); // TCP_KEEPALIVE_PROBES = 2 // AT+SETSOCKOPT=0,6,4005,2,4 // cmd = PB("AT+SETSOCKOPT=%,6,4005,2,4",1); // cmd[ 14 ] = xid; // command(cmd, GSCOMMANDMODE_NORMAL); // TCP_KEEPALIVE_INTVL = 150 // AT+SETSOCKOPT=0,6,4001,150,4 // mysteriously, GS1011MIPS denies with "ERROR: INVALID INPUT" for seconds less than 150 // cmd = PB("AT+SETSOCKOPT=%,6,4001,150,4",1); // cmd[ 14 ] = xid; // command(cmd, GSCOMMANDMODE_NORMAL); // if (did_timeout_) { // return -1; // } cmd = PB("S0",1); cmd[ 1 ] = xid; escape( cmd ); if (method == GSMETHOD_POST) { serial_->print(P("POST ")); } else { serial_->print(P("GET ")); } serial_->print(path); serial_->print(P(" HTTP/1.1\r\nUser-Agent: IRKit/")); serial_->println(version); // modified by eqiglii 2015-12-12 if ( strstr (path, "proxy.php") ) { // this means it is my temperature reporting service, which should go via my proxy server serial_->println("Host: 192.168.0.18"); // my raspberrypi IP address, it changes depending on connected home devices //serial_->println("Host: raspberrypi.local"); // this does NOT work } else { // this means it is normal IRKit service serial_->println("Host: " DOMAIN); } // end by eqiglii 2015-12-12 //serial_->println("Host: " DOMAIN); if (method == GSMETHOD_POST) { serial_->print(P("Content-Length: ")); serial_->println( is_binary ? base64_length(length) : length ); serial_->println("Content-Type: application/x-www-form-urlencoded\r\n"); if (is_binary) { base64_encode((const uint8_t*)body, length, &base64encoded); } else { serial_->print(body); } } else { serial_->println(); } // we're long polling here, to receive other events, we're going back to our main loop // ignore timeout, we always timeout here escape( "E" ); TIMER_START(timers_[cid], timeout); return cid; }