// File: jkingan[P]ladyheather/heathgps.cpp
// Function: set_driver_mode()
void set_driver_mode()
{
int i, j;
int len;
int count;
float vals[128];
float val;

   // This routine sets the LED driver mode by pulsing the battery on and off.
   // It is also used to switch the battery on, off, or PWM it.

   end_sweep();
   count = 0;
   i = 0;
   len = strlen(edit_buffer);

   while(i < len) {   // get a list of numbers on the line
      for(j=i; j<len; j++) {   // find next number field
         if(edit_buffer[j] == 0) goto no_num;
         if(edit_buffer[j] == 0x0A) goto no_num;
         if(edit_buffer[j] == 0x0D) goto no_num;
         if(edit_buffer[j] == '.') goto got_num;
         if(isdigit(edit_buffer[j])) goto got_num;
         if(edit_buffer[j] == '*') {  // '*' is a double 100 msec pulse
            if(count >= 125) break;
            vals[count++] = 100;     // off
            vals[count++] = 100;     // on
            vals[count++] = 100;     // off
            vals[count++] = 100;     // on
         }
      }
      break;

      got_num:
      i = j;
      val = 0;
      val = (float) atof(&edit_buffer[i]);
      vals[count] = val;
      ++count;
      if(count >= 128) break;

      while(i < len) {  // skip over number
         ++i;
         if(edit_buffer[i] == 0) goto no_num;
         if(edit_buffer[i] == 0x0A) goto no_num;
         if(edit_buffer[i] == 0x0D) goto no_num;
         if(edit_buffer[i] == '.') continue;
         if(edit_buffer[i] < '0') break;
         if(edit_buffer[i] > '9') break;
         if(edit_buffer[i] == '*') break;
      }
   }

   no_num:
   if(count == 0) {  // no numbers on the line, toggle battery state
      if(batt_pwm) {
         set_batt_pwm(0);
      }
      else {
update_check();
         set_batt_pwm(65535);
      }
//    set_luxor_delay(CLICK_TIME);
      Sleep(CLICK_TIME);
   }
   else if((count == 1) && (vals[0] <= 1.0)) {  // single number
      if(vals[0] == 0.0F) {
         set_batt_pwm(0);
      }
      else {
update_check();
         if(vals[0] == 1.0F) set_batt_pwm(65535);
         else set_batt_pwm((u16) (val*65536.0F));
      }
   }
   else {  // pulse the battery
      for(i=0; i<count; i++) {
         if(i & 1) {  // an ON time
            set_batt_pwm(65535);
         }
         else {           // an OFF time
            set_batt_pwm(0);
         }
//       set_luxor_delay((u16) vals[i]);
         Sleep((DWORD) vals[i]);
         set_batt_pwm(65535);
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
callq 0xaa1f0
movl $0x0, -0x10(%rbp)
movl $0x0, -0x4(%rbp)
leaq 0x2f0c2b(%rip), %rdi    # 0x39e4c0
callq 0x9120
movl %eax, -0xc(%rbp)
movl -0x4(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0xadb07
movl -0x4(%rbp), %eax
movl %eax, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0xad9e8
movslq -0x8(%rbp), %rcx
leaq 0x2f0bfa(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xad8d4
jmp 0xadb09
movslq -0x8(%rbp), %rcx
leaq 0x2f0be1(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0xad8ed
jmp 0xadb09
movslq -0x8(%rbp), %rcx
leaq 0x2f0bc8(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0xad906
jmp 0xadb09
movslq -0x8(%rbp), %rcx
leaq 0x2f0baf(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0xad91f
jmp 0xad9ed
movslq -0x8(%rbp), %rcx
leaq 0x2f0b96(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %edi
callq 0x9330
cmpl $0x0, %eax
je 0xad93d
jmp 0xad9ed
movslq -0x8(%rbp), %rcx
leaq 0x2f0b78(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x2a, %eax
jne 0xad9d8
cmpl $0x7d, -0x10(%rbp)
jl 0xad960
jmp 0xad9e8
movl -0x10(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x10(%rbp)
cltq
movss 0xd59af(%rip), %xmm0    # 0x183324
movss %xmm0, -0x210(%rbp,%rax,4)
movl -0x10(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x10(%rbp)
cltq
movss 0xd5991(%rip), %xmm0    # 0x183324
movss %xmm0, -0x210(%rbp,%rax,4)
movl -0x10(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x10(%rbp)
cltq
movss 0xd5973(%rip), %xmm0    # 0x183324
movss %xmm0, -0x210(%rbp,%rax,4)
movl -0x10(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x10(%rbp)
cltq
movss 0xd5955(%rip), %xmm0    # 0x183324
movss %xmm0, -0x210(%rbp,%rax,4)
jmp 0xad9da
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0xad8af
jmp 0xadb07
movl -0x8(%rbp), %eax
movl %eax, -0x4(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x214(%rbp)
movslq -0x4(%rbp), %rax
leaq 0x2f0ab7(%rip), %rdi    # 0x39e4c0
addq %rax, %rdi
callq 0x9480
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x214(%rbp)
movss -0x214(%rbp), %xmm0
movslq -0x10(%rbp), %rax
movss %xmm0, -0x210(%rbp,%rax,4)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
cmpl $0x80, -0x10(%rbp)
jl 0xada49
jmp 0xadb07
jmp 0xada4b
movl -0x4(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0xadb02
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0x2f0a55(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xada79
jmp 0xadb09
movslq -0x4(%rbp), %rcx
leaq 0x2f0a3c(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0xada8f
jmp 0xadb09
movslq -0x4(%rbp), %rcx
leaq 0x2f0a26(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0xadaa5
jmp 0xadb09
movslq -0x4(%rbp), %rcx
leaq 0x2f0a10(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0xadabb
jmp 0xada4b
movslq -0x4(%rbp), %rcx
leaq 0x2f09fa(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x30, %eax
jge 0xadad1
jmp 0xadb02
movslq -0x4(%rbp), %rcx
leaq 0x2f09e4(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x39, %eax
jle 0xadae7
jmp 0xadb02
movslq -0x4(%rbp), %rcx
leaq 0x2f09ce(%rip), %rax    # 0x39e4c0
movsbl (%rax,%rcx), %eax
cmpl $0x2a, %eax
jne 0xadafd
jmp 0xadb02
jmp 0xada4b
jmp 0xad89d
jmp 0xadb09
cmpl $0x0, -0x10(%rbp)
jne 0xadb43
leaq 0x2be4dc(%rip), %rax    # 0x36bff2
cmpw $0x0, (%rax)
je 0xadb25
xorl %edi, %edi
callq 0xaa160
jmp 0xadb34
callq 0x4a300
movl $0xffff, %edi           # imm = 0xFFFF
callq 0xaa160
movl $0x1f4, %edi            # imm = 0x1F4
callq 0xb8b0
jmp 0xadc26
cmpl $0x1, -0x10(%rbp)
jne 0xadbce
movss -0x210(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xc1507(%rip), %xmm0    # 0x16f068
ucomisd %xmm1, %xmm0
jb 0xadbce
movss -0x210(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0xadb82
jp 0xadb82
xorl %edi, %edi
callq 0xaa160
jmp 0xadbcc
callq 0x4a300
movss -0x210(%rbp), %xmm0
movss 0xc3da5(%rip), %xmm1    # 0x17193c
ucomiss %xmm1, %xmm0
jne 0xadbaa
jp 0xadbaa
movl $0xffff, %edi           # imm = 0xFFFF
callq 0xaa160
jmp 0xadbca
movss -0x214(%rbp), %xmm0
movss 0xd5786(%rip), %xmm1    # 0x183340
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movzwl %ax, %edi
callq 0xaa160
jmp 0xadbcc
jmp 0xadc24
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0xadc22
movl -0x4(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xadbf4
movl $0xffff, %edi           # imm = 0xFFFF
callq 0xaa160
jmp 0xadbfb
xorl %edi, %edi
callq 0xaa160
movslq -0x4(%rbp), %rax
cvttss2si -0x210(%rbp,%rax,4), %edi
callq 0xb8b0
movl $0xffff, %edi           # imm = 0xFFFF
callq 0xaa160
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0xadbd5
jmp 0xadc24
jmp 0xadc26
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
nop