#macro ReadLineData128()
SampleLine 'Sample line at 8MHz
GetLeftLightLevel 'Measure light level on pixel on the left side
GetLineState 'Store binary pixel data into Line_state
GetRightLightLevel 'Measure light level on pixel on the right side
SetIntegrationTime 'Perform mathematics on Vout to get Tint
#endmacro
#macro SIPulse()
high SI
pulsout CLK, LinePulse
low SI
#endmacro
#macro SampleLine()
SIPulse 'Start a sample
pwmout CLK, 1, 4 'Clock out some fast pulses (8MHz @ 64MHz clock speed)
pauseus Tint 'Allow for sufficient integration time
pwmout CLK, OFF 'Finish the sample
SIPulse
#endmacro
#macro GetLeftLightLevel()
Vout = 0 'Set light level to zero for each loop
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
readadc 10, pixel_data 'Measure voltage on pixel #4 for light intensity
Vout = Vout + pixel_data 'Add the value to the accumulator
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
#endmacro
#macro GetRightLightLevel()
readadc 10, pixel_data 'Measure voltage on pixel #124 for light intensity
Vout = Vout + pixel_data 'Add the value to the accumulator
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
pulsout CLK, LinePulse 'Pulse clock to start reading next pixel ...
#endmacro
#macro GetLineState()
adcsetup = 0 'Reset inputs to digital for digital read of 120 pixels
Line_state = 0 'Set line_state to 0 to avoid clearing individual bits
'Shift sensor data into variables in location 56 to 70
shiftin CLK, AO, 0, (@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc)
read @bptrdec, bit14 'Get pixel state from position 70 and put 1 or 0 into bits of Line_state
read @bptrdec, bit13
read @bptrdec, bit12
read @bptrdec, bit11
read @bptrdec, bit10
read @bptrdec, bit8
read @bptrdec, bit8
read @bptrdec, bit7
read @bptrdec, bit6
read @bptrdec, bit5
read @bptrdec, bit4
read @bptrdec, bit3
read @bptrdec, bit2
read @bptrdec, bit1
read @bptrdec, bit0
#endmacro
#macro SetIntegrationTime()
Vout = Vout / 2 'Obtain average of pixel #4 and #124 outputs
Select case Vout
case > Vtarget 'If voltage above target (saturation possible) ...
if Tint > TintMin then '... if Tint not already at minimum ...
Tint = Tint - TintStep '... reduce by TintStep to try to converge
elseif LEDPWM > LEDMin then 'In case Tint cannot be reduced, try to reuce LED intensity ...
LEDPWM = LEDPWM - LEDStep '... reduce LED intesity by LEDStep
pwmduty LED, LEDPWM
endif
case < Vtarget 'If voltage below target (cannot see in the dark) ...
if Tint < TintMax then '... if Tint not already at maximum ...
Tint = Tint + TintStep '... increase by TintStep to try to converge
elseif LEDPWM < LEDMax then 'In case Tint cannot be reduced, try to increase LED intensity ...
LEDPWM = LEDPWM + LEDStep '... increase LED intensity by LEDStep
pwmduty LED, LEDPWM
endif
endselect
#endmacro
#macro GetLinePos(line_state)
'Determine the position of the line
if line_state > 0 then 'If there is any line at all ...
line_width = NOB line_state '... find the number of set bits = line width ...
line_edge = NCD line_state '... find the highest set bit = rightmost edge of the line ...
line_centre = line_width / 2 '... store half of the line in a new variable to save on number of operations
select case line_edge 'Cater for right and left side limits situations when not all of the line is seen ...
case 1 : line_pos = line_centre '... line dissapearing on the left side ...
case 15 : line_pos = 15 - line_centre '... line dissapearing on the right side ...
else : line_pos = line_edge - line_centre '... both edges inside field of view
endselect
else
endif
select case line_pos 'Maybe can be done faster, but takes us as it is
case 1 to 7 : Eline = -8 + line_pos 'Set negative Eline from line_pos
case 9 to 15 : Eline = line_pos - 8 'Set positive Eline from line_pos
else : ELine = 0 'Set error to zero
endselect
#endmacro