/*
 * scan.c - Phase 1 barcode diagnostic: read the integral laser via WL2:D.
 *
 * The MX ROM shows the scanner port is WL2 with units A and D, decoder
 * "Symbol2" (Workabout MX scanner). WL2:A returned -9 (in use); WL2:D (the
 * top-slot position, as TTY:D was) is untried. This opens WL2:D (falling back
 * to WL2:A), and if a channel opens, reads it barcode-style
 * (p_iow(pcb, P_FREAD, buf)) and dumps the bytes. Press the scan key to fire.
 */
#include <plib.h>

#define NU 2

GLDEF_C INT main(VOID)
{
    VOID *chan;
    UBYTE buf[256];
    INT   r, i, opened;
    UINT  k;

    static TEXT *names[NU] = { "WL2:D", "WL2:A" };
    INT results[NU];

    opened = -1;
    for (k = 0; k < NU; k++) {
        r = p_open(&chan, names[k], (UINT)-1);
        results[k] = r;
        if (r >= 0 && opened < 0)
            opened = k;
    }

    for (k = 0; k < NU; k++)
        p_printf("open %s: %d\n", names[k], results[k]);

    if (opened < 0) {
        p_printf("\nNothing opened. Press a key.\n");
        p_getch();
        return 1;
    }

    p_open(&chan, names[opened], (UINT)-1);   /* reopen the winner */
    p_printf("\nOpened %s. Scan (press scan key). Dumping:\n\n", names[opened]);

    for (;;) {
        r = p_iow(chan, P_FREAD, buf);
        if (r < 0) {
            p_printf("read status %d\n", r);
            continue;
        }
        p_printf("r=%d b0=%d:", r, buf[0]);
        for (i = 0; i < 24 && i < 256; i++)
            p_printf(" %d", buf[i]);
        p_printf("  \"");
        for (i = 0; i < 24 && i < 256; i++)
            p_putch(buf[i] >= ' ' ? buf[i] : '.');
        p_printf("\"\n");
    }
}
