kima2/src/printer/posprinter.cpp

137 lines
4.5 KiB
C++
Raw Normal View History

2018-08-05 21:51:30 +02:00
#include "posprinter.h"
#include <algorithm>
2018-08-05 21:51:30 +02:00
#include <exception>
#include <iostream>
2018-08-06 12:52:45 +02:00
#include <sstream>
2018-08-05 21:51:30 +02:00
#include <string>
#include <boost/algorithm/string.hpp>
2018-08-05 21:51:30 +02:00
2018-08-06 12:52:45 +02:00
const std::string PosPrinter::Command::RESET = {0x1b, 0x40};
const std::string PosPrinter::Command::ENCODING = {'\x1b', '\x74', 16};
const std::string PosPrinter::Command::CENTERING = {'\x1b', '\x61', '\x01'};
const std::string PosPrinter::Command::FONT_SIZE_BIG = {'\x1b', '\x21', '\x10'};
const std::string PosPrinter::Command::FONT_SIZE_NORMAL = {'\x1b', '\x21', '\x00'};
const std::string PosPrinter::Command::LEFT_ALIGN = {'\x1b', '\x61', '\x00'};
const std::string PosPrinter::Command::FEED = {0x1b, 0x64, 0x03};
2018-08-06 09:59:06 +02:00
PosPrinter::PosPrinter()
2018-08-05 21:51:30 +02:00
{
int retValue;
retValue = libusb_init(&contextPtr_);
if (retValue < 0) {
throw std::runtime_error("Init error");
}
2018-08-06 13:52:58 +02:00
#if defined(_WIN32)
libusb_set_option(contextPtr_, LIBUSB_OPTION_USE_USBDK);
#endif
2018-08-06 09:59:06 +02:00
libusb_device** devList;
int devCount = libusb_get_device_list(contextPtr_, &devList);
if (devCount <= 0) {
libusb_exit(contextPtr_);
throw std::runtime_error("Could not receive device list");
}
int numDevice = -1;
for (int i = 0; i < devCount; ++i) {
libusb_device_descriptor desc;
libusb_get_device_descriptor(devList[i], &desc);
for (const auto& supported : supportedPrinters_.models) {
if (desc.idVendor == supported.first && desc.idProduct == supported.second) {
numDevice = i;
break;
}
}
}
if (numDevice < 0) {
libusb_exit(contextPtr_);
return;
}
2018-08-05 21:51:30 +02:00
2018-08-06 09:59:06 +02:00
retValue = libusb_open(devList[numDevice], &devicePtr_); // these are vendorID and productID
if (retValue != 0) {
libusb_free_device_list(devList, 1);
libusb_exit(contextPtr_);
2018-08-05 21:51:30 +02:00
throw std::runtime_error("Cannot open printer device");
}
2018-08-06 09:59:06 +02:00
if (libusb_kernel_driver_active(devicePtr_, 0) == 1) { // find out if kernel driver is attached
2018-08-06 10:00:22 +02:00
std::cout << "Kernel driver active" << std::endl;
2018-08-06 09:59:06 +02:00
if (libusb_detach_kernel_driver(devicePtr_, 0) == 0) // detach it
2018-08-06 10:00:22 +02:00
std::cout << "Kernel driver detached!" << std::endl;
2018-08-06 09:59:06 +02:00
}
retValue = libusb_claim_interface(
devicePtr_, 0); // claim interface 0 (the first) of device (mine had jsut 1)
if (retValue < 0) {
2018-08-06 10:00:22 +02:00
std::cout << "Cannot claim printer interface" << std::endl;
throw std::runtime_error("Cannot claim printer interface");
2018-08-06 09:59:06 +02:00
}
libusb_free_device_list(devList, 1);
2018-08-05 21:51:30 +02:00
}
PosPrinter::~PosPrinter()
{
int retValue;
retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface
if (retValue != 0) {
2018-08-06 10:00:22 +02:00
std::cout << "Cannot release printer interface" << std::endl;
2018-08-05 21:51:30 +02:00
} else {
2018-08-06 10:00:22 +02:00
std::cout << "Printer interface released" << std::endl;
2018-08-05 21:51:30 +02:00
}
libusb_close(devicePtr_); // close the device we opened
libusb_exit(contextPtr_); // close the session
}
2018-08-06 12:52:45 +02:00
void PosPrinter::write(const std::string& text)
{
if (devicePtr_ == NULL)
return;
using namespace std::string_literals;
std::string text_escaped = text;
boost::replace_all(text_escaped, "ä", "\xe4");
boost::replace_all(text_escaped, "ö", "\xf6");
boost::replace_all(text_escaped, "ü", "\xfc");
boost::replace_all(text_escaped, "Ä", "\xc4");
boost::replace_all(text_escaped, "Ö", "\xd6");
boost::replace_all(text_escaped, "Ü", "\xdc");
boost::replace_all(text_escaped, "", "\x80");
int length = text_escaped.length();
2018-08-06 12:52:45 +02:00
int actual{0};
int retValue =
libusb_bulk_transfer(devicePtr_, (0x03 | LIBUSB_ENDPOINT_OUT),
(unsigned char*)text_escaped.c_str(), length, &actual, 10000);
2018-08-06 12:52:45 +02:00
if (retValue != 0 || actual != length)
std::cerr << "Write Error" << std::endl;
}
void PosPrinter::printHeader()
{
std::stringstream commandStream;
commandStream << Command::RESET << Command::ENCODING << Command::CENTERING
<< Command::FONT_SIZE_BIG;
commandStream << "Kindersachenmarkt\nDettingen\n\n";
commandStream << Command::LEFT_ALIGN << Command::Command::FONT_SIZE_NORMAL;
write(commandStream.str());
}
void PosPrinter::printTest()
{
using namespace std::string_literals;
std::stringstream commandStream;
commandStream << Command::ENCODING;
commandStream << "Der Drucker kann von KIMA2\nangesprochen werden.\n\n"
<< "Beachten Sie, dass nicht\nalle Modelle Strichcodes\nausdrucken können.";
2018-08-06 12:52:45 +02:00
commandStream << Command::FEED;
printHeader();
write(commandStream.str());
}