
© Bue o Systems, I c. • TSL1401-DB (2009.10.01) Page 9 of 52
' bright edges, displaying them graphically using DEBUG. Finally it
' computes both the area and extent of the object found.
' -----[ evision History ]------------------------------------------------
' -----[ I/O Definitions ]-------------------------------------------------
ao PIN 0 'TSL1401 's analog output (threhsolded by Stamp).
si PIN 1 'TSL1401 's SI pin.
clk PIN 2 'TSL1401 's CLK pin.
' -----[ Constants ]-------------------------------------------------------
D K CON 0 'Value assignd to "which" for dark pixels.
B T CON 1 'Value assigned to "which" for bright pixels.
FWD CON 0 'Value assigned to "dir" for left-to-right search.
BKWD CON 1 'Value assigned to "dir" for right-to-left search.
' -----[ Variables ]-------------------------------------------------------
VariableName VA Byte ' What is variable for?
pdata VA Word(8) 'Pixel data, as acquired LSB first from sensor.
pixels VA pdata.BIT0 '128-bit pixel array mapped onto pdata.
exp VA Word 'Exposure (integration) time in 2uSec units.
lptr VA Byte 'Left pixel pointer for count and find operations.
rptr VA Byte ' ight pixel pointer for count and find operations.
i VA Byte 'General-purpose index.
cnt VA Byte ' esult of pixel-count routine.
which VA Bit 'Indicates seeking D K or B T pixels/edges.
dir VA Bit 'Indicates direction of search (FWD or BKWD).
found VA Bit 'Indicates pixels found (= 1), or not found (= 0).
' -----[ Program Code ]----------------------------------------------------
' NOTE: This code assumes that DEBUG will wrap after 128 characters,
' regardless of the DEBUG window width. Later versions of DEBUG
' may not do this, and you will have to add C s where needed.
exp = 8333 'Set exposure time to 8333uSec (1/120th sec).
DEBUG HOME 'Go to home position on DEBUG screen.
GOSUB DispHdr 'Display the pixel location header.
DO 'Begin the scan-and-process loop.
GOSUB GetPix 'Obtain a pixel scan.
DEBUG C S XY, 0, 2 'Move to column 0, row 2.
GOSUB DispPix 'Display the pixels here.
lptr = 0 : rptr = 127: which = B T : dir = FWD : found = 1
'Initialize parameters for find.
GOSUB FindEdge 'Find first dark-to-light edge going L-> .
dir = BKWD 'Switch directions.
GOSUB FindEdge 'Find first dark-to-light edge going L<- .
DEBUG CL EOL 'Clear the next line.
IF found THEN 'Both edges found?
DEBUG C S X, (lptr - 1), "_|" 'Yes: Display left edge.
DEBUG C S X, rptr, "|_" ' Display right edge.
GOSUB CountPix ' Compute area (light pixel count).
DEBUG C , C , "Area = ", DEC cnt,' Display area ...
" Extent = ", DEC rptr - lptr + 1, CL EOL '... and extent of object.
ELSE 'No: Display failure message.
DEBUG C , C , "No object found.", CL EOL
ENDIF