@title CR-CHDK
rem by VP-Systems
rem 15-04-2012

m=0
z=0
f=0
rem set_zoom_speed 10
rem set_zoom 0

rem goto "cr_fast"

rem CAMremote-1/2 (4-pin USB connector). Control Method: USB/CHDK
:cr_normal
print "CR: USB/CHDK"
while 1
 do
  c = get_usb_power
 until c>0
 print c
 if c>67 then gosub "pwroff"
 if c>58 then gosub "stop_video"
 if c>53 then gosub "tvdn"
 if c>48 then gosub "tvup"
 if c>40 then gosub "avdn"
 if c>35 then gosub "avup"
 if c>28 then gosub "rec_video"
 if c>21 then gosub "zout"
 if c>13 then gosub "zin"
 if c>8 then gosub "focusoff"
 if c>5 then gosub "focus"
 if c>0 then gosub "shoot"
wend
end

rem CAMremote-2 (Mini USB connector). Control Method: USB/CHDK+
:cr_fast
print "CR: USB/CHDK+"
while 1
 do
  c = get_usb_power
 until c>0
 if c>41 then gosub "pwroff"
 if c>21 then gosub "lensoff"
 if c>19 then gosub "stop_video"
 if c>17 then gosub "tvdn"
 if c>15 then gosub "tvup"
 if c>13 then gosub "avdn"
 if c>11 then gosub "avup"
 if c>8 then gosub "rec_video"
 if c>7 then gosub "zout"
 if c>5 then gosub "zin"
 if c>3 then gosub "focusoff"
 if c>1 then gosub "focus"
 if c>0 then gosub "shoot"
wend
end

:shoot
 if m=1 then gosub "stop_video"
 print c;":shoot"
 c=0
 press "shoot_full"
 sleep 1500
 release "shoot_full"
 release "shoot_half"
 return

:rec_video
 if m=1 then return
 set_record 2
 sleep 2000
 print c;":rec_video"
 c=0
 press "shoot_full"
 sleep 500
 release "shoot_full"
 m=1
 c=0
 return

:stop_video
 if m=0 then return
 print c;":stop_video"
 c=0
 press "shoot_full"
 sleep 500
 release "shoot_full"
 m=0
 sleep 1000
 set_record 1
 sleep 1000
 return

:focus
 print c;":focus"
 c=0
 press "shoot_half"
 f=1
 sleep 500
 return

:focusoff
 print c;":release"
 c=0
 release "shoot_half"
 sleep 500
 return

:zin
 if f=1 then release "shoot_half"
 if z<9 then z=z+1
 print c;":zoomin"
 c=0
 click "zoom_in"
rem set_zoom_to_step z
rem set_zoom_rel +1
 sleep 1000
 return

:zout
 if f=1 then release "shoot_half"
 if z<>0 then z=z-1
 print c;":zoomout"
 c=0
 click "zoom_out"
rem set_zoom_to_step z
rem set_zoom z
 sleep 1000
 return

:avup
 get_av a
 a=a+1
 set_av a
 print c;":Av+=";a
 c=0
 return

:avdn
 get_av a
 a=a-1
 set_av a
 print c;":Av-=";a
 c=0
 return

:tvup
 get_tv a
 a=a+1
 set_tv a
 print c;":Tv+=";a
 c=0
 return

:tvdn
 get_tv a
 a=a-1
 set_tv a
 print c;":Tv-=";a
 c=0
 return

:lensoff
 print c;":lensoff not supported"
 goto "pwroff"

:pwroff
 print c;":poweroff"
 c=0
 sleep 500
 shut_down
 sleep 500
 return
