/* * G R I D _ S E T U P * * In theory, the grid can be specified by providing any two of * these sets of parameters: * number of pixels (width, height) * viewsize (in model units, mm) * number of grid cells (cell_width, cell_height) * however, for now, it is required that the view size always be specified, * and one or the other parameter be provided. */ void grid_setup(void) { vect_t temp; mat_t toEye; if ( viewsize <= 0.0 ) bu_exit(EXIT_FAILURE, "viewsize <= 0"); /* model2view takes us to eye_model location & orientation */ MAT_IDN( toEye ); MAT_DELTAS_VEC_NEG( toEye, eye_model ); Viewrotscale[15] = 0.5*viewsize; /* Viewscale */ bn_mat_mul( model2view, Viewrotscale, toEye ); bn_mat_inv( view2model, model2view ); /* Determine grid cell size and number of pixels */ if ( cell_newsize ) { if ( cell_width <= 0.0 ) cell_width = cell_height; if ( cell_height <= 0.0 ) cell_height = cell_width; width = (viewsize / cell_width) + 0.99; height = (viewsize / (cell_height*aspect)) + 0.99; cell_newsize = 0; } else { /* Chop -1.0..+1.0 range into parts */ cell_width = viewsize / width; cell_height = viewsize / (height*aspect); } /* * Optional GIFT compatabilty, mostly for RTG3. * Round coordinates of lower left corner to fall on integer- * valued coordinates, in "gift_grid_rounding" units. */ if ( gift_grid_rounding > 0.0 ) { point_t v_ll; /* view, lower left */ point_t m_ll; /* model, lower left */ point_t hv_ll; /* hv, lower left*/ point_t hv_wanted; vect_t hv_delta; vect_t m_delta; mat_t model2hv; mat_t hv2model; /* Build model2hv matrix, including mm2inches conversion */ MAT_COPY( model2hv, Viewrotscale ); model2hv[15] = gift_grid_rounding; bn_mat_inv( hv2model, model2hv ); VSET( v_ll, -1, -1, 0 ); MAT4X3PNT( m_ll, view2model, v_ll ); MAT4X3PNT( hv_ll, model2hv, m_ll ); VSET( hv_wanted, floor(hv_ll[X]), floor(hv_ll[Y]), floor(hv_ll[Z]) ); VSUB2( hv_delta, hv_ll, hv_wanted ); MAT4X3PNT( m_delta, hv2model, hv_delta ); VSUB2( eye_model, eye_model, m_delta ); MAT_DELTAS_VEC_NEG( toEye, eye_model ); bn_mat_mul( model2view, Viewrotscale, toEye ); bn_mat_inv( view2model, model2view ); } /* Create basis vectors dx and dy for emanation plane (grid) */ VSET( temp, 1, 0, 0 ); MAT3X3VEC( dx_unit, view2model, temp ); /* rotate only */ VSCALE( dx_model, dx_unit, cell_width ); VSET( temp, 0, 1, 0 ); MAT3X3VEC( dy_unit, view2model, temp ); /* rotate only */ VSCALE( dy_model, dy_unit, cell_height ); if ( stereo ) { /* Move left 2.5 inches (63.5mm) */ VSET( temp, -63.5*2.0/viewsize, 0, 0 ); bu_log("red eye: moving %f relative screen (left)\n", temp[X]); MAT4X3VEC( left_eye_delta, view2model, temp ); VPRINT("left_eye_delta", left_eye_delta); } /* "Lower left" corner of viewing plane */ if ( rt_perspective > 0.0 ) { fastf_t zoomout; zoomout = 1.0 / tan( bn_degtorad * rt_perspective / 2.0 ); VSET( temp, -1, -1/aspect, -zoomout ); /* viewing plane */ /* * divergence is perspective angle divided by the number * of pixels in that angle. Extra factor of 0.5 is because * perspective is a full angle while divergence is the tangent * (slope) of a half angle. */ ap.a_diverge = tan( bn_degtorad * rt_perspective * 0.5 / width ); ap.a_rbeam = 0; } else { /* all rays go this direction */ VSET( temp, 0, 0, -1 ); MAT4X3VEC( ap.a_ray.r_dir, view2model, temp ); VUNITIZE( ap.a_ray.r_dir ); VSET( temp, -1, -1/aspect, 0 ); /* eye plane */ ap.a_rbeam = 0.5 * viewsize / width; ap.a_diverge = 0; } if ( NEAR_ZERO(ap.a_rbeam, SMALL) && NEAR_ZERO(ap.a_diverge, SMALL) ) bu_exit(EXIT_FAILURE, "zero-radius beam"); MAT4X3PNT( viewbase_model, view2model, temp ); if ( jitter & JITTER_FRAME ) { /* Move the frame in a smooth circular rotation in the plane */ fastf_t ang; /* radians */ fastf_t dx, dy; ang = curframe * frame_delta_t * bn_twopi / 10; /* 10 sec period */ dx = cos(ang) * 0.5; /* +/- 1/4 pixel width in amplitude */ dy = sin(ang) * 0.5; VJOIN2( viewbase_model, viewbase_model, dx, dx_model, dy, dy_model ); } if ( cell_width <= 0 || cell_width >= INFINITY || cell_height <= 0 || cell_height >= INFINITY ) { bu_log("grid_setup: cell size ERROR (%g, %g) mm\n", cell_width, cell_height ); bu_exit(EXIT_FAILURE, "cell size"); } if ( width <= 0 || height <= 0 ) { bu_log("grid_setup: ERROR bad image size (%d, %d)\n", width, height ); bu_exit(EXIT_FAILURE, "bad size"); } }
/** * Brute force through all possible plane intersections. * Generate all edge lines, then intersect the line with all * the other faces to find the vertices on that line. * If the geometry is correct, there will be no more than two. * While not the fastest strategy, this will produce an accurate * plot without requiring extra bookkeeping. * Note that the vectors will be drawn in no special order. */ int rt_arbn_plot(struct bu_list *vhead, struct rt_db_internal *ip, const struct rt_tess_tol *UNUSED(ttol), const struct bn_tol *tol, const struct rt_view_info *UNUSED(info)) { struct rt_arbn_internal *aip; size_t i; size_t j; size_t k; BU_CK_LIST_HEAD(vhead); RT_CK_DB_INTERNAL(ip); aip = (struct rt_arbn_internal *)ip->idb_ptr; RT_ARBN_CK_MAGIC(aip); for (i = 0; i < aip->neqn - 1; i++) { for (j = i + 1; j < aip->neqn; j++) { double dot; int point_count; /* # points on this line */ point_t a, b; /* start and end points */ vect_t dist; VSETALL(a, 0); VSETALL(b, 0); /* If normals are parallel, no intersection */ dot = VDOT(aip->eqn[i], aip->eqn[j]); if (BN_VECT_ARE_PARALLEL(dot, tol)) continue; /* Have an edge line, isect with all other planes */ point_count = 0; for (k = 0; k < aip->neqn; k++) { size_t m; point_t pt; size_t next_k; next_k = 0; if (k == i || k == j) continue; if (bn_mkpoint_3planes(pt, aip->eqn[i], aip->eqn[j], aip->eqn[k]) < 0) continue; /* See if point is outside arb */ for (m = 0; m < aip->neqn; m++) { if (i == m || j == m || k == m) continue; if (VDOT(pt, aip->eqn[m])-aip->eqn[m][3] > tol->dist) { next_k = 1; break; } } if (next_k != 0) continue; if (point_count <= 0) { RT_ADD_VLIST(vhead, pt, BN_VLIST_LINE_MOVE); VMOVE(a, pt); } else if (point_count == 1) { VSUB2(dist, pt, a); if (MAGSQ(dist) < tol->dist_sq) continue; RT_ADD_VLIST(vhead, pt, BN_VLIST_LINE_DRAW); VMOVE(b, pt); } else { VSUB2(dist, pt, a); if (MAGSQ(dist) < tol->dist_sq) continue; VSUB2(dist, pt, b); if (MAGSQ(dist) < tol->dist_sq) continue; bu_log("rt_arbn_plot() error, point_count=%d (>2) on edge %zu/%zu, non-convex\n", point_count+1, i, j); VPRINT(" a", a); VPRINT(" b", b); VPRINT("pt", pt); RT_ADD_VLIST(vhead, pt, BN_VLIST_LINE_DRAW); /* draw it */ } point_count++; } /* Point counts of 1 are (generally) not harmful, * occurring on pyramid peaks and the like. */ } } return 0; }
// ************************************************** // check matching and non-matching periodic nodes int ReadjustPeriodic(struct Nodelist *n, struct Ilist *psle, struct Ilist *ssle,struct Ilist *pste, struct Ilist *sste, int ge_num, int clock) { int i, j, k, knext; int ssnum, psnum; int *iss, *ips; double dphi, invpara; float u[3], v[3]; struct node **tmpps = NULL; struct node **tmpss = NULL; struct node **tmpssprev = NULL; #ifdef DEBUG_PERIODIC char *fn = "rr_debugperiodic.txt"; FILE *fp; if( (fp = fopen(fn,"w+")) == NULL ) { fprintf(stderr, "file '%s'\n", fn); exit(-1); } fprintf(stderr," ReadjustPeriodic:\n"); #endif // ************************************************** // calc. periodic angle from nodes u[2] = v[2] = 0.0; u[0] = n->n[psle->list[0]]->x; u[1] = n->n[psle->list[0]]->y; v[0] = n->n[ssle->list[0]]->x; v[1] = n->n[ssle->list[0]]->y; dphi = 2.0 * M_PI/(int)(floor(2.0 * M_PI/(acos(V_Angle(u,v)))+0.5 )); if(clock) dphi *= -1.0; #ifdef DEBUG_PERIODIC fprintf(fp,"acos(V_Angle(u,v)) = %f\n", acos(V_Angle(u,v)) * 180/M_PI); fprintf(fp, "psle[0] = %d, ssle[0] = %d, tmpps = %d, tmpss = %d\n" , psle->list[0], ssle->list[0], n->n[psle->list[0]]->index, n->n[ssle->list[0]]->index); fprintf(fp, "(*tmpps)->x = %f, (*tmpps)->y = %f, (*tmpss)->x = %f, (*tmpss)->y = %f, dphi = %f\n", n->n[psle->list[0]]->x, n->n[psle->list[0]]->y, n->n[ssle->list[0]]->x, n->n[ssle->list[0]]->y, dphi * 180/M_PI); fprintf(stderr,"dphi = %f\n", dphi *180/M_PI); #endif // ************************************************** // modify matching periodic nodes to fit better // recalc. ps-node's coord. from respective // ss node by rotation with dphi invpara = 1.0 - PERI_MOD_MATCH; #ifdef DEBUG_PERIODIC fprintf(stderr," matching: invpara = %f\n",invpara); #endif for(i = 0; i < ssle->num; i++) { #ifdef DEBUG_PERIODIC u[0] = n->n[ssle->list[i]]->x; u[1] = n->n[ssle->list[i]]->y; v[0] = n->n[psle->list[i]]->x; v[1] = n->n[psle->list[i]]->y; fprintf(fp," ids: %d, %d, angle = %f, phips - phiss = %f\n", n->n[ssle->list[i]]->id, n->n[psle->list[i]]->id, 180 / M_PI * acos(V_Angle(u,v)), 180 / M_PI * (n->n[psle->list[i]]->phi - n->n[ssle->list[i]]->phi)); #endif u[0] = n->n[ssle->list[i]]->x; u[1] = n->n[ssle->list[i]]->y; v[0] = u[0]*cos(dphi) - u[1]*sin(dphi); v[1] = u[0]*sin(dphi) + u[1]*cos(dphi); #ifdef DEBUG_PERIODIC fprintf(fp, " v[0] = %f, v[1] = %f, x = %f, y = %f\n", v[0], v[1], n->n[psle->list[i]]->x, n->n[psle->list[i]]->y); #endif n->n[psle->list[i]]->x = invpara * n->n[psle->list[i]]->x + PERI_MOD_MATCH * v[0]; n->n[psle->list[i]]->y = invpara * n->n[psle->list[i]]->y + PERI_MOD_MATCH * v[1]; n->n[psle->list[i]]->z = invpara * n->n[psle->list[i]]->z + PERI_MOD_MATCH * n->n[ssle->list[i]]->z; } // ************************************************** // modify non-matching nodes // interpolate node on ps from z coord. of ss // node and rotate this node to obtain new loc. // of ss node. ssnum = sste->num / ge_num; psnum = pste->num / ge_num; invpara = (1.0 - PERI_MOD); #ifdef DEBUG_PERIODIC fprintf(fp," ssnum = %d, psnum = %d, invpara = %f\n", ssnum, psnum, invpara); fprintf(stderr," non-matching: invpara = %f\n",invpara); #endif for(i = 0; i < ge_num; i++) { ips = &pste->list[psnum * i]; knext = 1; for(j = 0; j < psnum; j++) { iss = (&sste->list[ssnum * i]) + knext; for(k = knext; k < ssnum; k++) { tmpps = n->n + (*ips); tmpss = n->n + (*iss); tmpssprev = n->n + (*(iss-1)); #ifdef DEBUG_PERIODIC fprintf(fp," i: %d, j: %d, k: %d:\n", i, j, k); fprintf(stderr," i: %d, j: %d, k: %d:\n", i, j, k); fprintf(stderr," tmpss, tmpps: %d, %d\n",(*tmpss)->id, (*tmpps)->id); fprintf(stderr," iss = %d, iss-1 = %d\n", (*iss), (*(iss-1)) ); fprintf(stderr," sste->list[%d] = %d\n",ssnum * i + knext-1, sste->list[ssnum * i + knext-1]); fprintf(stderr," tmpssprev: %d\n",(*tmpssprev)->id); #endif if( ((*tmpps)->z <= (*tmpssprev)->z) && ((*tmpps)->z >= (*tmpss)->z)) { if( (knext = k - 1) <= 0) knext = 1; u[2] = (*tmpps)->z; u[0] = (*tmpssprev)->x + ( (*tmpss)->x - (*tmpssprev)->x) / ((*tmpss)->z - (*tmpssprev)->z) * (u[2] - (*tmpssprev)->z); u[1] = (*tmpssprev)->y + ( (*tmpss)->y - (*tmpssprev)->y) / ((*tmpss)->z - (*tmpssprev)->z) * (u[2] - (*tmpssprev)->z); v[0] = u[0]*cos(dphi) - u[1]*sin(dphi); v[1] = u[0]*sin(dphi) + u[1]*cos(dphi); #ifdef DEBUG_PERIODIC fprintf(fp,"ssnode, psnode: %d, %d\n",(*tmpss)->id, (*tmpps)->id); fprintf(fp,"tmpps->z = %f, u[2] = %f, tmpps->x = %f, tmpps->y = %f\n", (*tmpps)->z, u[2], (*tmpps)->x, (*tmpps)->y); VPRINTF(v,fp); fprintf(stderr,"ssnode, psnode: %d, %d\n",(*tmpss)->id, (*tmpps)->id); fprintf(stderr,"tmpps->z = %f, u[2] = %f, tmpps->x = %f, tmpps->y = %f\n", (*tmpps)->z, u[2], (*tmpps)->x, (*tmpps)->y); VPRINT(v); #endif (*tmpps)->x = invpara * ((*tmpps)->x) + PERI_MOD * v[0]; (*tmpps)->y = invpara * ((*tmpps)->y) + PERI_MOD * v[1]; #ifdef DEBUG_PERIODIC fprintf(stderr," ... brech ...\n!"); #endif break; } iss++; #ifdef DEBUG_PERIODIC fprintf(fp," i: %d, j: %d, k: %d:\n", i, j, k); fprintf(stderr,"end i: %d, j: %d, k: %d:\n", i, j, k); #endif continue; } ips++; } // end j, psnum } // end i, ge_num // ************************************************** #ifdef DEBUG_PERIODIC fclose(fp); #endif return 0; }
/* * M A K E _ N T S C _ X Y Z 2 R G B * * Create the map from * CIE XYZ perceptual space into * an idealized RGB space assuming NTSC primaries with D6500 white. * Only high-quality television-studio monitors are like this, but... */ void make_ntsc_xyz2rgb(fastf_t *xyz2rgb) { mat_t rgb2xyz; point_t tst, newpt; if (rt_clr__cspace_to_xyz(rgb_NTSC, rgb2xyz) == 0) bu_exit(EXIT_FAILURE, "make_ntsc_xyz2rgb() can't initialize color space\n"); bn_mat_inv(xyz2rgb, rgb2xyz); #if 1 /* Verify that it really works, I'm a skeptic */ VSET(tst, 1, 1, 1); MAT3X3VEC(newpt, rgb2xyz, tst); VPRINT("white_rgb (i)", tst); VPRINT("white_xyz (o)", newpt); VSET(tst, 0.313, 0.329, 0.358); MAT3X3VEC(newpt, xyz2rgb, tst); VPRINT("white_xyz (i)", tst); VPRINT("white_rgb (o)", newpt); VSET(tst, 1, 0, 0); MAT3X3VEC(newpt, rgb2xyz, tst); VPRINT("red_rgb (i)", tst); VPRINT("red_xyz (o)", newpt); VSET(tst, 0.670, 0.330, 0.000); MAT3X3VEC(newpt, xyz2rgb, tst); VPRINT("red_xyz (i)", tst); VPRINT("red_rgb (o)", newpt); VSET(tst, 0, 1, 0); MAT3X3VEC(newpt, rgb2xyz, tst); VPRINT("grn_rgb (i)", tst); VPRINT("grn_xyz (o)", newpt); VSET(tst, 0.210, 0.710, 0.080); MAT3X3VEC(newpt, xyz2rgb, tst); VPRINT("grn_xyz (i)", tst); VPRINT("grn_rgb (o)", newpt); VSET(tst, 0, 0, 1); MAT3X3VEC(newpt, rgb2xyz, tst); VPRINT("blu_rgb (i)", tst); VPRINT("blu_xyz (o)", newpt); VSET(tst, 0.140, 0.080, 0.780); MAT3X3VEC(newpt, xyz2rgb, tst); VPRINT("blu_xyz (i)", tst); VPRINT("blu_rgb (o)", newpt); #endif }
/*+++++++++++++ OnionServer-Class ++++++++++++++++++ */ OnionServer::OnionServer(B9CreatorSettings &b9CreatorSettings ): //m_onion( O_ONE_LOOP), //m_onion( O_THREADED),//never shutdown server m_onion( O_THREADED|O_DETACH_LISTEN|O_NO_SIGTERM ), //m_onion( O_ONE_LOOP|O_DETACH_LISTEN ), m_url(m_onion), m_mimedict(), m_mimes(), m_urls(), m_b9CreatorSettings(b9CreatorSettings) { //m_onion.setTimeout(5000); // Store used urls m_urls.push_back( "" ); m_urls.push_back("index.html"); m_urls.push_back("b9creator_settings.js"); m_urls.push_back("settings"); m_urls.push_back("files.js"); m_urls.push_back("files"); m_urls.push_back("messages"); m_urls.push_back("preview.png"); m_urls.push_back("update"); m_urls.push_back("jobtimings.js"); m_urls.push_back("jobtimings"); m_urls.push_back("^.*$"); // Store used mime types m_mimes.push_back("html"); m_mimes.push_back("text/html; charset: utf-8"); m_mimes.push_back("css"); m_mimes.push_back("text/css; charset: utf-8"); m_mimes.push_back("js"); m_mimes.push_back("application/javascript; charset: utf-8"); m_mimes.push_back("png"); m_mimes.push_back("image/png"); //Set mime types dict m_mimedict.add( m_mimes[0], m_mimes[1], 0); m_mimedict.add( m_mimes[2], m_mimes[3], 0); m_mimedict.add( m_mimes[4], m_mimes[5], 0); m_mimedict.add( m_mimes[6], m_mimes[7], 0); #ifndef WIN32 /* boost:filesystem require proper locale settings.*/ try { boost::filesystem::path::codecvt(); // Raises runtime error if current locale is invalid. } catch(std::runtime_error &e) { VPRINT("Locale settings produces runtime error. Enable LC_ALL=C as fallback."); setenv("LC_ALL", "C", 1); // Force C locale } #endif //add default signal handler. updateSignal.connect( boost::bind(&OnionServer::updateWebserver,this, _1, _2, _3) ); //add signal handler of b9CreatorSettings. updateSignal.connect( boost::bind(&B9CreatorSettings::webserverUpdateConfig,&b9CreatorSettings, _1, _2, _3) ); //start_server(); }
void Visualight::setup(char* _URL, uint16_t _PORT, uint8_t _wiflyLedFlag){ VPRINTLN(F("-SETUP-")); wiflyLedFlag = _wiflyLedFlag; URL = _URL; PORT = _PORT; //colorLED(255,255,255); fadeOn(); pinMode(resetButton, INPUT); pinMode(resetPin,OUTPUT); digitalWrite(resetButton, HIGH); digitalWrite(resetPin, HIGH); //attachInterrupt(4, processButton, CHANGE); #if DEBUG Serial.begin(115200); while(!Serial){ ; } #endif VPRINTLN(F("Starting")); VPRINT(F("Free memory: ")); int freeMem = wifly.getFreeMemory(); VPRINTLN(freeMem); Serial1.begin(9600); if (!wifly.begin(&Serial1,&Serial)) { VPRINTLN(F("Failed to start wifly")); //RESET WIFI MODULE -- Toggle reset pin } wifly.getDeviceID(devID,sizeof(devID)); if(strcmp(devID, "Visualight")==0){ VPRINTLN(F("ID SET")); } else{ VPRINTLN(F("ID NOT SET")); EEPROM.write(0,1); configureWifi(); } isServer = EEPROM.read(0); wifly.getMAC(MAC, sizeof(MAC)); VPRINT(F("MAC: ")); VPRINTLN(MAC); VPRINT(F("IP: ")); VPRINTLN(wifly.getIP(buf, sizeof(buf))); VPRINTLN(F("Ready")); if(isServer){ /* Create AP*/ VPRINTLN(F("Creating AP")); setAlert(0, 600000, 1, 0, 0, 255, 0); // Set a server timeout of 10 minutes setWiFlyLeds(1, true); VPRINTLN(F("Create server")); wifly.setSoftAP(); EEPROM.write(0, 1); } else{ VPRINTLN(F("Already Configured")); //isServer = false; if(wiflyLedFlag == 0){ setWiFlyLeds(0, true); //disable wifly LEDs, reboot }else { setWiFlyLeds(1, true); //enable wifly LEDs for debug, reboot } if(!connectToServer()){ reconnectCount++; } } }
void Visualight::processServer() { if (wifly.available() > 0) { // check for data from wifly if (wifly.gets(buf, sizeof(buf))) { /* See if there is a request */ if (strstr_P(buf, PSTR("GET /mac")) > 0) { /* GET request */ VPRINTLN(F("Got GET MAC request")); while (wifly.gets(buf, sizeof(buf)) > 0) { /* Skip rest of request */ } //int numNetworks = wifly.getNumNetworks(); sendMac(); wifly.flushRx(); // discard rest of input VPRINTLN(F("Sent Mac address")); wifly.close(); } else if (strstr_P(buf, PSTR("GET / ")) > 0) { // GET request VPRINTLN(F("Got GET request")); while (wifly.gets(buf, sizeof(buf)) > 0) { /* Skip rest of request */ } //int numNetworks = wifly.getNumNetworks(); wifly.flushRx(); // discard rest of input sendIndex(); VPRINTLN(F("Sent index page")); wifly.close(); } else if (strstr_P(buf, PSTR("POST")) > 0) { /* Form POST */ VPRINTLN(F("Got POST")); if (wifly.match(F("net="))) { /* Get posted field value */ //Serial.println(buf); memset(decodeBuffer, 0, 65); wifly.getsTerm(decodeBuffer, sizeof(decodeBuffer),'&'); urldecode(network, decodeBuffer); replaceAll(network,"+","$"); VPRINTLN(F("Got network: ")); VPRINTLN(network); if (wifly.match(F("pas="******"+","$"); //wifly.gets(security, 1); //no need for sizeof() -- it's a 1 or 2 VPRINTLN(F("Got password: "******"sec="))) { wifly.gets(security, sizeof(security)); VPRINTLN(F("Got security: ")); VPRINTLN(security); } } wifly.flushRx(); // discard rest of input VPRINT(F("Sending greeting page: ")); sendGreeting(network); //send greeting back delay(500); sendGreeting(network); //send a second time *just in case* delay(500); wifly.flushRx(); joinWifi(); } } else { /* Unexpected request */ delay(100); wifly.flushRx(); // discard rest of input VPRINTLN(F("Sending 404")); send404(); } } } }
// process all incoming server messages void Visualight::processClient(){ int available; if(millis()-lastHeartbeat > heartBeatInterval && reconnectCount == 0){ sendHeartbeat(); } if(millis()-connectTime > connectServerInterval){ VPRINTLN(F("reconnect from timeout")); //reconnect = true; //connectTime = millis(); if(!connectToServer()){ reconnectCount++; } } else { available = wifly.available(); if (available < 0) { VPRINT(F("reconnect from available()")); reconnectCount++; if(!connectToServer()){ //reconnectCount++; } } else if(available > 0){ connectTime = millis(); char thisChar; thisChar = wifly.read(); if( thisChar == 97){ wifly.readBytesUntil('x', serBuf, 31); int duration; int red, green, blue, white; sscanf(serBuf,"%i,%i,%i,%i,%i,%i,%i",&red,&green,&blue,&white,&duration,&_frequency,&_blinkType); // INDIGO v0.1.1 VPRINT("buf: "); VPRINTLN(serBuf); if(duration > 0 && _blinkType <=1){ //we are STARTING AN ALERT _durationTime = duration*1000; _frequency = (_frequency+1); //* 100; //get the right freq out //100 - 1000 setAlert(_blinkType, _durationTime, _frequency, red, green, blue, white); } else if(_blinkType == 2){ // we are setting the start color here saveStartColor(red,green,blue,white); } else if(_blinkType == 3){ // reset WiFi, become a server //this is sent from server when a bulb is deleted from a Visualight account. wifiReset(); //set isServer = true, turn on AP mode } else { //simple set color if(alerting){ _durationTime = 0; // will time out any currently running alert } setColor(red, green, blue, white); } memset(serBuf,0,31); } } } }
void Visualight::joinWifi(){ VPRINTLN(F("-JOINWIFI-")); /* Setup the WiFly to connect to a wifi network */ processButton(); //check button for reset VPRINT("network: "); VPRINTLN(network); VPRINT("password: "******"security: "); VPRINTLN(security); wifly.reboot(); wifly.setSSID(network); wifly.setAuth(0); if (security[0] == '1'){ //WPA2 VPRINTLN(F("type: WPA2")); wifly.setPassphrase(password); } else if (security[0] == '2'){ VPRINTLN(F("type: WEP-128")); wifly.setKey(password); } else if (security[0] == '3'){ VPRINTLN(F("type: OPEN")); // no setKey / passphrase } else if(security[0] == '4'){ //WEP-64 VPRINTLN(F("CONFIGURE WEP-64")); wifly.setAuth(8); wifly.setKey(password); } else { VPRINT(F("type UNKNOWN: ")); VPRINTLN(security[0]); } wifly.setJoin(WIFLY_WLAN_JOIN_AUTO); wifly.save(); //saving in setWiFlyLeds setWiFlyLeds(wiflyLedFlag, false); wifly.reboot(); if(!wifly.isAssociated()){ //-- not currently on a wifi network VPRINTLN(F("Joining network")); if (wifly.join()) { //-- joined VPRINTLN(F("Joined wifi network")); if(!connectToServer()){ reconnectCount++; } } else { //-- not able to join wifi network VPRINTLN(F("Failed to join wifi network")); delay(1000); for (int i=0; i<5; i++){ digitalWrite(_red, HIGH); delay(100); digitalWrite(_red, LOW); delay(100); } reconnectCount++; if(reconnectCount > 2){ reconnectCount = 0; wifiReset(); // sets light to server mode after wifi fail 3x // most likely bad WiFi credentials/security } else joinWifi(); } } else{ //-- associated with wifi network, continue on as normal if(isServer){ VPRINTLN(F("Switching to Client Mode")); EEPROM.write(0, 0); alerting = false; isServer = false; fadeOn(); // turn on LEDs to startColor (in EEPROM, white from factoryRestore) } if(!connectToServer()){ reconnectCount++; } } }