Header Header

Examples
[Device handling]

Table of contents:

pr_calib - Calibration example

Show how to calibrate the device.

The needed gxsdldr.c loader file is included directly:

#include "prapi.h"
#include "gxsdldr.c"

The __lib__.h file contains some library functions used only in the sample programs, such as the lib_function_start and lib_function_end functions that implement the simple command line interface and measures the elapsed time between the two function calls.

#include "../__lib__.h"

The main function:

int main() {
    const char *s = NULL;

Opening the Passport Reader system:

    /* Opening the PR system */
    gxHANDLE hpr;   /* handle for the PR system */
    lib_function_start("Opening system files");
    if (gx_openmodulea(&hpr,"prapi","default")) lib_function_end();
    else {
        lib_displ_err();
        return 0;
    }

Connecting to the first device. If no device is connected then the pr_usedevicen() function returns false and the program ends:

    /* Connecting to the first device */
    lib_function_start("Connecting to the first device");
    if (pr_usedevicen(hpr,0,PR_UMODE_FULL_CONTROL)) {
        lib_function_end();
    } else {
        lib_displ_err();
        gx_closehandle(&hpr);
        return 0;
    }

Displaying the calibration state of the device:

    lib_write_line("The device is %scalibrated.", pr_iscalibrated(hpr,0)?"":"not ");

The calibration loop. Putting the calibration image on the device and calling the pr_calibrate() function to start the calibration process. In case of error false is returned and the error code and error message is displayed by the lib_display_err library function:

    lib_write_line("Put the calibration image on the document window.");

    for (;;) {
        s = lib_read_line("Do you want to recalibrate? (yes/no) ");

        if (!strcmp(s, "no")) break;
        if (strcmp(s, "yes")) {
            lib_write_line(" !!! Type 'yes' or 'no' please!");
            continue;
        }

        lib_function_start("Calibrating device");
        if (pr_calibrate(hpr,0)) {
            lib_function_end();
        } else {
            lib_displ_err();
            continue;
        }
    }

Displaying the state of the device after the calibration process:

    /* Overall result */
    lib_write_line("The device is %scalibrated.", pr_iscalibrated(hpr,0)?"":"not ");

Closing the device:

    /* Closing the device */
    lib_function_start("Closing the device");
    if (pr_closedevice(hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

Closing the Passport Reader system:

    /* Closing the PR system */
    lib_function_start("Closing the PR system");
    if (gx_closehandle(&hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

The sample program ends:

    return 1;
}

The complete source code:

#include "prapi.h"
#include "gxsdldr.c"

#include "../__lib__.h"

int main() {
    const char *s = NULL;

    /* Opening the PR system */
    gxHANDLE hpr;   /* handle for the PR system */
    lib_function_start("Opening system files");
    if (gx_openmodulea(&hpr,"prapi","default")) lib_function_end();
    else {
        lib_displ_err();
        return 0;
    }

    /* Connecting to the first device */
    lib_function_start("Connecting to the first device");
    if (pr_usedevicen(hpr,0,PR_UMODE_FULL_CONTROL)) {
        lib_function_end();
    } else {
        lib_displ_err();
        gx_closehandle(&hpr);
        return 0;
    }

    lib_write_line("The device is %scalibrated.", pr_iscalibrated(hpr,0)?"":"not ");
    lib_write_line("Put the calibration image on the document window.");

    for (;;) {
        s = lib_read_line("Do you want to recalibrate? (yes/no) ");

        if (!strcmp(s, "no")) break;
        if (strcmp(s, "yes")) {
            lib_write_line(" !!! Type 'yes' or 'no' please!");
            continue;
        }

        lib_function_start("Calibrating device");
        if (pr_calibrate(hpr,0)) {
            lib_function_end();
        } else {
            lib_displ_err();
            continue;
        }
    }

    /* Overall result */
    lib_write_line("The device is %scalibrated.", pr_iscalibrated(hpr,0)?"":"not ");

    /* Closing the device */
    lib_function_start("Closing the device");
    if (pr_closedevice(hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

    /* Closing the PR system */
    lib_function_start("Closing the PR system");
    if (gx_closehandle(&hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

    return 1;
}

pr_motdet - Device controlling example

Show how to control the device.

The needed gxsdldr.c loader file is included directly:

#include "prapi.h"
#include "gxsdldr.c"

The __lib__.h file contains some library functions used only in the sample programs, such as the lib_function_start and lib_function_end functions that implement the simple command line interface and measures the elapsed time between the two function calls.

#include "../__lib__.h"

The main function:

int main() {
    int state;
    char *statxt[]={"No document detected.","Document is moving.",
        "Take out the document!","","Ready for reading.","Button is pressed."};

Opening the Passport Reader system:

    /* Opening the PR system */
    gxHANDLE hpr;   /* handle for the PR system */
    lib_function_start("Opening system files");
    if (gx_openmodulea(&hpr,"prapi","default")) lib_function_end();
    else {
        lib_displ_err();
        return 0;
    }

Connecting to the first device. If no device is connected then the pr_usedevicen() function returns false and the program ends:

    /* Connecting to the first device */
    lib_function_start("Connecting to the first device");
    if (pr_usedevicen(hpr,0,PR_UMODE_FULL_CONTROL)) {
        lib_function_end();
    } else {
        lib_displ_err();
        gx_closehandle(&hpr);
        return 0;
    }

Testing the power state of devices with power switch:

    /* Testing power switch */
    lib_function_start("Testing the power switch");
    if (pr_testpower(hpr,&state,0)) {
        if ((state & PR_PWR_MASK) == PR_PWR_OFF)
            lib_write_line("Power is off! Please turn power on!");

        while ((state & PR_PWR_MASK) == PR_PWR_OFF) {
            if (!pr_testpower(hpr,&state,0)) lib_displ_err();
            lib_wait(300);
        }

        lib_function_end();
    } else {
        lib_displ_err();
    }

Enabling document motion detection by setting the proper value for the freerun_mode property:

    /* Enabling motion detection */
    lib_function_start("Enabling motion detection");
    if (gx_setmodulepropertyinta(hpr,"freerun_mode",PR_FRMODE_TESTDOCUMENT)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

The main loop. In this short sample the handling of the start button and the document detection is presented. In normal operation when the start button is pressed or a document is detected on the document window, that is the document has PR_TD_IN status, the capture and recognize process can be started. The display leds of the device are used to indicate the status of the device and of the document.

    while(!lib_kbhit()) {
        /* Testing the start button */
        if (pr_testbutton(hpr,PR_KEY_START,&state)) {
            if (state == PR_KEY_PRESSED) state=5;
            else {
                /* If the start button is not pressed testing the document detection */
                if (pr_testdocument(hpr,&state,0)) {

                    /* Turning the display leds depending on the status */
                    int color = PR_SLC_OFF;
                    switch(state) {
                        case PR_TD_OUT:     color = PR_SLC_GREEN; break;
                        case PR_TD_MOVE:    color = PR_SLC_ANY; break;
                        case PR_TD_NOMOVE:  color = PR_SLC_RED; break;
                    }
                    if (!pr_setstatusled(hpr, 0xFF, color)) lib_displ_err();
                } else {
                    lib_displ_err();
                }
            }
            lib_write("\rState of the device: %s        \r", statxt[state]);
            lib_wait(200);
            if ((state == PR_TD_IN) || (state == 5)) lib_wait(800);
        } else {
            lib_displ_err();
        }
    }
    lib_write_line("");

Turning off the lights and stopping the document detection process:

    /* Turning off the lights */
    lib_function_start("Turning off the lights");
    if (gx_setmodulepropertyinta(hpr, "freerun_mode", PR_FRMODE_NULL)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

Turning off the display leds:

    /* Turning off the display leds */
    lib_function_start("Turning off the display leds");
    if (pr_setstatusled(hpr, 0xFF, PR_SLC_OFF)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

Closing the device:

    /* Closing the device */
    lib_function_start("Closing the device");
    if (pr_closedevice(hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

Closing the Passport Reader system:

    /* Closing the PR system */
    lib_function_start("Closing the PR system");
    if (gx_closehandle(&hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

The sample program ends:

    return 1;
}

The complete source code:

#include "prapi.h"
#include "gxsdldr.c"

#include "../__lib__.h"

int main() {
    int state;
    char *statxt[]={"No document detected.","Document is moving.",
        "Take out the document!","","Ready for reading.","Button is pressed."};

    /* Opening the PR system */
    gxHANDLE hpr;   /* handle for the PR system */
    lib_function_start("Opening system files");
    if (gx_openmodulea(&hpr,"prapi","default")) lib_function_end();
    else {
        lib_displ_err();
        return 0;
    }

    /* Connecting to the first device */
    lib_function_start("Connecting to the first device");
    if (pr_usedevicen(hpr,0,PR_UMODE_FULL_CONTROL)) {
        lib_function_end();
    } else {
        lib_displ_err();
        gx_closehandle(&hpr);
        return 0;
    }

    /* Testing power switch */
    lib_function_start("Testing the power switch");
    if (pr_testpower(hpr,&state,0)) {
        if ((state & PR_PWR_MASK) == PR_PWR_OFF)
            lib_write_line("Power is off! Please turn power on!");

        while ((state & PR_PWR_MASK) == PR_PWR_OFF) {
            if (!pr_testpower(hpr,&state,0)) lib_displ_err();
            lib_wait(300);
        }

        lib_function_end();
    } else {
        lib_displ_err();
    }

    /* Enabling motion detection */
    lib_function_start("Enabling motion detection");
    if (gx_setmodulepropertyinta(hpr,"freerun_mode",PR_FRMODE_TESTDOCUMENT)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

    while(!lib_kbhit()) {
        /* Testing the start button */
        if (pr_testbutton(hpr,PR_KEY_START,&state)) {
            if (state == PR_KEY_PRESSED) state=5;
            else {
                /* If the start button is not pressed testing the document detection */
                if (pr_testdocument(hpr,&state,0)) {

                    /* Turning the display leds depending on the status */
                    int color = PR_SLC_OFF;
                    switch(state) {
                        case PR_TD_OUT:     color = PR_SLC_GREEN; break;
                        case PR_TD_MOVE:    color = PR_SLC_ANY; break;
                        case PR_TD_NOMOVE:  color = PR_SLC_RED; break;
                    }
                    if (!pr_setstatusled(hpr, 0xFF, color)) lib_displ_err();
                } else {
                    lib_displ_err();
                }
            }
            lib_write("\rState of the device: %s        \r", statxt[state]);
            lib_wait(200);
            if ((state == PR_TD_IN) || (state == 5)) lib_wait(800);
        } else {
            lib_displ_err();
        }
    }
    lib_write_line("");

    /* Turning off the lights */
    lib_function_start("Turning off the lights");
    if (gx_setmodulepropertyinta(hpr, "freerun_mode", PR_FRMODE_NULL)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

    /* Turning off the display leds */
    lib_function_start("Turning off the display leds");
    if (pr_setstatusled(hpr, 0xFF, PR_SLC_OFF)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

    /* Closing the device */
    lib_function_start("Closing the device");
    if (pr_closedevice(hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

    /* Closing the PR system */
    lib_function_start("Closing the PR system");
    if (gx_closehandle(&hpr)) {
        lib_function_end();
    } else {
        lib_displ_err();
    }

    return 1;
}


Generated  for Passport Reader
(c) ARH Inc.