forked from vycdev/thermal-printer-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryclock_print.py
More file actions
82 lines (67 loc) · 2.73 KB
/
Copy pathbinaryclock_print.py
File metadata and controls
82 lines (67 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""Print a binary clock on the S01 thermal printer.
Shows the current time as binary-coded decimal: six columns (HH MM SS), each a
stack of bits worth 8/4/2/1 from top to bottom. A filled dot is 1, hollow is 0.
Read each column, add the lit values, and you get that digit.
Examples:
python binaryclock_print.py
python binaryclock_print.py --no-print
"""
from __future__ import annotations
import argparse
from datetime import datetime
from pathlib import Path
from PIL import Image, ImageDraw
from print_common import ROOT, Card, font
BITS = [8, 4, 2, 1]
def draw_clock(h: int, m: int, s: int, w: int) -> Image.Image:
digits = [h // 10, h % 10, m // 10, m % 10, s // 10, s % 10]
r = 11
gap = 8
group_gap = 16
left = 26
# x position of each of the 6 columns (with extra space between HH MM SS)
xs = []
x = left
for i in range(6):
xs.append(x)
x += 2 * r + gap + (group_gap if i % 2 == 1 else 0)
top = 22
img_w = xs[-1] + 2 * r + 10
img_h = top + 4 * (2 * r + gap) + 18
img = Image.new("L", (img_w, img_h), 255)
d = ImageDraw.Draw(img)
lab = font(True, 13)
for row, bit in enumerate(BITS):
cy = top + row * (2 * r + gap) + r
d.text((left - 8, cy), str(bit), fill=0, font=lab, anchor="rm") # 8/4/2/1 labels
for col, dig in enumerate(digits):
cx = xs[col] + r
box = (cx - r, cy - r, cx + r, cy + r)
if dig & bit:
d.ellipse(box, fill=0)
else:
d.ellipse(box, outline=0, width=2)
# decimal digit under each column
by = top + 4 * (2 * r + gap) + 2
for col, dig in enumerate(digits):
d.text((xs[col] + r, by), str(dig), fill=0, font=lab, anchor="ma")
return img
def main() -> int:
parser = argparse.ArgumentParser(description="Print a binary clock on the S01 thermal printer.")
parser.add_argument("--out", type=Path, default=ROOT / "binaryclock.png")
parser.add_argument("--darkness", type=int, choices=range(1, 6), default=3)
parser.add_argument("--bottom-feed", type=int, default=24)
parser.add_argument("--no-print", action="store_true")
args = parser.parse_args()
now = datetime.now()
print(f"Binary clock for {now:%H:%M:%S}")
card = Card()
card.title("BINARY CLOCK")
card.line("HH MM SS - filled = 1", size=11, bold=False, center=True)
card.gap(4).image(draw_clock(now.hour, now.minute, now.second, card.inner_w))
card.gap(2).heading(now.strftime("%H:%M:%S"), size=22)
card.footer("binary-coded decimal", now.strftime("%Y-%m-%d"))
return card.finish(args.out, args.bottom_feed, args.darkness, do_print=not args.no_print)
if __name__ == "__main__":
raise SystemExit(main())