-- rc10_to_scripting1.lua
-- Mirrors RC input channel 10 onto the output assigned SERVOx_FUNCTION=94 (Scripting1).
-- Works even if that SERVO output is "disabled" as a physical PWM pin.

local RC_CH = 10                 -- RC input channel (1-based)
local OUT_FUNC = 97              -- Scripting1 function id (94..109)
local PERIOD_MS = 20             -- 50 Hz updates
local TIMEOUT_MS = 100           -- must be > PERIOD_MS

local out_chan = SRV_Channels:find_channel(OUT_FUNC)
if not out_chan then
  gcs:send_text(4, "Lua: no output assigned to function PAN_SERVO (set SERVOx_FUNCTION=SCRIPT NUMBER + 93)")
  return
end

gcs:send_text(6, string.format("Lua: RC%d -> FUNC%d on servo index %d", RC_CH, OUT_FUNC, out_chan + 1))

function update()
  local pwm = rc:get_pwm(RC_CH)  -- returns false if channel not available
  if pwm then
    -- safety clamp (optional)
    if pwm < 900 then pwm = 900 end
    if pwm > 2200 then pwm = 2200 end

    SRV_Channels:set_output_pwm_chan_timeout(out_chan, pwm, TIMEOUT_MS)
  end
  return update, PERIOD_MS
end

return update()
