From 5488773d394a5e63b2b3f028244132e1ce7717ba Mon Sep 17 00:00:00 2001 From: Joe Lothan Date: Mon, 15 Jun 2026 17:08:22 -0400 Subject: [PATCH] initial attempt at esc/pos generation --- print_tape.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 print_tape.py diff --git a/print_tape.py b/print_tape.py new file mode 100644 index 0000000..a768139 --- /dev/null +++ b/print_tape.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""Generate ESC/POS ticker tape output from trade CSV data. + +Reads a CSV of trades (timestamp, symbol, lots, price) and generates +ESC/POS commands with 90° rotated text to emulate an old stock ticker tape. + +When the receipt paper is turned sideways: + - Symbol appears at the top of the tape + - Price (and lot count) appears at the bottom +""" + +import csv +import sys + +from escpos.printer import Dummy + +# TM-T88V: 42 columns at Font A on 80mm paper +LINE_WIDTH = 42 + + +def format_trade(symbol, lots, price): + """Format a trade line for the ticker tape. + + Symbol is placed at the start of the line, price info at the end. + With 90° rotation, start-of-line = top of tape, end-of-line = bottom. + """ + if lots > 1: + price_str = f"{lots}s{price:.2f}" + else: + price_str = f"{price:.2f}" + + padding = LINE_WIDTH - len(symbol) - len(price_str) + if padding < 1: + padding = 1 + return symbol + " " * padding + price_str + + +def main(): + csv_file = sys.argv[1] if len(sys.argv) > 1 else "trades_sample_sorted.csv" + output_file = sys.argv[2] if len(sys.argv) > 2 else "ticker_tape.bin" + + p = Dummy(profile="TM-T88V") + p.hw("INIT") + # ESC V 1: 90° clockwise character rotation (not exposed by python-escpos) + p._raw(b"\x1b\x56\x01") + + with open(csv_file) as f: + reader = csv.reader(f) + for row in reader: + _timestamp, symbol, lots_str, price_str = row + lots = int(lots_str) + price = float(price_str) + line = format_trade(symbol, lots, price) + p.text(line + "\n") + + p.cut() + + with open(output_file, "wb") as f: + f.write(p.output) + print(f"Wrote {len(p.output)} bytes to {output_file}", file=sys.stderr) + + +if __name__ == "__main__": + main()