Code: Select all
-- fast/slow encoder
-- A fast timer (100ms) is used to detect the time between 2 encoder move.
-- the timer is activated/reactivated at the end of each encoder move
-- if the timer is still running when the next encoder move comes then it is a fast move
-- else it is a slow move
function ApHeadingEncoder(dir)
if timer_running(ApHdgTimer) then
--fast change
if (dir==1) then
print("+10")
ApHdg=ApHdg+10
if(ApHdg>360) then -- check boundaries
ApHdg=ApHdg-360
end
xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg) --update dataref
end
if (dir==-1) then
print("-10")
ApHdg=ApHdg-10
if(ApHdg<0) then
ApHdg=ApHdg+360
end
xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg)
end
-- reset fast change detection timer
timer_stop(ApHdgTimer)
ApHdgTimer=timer_start(100,0,DummyCallback) --100ms
else
--slow change
if (dir==1) then
print("+1")
ApHdg=ApHdg+1
if(ApHdg>360) then
ApHdg=ApHdg-360
end
xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg)
end
if (dir==-1) then
print("-1")
ApHdg=ApHdg-1
if(ApHdg<0) then
ApHdg=ApHdg+360
end
xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg)
end
--start fast change detection timer
ApHdgTimer=timer_start(100,0,DummyCallback)
end
end