' SwitchLinc Programming Commands
' Author:  Glenn Steiner
' Create event P16 ON to Call this Script
' Remember to disable event when not in use!

' P1 ON:	Send Sequence to Remotely Set Ramp Rate
' P2 ON:	Send Sequence to Remotely Set On-Level

' P5 ON:	Send Sequence to set Scene Membership
' P6 ON:	Send Sequence to Remove Scene Membership
' P7 ON:	Send Sequence to Set Scene Ramp Rate

' P9 ON:	Send Sequence to Disable X-10 Transmit
' P10 ON:	Send Sequence to Re-Enable X-10 Transmit
' P11 ON:	Send Sequence to Disable Programming
' P12 ON:	Send Sequence to Enable Programming
' P0 ON:	Send Clear Sequence (not really a command)
' P16 Off:	Exit Setup

dim cmdarray,cmdtalk1,cmdtalk2	' Global Variables

sub main()
	dim x10,hc,uc,uc1,cmd,txtcmd,extended,unitno

	' commands are stored in array in same number sequence as noted above
	cmdarray = array ("O16+N16+M16+P16+M16",_
	"O16+P16+N16+M16+M16","P16+N16+M16+O16+M16","","",_
	"M16+N16+O16+P16","O16+P16+M16+N16","N16+O16+P16+M16","",_
	"M16+N16+P16+O16+P16","O16+M16+N16+P16+P16",_
	"M16+O16+P16+N16+P16","N16+M16+O16+P16+P16")

	' messages are stored in array in same number sequence as noted above
	cmdtalk1 = array("",_
	"Set Primary Ramp Rate: Set Switches to Ramp Rate and Press On",_
	"Set Primary On Level: Set Switches to On-Level and Press On",_
	"","",_
	"Set Scene Members and On-Level: Set Switches On-Level and Press On",_
	"Remove Secene Membership: Adjust Switches to be Removed and Press On",_
	"Set Scene Member Ramp Rate: Set Switches to Ramp Rate and Press On",_
	"",_
	"Disable X-10 Transmitt: Adjust Switches to be Disabled and Press On",_
	"Enable X-10 Transmitt: Adjust Switches to be Enabled and Press On",_
	"","")

	' secondary message upon command completion
	cmdtalk2 = array("Clear Sequence Complete, Enter Next Command",_
	"Primary Ramp Rate Has Been Set",_
	"Primary On-Level Has Been Set",_
	"","",_
	"Transmit Scene Address for Membership",_
	"Transmit Scene Address for Membership Removal",_
	"Transmit Scene Address for Ramp Rate Change",_
	"",_
	"Selected Switches Disabled for X-Ten Transmit",_
	"Selected Switches Enabled for X-Ten Transmit",_
	"Programming Disabled for All Switches",_
	"Programming Enabled for All Switches")

	ah.speak "Entering Switch Linc Programming Mode." ,TRUE
	ah.speak "Select a Command, P-One through P-Twelve On", TRUE
	ah.speak "or Press P-Sixteen Off to Exit.", TRUE
	hs.ClearLastx10
	uc1=""

	do
		' don't hang homeseer or the script
		hs.waitevents
		' get the last X10 command received
		x10 = hs.lastx10
		' clear for next command
		hs.ClearLastx10

		if x10 <> "" then
			' got an x10 command 
			' format of command string is:
				' house;unit(s);command;dimlevel;extended data 
			hc = hs.stringitem(x10,1,";") 
			uc = hs.stringitem(x10,2,";") 
			if uc = "" then unitno=0 else unitno = eval(mid(uc,2))
			cmd = hs.stringitem(x10,3,";")
			extended = hs.stringitem(x10,4,";")
			txtcmd = "no command"
			select case cmd
				case 0 txtcmd = "All units off"
				case 1 txtcmd = "all lights on"
				case 2 txtcmd = "On"
				case 3 txtcmd = "Off"
				case 4 txtcmd = "Dim"
				case 5 txtcmd = "Bright"
				case 11 txtcmd = "Preset Dim " & extended
				end select

			' Process Programming Commands
			' Command format must be P[number] and ON
			' Exit if P16 OFF

'hs.writelog "Command","uc='" & uc & "','" & unitno & "'  " & cmdarray(unitno)
			if (hc = "P") then
			select case uc
				case "P11","P12" call SwitchCmd(unitno)
				case "P1","P2","P5","P6","P7","P9","P10"
					call ClrCmd(unitno)
				case "P16" exit do
				' Execute secondary action upon "ON" press
				case "" select case uc1
					case "P1","P2","P5","P6","P7","P9","P10"
						call SwitchCmd(Eval(mid(uc1,2)))
					case else call SpeakCmd(hc,uc,cmd,txtcmd)
					end select
				case else call SpeakCmd(hc,uc,cmd,txtcmd)
				end select
			uc1 = uc
			else call SpeakCmd(hc,uc,cmd,txtcmd)
			end if
			end if
		loop
		hs.speak "End Switch Linc Programming Mode", TRUE
	end sub

' SwitchLinc Programming Subroutines

' Send Clear Sequence and appropriate next step message
sub ClrCmd (cmdnumber)
	SendString(cmdarray(0))
	ah.speak cmdtalk1(cmdnumber), TRUE
	end sub

' Send Command and appropriate message
sub SwitchCmd (cmdnumber)
	SendString(cmdarray(cmdnumber))
	ah.speak cmdtalk2(cmdnumber), TRUE
	end sub

' Send a SwitchLinc String
sub SendString (sequence)
	for i = 1 to 10
		cmd = hs.stringitem(sequence,i,"+")
		if cmd = "" then exit for 
		hs.ExecX10 cmd,"No Cmd",0,0
		next
	end sub

' Speak Command
sub SpeakCmd(hc,uc,cmd,txtcmd)
	hs.speak "X10 Command, House Code is " & hc & ", Unit Code is " & uc & _
		", Command is " & cmd & " " & txtcmd, TRUE
	end sub

