// File: /workspace/github_cpp_makefile_star2_O2/hollanderic[P]dp832/main.cpp
// Function: main
int main (int argc, char** argv) {
    int opt;
    int channel=1;
    double voltage=0.0;
    double current=0.0;
    bool state=false;
    bool setvoltage=false;
    bool setcurrent=false;
    bool setstate=false;
    bool extra = isExtraMode(argc, argv);

    const std::string psuDevicePath = getDevicePathArg(argc, argv);
    dp830 psu(psuDevicePath.c_str());

    while ((opt = getopt(argc, argv, kValidArgs.c_str())) != -1) {
        switch (opt) {
            case 'c':
                channel = atoi(optarg);
                if ((channel < 1) || (channel > 3)) {
                   fprintf(stderr, "Channel must be in range 1..3\n");
                   return -1;
                }
                break;
            case 'v':
                voltage = std::stod(optarg);
                psu.SetVoltage(channel,voltage);
                setvoltage=true;
                break;
            case 'i':
                current = std::stod(optarg);
                psu.SetCurrent(channel,current);
                setcurrent=true;
                break;
            case 't':
                current = std::stod(optarg);
                psu.SetOCP(channel,current);
                break;
            case 's':
                state = !(atoi(optarg) == 0);
                if (state)
                    psu.On(channel);
                else
                    psu.Off(channel);
                setstate=true;
                break;
            case 'b':
                psu.Bounce(channel,std::stod(optarg));
                break;
            case 'r':
            case 'm': {
                if (extra) {
                  int reps = 1;
                  int tt = 0;
		  if (opt == 'r') {
                     tt = std::stod(optarg);
                     reps = 1000/tt;
                  }
                  for (int i = 0; i < reps; i++) {
                    auto now = (duration_cast<milliseconds>(system_clock::now().time_since_epoch())).count();
                    printf("t=%lu,c=%d,s=%d,vs=%0.03f,is=%0.03f,ocp=%0.03f,v=%0.03f,i=%0.03f,p=%0.03f,tr=%d\n", 
                      now,
                      channel,
                      psu.GetState(channel),
                      psu.GetVoltageSetPoint(channel),
                      psu.GetCurrentSetPoint(channel),
                      psu.GetOCP(channel),
                      psu.MeasureVoltage(channel),
                      psu.MeasureCurrent(channel),
                      psu.MeasurePower(channel),
                      psu.GetOCPTripped(channel));
                    auto delta = (duration_cast<milliseconds>(system_clock::now().time_since_epoch())).count() - now;
                    if ((tt > delta)) {
                      usleep(1000*(tt-delta));
                    }
                  }
                } else { 
                    fprintf(stderr, "Can't use -m in legacy mode, add -x flag\n");
                }
                break;
            }
            case 'w': // ms delay
                if (extra) {
                    usleep(1000*std::stod(optarg));
                } else { 
                    fprintf(stderr, "Can't use -w in legacy mode, add -x flag\n");
                }
                break;
            default:
                break;
        }
    }

    if (!extra) {
      usleep(1*1e6);

      printf("Voltage = %f\n",psu.MeasureVoltage(channel));
      printf("Current = %f\n",psu.MeasureCurrent(channel));
      printf("Power   = %f\n",psu.MeasurePower(channel));
      printf("Volt sp   = %f\n",psu.GetVoltageSetPoint(channel));
      printf("Curr sp   = %f\n",psu.GetCurrentSetPoint(channel));
    }
    //Test Bounce feature - used to bounce a target
    //usleep(3*1e6);
    //psu.Bounce(1,2.5);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, %r14
movl %edi, %ebp
callq 0x2be2
movb %al, 0x7(%rsp)
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x2b4c
movq (%rbx), %rsi
leaq 0x88(%rsp), %rdi
callq 0x23cc
pushq $0x1
popq %rbx
leaq 0x1513(%rip), %r12      # 0x4180
leaq 0x88(%rsp), %r15
movq %r14, 0x38(%rsp)
movl %ebp, 0x14(%rsp)
movq 0x34ab(%rip), %rdx      # 0x6130
movl %ebp, %edi
movq %r14, %rsi
callq 0x2080
leal -0x62(%rax), %ecx
cmpl $0x15, %ecx
ja 0x2fce
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
cmpb $0x0, 0x7(%rsp)
je 0x2fe8
cmpl $0x72, %eax
jne 0x2ea8
movq 0x32f9(%rip), %rax      # 0x5fb8
movq (%rax), %rsi
leaq 0x18(%rsp), %r14
movq %r14, %rdi
leaq 0x6(%rsp), %rdx
callq 0x29c6
movq %r14, %rdi
xorl %esi, %esi
callq 0x29ad
cvttsd2si %xmm0, %ebp
movq %r14, %rdi
callq 0x2218
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
idivl %ebp
movl %eax, %r12d
movslq %ebp, %r13
jmp 0x2eaf
movq 0x32b3(%rip), %rax      # 0x5fb8
movq (%rax), %rsi
leaq 0x18(%rsp), %r13
movq %r13, %rdi
leaq 0x6(%rsp), %rdx
callq 0x29c6
movq %r13, %rdi
xorl %esi, %esi
callq 0x29ad
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x2218
movq %r15, %rdi
movl %ebx, %esi
movsd 0x8(%rsp), %xmm0
callq 0x266c
jmp 0x2c7e
movq 0x326a(%rip), %rax      # 0x5fb8
movq (%rax), %rsi
leaq 0x18(%rsp), %r13
movq %r13, %rdi
leaq 0x6(%rsp), %rdx
callq 0x29c6
movq %r13, %rdi
xorl %esi, %esi
callq 0x29ad
movq %r15, %rdi
movl %ebx, %esi
callq 0x2946
jmp 0x2e99
movq 0x3235(%rip), %rax      # 0x5fb8
movq (%rax), %rdi
callq 0x21c0
movl %eax, %ebx
addl $-0x4, %eax
cmpl $-0x4, %eax
ja 0x2c7e
jmp 0x30ef
movq 0x3213(%rip), %rax      # 0x5fb8
movq (%rax), %rsi
leaq 0x18(%rsp), %r13
movq %r13, %rdi
leaq 0x6(%rsp), %rdx
callq 0x29c6
movq %r13, %rdi
xorl %esi, %esi
callq 0x29ad
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x2218
movq %r15, %rdi
movl %ebx, %esi
movsd 0x8(%rsp), %xmm0
callq 0x2660
jmp 0x2c7e
movq 0x31ca(%rip), %rax      # 0x5fb8
movq (%rax), %rdi
callq 0x21c0
testl %eax, %eax
je 0x2fd9
movq %r15, %rdi
movl %ebx, %esi
callq 0x253e
jmp 0x2c7e
movq 0x31a4(%rip), %rax      # 0x5fb8
movq (%rax), %rsi
leaq 0x18(%rsp), %r13
movq %r13, %rdi
leaq 0x6(%rsp), %rdx
callq 0x29c6
movq %r13, %rdi
xorl %esi, %esi
callq 0x29ad
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x2218
movq %r15, %rdi
movl %ebx, %esi
movsd 0x8(%rsp), %xmm0
callq 0x2678
jmp 0x2c7e
cmpb $0x0, 0x7(%rsp)
je 0x2ffb
movq 0x3150(%rip), %rax      # 0x5fb8
movq (%rax), %rsi
leaq 0x18(%rsp), %r13
movq %r13, %rdi
leaq 0x6(%rsp), %rdx
callq 0x29c6
movq %r13, %rdi
xorl %esi, %esi
callq 0x29ad
mulsd 0x12e9(%rip), %xmm0     # 0x4178
cvttsd2si %xmm0, %rdi
callq 0x2190
leaq 0x18(%rsp), %rdi
callq 0x2218
jmp 0x2c7e
pushq $0x1
popq %r12
xorl %r13d, %r13d
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
subl $0x1, %r12d
jb 0x2fb9
callq 0x2050
cqto
movl $0xf4240, %ecx          # imm = 0xF4240
idivq %rcx
movq %rax, %r14
movq %r15, %rdi
movl %ebx, %esi
callq 0x24e8
movl %eax, %ebp
movq %r15, %rdi
movl %ebx, %esi
callq 0x286a
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq 0x2876
movsd %xmm0, 0x60(%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq 0x26ec
movsd %xmm0, 0x58(%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq 0x2922
movsd %xmm0, 0x50(%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq 0x292e
movsd %xmm0, 0x48(%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq 0x293a
movsd %xmm0, 0x40(%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq 0x277c
leaq 0x12c5(%rip), %rdi      # 0x4219
movq %r14, %rsi
movl %ebx, %edx
movl %ebp, %ecx
movsd 0x8(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x50(%rsp), %xmm3
movsd 0x48(%rsp), %xmm4
movsd 0x40(%rsp), %xmm5
movl %eax, %r8d
movb $0x6, %al
callq 0x2040
callq 0x2050
cqto
movl $0xf4240, %ecx          # imm = 0xF4240
idivq %rcx
subq %r14, %rax
cmpq %rax, %r13
jle 0x2ebb
movl %r13d, %ecx
subl %eax, %ecx
imull $0x3e8, %ecx, %edi      # imm = 0x3E8
callq 0x2190
jmp 0x2ebb
movq 0x38(%rsp), %r14
movl 0x14(%rsp), %ebp
leaq 0x11b7(%rip), %r12      # 0x4180
jmp 0x2c7e
cmpl $-0x1, %eax
jne 0x2c7e
jmp 0x301c
movq %r15, %rdi
movl %ebx, %esi
callq 0x25ca
jmp 0x2c7e
movq 0x2fe1(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x1271(%rip), %rdi      # 0x426a
jmp 0x300c
movq 0x2fce(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x1288(%rip), %rdi      # 0x4294
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x2200
jmp 0x2c7e
xorl %ebp, %ebp
cmpb $0x0, 0x7(%rsp)
jne 0x30c4
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x2190
leaq 0x88(%rsp), %rdi
movl %ebx, %esi
callq 0x2922
leaq 0x1275(%rip), %rdi      # 0x42be
movb $0x1, %al
callq 0x2040
leaq 0x88(%rsp), %rdi
movl %ebx, %esi
callq 0x292e
leaq 0x1266(%rip), %rdi      # 0x42cc
movb $0x1, %al
callq 0x2040
leaq 0x88(%rsp), %rdi
movl %ebx, %esi
callq 0x293a
leaq 0x1257(%rip), %rdi      # 0x42da
movb $0x1, %al
callq 0x2040
leaq 0x88(%rsp), %rdi
movl %ebx, %esi
callq 0x286a
leaq 0x1248(%rip), %rdi      # 0x42e8
movb $0x1, %al
callq 0x2040
leaq 0x88(%rsp), %rdi
movl %ebx, %esi
callq 0x2876
leaq 0x123b(%rip), %rdi      # 0x42f8
movb $0x1, %al
callq 0x2040
leaq 0x88(%rsp), %rdi
callq 0x2404
leaq 0x68(%rsp), %rdi
callq 0x2218
movl %ebp, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2eda(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x10fa(%rip), %rdi      # 0x41fa
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x2200
pushq $-0x1
popq %rbp
jmp 0x30c4
movq %rax, %rbx
jmp 0x3150
jmp 0x3140
jmp 0x3140
jmp 0x312d
jmp 0x3140
jmp 0x312d
jmp 0x3140
jmp 0x3140
jmp 0x3140
jmp 0x312d
jmp 0x3140
jmp 0x312d
jmp 0x312d
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2218
jmp 0x3143
jmp 0x3140
jmp 0x3140
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2404
leaq 0x68(%rsp), %rdi
callq 0x2218
movq %rbx, %rdi
callq 0x21e0