// File: /workspace/github_cpp_makefile_star2_O2/probonopd[P]decodeir/DecodeIR.cpp
// Function: Signal::tryLutron()
void Signal::tryLutron()
{
	if ( nFrameL < 4 || nFrameL > 10)	
		return;

	if (   pFrame[0] < 14000
		|| pFrame[0] > 30000
		|| nTotDur < 26*2300		// Nominal min is 29 units
		|| nTotDur > 35*2300 		// Nominal max is 32 units
		|| sortOn.min1 < 2100		// Nominal min is 2100
		|| sortOff.min1 < 2100 )	// Nominal min is 2100
	{
		return;
	}
		
	int bParityError = true;
	// Number of bits between lead-in and lead-out can be anything from 18 to 24
	for (int nBitCnt = 18; bParityError && nBitCnt < 25; nBitCnt++)
	{
		// Work out m_rawUnit from the total duration of these 18 to 24 bits
		m_rawUnit =  float(nBitCnt) / (nTotDur - pFrame[0]);
		m_rawPulseAdjust = .4f;				// Pulse from -.4 to +.2
		m_rawGapAdjust = .2f;				// Gap from -.2 to +.4
		m_rawErrorLimit = .6f;

		cleanup();
		pBit += 1;
		if ( ! decodeRaw(nBitCnt+4) || pBit < pFrameEnd ) continue;
		makeMsb();
		// Check that nBitCnt really is the number of bits before the lead-out.
		if ( ! (getMsb(nBitCnt-1, 1) == 1 && getMsb(nBitCnt, 8) == 0 ) ) continue;

		// Do not rely on the length of the lead-in distinguishing correctly between it being
		// 8,9,10 or 11 units, i.e. concatenated with 0 to 3 data 1-bits.  So loop through 
		// the possibilities until we get a valid decode.
		for (int nOnes=0; bParityError && nOnes<25-nBitCnt; nOnes++)
		{
			// Store the 18-bit decode of the 24-bit data starting at cBits[4]
			cBits[4] = cBits[5] = cBits[6] = 0;
			int nBitPos = 0;	// Next position within 18-bit decode
			bParityError = false;
			for (int i=0; i<24; i++)
			{
				// To decode 3-bit reversed binary with odd parity into 3-bit binary, each binary
				// bit starting msb is XOR of all the reversed binary bits from msb to the corresponding
				// bit, e.g. 1101 decodes to 100 since 1=1, 0=1^1, 0=1^1^0.  The parity check requires
				// XOR of all four bits to be 1, so finally test 1^1^0^1 == 1.
				if ( i<nOnes ? 1 : getMsb(i-nOnes, 1) )
				{
					for (int n = nBitPos; n < (i/4)*3 + 4; n++) { cBits[4+(n>>3)] ^= 0x80>>(n&7); }
				}
				if ((i&3)==3)
				{
					bParityError |= (getMsb(nBitPos+32,1) != 1);
					// Clear the bit position where the parity check was temporarily stored.
					cBits[4+(nBitPos>>3)] &= (0xFF - (0x80>>(nBitPos&7)));
				}
				else nBitPos++;
			}

			// Now check the two overall parity bits of the 18-bit decode
			int nOverallParity = 0;
			for (int i=0; i < 9; i++) nOverallParity ^= getMsb(32+2*i, 2);
			bParityError |= (nOverallParity != 0);
			// If all OK, check that number of initial 1-bits is consistent with the lead-in duration.
			// Note that overall parity means that if nOnes is in error then it can only be out by 2.
			if (	pFrame[0]*m_rawUnit < 7.2 + nOnes
				||	pFrame[0]*m_rawUnit > 8.8 + nOnes ) continue;
/*
			char str[100];
			sprintf(str, "Lutron bitcnt=%d nOnes=%d data = %X", nBitCnt, nOnes, getMsb(32,18));
			MessageBox(0,str,"DecodeIR",0);
*/
		}
	}
	if (bParityError) return;
	strcpy(pProtocol,"Lutron");
	*pDevice = getMsb(32,8);
	*pOBC = getMsb(40,8);		// Hex is 2-byte so cannot be shown
}

// Assembly:
movl 0x48(%rdi), %eax
addl $-0xb, %eax
cmpl $-0x7, %eax
jb 0xd771
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movss (%rax), %xmm0
movss 0x4005(%rip), %xmm1     # 0x114f0
ucomiss %xmm0, %xmm1
ja 0xd763
ucomiss 0x409d(%rip), %xmm0     # 0x11598
ja 0xd763
movss 0x98(%rbx), %xmm0
movss 0x41a3(%rip), %xmm1     # 0x116b4
ucomiss %xmm0, %xmm1
ja 0xd763
ucomiss 0x4197(%rip), %xmm0     # 0x116b8
ja 0xd763
movss 0x418d(%rip), %xmm0     # 0x116bc
ucomiss 0x180(%rbx), %xmm0
ja 0xd763
ucomiss 0x18c(%rbx), %xmm0
ja 0xd763
leaq 0xa0(%rbx), %r14
pushq $0x12
popq %rcx
pushq $0x1
popq %r13
testl %r13d, %r13d
je 0xd72a
cmpl $0x19, %ecx
jae 0xd72a
cvtsi2ss %ecx, %xmm0
movss 0x98(%rbx), %xmm1
movq 0x38(%rbx), %rax
subss (%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0xfc(%rbx)
movabsq $0x3e4ccccd3ecccccd, %rdx # imm = 0x3E4CCCCD3ECCCCCD
movq %rdx, 0x100(%rbx)
movl $0x3f19999a, 0x108(%rbx) # imm = 0x3F19999A
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
andq $0x0, 0xc0(%rbx)
addq $0x4, %rax
movq %rax, 0xc8(%rbx)
movq %rcx, -0x38(%rbp)
leal 0x4(%rcx), %esi
movq %rbx, %rdi
callq 0xeebe
pushq $0x1
popq %r13
testl %eax, %eax
je 0xd614
movq 0xc8(%rbx), %rax
cmpq 0x40(%rbx), %rax
jb 0xd614
movq %rbx, %rdi
callq 0xec52
movq -0x38(%rbp), %rax
leal -0x1(%rax), %esi
movq %rbx, %rdi
movl %r13d, %edx
callq 0xeb6a
cmpl $0x1, %eax
jne 0xd614
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0xeb6a
testl %eax, %eax
je 0xd61f
movq -0x38(%rbp), %rcx
incl %ecx
jmp 0xd557
pushq $0x19
popq %rax
subl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x2c(%rbp)
testl %r13d, %r13d
je 0xd614
movl -0x2c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0xd614
movb $0x0, 0xa6(%rbx)
andw $0x0, 0xa4(%rbx)
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpl $0x18, %r15d
je 0xd6f3
movl %r15d, %esi
subl -0x2c(%rbp), %esi
jb 0xd675
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0xeb6a
testl %eax, %eax
je 0xd6a5
movl %r15d, %eax
shrl $0x2, %eax
leal (%rax,%rax,2), %eax
addl $0x4, %eax
movl %r12d, %edx
cmpl %eax, %edx
jge 0xd6a5
movl %edx, %ecx
andb $0x7, %cl
movl $0x80, %esi
shrl %cl, %esi
movl %edx, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
xorb %sil, 0x4(%rcx,%r14)
incl %edx
jmp 0xd684
movl %r15d, %eax
notl %eax
testb $0x3, %al
jne 0xd6e8
leal 0x20(%r12), %esi
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0xeb6a
xorl %edx, %edx
cmpl $0x1, %eax
setne %dl
movl %r12d, %ecx
andb $0x7, %cl
movl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
sarl %cl, %eax
orl %edx, %r13d
movl %r12d, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
andb %al, 0xa4(%rbx,%rcx)
jmp 0xd6eb
incl %r12d
incl %r15d
jmp 0xd654
xorl %r15d, %r15d
pushq $0x20
popq %r12
cmpl $0x32, %r12d
je 0xd717
movq %rbx, %rdi
movl %r12d, %esi
pushq $0x2
popq %rdx
callq 0xeb6a
xorl %eax, %r15d
addl $0x2, %r12d
jmp 0xd6fa
xorl %eax, %eax
testl %r15d, %r15d
setne %al
orl %eax, %r13d
incl -0x2c(%rbp)
jmp 0xd62f
testl %r13d, %r13d
jne 0xd763
movq 0x148(%rbx), %rax
movl $0x6e6f72, 0x3(%rax)    # imm = 0x6E6F72
movl $0x7274754c, (%rax)     # imm = 0x7274754C
movzbl 0xa4(%rbx), %eax
movq 0x160(%rbx), %rcx
movl %eax, (%rcx)
movzbl 0xa5(%rbx), %eax
movq 0x170(%rbx), %rcx
movl %eax, (%rcx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq