kima2/src/printer/posprinter.cpp

55 lines
1.4 KiB
C++

#include "posprinter.h"
#include <exception>
#include <iostream>
#include <string>
PosPrinter::PosPrinter() : PosPrinter(std::pair<int, int>{0x0456, 0x0808}) {}
PosPrinter::PosPrinter(std::pair<int, int> vendorModelId)
{
int retValue;
retValue = libusb_init(&contextPtr_);
if (retValue < 0) {
throw std::runtime_error("Init error");
}
// libusb_set_debug(contextPtr_, 3); // set verbosity level to 3, as suggested in the
// documentation
devicePtr_ =
libusb_open_device_with_vid_pid(contextPtr_, vendorModelId.first,
vendorModelId.second); // these are vendorID and productID
if (devicePtr_ == NULL) {
throw std::runtime_error("Cannot open printer device");
}
// ...
}
PosPrinter::~PosPrinter()
{
int retValue;
retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface
if (retValue != 0) {
std::cout << "Cannot Release Interface" << std::endl;
} else {
std::cout << "Released Interface" << std::endl;
}
libusb_close(devicePtr_); // close the device we opened
libusb_exit(contextPtr_); // close the session
delete instance_;
}
void PosPrinter::initialize(std::pair<int, int> vendorModelId)
{
if (!instance_) {
instance_ = new PosPrinter(vendorModelId);
}
}
PosPrinter* PosPrinter::getInstance() { return instance_; }