Skip to content

Examples

dilshan edited this page Nov 22, 2026 · 2 revisions

Examples

Two GTK 3 programs live in examples/. Build them with make (needs libgtk-3-dev) and run from the openlru-lib directory.

gtk_probe

./build/gtk_probe

Shows the probe half of the library.

[ Scan USB bus ] [ Open selected sensor ]
+--------------------------------------------------+
| Model        | USB id    | Bus:Addr | Link        |
+--------------------------------------------------+
status line
Control Library call
Scan USB bus (also run at start-up) olru_probe()
Open selected sensor olru_open() then olru_close() - confirms the link handshake works

Source: examples/gtk_probe.c. The list is a GtkListStore; the selected row index is used as the index into the olru_device_info array.

gtk_capture

./build/gtk_capture

Shows the read half of the library.

Button What happens Library calls
Open sensor / Close sensor opens the first sensor found olru_probe, olru_open, olru_close
Capture polls every 100 ms for up to 15 s, then downloads and shows the image olru_capture_image, olru_read_image, olru_image_to_bmp
Read template shows the 256-byte template as hex olru_read_template
Save image... file chooser, writes a BMP olru_image_save_bmp

Design notes (examples/gtk_capture.c):

  • No threads. Finger polling uses g_timeout_add(). Each poll is one quick USB exchange, so the window stays responsive.
  • The image is displayed by feeding the library's BMP to a GdkPixbufLoader, so it looks the same as a saved file.
  • State lives in one struct app; every callback gets it as user_data.

Recipes

Poll from your own event loop

/* call every ~100 ms from a timer */
int rc = olru_capture_image(dev);
if (rc == OLRU_ERR_NO_FINGER)
    return;                       /* try again next tick */
if (rc == OLRU_OK)
    olru_read_image(dev, image, sizeof image);

Save the template to a file

uint8_t tpl[OLRU_TEMPLATE_SIZE];
if (olru_read_template(dev, tpl) == OLRU_OK) {
    FILE *f = fopen("finger.tpl", "wb");
    fwrite(tpl, 1, sizeof tpl, f);
    fclose(f);
}

Capture several fingers

for (int i = 0; i < 5; i++) {
    printf("finger %d...\n", i + 1);
    if (olru_wait_for_finger(dev, 10000) != OLRU_OK) break;
    olru_read_image(dev, image, sizeof image);
    char name[32];
    snprintf(name, sizeof name, "finger%d.bmp", i);
    olru_image_save_bmp(image, name);
    sleep(2);                     /* time to lift the finger */
}

Reconnect after unplugging

olru_close(dev);                  /* drop the old handle */
n = olru_probe(list, OLRU_MAX_DEVICES);   /* bus/address changed */
if (n > 0) olru_open(&list[0], &dev);

Convert the raw image to PGM

FILE *f = fopen("finger.pgm", "wb");
fprintf(f, "P5\n%d %d\n255\n", OLRU_IMAGE_WIDTH, OLRU_IMAGE_HEIGHT);
for (int y = OLRU_IMAGE_HEIGHT - 1; y >= 0; y--)      /* match the BMP orientation */
    fwrite(image + y * OLRU_IMAGE_WIDTH, 1, OLRU_IMAGE_WIDTH, f);
fclose(f);

(PGM is top-down, so the rows are written in reverse to match what the BMP shows.)

Clone this wiki locally