diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index c891909..398a22a 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -24,6 +24,7 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par connect(ui_.testPosPrinterButton, &QPushButton::clicked, this, [](){ //PosPrinter::initialize({0, 0}); PosPrinter printer; + printer.printTest(); }); } diff --git a/src/printer/posprinter.cpp b/src/printer/posprinter.cpp index dcd242d..c174806 100644 --- a/src/printer/posprinter.cpp +++ b/src/printer/posprinter.cpp @@ -2,8 +2,17 @@ #include #include +#include #include +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}; + PosPrinter::PosPrinter() { int retValue; @@ -72,3 +81,39 @@ PosPrinter::~PosPrinter() libusb_close(devicePtr_); // close the device we opened libusb_exit(contextPtr_); // close the session } + +void PosPrinter::write(const std::string& text) +{ + if (devicePtr_ == NULL) + return; + 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() +{ + 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()); +} \ No newline at end of file diff --git a/src/printer/posprinter.h b/src/printer/posprinter.h index 33dfb19..ffdf166 100644 --- a/src/printer/posprinter.h +++ b/src/printer/posprinter.h @@ -18,6 +18,19 @@ class PosPrinter public: PosPrinter(); ~PosPrinter(); + 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: libusb_context* contextPtr_{};