// File: /workspace/github_c_makefile_star2_O1/patriciogonzalezvivo[P]tprint/src/thermalPrinter.cpp
// Function: ThermalPrinter::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
bool ThermalPrinter::open(const std::string& _portName){
    try {
        port = new serial::Serial( _portName,
                                   BAUDRATE,
                                   serial::Timeout::simpleTimeout(1000),
                                   serial::eightbits,
                                   serial::parity_none,
                                   serial::stopbits_one,
                                   serial::flowcontrol_none );
    }
    
    catch (const std::exception& exc){
        std::cout << "ThermalPrinter:: Fail to open" << exc.what() << std::endl;
        bConnected = false;
        return bConnected;
    }
    
    bConnected = true;
    usleep(50000);
    reset();
    usleep(50000);
    
    setControlParameter(7, 100, 2);
    // Set “max heating dots”,”heating time”, “heating interval”
    // n1 = 0-255 Max printing dots,Unit(8dots),Default:7(64 dots)
    // n2 = 3-255 Heating time,Unit(10us),Default:80(800us)
    // n3 = 0-255 Heating interval,Unit(10us),Default:2(20us)
    // The more max heting dots, the more peak current will cost
    // whenprinting, the faster printing speed. The max heating dots is 8*(n1+1)
    // The more heating time, the more density , but the slower printing speed. If heating time is too short, blank page may occur.
    // The more heating interval, the more clear, but the slower
    
    // printDensity (def. 14) / printBreakTime (def.40)
    // setPrintDensity(14,40);
    setStatus(true);

    port->flushOutput();
    
    feed();

    return bConnected;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x50, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movl $0x10, %edi
callq 0x8310
movq %rax, %r15
movaps 0xfeb2(%rip), %xmm0     # 0x21910
movaps %xmm0, 0x30(%rsp)
xorl %eax, %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movl %eax, 0x18(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl $0x4b00, %edx           # imm = 0x4B00
movl $0x8, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xa960
movq %r15, 0x8(%rbx)
movb $0x1, %bpl
movb %bpl, 0x10(%rbx)
movl $0xc350, %edi           # imm = 0xC350
callq 0x8490
leaq 0x2d(%rsp), %r14
movw $0x401b, (%r14)         # imm = 0x401B
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0x12594
movl $0x478, %edi            # imm = 0x478
callq 0x8490
movl $0xc350, %edi           # imm = 0xC350
callq 0x8490
movq %rbx, %rdi
movl $0x7, %esi
movl $0x64, %edx
movl $0x2, %ecx
callq 0x11bf6
movw $0x3d1b, (%r14)         # imm = 0x3D1B
movb %bpl, 0x2(%r14)
leaq 0x2d(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl $0x3, %edx
callq 0x12594
movl $0x6b4, %edi            # imm = 0x6B4
callq 0x8490
movq 0x8(%rbx), %rdi
callq 0xb466
movb $0xa, (%r14)
leaq 0x2d(%rsp), %rsi
movq %rbx, %rdi
callq 0x11cc4
movb 0x10(%rbx), %al
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
movq %rax, %r14
movl $0x10, %esi
movq %r15, %rdi
callq 0x8320
jmp 0x11b65
movq %rdx, %r12
movq %rax, %r14
cmpl $0x1, %r12d
jne 0x11bc3
movq %r14, %rdi
callq 0x8160
movq %rax, %r14
movq 0x16443(%rip), %rdi     # 0x27fc0
leaq 0x102fb(%rip), %rsi     # 0x21e7f
movl $0x1d, %edx
callq 0x83c0
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq 0x16422(%rip), %rdi     # 0x27fc0
movq %rax, %rsi
callq 0x82f0
movq %rax, %rdi
callq 0x8190
movb $0x0, 0x10(%rbx)
callq 0x8580
xorl %eax, %eax
jmp 0x11b3d
movq %rax, %r14
callq 0x8580
movq %r14, %rdi
callq 0x8600
movq %rax, %rdi
callq 0x9f28
nop