void startup(int argc, char *argv[]) { Dampkt dam_p; char tmp; char *ep = &tmp; ulong start = 0; long sluimer; int period = 0; int tmpperiod, tmp_alpha, tmp_beta, tmp_ftable_alpha, tmp_eij, tmp_maxhops; strncpy(dam_myID, (char *)NIC_OUI, DAM_ID_LEN); hdlr_install(); print("DAM node [%s] installed vector code.\n", dam_myID); devmac_ctl(NIC_NCR_WRITE, NIC_CMD_POWERUP, 0); print("DAM node [%s] powered up NIC.\n", dam_myID); dam_period = DAM_PROTOCOL_PERIOD; if (argc == 6) { tmpperiod = strtol(argv[0], &ep, 0); if (*ep != '\0') { printf("Invalid DAM period supplied as argument.\n"); } else { dam_period = tmpperiod; printf("Set dam_period to [%d] usecs\n", dam_period); } tmp_alpha = strtol(argv[1], &ep, 0); if (*ep != '\0') { printf("Invalid EAR alpha supplied as argument.\n"); } tmp_beta = strtol(argv[2], &ep, 0); if (*ep != '\0') { printf("Invalid EAR beta supplied as argument.\n"); } tmp_ftable_alpha = strtol(argv[3], &ep, 0); if (*ep != '\0') { printf("Invalid EAR ftable_alpha supplied as argument.\n"); } tmp_eij = strtol(argv[4], &ep, 0); if (*ep != '\0') { printf("Invalid EAR eij supplied as argument.\n"); } tmp_maxhops = strtol(argv[5], &ep, 0); if (*ep != '\0') { printf("Invalid EAR maxhops supplied as argument.\n"); } Ear = ear_init(dam_myID, 0, tmp_alpha, tmp_beta, tmp_ftable_alpha, tmp_eij, tmp_maxhops, (uchar)devloc_getxloc(), (uchar)devloc_getyloc(), (uchar)devloc_getzloc()); fprintf(stderr, "DAM node [%s] completed EAR init with runtime args:\n" "\talpha=%d\n\tbeta=%d\n\tftable_alpha=%d\n\teij=%d\n\tmaxhops=%d\n\n\n\n", dam_myID, tmp_alpha, tmp_beta, tmp_ftable_alpha, tmp_eij, tmp_maxhops); } else { Ear = ear_init(dam_myID, 0, 1, 50, 1, 1 /* eij */, 8 /* maxhops */, (uchar)devloc_getxloc(), (uchar)devloc_getyloc(), (uchar)devloc_getzloc()); print("DAM node [%s] completed EAR init using defaults.\n\n\n", dam_myID); } /* */ /* Implemented to mirror description in paper */ /* All variables beginning w/ dam_ correspond */ /* to variables in the paper's algorithm descr. */ /* */ while (1) { start = devrtc_getusecs(); dam_p.timestamp = start; /* */ /* Write the log for the previous period. We want all actions to */ /* be in the timed loop, and though this log writing may seem to */ /* be not inherent to application, you can think of it as some */ /* post-peak detection actions that the algorithm must perform. */ /* */ if ((period > 0) && dam_participating) { if (strlen(dam_sink) > 0) { int id; char tmp; char *ep = &tmp; id = strtol(dam_leaderID, &ep, 0); ear_response(Ear, dam_sink, NULL, 0, id); } //print("\n\ndam_sink = [%s]\n\n\n", dam_sink); write_log(period - 1); } /* */ /* The values in this case are in Lux (see test.m) */ /* Noise floor is 0.1 Lux. DAM_THRESHOLD_ELECTION is */ /* thus set to 10 Lux. */ /* */ dam_myPr = devsignal_read(LIGHT_SENSOR); /* */ /* Algorithm description in PARC paper does not reset */ /* maxPrHeard. For each DAM period, until a packet is */ /* received, or our local reading is > threshold, the */ /* maxPrHeard should be 0. */ /* */ dam_maxPrHeard = 0; if (dam_myPr > DAM_THRESHOLD_ELECTION) { LOGMARK(12); dam_participating = TRUE; dam_maxPrHeard = dam_myPr; strncpy(dam_leaderID, dam_myID, DAM_ID_LEN); dam_p.maxPr = dam_p.transPr = dam_myPr; strncpy((char *)dam_p.transID, dam_myID, DAM_ID_LEN); strncpy((char *)dam_p.maxID, dam_myID, DAM_ID_LEN); dam_broadcast(&dam_p); LOGMARK(13); } else { dam_participating = FALSE; } sluimer = dam_period - (devrtc_getusecs() - start); sluimer = max(sluimer, 0); LOGMARK(2); xusleep(sluimer); LOGMARK(3); period++; } return; }
void triangulate ( int nvertices, int xmin, int xmax, int ymin, int ymax, int scale ) /******************************************************************************/ /* Purpose: TRIANGULATE prints N-3 diagonals which triangulate the polygon. Modified: 30 April 2007 Author: Joseph O'Rourke Parameters: Input, int NVERTICES, the number of vertices. Input, int XMIN, XMAX, YMIN, YMAX, the minimum and maximum X and Y values of the coordinates of the vertices of the polygon. Input, int SCALE, an appropriate scaling for the data. */ { tVertex v0, v1, v2, v3, v4; /* five consecutive vertices */ int x; int y; int n = nvertices; /* N is the "current" number of vertices; It starts at NVERTICES, but shrinks to 3. */ ear_init ( ); printf ( "\n" ); printf ( "newpath\n" ); /* Each step of outer loop removes one ear. */ while ( 3 < n ) { /* Inner loop searches for an ear. */ v2 = vertices; do { if ( v2->ear ) { /* Ear found. Fill variables. */ v3 = v2->next; v4 = v3->next; v1 = v2->prev; v0 = v1->prev; /* (v1,v3) is a diagonal */ printf ( "%%Diagonal: (%d,%d)\n", v1->vnum, v3->vnum ); x = 36 + scale * ( v1->v[X] - xmin ); y = 36 + scale * ( v1->v[Y] - ymin ); printf ( "%d\t%d\tmoveto\n", x, y ); x = 36 + scale * ( v3->v[X] - xmin ); y = 36 + scale * ( v3->v[Y] - ymin ); printf ( "%d\t%d\tlineto\n", x, y ); /* Update earity of diagonal endpoints. */ v1->ear = diagonal ( v0, v3 ); v3->ear = diagonal ( v1, v4 ); /* Cut off the ear v2. */ v1->next = v3; v3->prev = v1; vertices = v3; /* In case the head was v2. */ n--; break; } /* End if ear found. */ v2 = v2->next; } while ( v2 != vertices ); } printf ( "closepath stroke\n" ); printf ( "\n" ); return; }