Skip to content

Commit

Permalink
Test unknown device on cli side, fixed usability of web side;
Browse files Browse the repository at this point in the history
prompt to select a device if there are many (web & cli);
Better routine, connect to device only if everything is ok;
Adjusted default printing configurations & behavior;
Added model MX08(C15);
Update about page;
Other fixes and tweaks.
  • Loading branch information
NaitLee committed Apr 16, 2023
1 parent 23d9d6d commit 5519bbe
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ build-common/bleak_winrt
pf2
pf2.zip
*.pf2
# archlinux package build files
cat-printer-git
pkg
*.pkg.tar.zst
# dev config
config.json
# dev backup
Expand Down
5 changes: 3 additions & 2 deletions PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Maintainer :

pkgname=cat-printer-git
pkgver=r30.eafaa6e
pkgver=r153.85cb5a8
pkgrel=1
pkgdesc="A project that provides support to some Bluetooth Cat Printer models, on many platforms!"
arch=('any')
url="https://github.com/NaitLee/Cat-Printer"
license=('GPL3')
depends=('python' 'bluez' 'bluez-utils' 'python-bleak' 'ghostscript' 'imagemagick')
depends=('python' 'bluez' 'python-bleak')
optdepends=('bluez-utils' 'ghostscript' 'imagemagick')
makedepends=('git' 'unzip')
provides=("cat-printer=${pkgver}")
source=("$pkgname::git+https://github.com/NaitLee/Cat-Printer.git")
Expand Down
73 changes: 50 additions & 23 deletions printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ExitCodes():
PrinterError = 64
IncompleteProgram = 128
MissingDependency = 129
UserInterrupt = 254

def info(*args, **kwargs):
'Just `print` to `stdout`'
Expand Down Expand Up @@ -345,7 +346,7 @@ def connect(self, name=None, address=None):
self.device = None
if name is None and address is None:
return
self.model = Models[name]
self.model = Models.get(name, Models['_ZZ00'])
self.device = BleakClient(address)
def notify(_char, data):
if data == self.data_flow_pause:
Expand Down Expand Up @@ -595,10 +596,10 @@ def fallback_program(*programs):

def magick_text(stdin, image_width, font_size, font_family):
'Pipe an io to ImageMagick for processing text to image, return output io'
read_fd, write_fd = os.pipe()
if _MagickExe is None:
fatal(i18n("imagemagick-not-found"), code=129)

fatal(i18n("imagemagick-not-found"), code=ExitCodes.MissingDependency)

read_fd, write_fd = os.pipe()
subprocess.Popen([_MagickExe, '-background', 'white', '-fill', 'black',
'-size', f'{image_width}x', '-font', font_family, '-pointsize',
str(font_size), 'caption:@-', 'pbm:-'],
Expand All @@ -607,10 +608,10 @@ def magick_text(stdin, image_width, font_size, font_family):

def magick_image(stdin, image_width, dither):
'Pipe an io to ImageMagick for processing "usual" image to pbm, return output io'
read_fd, write_fd = os.pipe()
if _MagickExe is None:
fatal(i18n("imagemagick-not-found"), code=129)
fatal(i18n("imagemagick-not-found"), code=ExitCodes.MissingDependency)

read_fd, write_fd = os.pipe()
subprocess.Popen([_MagickExe, '-', '-fill', 'white', '-opaque', 'transparent',
'-resize', f'{image_width}x', '-dither', dither, '-monochrome', 'pbm:-'],
stdin=stdin, stdout=io.FileIO(write_fd, 'w'))
Expand Down Expand Up @@ -677,6 +678,10 @@ def _main():
help=i18n('print-quality'))
parser.add_argument('-d', '--dry', action='store_true',
help=i18n('dry-run-test-print-process-only'))
parser.add_argument('-u', '--unknown', action='store_true',
help=i18n('try-to-print-through-an-unknown-device'))
parser.add_argument('-0', '--0th', action='store_true',
help=i18n('no-prompt-for-multiple-devices'))
parser.add_argument('-f', '--fake', metavar='XY01', type=str, default='',
help=i18n('virtual-run-on-specified-model'))
parser.add_argument('-m', '--dump', action='store_true',
Expand Down Expand Up @@ -729,18 +734,6 @@ def _main():

mode = 'pbm'

# Connect to printer
if args.dry:
info(i18n('dry-run-test-print-process-only'))
printer.dry_run = True
if args.fake:
printer.fake = True
printer.model = Models[args.fake]
else:
info(i18n('connecting'))
printer.scan(identifier, use_result=True)
printer.dump = args.dump

# Prepare image / text
if args.text:
info(i18n('text-printing-mode'))
Expand All @@ -758,15 +751,46 @@ def _main():
else 'FloydSteinberg')
)

# Connect to printer
if args.dry:
info(i18n('dry-run-test-print-process-only'))
printer.dry_run = True
if args.fake:
printer.fake = True
printer.model = Models[args.fake]
else:
info(i18n('scanning-for-devices'))
devices = printer.scan(identifier, everything=args.unknown)

printer.dump = args.dump

if args.nothing:
global Printer
Printer = printer
return
if len(devices) == 0:
error(i18n('no-available-devices-found'), error=PrinterError)
if len(devices) == 1 or getattr(args, '0th'):
info(i18n('connecting'))
printer.connect(devices[0].name, devices[0].address)
else:
info(i18n('there-are-multiple-devices-'))
for i in range(len(devices)):
d = devices[i]
n = str(d.name) + "-" + d.address[3:5] + d.address[0:2]
info('%4i\t%s' % (i, n))
choice = 0
try:
choice = int(input(i18n('choose-which-one-0-', choice)))
except KeyboardInterrupt:
raise
except:
pass
info(i18n('connecting'))
printer.connect(devices[choice].name, devices[choice].address)
try:
printer.print(file, mode=mode)
info(i18n('finished'))
except KeyboardInterrupt:
info(i18n('stopping'))
finally:
file.close()
printer.unload()
Expand All @@ -778,8 +802,9 @@ def main():
except BleakError as e:
error_message = str(e)
if (
('not turned on' in error_message) or # windows or android
(isinstance(e, BleakDBusError) and # linux/dbus/bluetoothctl
'not turned on' in error_message or
'No powered Bluetooth adapter' in error_message or
(isinstance(e, BleakDBusError) and
getattr(e, 'dbus_error') == 'org.bluez.Error.NotReady')
):
fatal(i18n('please-enable-bluetooth'), code=ExitCodes.GeneralError)
Expand All @@ -789,9 +814,11 @@ def main():
fatal(e.message_localized, code=ExitCodes.PrinterError)
except RuntimeError as e:
if 'no running event loop' in str(e):
pass # non-sense
pass # ignore this
else:
raise
except KeyboardInterrupt:
fatal(i18n('stopping'), code=ExitCodes.UserInterrupt)

if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions printer_lib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class Model():
Models = {}

# all known supported models
for name in 'GB01 GB02 GB03 GT01 MX05 MX06 YT01'.split(' '):
for name in '_ZZ00 GB01 GB02 GB03 GT01 MX05 MX06 MX08 YT01'.split(' '):
Models[name] = Model()

# that can receive compressed data
for name in 'GB03'.split(' '):
Models[name].is_new_kind = True

# that have problem giving feed command
for name in 'MX05 MX06'.split(' '):
for name in 'MX05 MX06 MX08'.split(' '):
Models[name].problem_feeding = True
4 changes: 2 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ class PrinterServerHandler(BaseHTTPRequestHandler):

settings = DictAsObject({
'config_path': 'config.json',
'version': 3,
'version': 4,
'first_run': True,
'is_android': False,
'scan_time': 4.0,
'dry_run': False,
'energy': 64,
'quality': 32
'quality': 36
})
_settings_blacklist = (
'printer', 'is_android'
Expand Down
8 changes: 7 additions & 1 deletion www/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ <h2 data-i18n="contributors">Contributors</h2>
<dd data-i18n="collaborator">Collaborator</dd>
<dd data-i18n="translator">Translator</dd>
</dl>
<dl>
<dt>
<a target="_blank" href="https://github.com/nodlek-ctrl">nodlek-ctrl</a>
</dt>
<dd data-i18n="minor-tweaks">Minor Tweaks</dd>
</dl>
<dl>
<dt data-i18n="all-users-and-developers">All testers & users</dt>
<dd data-i18n="everyone-is-awesome">Everyone is awesome!</dd>
</dl>
</div>
<h2 data-i18n="copyright-and-license">Copyright</h2>
<p>
<span>Copyright &copy; 2021-2022 NaitLee Soft.</span>
<span>Copyright &copy; 2021-2023 NaitLee Soft.</span>
<span data-i18n="some-rights-reserved">Some rights reserved.</span>
</p>
<p class="force-ltr">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.</p>
Expand Down
6 changes: 3 additions & 3 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ <h1 id="title" data-i18n="cat-printer">Cat Printer</h1>
<!-- "brightness" is historically "threshold" -->
<label class="label-span-input">
<span data-i18n="brightness-">Brightness:</span>
<input type="range" min="0" max="256" value="86" step="16" name="threshold" data-key data-default />
<input type="range" min="0" max="256" value="86" step="1" name="threshold" data-key data-default />
</label>
<!-- Thermal "Strength", so-called "darkness", or internally "energy" -->
<label class="label-span-input" data-hide-as="print">
<span data-i18n="strength-">Strength:</span>
<input type="range" min="0" max="256" value="64" step="16" name="energy" data-key data-default />
<input type="range" min="0" max="256" value="64" step="32" name="energy" data-key data-default />
</label>
<!-- "Quality", just speed of paper feeding, but slower makes better heating -->
<label class="label-span-input" data-hide-as="print">
<span data-i18n="quality-">Quality:</span>
<input type="range" min="24" max="36" value="32" step="4" name="quality" data-key data-default />
<input type="range" min="28" max="40" value="36" step="4" name="quality" data-key data-default />
</label>
</div>
<label class="label-input-span">
Expand Down
8 changes: 7 additions & 1 deletion www/lang/de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,11 @@
"test-unknown-device": "Unbekanntes Gerät testen",
"now-will-scan-for-all-bluetooth-devices-nearby": "Der Scan sucht jetzt nach allen Bluetooth-Geräten in der Nähe",

"pf2-font-not-found-or-broken-0": "PF2 font not found or broken: '{0}'"
"pf2-font-not-found-or-broken-0": "PF2 font not found or broken: '{0}'",
"try-to-print-through-an-unknown-device": "Try to print through an unknown device",
"scanning-for-all-bluetooth-devices-nearby": "Scanning for all bluetooth devices nearby…",
"there-are-multiple-devices-": "There are multiple devices:",
"choose-which-one-0-": "Choose which one? [{0}]: ",
"multiple-devices-found-please-specify-one": "Multiple devices found, please specify one",
"no-prompt-for-multiple-devices": "No prompt for multiple devices"
}
10 changes: 7 additions & 3 deletions www/lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@
"rotate-image": "Rotate Image",
"test-unknown-device": "Test Unknown Device",
"scan": "Scan",
"now-will-scan-for-all-bluetooth-devices-nearby": "Scan set to search for all bluetooth devides nearby.",
"pf2-font-not-found-or-broken-0": "PF2 font not found or broken: '{0}'",
"imagemagick-not-found": "ImageMagick not found, please install it and retry."

"imagemagick-not-found": "ImageMagick not found, please install it and retry.",
"try-to-print-through-an-unknown-device": "Try to print through an unknown device",
"scanning-for-all-bluetooth-devices-nearby": "Scanning for all bluetooth devices nearby…",
"there-are-multiple-devices-": "There are multiple devices:",
"choose-which-one-0-": "Choose which one? [{0}]: ",
"multiple-devices-found-please-specify-one": "Multiple devices found, please specify one",
"no-prompt-for-multiple-devices": "No prompt for multiple devices"
}
29 changes: 17 additions & 12 deletions www/lang/lolcat.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
"misc": "OTHR",
"system": "SYS",
"disable-animation": "NO MOTION",
"exit": "BAK TO HOM",
"exit": "BAK 2 HOM",
"error-message": "SOMTHIN WRONG",
"preview": "LOOK",
"print": "PAW STEP",
"expand": "MORE",
"crop": "LESS",
"scanning-for-devices": "LUKIN FOR KITTEZ",
"scan-time-": "HOW LONG TO FIND",
"scanning-for-devices": "LUKIN 4 KITTEZ",
"scan-time-": "HOW LONG 2 FIND",
"-seconds": "SECS",
"no-available-devices-found": "NO KITTE FOUND",
"no-available-devices-found": "NO KITTE FINDZ",
"found-0-available-devices": {
"single": "THER IS {0} KITTE",
"multiple": "THER R {0} KITTEZ"
Expand All @@ -42,10 +42,10 @@
"please-enable-bluetooth": "OPEN YA BLUETOOTH PLZ",
"error-happened-please-check-error-message": "SOMTHIN WRONG CHK ERR LOG PLZ",
"you-can-seek-for-help-with-detailed-info-below": "ASK OTHR KITTE WITH THEZ",
"or-try-to-scan-longer": "TRY TO FIND BIT LONGER",
"print-to-cat-printer": "PAWS TO STEP ON PAPR OF KITTE PRINTER!",
"or-try-to-scan-longer": "TRY 2 FIND BIT LONGER",
"print-to-cat-printer": "PAWS 4 KITTEZ 2 STEP!!",
"supported-models-": "KNOWN KITTEZ>",
"path-to-input-file-dash-for-stdin": "WHAT FILE TO STEP '-' MEAN STDIN",
"path-to-input-file-dash-for-stdin": "WHAT FILE 2 STEP '-' MEAN STDIN",
"please-install-pyobjc-via-pip": "INSTAL `pyobjc` VIA pip",
"please-install-bleak-via-pip": "INSTAL `bleak` VIA pip",
"folder-printer_lib-is-incomplete-or-missing-please-check": "DIR `printer_lib` HAV PROBLM CHK PLZ",
Expand All @@ -69,7 +69,7 @@
"dump-traffic": "WACH KITTE PAWS",
"right-to-left-text-order": "YA READ RTL",
"auto-wrap-line": "WRAP MEOW",
"process-as-": "HOW TO STEP>",
"process-as-": "HOW 2 STEP>",
"text": "MEOW",
"picture": "PICS",
"pattern": "SPOTS",
Expand Down Expand Up @@ -104,14 +104,14 @@
"COMMA": "COMA",
"DOT": "DOT",
"to-enter-keyboard-mode-press-tab": "KEYBORD G33KS PRES TAB",
"usage-": "HOW TO USE>",
"usage-": "HOW 2 USE>",
"positional-arguments-": "ARGS>",
"options-": "OPTS>",
"show-this-help-message": "SHOW WHAT YA LOOKIN NOW",
"do-nothing": "SLEEPY KITTE",
"scan-for-a-printer": "FIND A KITTE",
"text-printing-mode-with-options": "NO STEP PIC WANT MEOW",
"image-printing-options": "HOW TO STEP A PIC",
"image-printing-options": "HOW 2 STEP A PIC",
"convert-input-image-with-imagemagick": "HLP WITH ImageMagick",
"reset-configuration-": "RLY SCREW CFG?",
"brightness-": "BLAK OR WHITE>",
Expand All @@ -138,6 +138,11 @@
"now-will-scan-for-all-bluetooth-devices-nearby": "WIL FIND ALL THINY KITTE OR NOT",
"scan": "FIND",
"pf2-font-not-found-or-broken-0": "KITTE FONT LOST OR DEAD> {0}",
"image-magick-not-found": "IMAGEMAGICK NOT FINDZ, PLZ INSTALL IT AND HAVES ANODA GO."

"image-magick-not-found": "IMAGEMAGICK NOT FINDZ, PLZ INSTALL IT AND HAVES ANODA GO.",
"try-to-print-through-an-unknown-device": "STEP WIZ STRENGE KITTE",
"scanning-for-all-bluetooth-devices-nearby": "LOOKIN 4 KITTEZ…",
"there-are-multiple-devices-": "MANY KITTEZ!!",
"choose-which-one-0-": "ADOPT A KITTE PLZ [{0}]: ",
"multiple-devices-found-please-specify-one": "MANY KITTEZ!! ADOPT ONE PLZ",
"no-prompt-for-multiple-devices": "ADOPT KITTE ONCE FINDZ ONE"
}
8 changes: 7 additions & 1 deletion www/lang/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,11 @@
"free-software": "自由软件",
"free-software-description": "尊重您计算自由的软件。",
"pf2-font-not-found-or-broken-0": "PF2 字体丢失或损坏:'{0}'",
"image-magick-not-found": "未找到 ImageMagick,请安装并重试。"
"imagemagick-not-found": "未找到 ImageMagick,请安装并重试。",
"try-to-print-through-an-unknown-device": "试着用未知的设备打印",
"scanning-for-all-bluetooth-devices-nearby": "正在搜索附近所有蓝牙设备……",
"there-are-multiple-devices-": "有多个设备:",
"choose-which-one-0-": "选择哪一个?[{0}]:",
"multiple-devices-found-please-specify-one": "找到多个设备,请指定",
"no-prompt-for-multiple-devices": "发现多个设备时不提示选择"
}
8 changes: 7 additions & 1 deletion www/lang/zh-HK.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,11 @@
"free-software": "自由軟件",
"free-software-description": "尊重您計算自由的軟件。",
"pf2-font-not-found-or-broken-0": "PF2 字體丟失或損壞:'{0}'",
"imagemagick-not-found": "未找到 ImageMagick,請安裝並重試。"
"imagemagick-not-found": "未找到 ImageMagick,請安裝並重試。",
"try-to-print-through-an-unknown-device": "試着用未知的設備打印",
"scanning-for-all-bluetooth-devices-nearby": "正在搜索附近所有藍牙設備……",
"there-are-multiple-devices-": "有多個設備:",
"choose-which-one-0-": "選擇哪一個?[{0}]:",
"multiple-devices-found-please-specify-one": "找到多個設備,請指定",
"no-prompt-for-multiple-devices": "發現多個設備時不提示選擇"
}
8 changes: 7 additions & 1 deletion www/lang/zh-Hant-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,11 @@
"free-software": "自由軟件",
"free-software-description": "尊重您計算自由的軟件。",
"pf2-font-not-found-or-broken-0": "PF2 字體丟失或損壞:'{0}'",
"imagemagick-not-found": "未找到 ImageMagick,請安裝並重試。"
"imagemagick-not-found": "未找到 ImageMagick,請安裝並重試。",
"try-to-print-through-an-unknown-device": "試着用未知的設備打印",
"scanning-for-all-bluetooth-devices-nearby": "正在搜索附近所有藍牙設備……",
"there-are-multiple-devices-": "有多個設備:",
"choose-which-one-0-": "選擇哪一個?[{0}]:",
"multiple-devices-found-please-specify-one": "找到多個設備,請指定",
"no-prompt-for-multiple-devices": "發現多個設備時不提示選擇"
}
Loading

0 comments on commit 5519bbe

Please sign in to comment.