// This function translates a set of bits read from the key IO ports into key events. // Adding more keys is a matter of adding a new enum value, and one reading // one more port in ReadKeys() int GetKeyEvents(void) { static int KeyPressFlag = 0; static int Count[NO_EVENT] = { 0 }; int LoopCount; int RawKeys; RawKeys = ReadKeys(); for (LoopCount = 0; LoopCount < NO_EVENT; LoopCount++) { if (RawKeys & (1 << LoopCount)) { Count[LoopCount]++; if ((Count[LoopCount] >= KEY_PRESS_MINIMUM) && !(KeyPressFlag & (1 << LoopCount))) { KeyPressFlag |= (1 << LoopCount); Count[LoopCount] = 0; return LoopCount; } } else { KeyPressFlag &= ~(1 << LoopCount); Count[LoopCount] = 0; } } return NO_EVENT; }
/* This reads a keypoint file from a given filename and returns the list * of keypoints. */ int ReadKeyFile(const char *filename, unsigned char **keys, keypt_t **info) { FILE *file; file = fopen (filename, "r"); if (! file) { /* Try to file a gzipped keyfile */ char buf[1024]; sprintf(buf, "%s.gz", filename); gzFile gzf = gzopen(buf, "rb"); if (gzf == NULL) { printf("Could not open file: %s\n", filename); return 0; } else { int n = ReadKeysGzip(gzf, keys, info); gzclose(gzf); return n; } } int n = ReadKeys(file, keys, info); fclose(file); return n; // return ReadKeysMMAP(file); }
/* This reads a keypoint file from a given filename and returns the list of keypoints. */ Keypoint ReadKeyFile(char *filename) { FILE *file; file = fopen (filename, "r"); if (! file) FatalError("Could not open file: %s", filename); return ReadKeys(file); }
int main(int argc, char **argv) { const int dim = 128; if (argc != 6 && argc != 7 && argc != 8) { printf("Usage: %s <tree.in> <db.in> <query.in> <num_nbrs> " "<matches.out> [distance_type:1] [normalize:1]\n", argv[0]); return 1; } char *tree_in = argv[1]; char *db_in = argv[2]; char *query_in = argv[3]; int num_nbrs = atoi(argv[4]); char *matches_out = argv[5]; DistanceType distance_type = DistanceMin; bool normalize = true; #if 0 if (argc >= 7) output_html = argv[6]; #endif if (argc >= 7) distance_type = (DistanceType) atoi(argv[6]); if (argc >= 8) normalize = (atoi(argv[7]) != 0); printf("[VocabMatch] Using tree %s\n", tree_in); switch (distance_type) { case DistanceDot: printf("[VocabMatch] Using distance Dot\n"); break; case DistanceMin: printf("[VocabMatch] Using distance Min\n"); break; default: printf("[VocabMatch] Using no known distance!\n"); break; } /* Read the tree */ printf("[VocabMatch] Reading tree...\n"); fflush(stdout); clock_t start = clock(); VocabTree tree; tree.Read(tree_in); clock_t end = clock(); printf("[VocabMatch] Read tree in %0.3fs\n", (double) (end - start) / CLOCKS_PER_SEC); #if 1 tree.Flatten(); #endif tree.SetDistanceType(distance_type); tree.SetInteriorNodeWeight(0, 0.0); /* Read the database keyfiles */ FILE *f = fopen(db_in, "r"); std::vector<std::string> db_files; char buf[256]; while (fgets(buf, 256, f)) { /* Remove trailing newline */ if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = 0; db_files.push_back(std::string(buf)); } fclose(f); /* Read the query keyfiles */ f = fopen(query_in, "r"); std::vector<std::string> query_files; while (fgets(buf, 256, f)) { /* Remove trailing newline */ if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = 0; char keyfile[256]; sscanf(buf, "%s", keyfile); query_files.push_back(std::string(keyfile)); } fclose(f); int num_db_images = db_files.size(); int num_query_images = query_files.size(); printf("[VocabMatch] Read %d database images\n", num_db_images); /* Now score each query keyfile */ printf("[VocabMatch] Scoring %d query images...\n", num_query_images); fflush(stdout); #if 0 FILE *f_html = fopen(output_html, "w"); PrintHTMLHeader(f_html, num_nbrs); #endif float *scores = new float[num_db_images]; double *scores_d = new double[num_db_images]; int *perm = new int[num_db_images]; FILE *f_match = fopen(matches_out, "w"); if (f_match == NULL) { printf("[VocabMatch] Error opening file %s for writing\n", matches_out); return 1; } for (int i = 0; i < num_query_images; i++) { start = clock(); /* Clear scores */ for (int j = 0; j < num_db_images; j++) scores[j] = 0.0; unsigned char *keys; int num_keys; keys = ReadKeys(query_files[i].c_str(), dim, num_keys); clock_t start_score = clock(); double mag = tree.ScoreQueryKeys(num_keys, normalize, keys, scores); clock_t end_score = end = clock(); printf("[VocabMatch] Scored image %s in %0.3fs " "( %0.3fs total, num_keys = %d, mag = %0.3f )\n", query_files[i].c_str(), (double) (end_score - start_score) / CLOCKS_PER_SEC, (double) (end - start) / CLOCKS_PER_SEC, num_keys, mag); /* Find the top scores */ for (int j = 0; j < num_db_images; j++) { scores_d[j] = (double) scores[j]; } qsort_descending(); qsort_perm(num_db_images, scores_d, perm); int top = MIN(num_nbrs, num_db_images); for (int j = 0; j < top; j++) { // if (perm[j] == index_i) // continue; fprintf(f_match, "%d %d %0.4f\n", i, perm[j], scores_d[j]); //fprintf(f_match, "%d %d %0.4f\n", i, perm[j], mag - scores_d[j]); } fflush(f_match); fflush(stdout); #if 0 PrintHTMLRow(f_html, query_files[i], scores_d, perm, num_nbrs, db_files); #endif delete [] keys; } fclose(f_match); #if 0 PrintHTMLFooter(f_html); fclose(f_html); #endif delete [] scores; delete [] scores_d; delete [] perm; return 0; }
void testReadKeys(void) { // Testing key0 only if ( ReadKeys(0x01) != NO_EVENT ) { printf(" failed on 1. call"); } else if ( ReadKeys(0x01) != NO_EVENT ) { printf(" failed on 2. call"); } else if ( ReadKeys(0x01) != NO_EVENT ) { printf(" failed on 3. call"); } else if ( ReadKeys(0x01) != NO_EVENT ) { printf(" failed on 4. call"); } else if ( ReadKeys(0x01) != NO_EVENT ) { printf(" failed on 5. call"); } else if ( ReadKeys(0x01) != KEY0_EVENT ) { printf(" failed on 6. call"); } // test key1 only else if ( ReadKeys(0x02) != NO_EVENT ) { printf(" failed on 7. call"); } else if ( ReadKeys(0x02) != NO_EVENT ) { printf(" failed on 8. call"); } else if ( ReadKeys(0x02) != NO_EVENT ) { printf(" failed on 9. call"); } else if ( ReadKeys(0x02) != NO_EVENT ) { printf(" failed on 10. call"); } else if ( ReadKeys(0x02) != NO_EVENT ) { printf(" failed on 11. call"); } else if ( ReadKeys(0x02) != KEY1_EVENT ) { printf(" failed on 12. call"); } // let go of keys else if ( ReadKeys(0x00) != NO_EVENT) { printf(" failed on 13. call"); } // test key0 and key1 at the same time else if ( ReadKeys(0x18) != NO_EVENT ) { printf(" failed on 14. call"); } else if ( ReadKeys(0x18) != NO_EVENT ) { printf(" failed on 15. call"); } else if ( ReadKeys(0x18) != NO_EVENT ) { printf(" failed on 16. call"); } else if ( ReadKeys(0x18) != NO_EVENT ) { printf(" failed on 17. call"); } else if ( ReadKeys(0x18) != NO_EVENT ) { printf(" failed on 18. call"); } else if ( ReadKeys(0x18) != KEY3_EVENT ) { printf(" failed on 19. call"); } else if ( ReadKeys(0x18) != KEY4_EVENT) { printf(" failed on 20. call"); } else if ( ReadKeys(0x18) != NO_EVENT) { printf(" failed on 21. call"); } else if ( ReadKeys(0x18) != NO_EVENT) { printf(" failed on 22. call"); } else printf("passed !"); }
// With this setup it would seem like main() must be the first function in this file, otherwise // the wrong function gets called on reset. int main(void) { volatile unsigned long ulLoop; volatile int event; initHW(); // Start the OLED display and write a message on it RIT128x96x4Init(ulSSI_FREQUENCY); RIT128x96x4StringDraw("Hi :)", 0, 0, mainFULL_SCALE); RIT128x96x4StringDraw("enter the code ...", 0, 5, mainFULL_SCALE); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_0)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)) ; // // Loop forever. // while (1) { // This is where a statemachine could be added event = GetKeyEvents( ReadKeys() ); if (event == KEY_ENTER) RIT128x96x4StringDraw("Enter Pressed", 0, 0, mainFULL_SCALE); if (event == KEY_UP) RIT128x96x4StringDraw("up Pressed", 0, 0, mainFULL_SCALE); if (event == KEY_DOWN) RIT128x96x4StringDraw("down Pressed", 0, 0, mainFULL_SCALE); if (event == KEY_CANCEL) RIT128x96x4StringDraw("cancel Pressed", 0, 0, mainFULL_SCALE); // // Turn on the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0); // GPIO_PORTF_DATA_R |= 0x01; // // Delay for a bit. // This is BAD STYLE (tm) any embedded system should be either free-running or timer based for (ulLoop = 0; ulLoop < 200000; ulLoop++) { } // // Turn off the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0); // // Delay for a bit. // // for (ulLoop = 0; ulLoop < 200000; ulLoop++) // { // } SysCtlSleep(); } return 0; }