pos printer introduced
This commit is contained in:
parent
c973b02013
commit
76d6c7c069
5 changed files with 184 additions and 0 deletions
|
@ -1,2 +1,3 @@
|
|||
add_subdirectory("core")
|
||||
add_subdirectory("printer")
|
||||
add_subdirectory("gui")
|
9
src/printer/CMakeLists.txt
Normal file
9
src/printer/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
find_package(LibUSB REQUIRED)
|
||||
|
||||
set(PRINTER_SOURCES
|
||||
posprinter.cpp
|
||||
)
|
||||
|
||||
add_library(printer STATIC ${PRINTER_SOURCES})
|
||||
target_link_libraries(printer ${LibUSB_LIBRARY})
|
||||
target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
54
src/printer/posprinter.cpp
Normal file
54
src/printer/posprinter.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#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_; }
|
22
src/printer/posprinter.h
Normal file
22
src/printer/posprinter.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef POS_PRINTER_H
|
||||
#define POS_PRINTER_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <libusb-1.0/libusb.h>
|
||||
|
||||
class PosPrinter
|
||||
{
|
||||
public:
|
||||
static void initialize(std::pair<int, int> vendorModelIds);
|
||||
static PosPrinter* getInstance();
|
||||
private:
|
||||
PosPrinter();
|
||||
PosPrinter(std::pair<int, int> vendorModelId);
|
||||
~PosPrinter();
|
||||
static PosPrinter* instance_;
|
||||
libusb_context* contextPtr_{};
|
||||
libusb_device_handle* devicePtr_{};
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue