Compare commits

..

No commits in common. "a34fd9aefd4f2da7b99f29030df697ebe9cfa404" and "76d6c7c06953e225bc52da2604101c2b6610de59" have entirely different histories.

5 changed files with 26 additions and 117 deletions

View file

@ -29,7 +29,7 @@ set(GUI_SOURCES
add_executable(kima2 ${GUI_SOURCES}) add_executable(kima2 ${GUI_SOURCES})
target_include_directories(kima2 PRIVATE ${PROJECT_BINARY_DIR}) target_include_directories(kima2 PRIVATE ${PROJECT_BINARY_DIR})
target_link_libraries(kima2 core printer Qt5::Widgets Qt5::PrintSupport stdc++fs) target_link_libraries(kima2 core Qt5::Widgets Qt5::PrintSupport stdc++fs)
if(WIN32) if(WIN32)
set_target_properties(kima2 PROPERTIES LINK_FLAGS "-mwindows") set_target_properties(kima2 PROPERTIES LINK_FLAGS "-mwindows")
endif(WIN32) endif(WIN32)

View file

@ -1,7 +1,6 @@
#include "settingsdialog.h" #include "settingsdialog.h"
#include <database.h> #include <database.h>
#include <posprinter.h>
#include <QMessageBox> #include <QMessageBox>
#include <QSettings> #include <QSettings>
@ -20,12 +19,6 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par
ui_.posPrinterDeviceEdit->setText(posPrinterDevice); ui_.posPrinterDeviceEdit->setText(posPrinterDevice);
ui_.feePercentSpinBox->setValue(feeInPercent); ui_.feePercentSpinBox->setValue(feeInPercent);
ui_.maxFeeSpinBox->setValue(maxFeeInEuro); ui_.maxFeeSpinBox->setValue(maxFeeInEuro);
connect(ui_.testPosPrinterButton, &QPushButton::clicked, this, [](){
//PosPrinter::initialize({0, 0});
PosPrinter printer;
printer.printTest();
});
} }
void SettingsDialog::accept() void SettingsDialog::accept()

View file

@ -5,5 +5,5 @@ set(PRINTER_SOURCES
) )
add_library(printer STATIC ${PRINTER_SOURCES}) add_library(printer STATIC ${PRINTER_SOURCES})
target_link_libraries(printer ${LIBUSB_1_LIBRARIES}) target_link_libraries(printer ${LibUSB_LIBRARY})
target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View file

@ -2,18 +2,11 @@
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <sstream>
#include <string> #include <string>
const std::string PosPrinter::Command::RESET = {0x1b, 0x40}; PosPrinter::PosPrinter() : PosPrinter(std::pair<int, int>{0x0456, 0x0808}) {}
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};
PosPrinter::PosPrinter() PosPrinter::PosPrinter(std::pair<int, int> vendorModelId)
{ {
int retValue; int retValue;
@ -22,50 +15,17 @@ PosPrinter::PosPrinter()
throw std::runtime_error("Init error"); throw std::runtime_error("Init error");
} }
libusb_device** devList; // libusb_set_debug(contextPtr_, 3); // set verbosity level to 3, as suggested in the
int devCount = libusb_get_device_list(contextPtr_, &devList); // documentation
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) { devicePtr_ =
libusb_exit(contextPtr_); libusb_open_device_with_vid_pid(contextPtr_, vendorModelId.first,
return; vendorModelId.second); // these are vendorID and productID
} if (devicePtr_ == NULL) {
retValue = libusb_open(devList[numDevice], &devicePtr_); // these are vendorID and productID
if (retValue != 0) {
libusb_free_device_list(devList, 1);
libusb_exit(contextPtr_);
throw std::runtime_error("Cannot open printer device"); throw std::runtime_error("Cannot open printer device");
} }
if (libusb_kernel_driver_active(devicePtr_, 0) == 1) { // find out if kernel driver is attached // ...
std::cout << "Kernel driver active" << std::endl;
if (libusb_detach_kernel_driver(devicePtr_, 0) == 0) // detach it
std::cout << "Kernel driver detached!" << std::endl;
}
retValue = libusb_claim_interface(
devicePtr_, 0); // claim interface 0 (the first) of device (mine had jsut 1)
if (retValue < 0) {
std::cout << "Cannot claim printer interface" << std::endl;
throw std::runtime_error("Cannot claim printer interface");
}
libusb_free_device_list(devList, 1);
} }
PosPrinter::~PosPrinter() PosPrinter::~PosPrinter()
@ -73,47 +33,22 @@ PosPrinter::~PosPrinter()
int retValue; int retValue;
retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface
if (retValue != 0) { if (retValue != 0) {
std::cout << "Cannot release printer interface" << std::endl; std::cout << "Cannot Release Interface" << std::endl;
} else { } else {
std::cout << "Printer interface released" << std::endl; std::cout << "Released Interface" << std::endl;
} }
libusb_close(devicePtr_); // close the device we opened libusb_close(devicePtr_); // close the device we opened
libusb_exit(contextPtr_); // close the session libusb_exit(contextPtr_); // close the session
delete instance_;
} }
void PosPrinter::write(const std::string& text) void PosPrinter::initialize(std::pair<int, int> vendorModelId)
{ {
if (devicePtr_ == NULL) if (!instance_) {
return; instance_ = new PosPrinter(vendorModelId);
int length = text.length(); }
int actual{0};
int retValue = libusb_bulk_transfer(devicePtr_, (0x03 | LIBUSB_ENDPOINT_OUT),
(unsigned char*)text.c_str(), length, &actual, 10000);
if (retValue != 0 || actual != length)
std::cerr << "Write Error" << std::endl;
} }
void PosPrinter::printHeader() PosPrinter* PosPrinter::getInstance() { return instance_; }
{
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.";
commandStream << Command::FEED;
printHeader();
write(commandStream.str());
}

View file

@ -5,37 +5,18 @@
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
struct SupportedPrinters {
std::array<std::pair<int, int>, 2> models{
// {Vendor ID, Model ID}
std::make_pair(0x0456, 0x0808),
std::make_pair(0x0416, 0x5011),
};
};
class PosPrinter class PosPrinter
{ {
public: public:
PosPrinter(); static void initialize(std::pair<int, int> vendorModelIds);
~PosPrinter(); static PosPrinter* getInstance();
void write(const std::string& text);
void printHeader();
void printTest();
struct Command {
static const std::string RESET;
static const std::string ENCODING;
static const std::string CENTERING;
static const std::string FONT_SIZE_BIG;
static const std::string FONT_SIZE_NORMAL;
static const std::string LEFT_ALIGN;
static const std::string FEED;
};
private: private:
PosPrinter();
PosPrinter(std::pair<int, int> vendorModelId);
~PosPrinter();
static PosPrinter* instance_;
libusb_context* contextPtr_{}; libusb_context* contextPtr_{};
libusb_device_handle* devicePtr_{}; libusb_device_handle* devicePtr_{};
SupportedPrinters supportedPrinters_;
}; };
#endif #endif