// File: /workspace/github_cpp_makefile_star2_O1/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 $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, 0x48(%rsp)
movl %edi, 0x44(%rsp)
movq 0x3213(%rip), %rdx      # 0x6140
movl 0x44(%rsp), %edi
movq 0x48(%rsp), %rsi
callq 0x2080
shll $0x18, %eax
cmpl $0x78000000, %eax       # imm = 0x78000000
je 0x2f49
testl %eax, %eax
jns 0x2f26
movl %eax, 0x40(%rsp)
movq 0x3084(%rip), %rax      # 0x5fd8
movl $0x1, (%rax)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movl 0x44(%rsp), %esi
movq 0x48(%rsp), %rdx
callq 0x2e04
movq (%rbx), %rsi
leaq 0x98(%rsp), %rdi
callq 0x24bc
movl $0x1, %ebx
leaq 0x98(%rsp), %r13
movq 0x31ac(%rip), %rdx      # 0x6140
movl 0x44(%rsp), %edi
movq 0x48(%rsp), %rsi
callq 0x2080
leal -0x62(%rax), %ecx
cmpl $0x15, %ecx
ja 0x326d
leaq 0x11cb(%rip), %rdx      # 0x4180
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl $0x0, 0x40(%rsp)
js 0x34e6
cmpl $0x72, %eax
jne 0x325f
movq 0x2fe7(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x370c
movq 0x10(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x30(%rsp)
je 0x3620
movl (%r14), %eax
testl %eax, %eax
je 0x333b
cmpl $0x22, %eax
jne 0x333e
jmp 0x3674
movq 0x2f88(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x370c
movq 0x10(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x30(%rsp)
je 0x3614
movl (%r14), %eax
testl %eax, %eax
je 0x327b
cmpl $0x22, %eax
jne 0x327e
jmp 0x3638
movq 0x2f29(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x370c
movq 0x10(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x30(%rsp)
je 0x35fc
movl (%r14), %eax
testl %eax, %eax
je 0x32b5
cmpl $0x22, %eax
jne 0x32b8
jmp 0x3644
movq 0x2eca(%rip), %rax      # 0x5fc0
movq (%rax), %rdi
callq 0x21d0
movl %eax, %ebx
addl $-0x4, %eax
cmpl $-0x4, %eax
ja 0x2f8d
jmp 0x35a4
movq 0x2ea8(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x370c
movq 0x10(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x30(%rsp)
je 0x35f0
movl (%r14), %eax
testl %eax, %eax
je 0x32c7
cmpl $0x22, %eax
jne 0x32ca
jmp 0x365c
movq 0x2e49(%rip), %rax      # 0x5fc0
movq (%rax), %rdi
callq 0x21d0
testl %eax, %eax
je 0x34d7
movq %r13, %rdi
movl %ebx, %esi
callq 0x2650
jmp 0x2f8d
movq 0x2e23(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x370c
movq 0x10(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x30(%rsp)
je 0x3608
movl (%r14), %eax
testl %eax, %eax
je 0x3301
cmpl $0x22, %eax
jne 0x3304
jmp 0x3650
cmpl $0x0, 0x40(%rsp)
js 0x34f0
movq 0x2db9(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x370c
movq 0x10(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x30(%rsp)
je 0x362c
movl (%r14), %eax
testl %eax, %eax
je 0x349d
cmpl $0x22, %eax
jne 0x34a0
jmp 0x3668
movl $0x1, %r15d
xorl %r12d, %r12d
jmp 0x3379
cmpl $-0x1, %eax
jne 0x2f8d
jmp 0x34fa
movl %ebp, (%r14)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x32a6
movq 0x20(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
movq %r13, %rdi
movl %ebx, %esi
callq 0x281e
jmp 0x2f8d
movl %ebp, (%r14)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2cba
jmp 0x34b2
movl %ebp, (%r14)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x32f2
movq 0x20(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
movq %r13, %rdi
movl %ebx, %esi
callq 0x27de
jmp 0x2f8d
movl %ebp, (%r14)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x332c
movq 0x20(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
movq %r13, %rdi
movl %ebx, %esi
callq 0x285e
jmp 0x2f8d
movl %ebp, (%r14)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3366
movq 0x20(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
idivl %ecx
movl %eax, %r15d
movslq %ecx, %r12
testl %r15d, %r15d
jle 0x2f8d
callq 0x2050
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %r14
movq %r13, %rdi
movl %ebx, %esi
callq 0x25f6
movl %eax, %ebp
movq %r13, %rdi
movl %ebx, %esi
callq 0x2b5a
movsd %xmm0, 0x38(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2b66
movsd %xmm0, 0x70(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x28cc
movsd %xmm0, 0x68(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2c96
movsd %xmm0, 0x60(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2ca2
movsd %xmm0, 0x58(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2cae
movsd %xmm0, 0x50(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x29e2
movq %r14, %rcx
shrq $0x3f, %rcx
sarq $0x12, %r14
addq %rcx, %r14
leaq 0xdf7(%rip), %rdi       # 0x4219
movq %r14, %rsi
movl %ebx, %edx
movl %ebp, %ecx
movsd 0x38(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movsd 0x60(%rsp), %xmm3
movsd 0x58(%rsp), %xmm4
movsd 0x50(%rsp), %xmm5
movl %eax, %r8d
movb $0x6, %al
callq 0x2040
callq 0x2050
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
subq %r14, %rdx
cmpq %rdx, %r12
jle 0x348f
movl %r12d, %eax
subl %edx, %eax
imull $0x3e8, %eax, %edi      # imm = 0x3E8
callq 0x21a0
decl %r15d
jne 0x3382
jmp 0x2f8d
movl %ebp, (%r14)
mulsd 0xcd0(%rip), %xmm0      # 0x4178
cvttsd2si %xmm0, %rdi
callq 0x21a0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2f8d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x2f8d
movq %r13, %rdi
movl %ebx, %esi
callq 0x26dc
jmp 0x2f8d
callq 0x2262
jmp 0x2f8d
callq 0x2240
jmp 0x2f8d
xorl %ebp, %ebp
cmpl $0x0, 0x40(%rsp)
jns 0x35ae
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x21a0
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2c96
leaq 0xd97(%rip), %rdi       # 0x42be
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2ca2
leaq 0xd88(%rip), %rdi       # 0x42cc
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2cae
leaq 0xd79(%rip), %rdi       # 0x42da
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2b5a
leaq 0xd6a(%rip), %rdi       # 0x42e8
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2b66
leaq 0xd5d(%rip), %rdi       # 0x42f8
movb $0x1, %al
callq 0x2040
jmp 0x35ae
callq 0x2284
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0x98(%rsp), %rdi
callq 0x24f6
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x35dc
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2160
movl %ebp, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb51(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb45(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb39(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb2d(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb21(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb15(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb09(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xafd(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xaf1(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xae5(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xad9(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xacd(%rip), %rdi       # 0x4148
callq 0x21b0
movq %rax, %rbx
jmp 0x36e2
jmp 0x36d2
jmp 0x36d2
jmp 0x36d2
jmp 0x3693
jmp 0x36d2
jmp 0x36d2
jmp 0x36d2
movq %rax, %rbx
jmp 0x36b0
jmp 0x36d2
jmp 0x36a4
jmp 0x36a4
jmp 0x36a4
jmp 0x36a4
jmp 0x36a4
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x36b0
movl %ebp, (%r14)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x36d5
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x36d5
jmp 0x36d2
jmp 0x36d2
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x24f6
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3703
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x21f0
nop