// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x10
popq %rdi
callq 0x8300
movq %rax, %r15
movaps 0x94bd(%rip), %xmm0     # 0x188e0
movaps %xmm0, 0x20(%rsp)
andl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rsp)
andl $0x0, 0x18(%rsp)
pushq $0x8
popq %rcx
pushq $0x1
popq %r9
movq %r15, %rdi
movq %r14, %rsi
movl $0x4b00, %edx           # imm = 0x4B00
xorl %r8d, %r8d
callq 0x9e4c
movq %r15, 0x8(%rbx)
movb $0x1, 0x10(%rbx)
movl $0xc350, %edi           # imm = 0xC350
callq 0x8480
movq %rbx, %rdi
callq 0xf53e
movl $0xc350, %edi           # imm = 0xC350
callq 0x8480
pushq $0x7
popq %rsi
pushq $0x64
popq %rdx
pushq $0x2
popq %rcx
movq %rbx, %rdi
callq 0xf556
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xf5bc
movq 0x8(%rbx), %rdi
callq 0xa7e2
movq %rbx, %rdi
callq 0xf5e0
movb 0x10(%rbx), %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rdx, %r12
movq %rax, %r14
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x8310
jmp 0xf4d6
movq %rdx, %r12
movq %rax, %r14
cmpl $0x1, %r12d
jne 0xf52e
movq %r14, %rdi
callq 0x8140
movq %rax, %r14
movq 0x10ad2(%rip), %rdi     # 0x1ffc0
leaq 0x989a(%rip), %rsi      # 0x18d8f
callq 0x82e0
movq %rax, %r15
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x82e0
movq %rax, %rdi
callq 0x8190
movb $0x0, 0x10(%rbx)
callq 0x8550
xorl %eax, %eax
jmp 0xf4b1
movq %rax, %r14
callq 0x8550
movq %r14, %rdi
callq 0x85c0
movq %rax, %rdi
callq 0x96b5