feat(inventory): Phase 1 - scan and validate UPC barcodes #1
+33
-69
@@ -1,94 +1,58 @@
|
|||||||
/*
|
/*
|
||||||
* scan.c - Phase 1 inventory demo: scan UPC barcodes and display them.
|
* scan.c - Phase 1 inventory demo: scan UPC barcodes and display them.
|
||||||
*
|
*
|
||||||
* Opens the barcode serial channel (TTY:D) and its own console/keyboard
|
* Blocking design: the app opens the barcode serial channel (TTY:D) and reads
|
||||||
* channel (CON:), then waits on both at once: a completed scan is validated
|
* it with a blocking p_read, assembling bytes until the scanner's terminator,
|
||||||
* as a UPC-A and printed; pressing Esc quits.
|
* then validates and prints each barcode. Exit via the System screen.
|
||||||
*
|
*
|
||||||
* Asynchronous two-channel wait (PLIB pattern): each request is started with
|
* (An earlier version waited on the keyboard asynchronously via CON: so Esc
|
||||||
* p_ioc, which sets a status word to E_FILE_PENDING until the request
|
* could quit; that panicked the Window Server - on SIBO the keyboard is
|
||||||
* completes. p_iowait blocks until some request finishes; we then check each
|
* event-driven through the window server, not a raw CON: byte read. A proper
|
||||||
* status word, cancel the other outstanding request, and loop. Every started
|
* scan-or-key loop needs the Window Server event API, whose reference manual
|
||||||
* request is matched by exactly one completion (real or cancelled) so the I/O
|
* is not among the SDK docs in this repo. Blocking output via p_printf is
|
||||||
* semaphore stays balanced. See "Asynchronous I/O" in the PLIB Reference.
|
* fine, so this version sticks to that.)
|
||||||
|
*
|
||||||
|
* This also serves as the format diagnostic: it prints the byte length of each
|
||||||
|
* decoded barcode alongside the text, so we can see exactly what the scanner
|
||||||
|
* emits for a UPC (12 vs 13 digits, any prefix/suffix) before Phase 2 uses it
|
||||||
|
* as a database key.
|
||||||
*/
|
*/
|
||||||
#include <plib.h>
|
#include <plib.h>
|
||||||
#include "bcode.h"
|
#include "bcode.h"
|
||||||
#include "upc.h"
|
#include "upc.h"
|
||||||
|
|
||||||
#define KEY_ESC 27
|
|
||||||
|
|
||||||
/*
|
|
||||||
* If a request on chan is still pending, cancel it and reap the resulting
|
|
||||||
* completion, so the started p_ioc is balanced by a wait.
|
|
||||||
*/
|
|
||||||
LOCAL_C VOID cancelPending(VOID *chan, WORD *pStat)
|
|
||||||
{
|
|
||||||
if (*pStat == E_FILE_PENDING) {
|
|
||||||
p_iow(chan, P_FCANCEL);
|
|
||||||
p_waitstat(pStat);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GLDEF_C INT main(VOID)
|
GLDEF_C INT main(VOID)
|
||||||
{
|
{
|
||||||
VOID *bchan; /* barcode serial channel */
|
VOID *bchan;
|
||||||
VOID *kchan; /* keyboard console channel */
|
UBYTE raw[BCODE_MAXLEN];
|
||||||
WORD bStat, kStat; /* async status words */
|
TEXT line[BCODE_MAXLEN];
|
||||||
UBYTE bRaw[BCODE_MAXLEN]; /* raw bytes from one serial read */
|
|
||||||
UBYTE kRaw[1]; /* one keystroke */
|
|
||||||
TEXT line[BCODE_MAXLEN]; /* barcode assembled across reads */
|
|
||||||
INT lineLen = 0;
|
INT lineLen = 0;
|
||||||
UWORD bLen, kLen;
|
INT err, n, textLen;
|
||||||
INT err;
|
|
||||||
INT quit = 0;
|
|
||||||
|
|
||||||
if ((err = bcodeOpen(&bchan)) < 0) {
|
if ((err = bcodeOpen(&bchan)) < 0) {
|
||||||
p_printf("Cannot open barcode port TTY:D (err %d)\n", err);
|
p_printf("Cannot open barcode port TTY:D (err %d)\n", err);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if ((err = p_open(&kchan, "CON:", (UINT)-1)) < 0) {
|
|
||||||
p_printf("Cannot open console (err %d)\n", err);
|
|
||||||
bcodeClose(bchan);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
p_printf("Inventory scan demo\n");
|
p_printf("Inventory scan demo\n");
|
||||||
p_printf("Scan a UPC, or press Esc to quit.\n\n");
|
p_printf("Scan UPC barcodes. Exit via the System screen.\n\n");
|
||||||
|
|
||||||
while (quit == 0) {
|
for (;;) {
|
||||||
/* Start one asynchronous read on each channel. */
|
n = p_read(bchan, raw, sizeof(raw)); /* blocks until bytes arrive */
|
||||||
bLen = sizeof(bRaw);
|
if (n < 0) {
|
||||||
p_ioc(bchan, P_FREAD, &bStat, bRaw, &bLen);
|
p_printf("read error %d\n", n);
|
||||||
kLen = sizeof(kRaw);
|
|
||||||
p_ioc(kchan, P_FREAD, &kStat, kRaw, &kLen);
|
|
||||||
|
|
||||||
p_iowait();
|
|
||||||
|
|
||||||
/* Keypress? Cancel the scan read and check for Esc. */
|
|
||||||
if (kStat != E_FILE_PENDING) {
|
|
||||||
cancelPending(bchan, &bStat);
|
|
||||||
if (kStat >= 0 && kRaw[0] == KEY_ESC)
|
|
||||||
quit = 1;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (bcodeAssemble(raw, n, line, &lineLen, BCODE_MAXLEN)) {
|
||||||
/* Scanner delivered bytes. Cancel the keypress read and assemble. */
|
textLen = 0;
|
||||||
if (bStat != E_FILE_PENDING) {
|
while (line[textLen] != '\0')
|
||||||
cancelPending(kchan, &kStat);
|
textLen++;
|
||||||
if (bStat >= 0 && bLen > 0) {
|
/* Diagnostic: length + text + validity. */
|
||||||
if (bcodeAssemble(bRaw, (INT)bLen, line, &lineLen, BCODE_MAXLEN)) {
|
if (upcIsValid(line))
|
||||||
if (upcIsValid(line))
|
p_printf("len=%d [%s] OK\n", textLen, line);
|
||||||
p_printf("OK %s\n", line);
|
else
|
||||||
else
|
p_printf("len=%d [%s] BAD\n", textLen, line);
|
||||||
p_printf("BAD %s (not a valid UPC-A)\n", line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* not reached in this diagnostic build */
|
||||||
p_printf("\nDone.\n");
|
|
||||||
p_close(kchan);
|
|
||||||
bcodeClose(bchan);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user