void loop() { char source; if (digitalRead(int1Pin)==1) // Interrupt pin, should probably attach to interrupt function { int accelCount[3]; // Stores the 12-bit signed value readAccelData(accelCount); // Read the x/y/z adc values // Now we'll calculate the accleration value into actual g's float accelG[3]; // Stores the real accel value in g's int i; for(i=0;i<3;i++) { accelG[i] = (float) accelCount[i] / ((1<<12)/(2*GSCALE)); // get actual g value, this depends on scale being set } // Print out values int j; for(j=0;j<3;j++) { //printf(accelG[j], 4); // Print g values printf("%G",accelG[j]); printf("\t"); // tabs in between axes } printf("\n"); } // If int2 goes high, either p/l has changed or there's been a single/double tap if (digitalRead(int2Pin)==1) { source = readRegister(0x0C); // Read the interrupt source reg. if ((source & 0x10)==0x10) // If the p/l bit is set, go check those registers portraitLandscapeHandler(); else if ((source & 0x08)==0x08) // Otherwise, if tap register is set go check that tapHandler(); } //Attempt a delay sleep(1); }
int main(int argc, char **argv) { char data[4]; int i, res= 0; res = inv_init_sysfs_attributes(); if (res) { printf("GT:ERR-Can't allocate mem"); return -1; } /* On Gesture/DMP supported features */ enableDMPFeatures(1); /* init Fds to poll for Gesture data */ initFds(); /* prompt user to make gesture and how to stop program */ printf("\n**Please make Gesture to see data. Press any key to stop Prog**\n\n"); do { for (i=0; i< numDMPFeatures; i++) { read(pfd[i].fd, data, 4); } poll(pfd, numDMPFeatures, POLL_TIME); for (i=0; i< numDMPFeatures; i++) { if(pfd[i].revents != 0) { switch(i) { case tap: tapHandler(); break; case flick: flickHandler(); break; case gOrient: googleOrientHandler(); break; case orient: orientHandler(); break; default: printf("GT:ERR-Not supported"); break; } pfd[i].revents= 0; //no need. reset anyway } } } while (!_kbhit()); /* Off DMP features */ enableDMPFeatures(0); /* release resources */ closeFds(); if (sysfs_names_ptr) { free(sysfs_names_ptr); } printf("\nThank You!\n"); return res; }