Compare commits
10 Commits
fix-logger
...
b42e866a49
Author | SHA1 | Date | |
---|---|---|---|
b42e866a49
|
|||
3e9c3267e2
|
|||
46cc16e431
|
|||
0704081326
|
|||
f0812d3166
|
|||
c3110ff32a
|
|||
f4036e6a83
|
|||
778a01429c | |||
d8b70e5d56 | |||
cd15930e1d |
@@ -14,7 +14,7 @@ jobs:
|
|||||||
- name: install meson
|
- name: install meson
|
||||||
run: apt-get -yq install meson
|
run: apt-get -yq install meson
|
||||||
- name: Install build dependencies
|
- name: Install build dependencies
|
||||||
run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev gcovr
|
run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev gcovr
|
||||||
- name: setup builddir
|
- name: setup builddir
|
||||||
run: meson setup builddir -Db_coverage=true
|
run: meson setup builddir -Db_coverage=true
|
||||||
- name: build all targets
|
- name: build all targets
|
||||||
|
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,9 +1,13 @@
|
|||||||
|
# Folder
|
||||||
builddir/
|
builddir/
|
||||||
|
ptouch-print/
|
||||||
subprojects/*
|
subprojects/*
|
||||||
!subprojects/*.wrap
|
|
||||||
.cache/
|
.cache/
|
||||||
|
coverageReport/
|
||||||
|
|
||||||
|
# Files
|
||||||
|
!subprojects/*.wrap
|
||||||
.vscode/*
|
.vscode/*
|
||||||
!.vscode/c_cpp_properties.json
|
!.vscode/c_cpp_properties.json
|
||||||
!.vscode/settings.json
|
!.vscode/settings.json
|
||||||
!.vscode/launch.json
|
!.vscode/launch.json
|
||||||
ptouch-print/
|
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -85,5 +85,6 @@
|
|||||||
},
|
},
|
||||||
"clang-tidy.buildPath": "builddir/",
|
"clang-tidy.buildPath": "builddir/",
|
||||||
"clangd.onConfigChanged": "restart",
|
"clangd.onConfigChanged": "restart",
|
||||||
"C_Cpp.default.compileCommands": "builddir/compile_commands.json"
|
"C_Cpp.default.compileCommands": "/home/moritz/Projekte/ptouch-prnt/builddir/compile_commands.json",
|
||||||
|
"gcovViewer.buildDirectories": ["/home/moritz/Projekte/ptouch-prnt/builddir"]
|
||||||
}
|
}
|
||||||
|
@@ -19,7 +19,7 @@ pacman -S libusb spdlog pango cairo meson gcovr
|
|||||||
|
|
||||||
Install dependencies on Debian/Ubuntu
|
Install dependencies on Debian/Ubuntu
|
||||||
``` bash
|
``` bash
|
||||||
apt-get install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev meson gcovr
|
apt-get install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev meson gcovr
|
||||||
```
|
```
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
27
generate_coverage.sh
Executable file
27
generate_coverage.sh
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
|
||||||
|
HTML_COV_PATH="coverageReport/html"
|
||||||
|
XML_COV_PATH="coverageReport/xml"
|
||||||
|
HTML_START_FILE="index.html"
|
||||||
|
|
||||||
|
echo "Generating Coverage report for ptouch-prnt"
|
||||||
|
|
||||||
|
ninja -C builddir
|
||||||
|
ninja -C builddir test
|
||||||
|
|
||||||
|
mkdir -p ${HTML_COV_PATH}
|
||||||
|
gcovr --html --html-details --html-syntax-highlighting --filter src --output ${HTML_COV_PATH}/${HTML_START_FILE}
|
||||||
|
|
||||||
|
mkdir -p ${XML_COV_PATH}
|
||||||
|
gcovr --xml-pretty --filter src --output ${XML_COV_PATH}/cov.xml
|
||||||
|
|
||||||
|
if [ $? ]
|
||||||
|
then
|
||||||
|
echo "Coverage report successful generated!"
|
||||||
|
echo "Open: file://${SCRIPT_PATH}/${HTML_COV_PATH}/${HTML_START_FILE}"
|
||||||
|
else
|
||||||
|
echo "Error generating coverage report!"
|
||||||
|
fi
|
||||||
|
|
||||||
|
rm *.gcov
|
41
meson.build
41
meson.build
@@ -1,11 +1,12 @@
|
|||||||
project('ptprnt', 'cpp',
|
project('ptprnt', 'cpp',
|
||||||
version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD', check: true).stdout().strip(),
|
version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD', check: true).stdout().strip(),
|
||||||
license: 'GPLv3',
|
license: 'GPLv3',
|
||||||
default_options : ['c_std=c11', 'cpp_std=c++17']
|
default_options : ['c_std=c11', 'cpp_std=c++17', 'b_sanitize=none', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true']
|
||||||
)
|
)
|
||||||
|
|
||||||
usb_dep = dependency('libusb-1.0')
|
usb_dep = dependency('libusb-1.0')
|
||||||
log_dep = dependency('spdlog')
|
log_dep = dependency('spdlog')
|
||||||
|
fmt_dep = dependency('fmt')
|
||||||
pangocairo_dep = dependency('pangocairo')
|
pangocairo_dep = dependency('pangocairo')
|
||||||
|
|
||||||
# CLI11
|
# CLI11
|
||||||
@@ -17,45 +18,15 @@ endif
|
|||||||
|
|
||||||
incdir = include_directories('src')
|
incdir = include_directories('src')
|
||||||
|
|
||||||
ptprnt_hpps = [
|
subdir('src')
|
||||||
'src/libusbwrap/interface/IUsbDeviceFactory.hpp',
|
|
||||||
'src/libusbwrap/interface/IUsbDevice.hpp',
|
|
||||||
'src/libusbwrap/UsbDeviceFactory.hpp',
|
|
||||||
'src/libusbwrap/LibUsbTypes.hpp',
|
|
||||||
'src/libusbwrap/UsbDevice.hpp',
|
|
||||||
'src/interface/IPrinterDriver.hpp',
|
|
||||||
'src/interface/IPrinterTypes.hpp',
|
|
||||||
'src/P700Printer.hpp',
|
|
||||||
'src/PtouchPrint.hpp',
|
|
||||||
'src/graphics/Bitmap.hpp',
|
|
||||||
'src/graphics/Image.hpp',
|
|
||||||
'src/graphics/Monochrome.hpp'
|
|
||||||
]
|
|
||||||
|
|
||||||
ptprnt_srcs = [
|
|
||||||
'src/PtouchPrint.cpp',
|
|
||||||
'src/P700Printer.cpp',
|
|
||||||
'src/graphics/Image.cpp',
|
|
||||||
'src/graphics/Bitmap.cpp',
|
|
||||||
'src/graphics/Monochrome.cpp',
|
|
||||||
'src/libusbwrap/UsbDeviceFactory.cpp',
|
|
||||||
'src/libusbwrap/UsbDevice.cpp',
|
|
||||||
]
|
|
||||||
|
|
||||||
ptprnt_lib = library('ptprnt',
|
|
||||||
include_directories: incdir,
|
|
||||||
install: true,
|
|
||||||
dependencies: [usb_dep, log_dep, pangocairo_dep, cli11_dep],
|
|
||||||
sources: [ptprnt_hpps, ptprnt_srcs])
|
|
||||||
|
|
||||||
ptprnt_dep = declare_dependency(include_directories: incdir,
|
|
||||||
link_with: ptprnt_lib)
|
|
||||||
|
|
||||||
ptprnt_exe = executable(
|
ptprnt_exe = executable(
|
||||||
'ptprnt',
|
'ptprnt',
|
||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
install: true,
|
install: true,
|
||||||
dependencies : [usb_dep, log_dep, cli11_dep, ptprnt_dep],
|
dependencies : [usb_dep, log_dep, fmt_dep, pangocairo_dep, cli11_dep],
|
||||||
|
include_directories: incdir,
|
||||||
|
sources: [ptprnt_srcs],
|
||||||
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'],
|
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -30,7 +30,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "graphics/Bitmap.hpp"
|
#include "graphics/Bitmap.hpp"
|
||||||
#include "graphics/Image.hpp"
|
#include "graphics/Label.hpp"
|
||||||
#include "graphics/Monochrome.hpp"
|
#include "graphics/Monochrome.hpp"
|
||||||
#include "libusb.h"
|
#include "libusb.h"
|
||||||
#include "libusbwrap/LibUsbTypes.hpp"
|
#include "libusbwrap/LibUsbTypes.hpp"
|
||||||
@@ -122,21 +122,23 @@ bool P700Printer::detachUsbDevice() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
|
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
|
||||||
auto bm = graphics::Bitmap<graphics::ALPHA8>(512, 128);
|
#ifdef DRYRUN
|
||||||
{
|
SPDLOG_DEBUG("DRYRUN enabled");
|
||||||
auto img = graphics::Image();
|
for (unsigned int lineNo = 0; lineNo < bitmap.getHeight(); lineNo++) {
|
||||||
bm.setPixels(std::vector<uint8_t>(img.getRaw(), img.getRaw() + 512 * 128));
|
auto line = bitmap.getLine(lineNo);
|
||||||
|
auto monoLine = graphics::Monochrome(*line);
|
||||||
|
monoLine.visualize();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
send(commands["rasterstart"]);
|
send(commands["rasterstart"]);
|
||||||
|
|
||||||
std::vector<uint8_t> rastercmd(4);
|
std::vector<uint8_t> rastercmd(4);
|
||||||
rastercmd[0] = 0x47;
|
rastercmd[0] = 0x47;
|
||||||
rastercmd[1] = 0x00; // size +1
|
rastercmd[1] = 0x00; // size +1
|
||||||
rastercmd[2] = 0x00;
|
rastercmd[2] = 0x00;
|
||||||
rastercmd[3] = 0x00; // size -1
|
rastercmd[3] = 0x00; // size -1
|
||||||
for (unsigned int i = 0; i < bm.getWidth(); i++) {
|
for (unsigned int i = 0; i < bitmap.getWidth(); i++) {
|
||||||
auto bmcol = bm.getCol(i);
|
auto bmcol = bitmap.getCol(i);
|
||||||
if (!bmcol) {
|
if (!bmcol) {
|
||||||
spdlog::error("Out of bounds bitmap access");
|
spdlog::error("Out of bounds bitmap access");
|
||||||
break;
|
break;
|
||||||
@@ -151,6 +153,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
|
|||||||
buf[1] = col.size() + 1;
|
buf[1] = col.size() + 1;
|
||||||
buf[3] = col.size() - 1;
|
buf[3] = col.size() - 1;
|
||||||
if (!send(buf)) {
|
if (!send(buf)) {
|
||||||
|
spdlog::error("Error sending buffer to printer");
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -182,11 +185,11 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
tx = data.size();
|
tx = data.size();
|
||||||
spdlog::debug("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
|
spdlog::info("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (tx != static_cast<int>(data.size())) {
|
if (tx != static_cast<int>(data.size())) {
|
||||||
spdlog::error("Could not transfer all data via USB bulk transfer. Only send {} of {} bytes",
|
spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes",
|
||||||
tx, data.size());
|
tx, data.size());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
@@ -67,7 +68,8 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
|
|||||||
.name = "Brother P-touch P700",
|
.name = "Brother P-touch P700",
|
||||||
.version = "v1.0",
|
.version = "v1.0",
|
||||||
.vid = 0x04f9,
|
.vid = 0x04f9,
|
||||||
.pid = 0x2061};
|
.pid = 0x2061,
|
||||||
|
.pixelLines = 128};
|
||||||
std::map<std::string, std::vector<uint8_t>> commands{
|
std::map<std::string, std::vector<uint8_t>> commands{
|
||||||
{"rasterstart",
|
{"rasterstart",
|
||||||
{0x1b, 0x69, 0x61,
|
{0x1b, 0x69, 0x61,
|
||||||
|
@@ -16,18 +16,32 @@
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#include "PtouchPrint.hpp"
|
#include "graphics/Monochrome.hpp"
|
||||||
|
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
|
||||||
|
#define SPDLOG_DEBUG_ON
|
||||||
|
#define SPDLOG_TRACE_ON
|
||||||
|
|
||||||
#include <CLI/App.hpp>
|
#include <CLI/App.hpp>
|
||||||
|
#include <fmt/core.h>
|
||||||
#include <spdlog/common.h>
|
#include <spdlog/common.h>
|
||||||
|
#include <spdlog/details/synchronous_factory.h>
|
||||||
|
#include <spdlog/logger.h>
|
||||||
|
#include <spdlog/sinks/base_sink.h>
|
||||||
|
#include <spdlog/sinks/basic_file_sink.h>
|
||||||
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "CLI/Option.hpp"
|
#include "CLI/Option.hpp"
|
||||||
#include "P700Printer.hpp"
|
#include "P700Printer.hpp"
|
||||||
|
#include "PtouchPrint.hpp"
|
||||||
#include "graphics/Bitmap.hpp"
|
#include "graphics/Bitmap.hpp"
|
||||||
|
#include "graphics/Label.hpp"
|
||||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||||
|
|
||||||
namespace ptprnt {
|
namespace ptprnt {
|
||||||
@@ -45,9 +59,9 @@ int PtouchPrint::init(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mVerboseFlag) {
|
if (mVerboseFlag) {
|
||||||
setupLogger(spdlog::level::trace);
|
setupLogger(spdlog::level::debug);
|
||||||
} else {
|
} else {
|
||||||
setupLogger(spdlog::level::critical);
|
setupLogger(spdlog::level::warn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mUsbDeviceFactory.init()) {
|
if (!mUsbDeviceFactory.init()) {
|
||||||
@@ -59,10 +73,12 @@ int PtouchPrint::init(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int PtouchPrint::run() {
|
int PtouchPrint::run() {
|
||||||
|
SPDLOG_DEBUG("ptprnt version {}", mVersionString);
|
||||||
|
SPDLOG_TRACE("testing trace");
|
||||||
auto numFoundPrinters = getCompatiblePrinters();
|
auto numFoundPrinters = getCompatiblePrinters();
|
||||||
if (numFoundPrinters == 0) {
|
if (numFoundPrinters == 0) {
|
||||||
spdlog::error(
|
spdlog::error(
|
||||||
"No compatible printers found, please make sure if they are turned on and connected");
|
"No compatible printers found, please make sure that they are turned on and connected");
|
||||||
return -1;
|
return -1;
|
||||||
} else if (numFoundPrinters > 1) {
|
} else if (numFoundPrinters > 1) {
|
||||||
spdlog::warn("Found more than one compatible printer. Currently not supported.");
|
spdlog::warn("Found more than one compatible printer. Currently not supported.");
|
||||||
@@ -76,15 +92,29 @@ int PtouchPrint::run() {
|
|||||||
"Found more than one device of the same printer on bus. Currently not supported");
|
"Found more than one device of the same printer on bus. Currently not supported");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
printer->attachUsbDevice(devices[0]);
|
printer->attachUsbDevice(std::move(devices[0]));
|
||||||
auto status = printer->getPrinterStatus();
|
auto status = printer->getPrinterStatus();
|
||||||
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
|
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
|
||||||
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128);
|
|
||||||
//printer->printBitmap(bm);
|
if (0 == mCommands.size()) {
|
||||||
//printer->printText("wurst", 1);
|
spdlog::warn("No command specified, nothing to do...");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
for (auto& cmd : mCommands) {
|
for (auto& cmd : mCommands) {
|
||||||
std::cout << cmd.second << std::endl;
|
SPDLOG_DEBUG("Command: {}", cmd.second);
|
||||||
|
if (cmd.first == CliCmdType::Text) {
|
||||||
|
auto label{graphics::Label()};
|
||||||
|
label.create(cmd.second, printer->getPrinterInfo().pixelLines);
|
||||||
|
auto bm = graphics::Bitmap<graphics::ALPHA8>(label.getLayoutWidth(),
|
||||||
|
printer->getPrinterInfo().pixelLines);
|
||||||
|
if (!bm.setPixels(label.getRaw())) {
|
||||||
|
spdlog::error("Non-matching bitmap size");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
label.writeToPng("salat.png");
|
||||||
|
printer->printBitmap(bm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -93,10 +123,10 @@ int PtouchPrint::run() {
|
|||||||
unsigned int PtouchPrint::getCompatiblePrinters() {
|
unsigned int PtouchPrint::getCompatiblePrinters() {
|
||||||
auto usbDevs = mUsbDeviceFactory.findAllDevices();
|
auto usbDevs = mUsbDeviceFactory.findAllDevices();
|
||||||
|
|
||||||
for (auto usbDev : usbDevs) {
|
for (auto& usbDev : usbDevs) {
|
||||||
auto foundPrinterIt =
|
auto foundPrinterIt =
|
||||||
std::find_if(mCompatiblePrinters.begin(), mCompatiblePrinters.end(),
|
std::find_if(mCompatiblePrinters.begin(), mCompatiblePrinters.end(),
|
||||||
[usbDev](const std::shared_ptr<ptprnt::IPrinterDriver>& printer) {
|
[&usbDev](const std::shared_ptr<ptprnt::IPrinterDriver>& printer) {
|
||||||
return printer->getPid() == usbDev->getPid() &&
|
return printer->getPid() == usbDev->getPid() &&
|
||||||
printer->getVid() == usbDev->getVid();
|
printer->getVid() == usbDev->getVid();
|
||||||
});
|
});
|
||||||
@@ -113,13 +143,27 @@ unsigned int PtouchPrint::getCompatiblePrinters() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
|
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
|
||||||
spdlog::set_level(lvl);
|
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||||
spdlog::debug("Starting ptprnt {}", mVersionString);
|
consoleSink->set_level(lvl);
|
||||||
|
if (spdlog::level::level_enum::debug == lvl || spdlog::level::level_enum::trace == lvl) {
|
||||||
|
// This will enable file and line number for debug and trace macros
|
||||||
|
consoleSink->set_pattern("%^%L:%$ %v (%s:%#)");
|
||||||
|
} else {
|
||||||
|
consoleSink->set_pattern("%^%L:%$ %v");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("ptprnt.log", true);
|
||||||
|
fileSink->set_level(spdlog::level::trace);
|
||||||
|
fileSink->set_pattern("%Y-%m-%d %H:%m:%S:%e [pid:%P tid:%t] [%^%l%$] %v (%@)");
|
||||||
|
std::vector<spdlog::sink_ptr> sinks{consoleSink, fileSink};
|
||||||
|
auto logger = std::make_shared<spdlog::logger>("default_logger", sinks.begin(), sinks.end());
|
||||||
|
logger->set_level(spdlog::level::trace);
|
||||||
|
spdlog::set_default_logger(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PtouchPrint::setupCliParser() {
|
void PtouchPrint::setupCliParser() {
|
||||||
auto printVersion = [this](std::size_t) {
|
auto printVersion = [this](std::size_t) {
|
||||||
std::cout << "ptprnt version: " << mVersionString << std::endl;
|
fmt::print("ptprnt version: {}\n", mVersionString);
|
||||||
};
|
};
|
||||||
|
|
||||||
// General options
|
// General options
|
||||||
|
@@ -22,6 +22,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -32,19 +33,20 @@ Bitmap<T>::Bitmap(uint16_t width, uint16_t height)
|
|||||||
: mWidth{width}, mHeight{height}, mPixels(width * height) {}
|
: mWidth{width}, mHeight{height}, mPixels(width * height) {}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
uint16_t Bitmap<T>::getWidth() {
|
[[nodiscard]] uint16_t Bitmap<T>::getWidth() const {
|
||||||
return mWidth;
|
return mWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
uint16_t Bitmap<T>::getHeight() {
|
[[nodiscard]] uint16_t Bitmap<T>::getHeight() const {
|
||||||
return mHeight;
|
return mHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
|
bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
|
||||||
if (pixels.size() != mPixels.size()) {
|
if (pixels.size() != mPixels.size()) {
|
||||||
spdlog::error("Invalid pixel buffer size.");
|
spdlog::error("Invalid pixel buffer size (got {} vs. {} bitmap size).", pixels.size(),
|
||||||
|
mPixels.size());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +55,12 @@ bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
|
[[nodiscard]] std::vector<T> Bitmap<T>::getPixelsCpy() const {
|
||||||
|
return mPixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
[[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) const {
|
||||||
if (line >= mHeight) {
|
if (line >= mHeight) {
|
||||||
// out of bound
|
// out of bound
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
@@ -65,7 +72,7 @@ std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) {
|
[[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) const {
|
||||||
if (col >= mWidth) {
|
if (col >= mWidth) {
|
||||||
// out of bound
|
// out of bound
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
@@ -22,6 +22,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -38,11 +39,18 @@ class Bitmap {
|
|||||||
Bitmap(uint16_t width, uint16_t height);
|
Bitmap(uint16_t width, uint16_t height);
|
||||||
~Bitmap() = default;
|
~Bitmap() = default;
|
||||||
|
|
||||||
uint16_t getWidth();
|
Bitmap(const Bitmap&) = default;
|
||||||
uint16_t getHeight();
|
Bitmap& operator=(const Bitmap&) = default;
|
||||||
|
Bitmap(Bitmap&&) = default;
|
||||||
|
Bitmap& operator=(Bitmap&&) = default;
|
||||||
|
|
||||||
|
[[nodiscard]] uint16_t getWidth() const;
|
||||||
|
[[nodiscard]] uint16_t getHeight() const;
|
||||||
bool setPixels(const std::vector<T>& pixels);
|
bool setPixels(const std::vector<T>& pixels);
|
||||||
std::optional<std::vector<T>> getLine(uint16_t line);
|
[[nodiscard]] std::vector<T> getPixelsCpy() const;
|
||||||
std::optional<std::vector<T>> getCol(uint16_t col);
|
[[nodiscard]] std::optional<std::vector<T>> getLine(uint16_t line) const;
|
||||||
|
[[nodiscard]] std::optional<std::vector<T>> getCol(uint16_t col) const;
|
||||||
|
void visualize() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t mWidth;
|
uint16_t mWidth;
|
||||||
|
@@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
ptrnt - print labels on linux
|
|
||||||
Copyright (C) 2023 Moritz Martinius
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Image.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "pango/pango-font.h"
|
|
||||||
|
|
||||||
namespace ptprnt::graphics {
|
|
||||||
Image::Image() {
|
|
||||||
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128);
|
|
||||||
cairo_t* cr = cairo_create(mSurface);
|
|
||||||
mFontDescription = pango_font_description_new();
|
|
||||||
pango_font_description_set_family(mFontDescription, "sans");
|
|
||||||
pango_font_description_set_weight(mFontDescription, PANGO_WEIGHT_SEMIBOLD);
|
|
||||||
pango_font_description_set_size(mFontDescription, 60 * PANGO_SCALE);
|
|
||||||
|
|
||||||
std::string printThis("Mist 💩");
|
|
||||||
|
|
||||||
mLayout = pango_cairo_create_layout(cr);
|
|
||||||
pango_layout_set_font_description(mLayout, mFontDescription);
|
|
||||||
pango_layout_set_text(mLayout, printThis.c_str(), -1);
|
|
||||||
|
|
||||||
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
|
|
||||||
cairo_move_to(cr, 0.0, 94.0);
|
|
||||||
pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 0));
|
|
||||||
|
|
||||||
cairo_surface_write_to_png(mSurface, "hello.png");
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t* Image::getRaw() {
|
|
||||||
cairo_surface_flush(mSurface);
|
|
||||||
return cairo_image_surface_get_data(mSurface);
|
|
||||||
}
|
|
||||||
|
|
||||||
Image::~Image() {
|
|
||||||
g_object_unref(mLayout);
|
|
||||||
pango_font_description_free(mFontDescription);
|
|
||||||
}
|
|
||||||
} // namespace ptprnt::graphics
|
|
@@ -1,38 +0,0 @@
|
|||||||
/*
|
|
||||||
ptrnt - print labels on linux
|
|
||||||
Copyright (C) 2023 Moritz Martinius
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <pango/pangocairo.h>
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
namespace ptprnt::graphics {
|
|
||||||
class Image {
|
|
||||||
public:
|
|
||||||
Image();
|
|
||||||
~Image();
|
|
||||||
uint8_t* getRaw();
|
|
||||||
|
|
||||||
private:
|
|
||||||
PangoLayout* mLayout;
|
|
||||||
PangoFontDescription* mFontDescription;
|
|
||||||
cairo_surface_t* mSurface;
|
|
||||||
};
|
|
||||||
} // namespace ptprnt::graphics
|
|
105
src/graphics/Label.cpp
Normal file
105
src/graphics/Label.cpp
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
ptrnt - print labels on linux
|
||||||
|
Copyright (C) 2023 Moritz Martinius
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "graphics/Label.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <iostream> // remove me
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "cairo.h"
|
||||||
|
#include "pango/pango-context.h"
|
||||||
|
#include "pango/pango-font.h"
|
||||||
|
#include "pango/pango-fontmap.h"
|
||||||
|
#include "pango/pango-layout.h"
|
||||||
|
#include "pango/pango-types.h"
|
||||||
|
#include "pango/pangocairo.h"
|
||||||
|
|
||||||
|
namespace ptprnt::graphics {
|
||||||
|
Label::Label()
|
||||||
|
: mPangoCtx(pango_font_map_create_context(pango_cairo_font_map_get_default())),
|
||||||
|
mPangoLyt(pango_layout_new(mPangoCtx)),
|
||||||
|
mPangoFontDesc(pango_font_description_from_string("Noto sans 32")) {}
|
||||||
|
|
||||||
|
std::vector<uint8_t> Label::getRaw() {
|
||||||
|
assert(mSurface != nullptr);
|
||||||
|
size_t len = mPrinterHeight * mLayoutWidth;
|
||||||
|
|
||||||
|
cairo_surface_flush(mSurface);
|
||||||
|
auto data = cairo_image_surface_get_data(mSurface);
|
||||||
|
return {data, data + len};
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Label::getNumLines(std::string_view strv) {
|
||||||
|
return std::count(strv.begin(), strv.end(), '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
int Label::getLayoutHeight() {
|
||||||
|
return mLayoutHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Label::getLayoutWidth() {
|
||||||
|
return mLayoutWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Label::create(const std::string& labelText, const uint16_t heightPixel) {
|
||||||
|
mPrinterHeight = heightPixel;
|
||||||
|
pango_layout_set_single_paragraph_mode(mPangoLyt, true);
|
||||||
|
pango_layout_set_height(mPangoLyt, getNumLines(labelText) * -1);
|
||||||
|
pango_layout_set_alignment(mPangoLyt, PANGO_ALIGN_CENTER);
|
||||||
|
pango_layout_set_font_description(mPangoLyt, mPangoFontDesc);
|
||||||
|
pango_context_load_font(mPangoCtx, mPangoFontDesc);
|
||||||
|
pango_layout_set_text(mPangoLyt, labelText.c_str(), static_cast<int>(labelText.length()));
|
||||||
|
|
||||||
|
pango_layout_get_size(mPangoLyt, &mLayoutWidth, &mLayoutHeight);
|
||||||
|
|
||||||
|
mLayoutWidth /= PANGO_SCALE;
|
||||||
|
mLayoutHeight /= PANGO_SCALE;
|
||||||
|
|
||||||
|
SPDLOG_DEBUG("Layout width: {}, height: {}", mLayoutWidth, mLayoutHeight);
|
||||||
|
|
||||||
|
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, mLayoutWidth, mPrinterHeight);
|
||||||
|
cairo_t* cr = cairo_create(mSurface);
|
||||||
|
pango_cairo_show_layout(cr, mPangoLyt);
|
||||||
|
|
||||||
|
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
|
||||||
|
cairo_surface_flush(mSurface);
|
||||||
|
cairo_destroy(cr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Label::writeToPng(const std::string& file) {
|
||||||
|
if (mSurface) {
|
||||||
|
cairo_surface_flush(mSurface);
|
||||||
|
cairo_surface_write_to_png(mSurface, file.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Label::~Label() {
|
||||||
|
SPDLOG_DEBUG("Image dtor...");
|
||||||
|
pango_font_description_free(mPangoFontDesc);
|
||||||
|
g_object_unref(mPangoCtx);
|
||||||
|
g_object_unref(mPangoLyt);
|
||||||
|
cairo_surface_destroy(mSurface);
|
||||||
|
}
|
||||||
|
} // namespace ptprnt::graphics
|
64
src/graphics/Label.hpp
Normal file
64
src/graphics/Label.hpp
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
ptrnt - print labels on linux
|
||||||
|
Copyright (C) 2023 Moritz Martinius
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <pango/pangocairo.h>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "cairo.h"
|
||||||
|
#include "pango/pango-font.h"
|
||||||
|
#include "pango/pango-types.h"
|
||||||
|
|
||||||
|
namespace ptprnt::graphics {
|
||||||
|
|
||||||
|
constexpr const char* DEFAULT_FONT_FAMILY = "sans";
|
||||||
|
constexpr const uint8_t DEFAULT_FONT_SIZE = 32;
|
||||||
|
|
||||||
|
class Label {
|
||||||
|
public:
|
||||||
|
Label();
|
||||||
|
~Label();
|
||||||
|
|
||||||
|
Label(const Label&) = delete;
|
||||||
|
Label& operator=(const Label&) = delete;
|
||||||
|
Label(Label&&) = delete;
|
||||||
|
Label& operator=(Label&&) = delete;
|
||||||
|
|
||||||
|
std::vector<uint8_t> getRaw();
|
||||||
|
void create(const std::string& labelText, const uint16_t heightPixel);
|
||||||
|
void writeToPng(const std::string& file);
|
||||||
|
int getLayoutWidth();
|
||||||
|
int getLayoutHeight();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// methods
|
||||||
|
uint8_t getNumLines(std::string_view str);
|
||||||
|
// members
|
||||||
|
PangoContext* mPangoCtx{nullptr};
|
||||||
|
PangoLayout* mPangoLyt{nullptr};
|
||||||
|
PangoFontDescription* mPangoFontDesc{nullptr};
|
||||||
|
cairo_surface_t* mSurface{nullptr};
|
||||||
|
int mLayoutWidth = 0, mLayoutHeight = 0;
|
||||||
|
int mPrinterHeight = 0;
|
||||||
|
};
|
||||||
|
} // namespace ptprnt::graphics
|
@@ -21,7 +21,9 @@
|
|||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace ptprnt::graphics {
|
namespace ptprnt::graphics {
|
||||||
@@ -41,9 +43,9 @@ std::vector<uint8_t> Monochrome::get() {
|
|||||||
|
|
||||||
unsigned int outIndex = 0;
|
unsigned int outIndex = 0;
|
||||||
|
|
||||||
for (unsigned int byteNum = 0; byteNum < mPixels.size(); byteNum += 8) {
|
for (unsigned int byteNo = 0; byteNo < mPixels.size(); byteNo += 8) {
|
||||||
for (unsigned int bitNo = 0; bitNo < 8; bitNo++) {
|
for (unsigned int bitNo = 0; bitNo <= 7 && (byteNo + bitNo < mPixels.size()); bitNo++) {
|
||||||
if (mPixels[byteNum + bitNo] > mThreshhold) {
|
if (mPixels[byteNo + bitNo] > mThreshhold) {
|
||||||
outPixels[outIndex] |= (1 << (7 - bitNo));
|
outPixels[outIndex] |= (1 << (7 - bitNo));
|
||||||
} else {
|
} else {
|
||||||
outPixels[outIndex] &= ~(1 << (7 - bitNo));
|
outPixels[outIndex] &= ~(1 << (7 - bitNo));
|
||||||
@@ -56,4 +58,20 @@ std::vector<uint8_t> Monochrome::get() {
|
|||||||
}
|
}
|
||||||
return outPixels;
|
return outPixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Monochrome::visualize() {
|
||||||
|
auto mono = get();
|
||||||
|
for (unsigned char pix : mono) {
|
||||||
|
std::cout << ((pix & (1 << 7)) == 0 ? "." : "x");
|
||||||
|
std::cout << ((pix & (1 << 6)) == 0 ? "." : "x");
|
||||||
|
std::cout << ((pix & (1 << 5)) == 0 ? "." : "x");
|
||||||
|
std::cout << ((pix & (1 << 4)) == 0 ? "." : "x");
|
||||||
|
std::cout << ((pix & (1 << 3)) == 0 ? "." : "x");
|
||||||
|
std::cout << ((pix & (1 << 2)) == 0 ? "." : "x");
|
||||||
|
std::cout << ((pix & (1 << 1)) == 0 ? "." : "x");
|
||||||
|
std::cout << ((pix & (1 << 0)) == 0 ? "." : "x");
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ptprnt::graphics
|
} // namespace ptprnt::graphics
|
@@ -31,11 +31,12 @@ class Monochrome {
|
|||||||
|
|
||||||
void setThreshold(uint8_t);
|
void setThreshold(uint8_t);
|
||||||
void invert(bool shouldInvert);
|
void invert(bool shouldInvert);
|
||||||
|
void visualize();
|
||||||
std::vector<uint8_t> get();
|
std::vector<uint8_t> get();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::vector<uint8_t>& mPixels;
|
const std::vector<uint8_t>& mPixels;
|
||||||
uint8_t mThreshhold = 127;
|
uint8_t mThreshhold = UINT8_MAX / 2;
|
||||||
bool mShouldInvert = false;
|
bool mShouldInvert = false;
|
||||||
};
|
};
|
||||||
} // namespace ptprnt::graphics
|
} // namespace ptprnt::graphics
|
7
src/hello.txt
Normal file
7
src/hello.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"text" : " Hello",
|
||||||
|
"font" : "FreeSans 32",
|
||||||
|
"single-paragraph" : true,
|
||||||
|
"alignment" : "center",
|
||||||
|
"height" : 0
|
||||||
|
}
|
@@ -30,6 +30,7 @@ namespace ptprnt {
|
|||||||
|
|
||||||
class IPrinterDriver {
|
class IPrinterDriver {
|
||||||
public:
|
public:
|
||||||
|
virtual ~IPrinterDriver() = default;
|
||||||
virtual const std::string_view getDriverName() = 0;
|
virtual const std::string_view getDriverName() = 0;
|
||||||
virtual const std::string_view getName() = 0;
|
virtual const std::string_view getName() = 0;
|
||||||
virtual const std::string_view getVersion() = 0;
|
virtual const std::string_view getVersion() = 0;
|
||||||
|
@@ -30,6 +30,7 @@ struct PrinterInfo {
|
|||||||
std::string_view version = "";
|
std::string_view version = "";
|
||||||
uint16_t vid = 0x00;
|
uint16_t vid = 0x00;
|
||||||
uint16_t pid = 0x00;
|
uint16_t pid = 0x00;
|
||||||
|
uint16_t pixelLines = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PrinterStatus {
|
struct PrinterStatus {
|
||||||
|
@@ -27,12 +27,13 @@
|
|||||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||||
|
|
||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
UsbDevice::UsbDevice(libusb_context* ctx, libusb_device* dev) : mLibusbCtx(ctx), mLibusbDev(dev) {
|
UsbDevice::UsbDevice(libusb_context* ctx, usbDevice_ptr dev)
|
||||||
|
: mLibusbCtx(ctx), mLibusbDev(std::move(dev)) {
|
||||||
if (mLibusbCtx == nullptr || mLibusbDev == nullptr) {
|
if (mLibusbCtx == nullptr || mLibusbDev == nullptr) {
|
||||||
throw std::invalid_argument("ctx or device are nullptr");
|
throw std::invalid_argument("ctx or device are nullptr");
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_get_device_descriptor(dev, &mLibusbDevDesc);
|
libusb_get_device_descriptor(mLibusbDev.get(), &mLibusbDevDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
UsbDevice::~UsbDevice() {
|
UsbDevice::~UsbDevice() {
|
||||||
@@ -44,7 +45,7 @@ UsbDevice::~UsbDevice() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool UsbDevice::open() {
|
bool UsbDevice::open() {
|
||||||
int openStatus = libusb_open(mLibusbDev, &mLibusbDevHandle);
|
int openStatus = libusb_open(mLibusbDev.get(), &mLibusbDevHandle);
|
||||||
if (openStatus != 0) {
|
if (openStatus != 0) {
|
||||||
mLastError = static_cast<Error>(openStatus);
|
mLastError = static_cast<Error>(openStatus);
|
||||||
return false;
|
return false;
|
||||||
@@ -113,15 +114,15 @@ const uint16_t UsbDevice::getPid() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const device::Speed UsbDevice::getSpeed() {
|
const device::Speed UsbDevice::getSpeed() {
|
||||||
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev));
|
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t UsbDevice::getBusNumber() {
|
const uint8_t UsbDevice::getBusNumber() {
|
||||||
return libusb_get_bus_number(mLibusbDev);
|
return libusb_get_bus_number(mLibusbDev.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t UsbDevice::getPortNumber() {
|
const uint8_t UsbDevice::getPortNumber() {
|
||||||
return libusb_get_port_number(mLibusbDev);
|
return libusb_get_port_number(mLibusbDev.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
const Error UsbDevice::getLastError() {
|
const Error UsbDevice::getLastError() {
|
||||||
|
@@ -27,14 +27,27 @@
|
|||||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||||
|
|
||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
|
|
||||||
|
struct usbDevice_deleter {
|
||||||
|
void operator()(libusb_device* dev_ptr) const {
|
||||||
|
if (nullptr != dev_ptr) {
|
||||||
|
libusb_unref_device(dev_ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using usbDevice_ptr = std::unique_ptr<libusb_device, usbDevice_deleter>;
|
||||||
|
|
||||||
class UsbDevice : public IUsbDevice {
|
class UsbDevice : public IUsbDevice {
|
||||||
public:
|
public:
|
||||||
UsbDevice(libusb_context* ctx, libusb_device* dev);
|
explicit UsbDevice(libusb_context* ctx, usbDevice_ptr dev);
|
||||||
~UsbDevice();
|
~UsbDevice() override;
|
||||||
|
|
||||||
// delete copy ctor and assignment
|
// delete copy ctor and assignment
|
||||||
UsbDevice(const UsbDevice&) = delete;
|
UsbDevice(const UsbDevice&) = delete;
|
||||||
UsbDevice& operator=(UsbDevice&) = delete;
|
UsbDevice& operator=(const UsbDevice&) = delete;
|
||||||
|
UsbDevice(UsbDevice&&) = delete;
|
||||||
|
UsbDevice& operator=(UsbDevice&&) = delete;
|
||||||
|
|
||||||
bool open() override;
|
bool open() override;
|
||||||
void close() override;
|
void close() override;
|
||||||
@@ -59,9 +72,9 @@ class UsbDevice : public IUsbDevice {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
libusb_context* mLibusbCtx{nullptr};
|
libusb_context* mLibusbCtx{nullptr};
|
||||||
libusb_device* mLibusbDev{nullptr};
|
usbDevice_ptr mLibusbDev{nullptr};
|
||||||
libusb_device_handle* mLibusbDevHandle{nullptr};
|
libusb_device_handle* mLibusbDevHandle{nullptr};
|
||||||
libusb_device_descriptor mLibusbDevDesc;
|
libusb_device_descriptor mLibusbDevDesc{0};
|
||||||
std::atomic<bool> mIsOpen = false;
|
std::atomic<bool> mIsOpen = false;
|
||||||
Error mLastError = Error::SUCCESS;
|
Error mLastError = Error::SUCCESS;
|
||||||
};
|
};
|
||||||
|
@@ -20,9 +20,11 @@
|
|||||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "libusb.h"
|
#include "libusb.h"
|
||||||
#include "libusbwrap/UsbDevice.hpp"
|
#include "libusbwrap/UsbDevice.hpp"
|
||||||
@@ -31,49 +33,56 @@
|
|||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
|
|
||||||
UsbDeviceFactory::~UsbDeviceFactory() {
|
UsbDeviceFactory::~UsbDeviceFactory() {
|
||||||
if (mDeviceListInitialized) {
|
if (mLibusbCtx) {
|
||||||
libusb_free_device_list(mLibusbDeviceList, 1);
|
libusb_exit(mLibusbCtx);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||||
refreshDeviceList();
|
refreshDeviceList();
|
||||||
return buildMaskedDeviceVector(0x0, 0x0, 0x0, 0x0);
|
return buildMaskedDeviceVector(0x0, 0x0, 0x0, 0x0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
||||||
refreshDeviceList();
|
refreshDeviceList();
|
||||||
return buildMaskedDeviceVector(0xffff, 0xffff, vid, pid);
|
return buildMaskedDeviceVector(LIBUSB_BITMASK_ALL, LIBUSB_BITMASK_ALL, vid, pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
int UsbDeviceFactory::refreshDeviceList() {
|
ssize_t UsbDeviceFactory::refreshDeviceList() {
|
||||||
int ret = libusb_get_device_list(mLibusbCtx, &mLibusbDeviceList);
|
libusb_device** list{nullptr};
|
||||||
|
|
||||||
|
ssize_t ret = libusb_get_device_list(mLibusbCtx, &list);
|
||||||
|
mLibusbDeviceList.clear();
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
spdlog::error("Error enumarating USB devices");
|
spdlog::error("Error enumarating USB devices");
|
||||||
} else if (ret == 0) {
|
} else if (ret == 0) {
|
||||||
spdlog::warn("No USB devices found");
|
spdlog::warn("No USB devices found");
|
||||||
}
|
}
|
||||||
mDeviceListInitialized = true;
|
|
||||||
|
for (ssize_t i = 0; i < ret; i++) {
|
||||||
|
mLibusbDeviceList.emplace_back(list[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
libusb_free_device_list(list, false);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
||||||
uint16_t pidMask,
|
uint16_t pidMask,
|
||||||
uint16_t vid,
|
uint16_t vid,
|
||||||
uint16_t pid) {
|
uint16_t pid) {
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> matchedDevices;
|
std::vector<std::unique_ptr<IUsbDevice>> matchedDevices;
|
||||||
// see libusb/examples/listdevs.c
|
// see libusb/examples/listdevs.c
|
||||||
int i = 0;
|
for (auto& currDev : mLibusbDeviceList) {
|
||||||
libusb_device* currDev = nullptr;
|
struct libusb_device_descriptor currDevDesc {};
|
||||||
while ((currDev = mLibusbDeviceList[i++]) != nullptr) {
|
|
||||||
struct libusb_device_descriptor currDevDesc;
|
int ret = libusb_get_device_descriptor(currDev.get(), &currDevDesc);
|
||||||
int ret = libusb_get_device_descriptor(currDev, &currDevDesc);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
||||||
((currDevDesc.idProduct & pidMask) == pid)) {
|
((currDevDesc.idProduct & pidMask) == pid)) {
|
||||||
matchedDevices.push_back(std::make_shared<UsbDevice>(mLibusbCtx, currDev));
|
matchedDevices.push_back(std::make_unique<UsbDevice>(mLibusbCtx, std::move(currDev)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matchedDevices;
|
return matchedDevices;
|
||||||
|
@@ -19,9 +19,16 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "libusb.h"
|
||||||
|
#include "libusbwrap/UsbDevice.hpp"
|
||||||
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
|
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
|
||||||
|
|
||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
|
|
||||||
|
constexpr const uint16_t LIBUSB_BITMASK_ALL = 0xffff;
|
||||||
|
|
||||||
class UsbDeviceFactory : public IUsbDeviceFactory {
|
class UsbDeviceFactory : public IUsbDeviceFactory {
|
||||||
public:
|
public:
|
||||||
UsbDeviceFactory() = default;
|
UsbDeviceFactory() = default;
|
||||||
@@ -39,7 +46,7 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
|
|||||||
*
|
*
|
||||||
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of all detected USB devices
|
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of all detected USB devices
|
||||||
*/
|
*/
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() override;
|
std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() override;
|
||||||
/**
|
/**
|
||||||
* @brief Gets all devices with certain vid/pid combination.
|
* @brief Gets all devices with certain vid/pid combination.
|
||||||
* If only one device of certain type is connected, vector is usually only one element
|
* If only one device of certain type is connected, vector is usually only one element
|
||||||
@@ -48,17 +55,17 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
|
|||||||
* @param pid ProductId of the devices to find
|
* @param pid ProductId of the devices to find
|
||||||
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of detected USB devices based on vid/pid
|
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of detected USB devices based on vid/pid
|
||||||
*/
|
*/
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
|
std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// methods
|
// methods
|
||||||
int refreshDeviceList();
|
ssize_t refreshDeviceList();
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> buildMaskedDeviceVector(uint16_t vidMask,
|
std::vector<std::unique_ptr<IUsbDevice>> buildMaskedDeviceVector(uint16_t vidMask,
|
||||||
uint16_t pidMask, uint16_t vid,
|
uint16_t pidMask, uint16_t vid,
|
||||||
uint16_t pid);
|
uint16_t pid);
|
||||||
// members
|
// members
|
||||||
libusb_context* mLibusbCtx{nullptr};
|
libusb_context* mLibusbCtx{nullptr};
|
||||||
libusb_device** mLibusbDeviceList{};
|
std::vector<usbDevice_ptr> mLibusbDeviceList{};
|
||||||
bool mDeviceListInitialized = false;
|
bool mDeviceListInitialized = false;
|
||||||
};
|
};
|
||||||
} // namespace libusbwrap
|
} // namespace libusbwrap
|
@@ -24,6 +24,7 @@ enum class Speed {
|
|||||||
|
|
||||||
class IUsbDevice {
|
class IUsbDevice {
|
||||||
public:
|
public:
|
||||||
|
virtual ~IUsbDevice() = default;
|
||||||
virtual bool open() = 0;
|
virtual bool open() = 0;
|
||||||
virtual void close() = 0;
|
virtual void close() = 0;
|
||||||
|
|
||||||
|
@@ -9,7 +9,8 @@
|
|||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
class IUsbDeviceFactory {
|
class IUsbDeviceFactory {
|
||||||
public:
|
public:
|
||||||
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0;
|
virtual ~IUsbDeviceFactory() = default;
|
||||||
virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
|
virtual std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() = 0;
|
||||||
|
virtual std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
|
||||||
};
|
};
|
||||||
} // namespace libusbwrap
|
} // namespace libusbwrap
|
@@ -16,10 +16,6 @@
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <spdlog/common.h>
|
|
||||||
#include <spdlog/spdlog.h>
|
|
||||||
|
|
||||||
#include "PtouchPrint.hpp"
|
#include "PtouchPrint.hpp"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
24
src/meson.build
Normal file
24
src/meson.build
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
ptprnt_hpps = files (
|
||||||
|
'libusbwrap/interface/IUsbDeviceFactory.hpp',
|
||||||
|
'libusbwrap/interface/IUsbDevice.hpp',
|
||||||
|
'libusbwrap/UsbDeviceFactory.hpp',
|
||||||
|
'libusbwrap/LibUsbTypes.hpp',
|
||||||
|
'libusbwrap/UsbDevice.hpp',
|
||||||
|
'interface/IPrinterDriver.hpp',
|
||||||
|
'interface/IPrinterTypes.hpp',
|
||||||
|
'P700Printer.hpp',
|
||||||
|
'PtouchPrint.hpp',
|
||||||
|
'graphics/Bitmap.hpp',
|
||||||
|
'graphics/Label.hpp',
|
||||||
|
'graphics/Monochrome.hpp'
|
||||||
|
)
|
||||||
|
|
||||||
|
ptprnt_srcs = files (
|
||||||
|
'PtouchPrint.cpp',
|
||||||
|
'P700Printer.cpp',
|
||||||
|
'graphics/Label.cpp',
|
||||||
|
'graphics/Bitmap.cpp',
|
||||||
|
'graphics/Monochrome.cpp',
|
||||||
|
'libusbwrap/UsbDeviceFactory.cpp',
|
||||||
|
'libusbwrap/UsbDevice.cpp',
|
||||||
|
)
|
0
src/ptprnt.log
Normal file
0
src/ptprnt.log
Normal file
@@ -21,7 +21,9 @@
|
|||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
TEST(basic_test, Bitmap_createBitmapWithCertainSize_yieldsSpecifiedSize) {
|
TEST(basic_test, Bitmap_createBitmapWithCertainSize_yieldsSpecifiedSize) {
|
||||||
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
|
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
|
||||||
@@ -64,3 +66,36 @@ TEST(basic_test, Bitmap_getBitmapColInsideOfImage_yieldsValidColSize) {
|
|||||||
auto colSize = col->size();
|
auto colSize = col->size();
|
||||||
ASSERT_EQ(8, colSize);
|
ASSERT_EQ(8, colSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(basic_test, Bitmap_setPixels_setsGivenPixels) {
|
||||||
|
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
|
||||||
|
std::vector<uint8_t> pix(16 * 8);
|
||||||
|
|
||||||
|
bm.setPixels(pix);
|
||||||
|
auto pixCpy = bm.getPixelsCpy();
|
||||||
|
|
||||||
|
ASSERT_EQ(pix, pixCpy);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(basic_test, Bitmap_setPixels_isSuccessfulWithValidPixels) {
|
||||||
|
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
|
||||||
|
std::vector<uint8_t> pix(16 * 8);
|
||||||
|
|
||||||
|
ASSERT_TRUE(bm.setPixels(pix));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(basic_test, Bitmap_setPixels_yieldsSamePixelsBack) {
|
||||||
|
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
|
||||||
|
std::vector<uint8_t> pix(16 * 8);
|
||||||
|
|
||||||
|
bm.setPixels(pix);
|
||||||
|
auto pixCpy = bm.getPixelsCpy();
|
||||||
|
ASSERT_EQ(pix, pixCpy);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(basic_test, Bitmap_setPixelsWithWrongSize_isFailing) {
|
||||||
|
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(5, 8);
|
||||||
|
std::vector<uint8_t> pix(16 * 8);
|
||||||
|
|
||||||
|
ASSERT_FALSE(bm.setPixels(pix));
|
||||||
|
}
|
||||||
|
@@ -17,12 +17,12 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "graphics/Image.hpp"
|
#include "graphics/Label.hpp"
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
TEST(basic_test, Image_smokeTest_succeeds) {
|
TEST(basic_test, Label_smokeTest_succeeds) {
|
||||||
auto im = ptprnt::graphics::Image();
|
auto im = ptprnt::graphics::Label();
|
||||||
}
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
tests = [['bitmap_test', 'bitmap_test_exe', ['bitmap_test/bitmap_test.cpp']],
|
tests = [['bitmap_test', 'bitmap_test_exe', ptprnt_srcs + ['bitmap_test/bitmap_test.cpp']],
|
||||||
['image_test', 'image_test_exe', ['image_test/image_test.cpp']],
|
['label_test', 'label_test_exe', ptprnt_srcs +['label_test/label_test.cpp']],
|
||||||
['monochrome_test', 'monochrome_test_exe', ['monochrome_test/monochrome_test.cpp']]
|
['monochrome_test', 'monochrome_test_exe', ptprnt_srcs +['monochrome_test/monochrome_test.cpp']]
|
||||||
]
|
]
|
||||||
|
|
||||||
foreach test : tests
|
foreach test : tests
|
||||||
@@ -8,8 +8,7 @@ foreach test : tests
|
|||||||
executable(test.get(1),
|
executable(test.get(1),
|
||||||
sources: test.get(2),
|
sources: test.get(2),
|
||||||
include_directories: incdir,
|
include_directories: incdir,
|
||||||
link_with:[ptprnt_lib],
|
dependencies: [gtest_dep, usb_dep, log_dep, pangocairo_dep, cli11_dep]
|
||||||
dependencies: [gtest_dep, pangocairo_dep]
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
endforeach
|
endforeach
|
Reference in New Issue
Block a user