1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-08-24 09:03:34 +00:00

reformat codebase

This commit is contained in:
Patrick Kanzler
2021-10-30 18:15:22 +02:00
parent 109a5d8a92
commit 435f2bba24
41 changed files with 1706 additions and 1398 deletions

View File

@@ -5,7 +5,7 @@ from escpos.printer import Usb
p = Usb(0x0416, 0x5011, profile="POS-5890")
# Print software and then hardware barcode with the same content
p.soft_barcode('code39', '123456')
p.text('\n')
p.text('\n')
p.barcode('123456', 'CODE39')
p.soft_barcode("code39", "123456")
p.text("\n")
p.text("\n")
p.barcode("123456", "CODE39")

View File

@@ -6,15 +6,23 @@ import six
import sys
from escpos import printer
from escpos.constants import CODEPAGE_CHANGE, ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT
from escpos.constants import (
CODEPAGE_CHANGE,
ESC,
CTL_LF,
CTL_FF,
CTL_CR,
CTL_HT,
CTL_VT,
)
def main():
dummy = printer.Dummy()
dummy.hw('init')
dummy.hw("init")
for codepage in sys.argv[1:] or ['USA']:
for codepage in sys.argv[1:] or ["USA"]:
dummy.set(height=2, width=2)
dummy._raw(codepage + "\n\n\n")
print_codepage(dummy, codepage)
@@ -36,14 +44,14 @@ def print_codepage(printer, codepage):
sep = ""
# Table header
printer.set(font='b')
printer.set(font="b")
printer._raw(" {}\n".format(sep.join(map(lambda s: hex(s)[2:], range(0, 16)))))
printer.set()
# The table
for x in range(0, 16):
# First column
printer.set(font='b')
printer.set(font="b")
printer._raw("{} ".format(hex(x)[2:]))
printer.set()
@@ -51,12 +59,12 @@ def print_codepage(printer, codepage):
byte = six.int2byte(x * 16 + y)
if byte in (ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT):
byte = ' '
byte = " "
printer._raw(byte)
printer._raw(sep)
printer._raw('\n')
printer._raw("\n")
if __name__ == '__main__':
if __name__ == "__main__":
main()

View File

@@ -7,7 +7,7 @@ def usage():
print("usage: qr_code.py <content>")
if __name__ == '__main__':
if __name__ == "__main__":
if len(sys.argv) != 2:
usage()
sys.exit(1)

View File

@@ -5,5 +5,5 @@ from escpos.printer import Usb
p = Usb(0x0416, 0x5011, profile="POS-5890")
# Some software barcodes
p.soft_barcode('code128', 'Hello')
p.soft_barcode('code39', '1234')
p.soft_barcode("code128", "Hello")
p.soft_barcode("code39", "1234")

View File

@@ -33,93 +33,95 @@ printer = Usb(0x0416, 0x5011, profile="POS-5890")
# Technically you can use any other weather service, of course :)
API_KEY = "YOUR API KEY"
LAT = "22.345490" # Your Location
LONG = "114.189945" # Your Location
LAT = "22.345490" # Your Location
LONG = "114.189945" # Your Location
def forecast_icon(idx):
icon = data['daily']['data'][idx]['icon']
icon = data["daily"]["data"][idx]["icon"]
image = GRAPHICS_PATH + icon + ".png"
return image
# Dumps one forecast line to the printer
def forecast(idx):
date = datetime.fromtimestamp(int(data['daily']['data'][idx]['time']))
date = datetime.fromtimestamp(int(data["daily"]["data"][idx]["time"]))
day = calendar.day_name[date.weekday()]
lo = data['daily']['data'][idx]['temperatureMin']
hi = data['daily']['data'][idx]['temperatureMax']
cond = data['daily']['data'][idx]['summary']
lo = data["daily"]["data"][idx]["temperatureMin"]
hi = data["daily"]["data"][idx]["temperatureMax"]
cond = data["daily"]["data"][idx]["summary"]
print(date)
print(day)
print(lo)
print(hi)
print(cond)
time.sleep(1)
printer.set(
font='a',
height=2,
align='left',
bold=False,
double_height=False)
printer.text(day + ' \n ')
time.sleep(5) # Sleep to prevent printer buffer overflow
printer.text('\n')
printer.set(font="a", height=2, align="left", bold=False, double_height=False)
printer.text(day + " \n ")
time.sleep(5) # Sleep to prevent printer buffer overflow
printer.text("\n")
printer.image(forecast_icon(idx))
printer.text('low ' + str(lo))
printer.text("low " + str(lo))
printer.text(deg)
printer.text('\n')
printer.text(' high ' + str(hi))
printer.text("\n")
printer.text(" high " + str(hi))
printer.text(deg)
printer.text('\n')
printer.text("\n")
# take care of pesky unicode dash
printer.text(cond.replace(u'\u2013', '-').encode('utf-8'))
printer.text('\n \n')
printer.text(cond.replace(u"\u2013", "-").encode("utf-8"))
printer.text("\n \n")
def icon():
icon = data['currently']['icon']
icon = data["currently"]["icon"]
image = GRAPHICS_PATH + icon + ".png"
return image
deg = ' C' # Degree symbol on thermal printer, need to find a better way to use a proper degree symbol
deg = " C" # Degree symbol on thermal printer, need to find a better way to use a proper degree symbol
# if you want Fahrenheit change units= to 'us'
url = "https://api.darksky.net/forecast/" + API_KEY + "/" + LAT + "," + LONG + \
"?exclude=[alerts,minutely,hourly,flags]&units=si" # change last bit to 'us' for Fahrenheit
url = (
"https://api.darksky.net/forecast/"
+ API_KEY
+ "/"
+ LAT
+ ","
+ LONG
+ "?exclude=[alerts,minutely,hourly,flags]&units=si"
) # change last bit to 'us' for Fahrenheit
response = urllib.urlopen(url)
data = json.loads(response.read())
printer.print_and_feed(n=1)
printer.control("LF")
printer.set(font='a', height=2, align='center', bold=True, double_height=True)
printer.set(font="a", height=2, align="center", bold=True, double_height=True)
printer.text("Weather Forecast")
printer.text("\n")
printer.set(align='center')
printer.set(align="center")
# Print current conditions
printer.set(font='a', height=2, align='center', bold=True, double_height=False)
printer.text('Current conditions: \n')
printer.set(font="a", height=2, align="center", bold=True, double_height=False)
printer.text("Current conditions: \n")
printer.image(icon())
printer.text("\n")
printer.set(font='a', height=2, align='left', bold=False, double_height=False)
temp = data['currently']['temperature']
cond = data['currently']['summary']
printer.set(font="a", height=2, align="left", bold=False, double_height=False)
temp = data["currently"]["temperature"]
cond = data["currently"]["summary"]
printer.text(temp)
printer.text(' ')
printer.text(" ")
printer.text(deg)
printer.text(' ')
printer.text('\n')
printer.text('Sky: ' + cond)
printer.text('\n')
printer.text('\n')
printer.text(" ")
printer.text("\n")
printer.text("Sky: " + cond)
printer.text("\n")
printer.text("\n")
# Print forecast
printer.set(font='a', height=2, align='center', bold=True, double_height=False)
printer.text('Forecast: \n')
printer.set(font="a", height=2, align="center", bold=True, double_height=False)
printer.text("Forecast: \n")
forecast(0)
forecast(1)
printer.cut()