From e28d91c330cd3cabd66d0fdf71d4a9bdafa075a7 Mon Sep 17 00:00:00 2001 From: foosinn Date: Fri, 29 Mar 2024 23:45:03 +0100 Subject: [PATCH] init --- README.md | 8 ++++++ send.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 README.md create mode 100644 send.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..22e93bc --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# phomemo-bluesock + +A bluetooth client for Phomemo M110. + + +Heavily based on [SFZ-aalen][1] + +[1]: https://gitlab.com/sfz.aalen/infra/labelprinter/-/tree/main?ref_type=heads diff --git a/send.py b/send.py new file mode 100644 index 0000000..85eff2c --- /dev/null +++ b/send.py @@ -0,0 +1,75 @@ +import socket +import sys +from pprint import pprint +import os.path + +from PIL import Image, ImageDraw, ImageFont + + +try: + _, device, img_file = sys.argv +except: + print("usage: send.py [bl:ue:to:ot:h0] [filename.png]") + exit(1) + +# -- handle image + +if os.path.isfile(img_file): + img = Image.open(img_file) +else: + img = Image.new("1", (384, 240), color=1) + draw = ImageDraw.Draw(img) + for size in range(100, 1, -1): + font = ImageFont.truetype( + font="/usr/share/fonts/OTF/CascadiaCode-Regular.otf", size=size + ) + if draw.textlength(img_file, font=font) <= 323: + break + draw.text((10, 10), img_file, font=font) + +if img.height > img.width: + img = img.transpose(Image.ROTATE_90) + +if 323 / img.width * img.height <= 240: + img = img.resize(size=(323, int(323 / img.width * img.height))) +else: + img = img.resize(size=(int(240 / img.height * img.width), 240)) + +imgborder = Image.new("1", (384, 240), color=1) +imgborder.paste(img, (61, 0)) +img = imgborder + +# -- print + +buf = [] +buf += [0x1B, 0x40] +buf += [0x1D, 0x76, 0x30, 0x0] + +width = img.width +height = img.height +width8 = width // 8 + +buf += [width8 & 0xFF, width8 >> 8] +buf += [height & 0xFF, height >> 8] + +ibuf = [0] * height * width8 +for y in range(height): + for x in range(width): + if img.getpixel((x, y)) == 0: + ibuf[x // 8 + y * width8] |= 1 << (7 - (x & 7)) +ibuf = [b if b != 0x0A else 0x14 for b in ibuf] + +buf += ibuf + +# -- send + +with open("out.bin", "wb") as f: + f.write(bytes(buf)) +exit(1) + +sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) +sock.bind((socket.BDADDR_ANY, 1)) +sock.connect((device, 1)) +sock.sendall(bytes(buf)) +sock.recv(1024) +sock.close()