Long/Double Key Press Special Protocol
======================================

Because the 1K extender needs to be kept as small as possible,
this special protocol is not pre-installed with the extender as
is done with most other extenders. If you want to use it, you
must install the device (TV/1106) and protocol (1F9) upgrades
below in IR.EXE after the extender has been installed.

This version of the LDKP special protocol is specific to the
Atlas DVR 1K extender.

Upgrade Code 0 = 0C 52 (TV/1106)
 F9 00 01
End

Upgrade Protocol 0 = 01 F9 (S3C8+)
 00 00 01 E4 03 4A F0 4A 56 4A 0F F6 10 F0 56 03
 07 38 3A 26 C3 03 76 2D 08 EB 0C F6 0C 66 FB 1F
 76 6D 20 6B F6 8B 1C C6 C0 25 00 F6 01 0D 56 6D
 7F E6 F1 FF 6F 76 6D 80 EB 09 76 6D 20 6B F5 38
 03 B0 03 A4 03 C3 2B 0D 87 43 2D A6 7F 80 3B 05
 82 7F C4 3A EE AF
End

This special protocol allows you to execute either one of two key
sequences based on two different key press patterns:

  Long Key Press: Shorter vs. Longer than the specified duration.

  Double Key Press: Single vs. Double press.  (A double press
      is two consecutive key presses within the specified duration.)

The way you set it up is very similar to the ToadTog special protocol
that is included in some extender packages.  You create a key
move with the setup code TV/1106 on each key of your choice.

The Hex command in the key move consists of three component pieces:

  {Control byte} {First command sequence} {Second command sequence}

  Control byte:

    The high nibble (the left hand side digit in hexadecimal) is the
    duration threshold in increment of 1/7.6 sec, up to 1.97
    (= 15/7.6) sec.

    The low nibble (the right hand side digit in hexadecimal) is the
    length of the {First command sequence}.

    This special protocol recognizes a Long Key Press by default.
    To make it a Double Key Press, add '8' to the right hand side digit.

  First and second command sequence:

    The keystrokes expressed by keycodes in hexadecimal.  The first
    command sequence is for a shorter press or a single press, and
    the second is for a longer press or a double press.  The first
    sequence can be up to 7 keystrokes long, and total of the both
    parts can be up to 13.


Setup
-----

Create a key move on each button that you want to use the feature.

  - Setup code:
      Long/Double Key Press protocol: TV/1106

  - Select the Hex Cmd (not EFC) mode.

  - Create and enter the Hex Cmd as follows:

    Pick the threshold time value between 1 and F (hex).
    Here are some pre-calculated values:

      1 ... 0.13 sec
      2 ... 0.26 sec
      3 ... 0.39 sec
      4 ... 0.52 sec
      7 ... 0.92 sec
      B ... 1.45 sec ($B = 11, 11/7.6 ~= 1.45)
      F ... 1.97 sec ($F = 15, 15/7.6 ~= 1.97)

    Create the first hex cmd byte by pairing the duration on the left
    hand side and length of the SHORT/SINGLE press part of the key strokes
    on the right hand side.  Maximum length of the SHORT/SINGLE press part
    is 7.

    To make the remote recognize a Double Press, add '8' to the right digit.
    (1 -> 9, 2 -> A, 3 -> B, 4 -> C, 5 -> D, 6 -> E, 7 -> F)

    For example,
      B1 ... The next 1 byte is executed for a key press shorter than
             1.45 sec or the rest of the bytes are executed otherwise.

      27 ... The next 7 bytes are executed for a key press shorter than
             0.26 sec or the rest of the bytes are executed otherwise.

      3A ... ($A = $2 + $8) The next 2 bytes are executed if a key is
             pressed twice within 0.39 sec or the rest of the bytes
             are executed otherwise.

    The rest of the command bytes are the key codes for the SHORT/SINGLE
    press case followed by the LONG/DOUBLE press key codes in hexadecimal
    numbers.

    You can use the ToadTog calculator spreadsheet to calculate the command
    bytes portion if it is available for your remote/extender.  Just replace
    the first byte with the one created in the above step.  Otherwise, use
    the KeyCodes.htm (if available in the extender package) or the Buttons
    section of the RDF file to see what the key codes are.


Example
-------

This example is not tested but hopefully good enough to illustrate how
you can set this up.  The key names, key codes and available device
index names are different in each remote.

Here we assume: POWER=$02, X_TV=$5F, X_AUX=$6F

While DVD is selected, the POWER button powers on/off the DVD only 
with a short press but it ALSO powers on/off the receiver (AUX) and
the TV (but leaving the remote in the DVD mode) if you hold it down
for about 0.5 seconds.

1. a) '4' causes 4/7.6 ~= 0.53 sec. b) 1 byte. From a) and b), we get $41.

2. Use Shift-POWER for toggling the power instead of POWER itself in
order to avoid infinite loop.  The extender will send the signal
assigned to the POWER button in the setup code if there's no key
move/macro defined on the Shift-POWER.  (shift cloaking)
Shift-POWER=$82 (=Shift-POWER. $80 added to $02 (POWER) to shift it.)

3. Shift-POWER; X_TV; Shift-POWER; X_AUX; Shift-POWER; = $82 $5F $82 $6F $82

You would create a key move like this:

Bound Key:
  Device: DVD Key: POWER
Function to Perform:
  Device: Device Type: TV  Setup Code: 1106
  EFC/Hex Cmd: 41 82 82 5F 82 6F 82 [ ] EFC [x] HexCmd

(Note that you don't include '$' in the hex cmd field.)


If you want to re-assemble the extender with this protocol built-in,
copy the source code below and paste into the extender .asm file
in the area indicated.

;-----------------------------------------------------------
; ---- Device/Protocol upgrades for Long/Double Key Press
					xorg	D_addr
					org	$$
TV_1106:
	db	F9	;Protocol = 0x1F9
	db	0	;No digit map
	db	1	;No defined keys
	
					D_addr = $$
					xorg	P_addr
					org	$$
Protocol_1F9:
	db	00,00,01
					org	Ram
	LD	R4A, R03	;Get the "HEX command" from the KeyMove
	SWAP	R4A		;Timeout value in high nibble
	AND	R4A, 0F		;Set timeout
	CALL	StartT0

	AND	R03, 07		;Length of SHORT/SINGLE press side
	LD	W3, R3A		;Body length of keymove
	SUB	RC3, 3		;keylist length

	TM	R2D, 08		;Check for a Double Key Press requested?
	JRNZ	L1F9_7

	;; Check for Long Key Press
L1F9_1:	CALL	ScanKeypad
	JRNC	L1F9_5		;Key is no longer pressed
L1F9_2:	TM	R_IFlags, 20	;Test timeout
	JRZ	L1F9_1
	JR	L1F9_3

	;; Check for Double Key Press
L1F9_7:	LDW	RC0, 2500	;Change this value to adjust sensitivity
	CALL	DelayWW0
	AND	R_IFlags, 7F	;Clear key interrupt test bit
	LD	RF1, FF		;Enable key sense interrupts
L1F9_6:	IDLE
	TM	R_IFlags, 80	;Test key interrupt
	JRNZ	L1F9_3
	TM	R_IFlags, 20	;Test timeout
	JRZ	L1F9_6

L1F9_5:	LD	W3, R03		;Length of SHORT/SINGLE press side
	CLR	R03
L1F9_3:	CP	RC3, R03	;Selected side empty?
	JRLE	L1F9_4		;Yes
	LD	W4, R2D[W3]	;Get a key code
	CP	R_Mac, AR_MacBuf ;Buffer full?
	JRULE	L1F9_4		;Yes
	PUSHUD	@R_Mac, RC4	;Insert KeyCode into pending keys
	DJNZ	W3, L1F9_3
L1F9_4:	RET

					P_addr = $$
					xorg	D_table+2+DT_index+DT_index
					org	$$
	dw	0C52		;Type=TV, Protocol>255, setup code = 0x452 = 1106
					xorg	D_table2+DT_index+DT_index
					org	$$
	dw	TV_1106
					xorg	P_table+2+DT_index+DT_index
					org	$$
	dw	1F9		;Protocol ID
					xorg	P_table2+DT_index+DT_index
					org	$$
	dw	Protocol_1F9
					DT_index = DT_index+1

;-----------------------------------------------------------
