/** * This method sets the gain and the integration time * * If gain = false (0), device is set to low gain (1X) * If gain = high (1), device is set to high gain (16X) * If time = 0, integration will be 13.7ms * If time = 1, integration will be 101ms * If time = 2, integration will be 402ms * If time = 3, use manual start / stop * * @param gain - the gain * @param time - the time */ void TSL2561::set_timing(uint8_t gain, uint8_t time){ //! Read the timing location i2c_packet* ptr = read_timing(); //! Get timing byte if (ptr->valid_packet){ this->_timing = ptr->buffer[0]; //! Set gain (0 or 1) if (gain) this->_timing |= 0x10; else this->_timing &= ~0x10; //! Set integration time (0 to 3) this->_timing &= ~0x03; this->_timing |= (time & 0x03); //! Write the timing back write_timing(); } return; }
/** * This method stops a manual integration period. * * @return bool - if the command was successful */ bool TSL2561::manual_stop(void){ //! We read i2c_packet* ptr = read_timing(); //! Get timing byte if (ptr->valid_packet){ //! Stop manual integration this->_timing &= ~0x08; //! Write modified timing byte back to device if (write_timing()) return true; } return false; }
/** * This method starts a manual integration period. * * @return - if the command was successful */ bool TSL2561::manual_start(void){ //! We read i2c_packet* ptr = read_timing(); //! Get timing byte if (ptr->valid_packet){ //! Set integration time to 3 (manual integration) this->_timing |= 0x03; if (write_timing()){ //! Begin manual integration this->_timing |= 0x08; //! Write modified timing byte back to device if (write_timing()) return true; } } return false; }
int main(int argc, char *argv[], char *envp[]) { char answer[128], filename[64]; int cache_page, line_offset, value; FILE *logfp; int i, j, k, ret; int *tmparray; /* for malloc'd static array */ struct rlimit rlims; struct rusage r_start, r_stop; cache_page = getpagesize(); /* Ask for line_offset */ again: fprintf(stdout, "\nEnter line_offset: "); fgets(answer, sizeof(answer) - 1, stdin); fflush(stdin); line_offset = atoi(answer); if ( (cache_page < line_offset) || (line_offset < 1) ) { fprintf(stderr, "\nline_offset=%d out of range [1, %d]", line_offset, cache_page); fflush(stderr); goto again; } /* Reduce MAX_RSS to be NICE to other users */ #if defined(__alpha) rlims.rlim_cur = NEW_MAX_RSS; /* soft */ rlims.rlim_max = NEW_MAX_RSS; /* hard */ if(setrlimit(RLIMIT_RSS, &rlims) < 0) { perror("setrlimit: "); exit(-1); } #endif /* Reduce our priority to be NICE to other users PRIO_MAX is 20 on pegasus so we are doing nice(19) */ if (nice(PRIO_MAX - 1) < 0) { perror("nice: "); exit(-1); } /* open log file */ sprintf(filename, "cache_%4.4d.log", line_offset); logfp = fopen(filename, "w"); if (logfp == (FILE *)NULL) { fprintf(stderr, "\n\nmain: *** fopen(\"%s\",\"w\") failed ***\n\n", filename); fflush(stderr); return(-3); } /* get starting baseline resource usage */ ret = get_resource_usage(RUSAGE_SELF, &r_start); if (ret != 0) { fprintf(stderr, "\n..get_resource_usage(RUSAGE_SELF,..) return %d ", ret); fflush(stderr); } /* initialize and start timers */ init_timing(); /* allocate tmparray[] in data segment */ tmparray = (int *)malloc(NUM_ENTRIES * cache_page * sizeof(int)); if (tmparray == (int *)NULL) { fprintf(stderr, "\nmalloc: fatal error:"); fflush(stderr); exit(-4); } fprintf(stdout, "\nUsing line_offset=%d cache_page=%d..", line_offset, cache_page); fflush(stdout); fprintf(logfp, "\nUsing line_offset=%d cache_page=%d..", line_offset, cache_page); fflush(logfp); value = 0; for (k = 0 ; k < REPEAT ; k++) { i = 0; /* only for fprintf() */ j = 0; if ((k % 32) == 0) { fprintf(stdout, "\n..setting entry, tmparry[], value [%9d,%9d,%9d]", i, j, value); fflush(stdout); } for (i = 0 ; i < NUM_ENTRIES ; i++) { tmparray[j] = value; /* tmparray[j] will be kept in the L2 cache until cache contention with newer data forces it to be written out to (slower) main memory */ j = j + line_offset; value++; if ( ((i % 1024) == 0) && ((k % 32) == 0) ) { putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); putchar(0x08); fprintf(stdout, "%9d,%9d,%9d]", i, j, value); fflush(stdout); } } /* end of for (i = .. */ } /* end of for (k = .. */ /* get ending resource usage */ ret = get_resource_usage(RUSAGE_SELF, &r_stop); if (ret != 0) { fprintf(stderr, "\n..get_resource_usage(RUSAGE_SELF,..) return %d ", ret); fflush(stderr); } /* read timers and then stop them */ read_timing(&real_sec, &real_usec, &virt_sec, &virt_usec, &prof_sec, &prof_usec); pause_timing(); /* print out resources used in rusage struct's to both stdout and our logfile */ fprintf(stdout, "\n..stopping timers "); fprintf_rusage(stdout, &r_start, &r_stop); fprintf(logfp, "\n..stopping timers "); fprintf_rusage(logfp, &r_start, &r_stop); /* printout the total times to both stdout and our logfile */ printf_timing(); fprintf(stdout, "\nNet Memory Bandwidth %8.3f Mbytes/sec: ", ((double)(value*sizeof(int))/((double)prof_sec + (double)prof_usec/1000000.0)) /1000000.0 ); fprintf(stdout, "\n\n"); fflush(stdout); fprintf_timing(logfp, real_sec, real_usec, virt_sec, virt_usec, prof_sec, prof_usec); fprintf(logfp, "\nNet Memory Bandwidth %8.3f Mbytes/sec: ", ((double)(value*sizeof(int))/((double)prof_sec + (double)prof_usec/1000000.0)) /1000000.0 ); fprintf(logfp, "\n\n"); fflush(logfp); fclose(logfp); /* close logfile */ return(0); } /* end of main */