2023-08-22 23:51:39 +00:00
# from PyESCPOS.impl.epson import GenericESCPOS
# from PyESCPOS.ifusb import USBConnection
# conn = USBConnection.create('5011:0416,interface=0,ep_out=3,ep_in=0')
# printer = ElginRM22(conn)
# printer.init()
# printer.text('Hello World!')
# p = Usb(0x5011, 0x0416, printer="simple")
# p.text("Hello World\n")
# p = printer.Usb(0x0416, 0x5011, 4, 0x81, 0x02) #initalize printer on raspberry pi
# p.text("hello world") #print this text
# p.cut() #move paper up enough to tear off (probably a better way, but this works.)
# import falcon
# from wsgiref import simple_server
# import os
# import os
# import falcon
# WORKING_DIRECTORY = os.getcwd()
# STATIC = 'static/'
# def apex(req, resp):
# resp.content_type = 'text/html; charset=utf-8'
# filename = os.path.abspath(os.path.join(WORKING_DIRECTORY, STATIC, 'index.html'))
# with open(filename, 'rt') as f:
# resp.body = f.read()
from escpos . printer import Usb
import usb
2023-08-23 02:52:02 +00:00
from wonderwords import RandomWord
2023-08-22 23:51:39 +00:00
from datetime import datetime
2023-08-23 03:17:40 +00:00
import textwrap
2023-08-22 23:51:39 +00:00
2023-08-23 02:37:32 +00:00
from . sudoku_generator import *
from . wordsearch_generator import WordSearch
from . database import TodoDatabase
2023-08-22 23:51:39 +00:00
class ThermalPrinter ( ) :
def __init__ ( self , database ) :
# self.p = printer.Usb(0x0416, 0x5011, 4, 0x81, 0x02) #initalize printer on raspberry pi
self . database = database
try :
2023-08-23 02:32:59 +00:00
# self.p = Usb(0x0416, 0x5011, 4, 0x81, 0x02) #initalize printer on raspberry pi
2023-08-23 14:27:00 +00:00
self . p = Usb ( idVendor = 0x0416 , idProduct = 0x5011 , usb_args = None , timeout = 4000 , in_ep = 0x81 , out_ep = 0x02 , profile = " POS-5890 " )
2023-08-22 23:51:39 +00:00
# pass
except usb . core . NoBackendError as e :
print ( e )
print ( " Try running `export DYLD_LIBRARY_PATH=/opt/homebrew/lib` if on m1 mac. source: https://github.com/pyusb/pyusb/issues/355#issuecomment-1062798576 " )
raise
def print_greeting ( self ) :
self . p . set ( align = " center " )
self . p . text ( datetime . today ( ) . strftime ( ' % Y- % m- %d ' ) )
self . p . set ( align = " left " )
2023-08-23 13:29:20 +00:00
self . p . text ( " \n \n " )
2023-08-22 23:51:39 +00:00
# pass #TODO: add other greetings?
2023-08-23 16:46:39 +00:00
def print_todos ( self , todos = [ { " time " : " 1:00pm to 2:00pm " , " text " : " Read a book " } , { " time " : " 2:00pm to 3:00pm " , " text " : " Go for a walk " } , { " time " : " 3:00pm to 4:00pm " , " text " : " Walk to school while jumping on one leg and scratching your head. " } , ] ) :
2023-08-23 16:31:51 +00:00
self . p . set ( align = " left " )
2023-08-22 23:51:39 +00:00
for x in self . _parse_todos ( todos ) :
# print(x)
2023-08-23 16:38:11 +00:00
this_line_width = len ( x [ 0 ] ) + len ( x [ 1 ] )
2023-08-23 14:13:34 +00:00
2023-08-23 16:37:28 +00:00
# # self.p.charcode("CP437")
# # self.p.set(align="left")
2023-08-23 16:46:39 +00:00
self . p . text ( x [ 0 ] )
2023-08-23 14:21:56 +00:00
2023-08-23 16:46:39 +00:00
# output = []
2023-08-23 14:21:56 +00:00
2023-08-23 14:13:34 +00:00
if this_line_width < 32 :
2023-08-23 16:46:39 +00:00
self . p . text ( ( " " * ( 32 - this_line_width ) ) + x [ 1 ] )
# for s in range((32-this_line_width)):
# self.p.text(" ")
# self.p.text("" + " "*(32-this_line_width))
else :
# self.p.text(" ")
2023-08-23 17:03:11 +00:00
todo_text_max_len = 32 - ( len ( x [ 0 ] ) + 1 )
2023-08-23 16:46:39 +00:00
todo_text = textwrap . wrap ( x [ 1 ] , todo_text_max_len )
# t_text = textwrap.wrap()
2023-08-23 17:03:11 +00:00
fele = todo_text . pop ( 0 )
2023-08-23 17:14:27 +00:00
# print(todo_text_max_len-len(fele))
# print(fele)
2023-08-23 17:08:01 +00:00
self . p . text ( " " + " " * ( todo_text_max_len - len ( fele ) ) + fele + " \n " )
2023-08-23 14:21:56 +00:00
2023-08-23 17:01:29 +00:00
todo_text = textwrap . wrap ( ' ' . join ( todo_text ) , 32 )
for t in todo_text :
self . p . text ( t + " \n " )
# for t in todo_text[1:]:
2023-08-23 16:46:39 +00:00
# self.p.text("" + " "*(len(x[0])+1) )
# for s in range((len(x[0])+1)):
# self.p.text(" ")
2023-08-23 14:21:56 +00:00
2023-08-23 16:55:00 +00:00
# self.p.text( (" "*(32-(len(x[0]))))+t+"\n" )
2023-08-23 16:56:58 +00:00
# self.p.text(' '*(32-len(x[0])-len(t))+t+"\n" )
2023-08-23 17:01:29 +00:00
# self.p.text(' '*(32-len(t))+t+"\n" )
2023-08-23 16:55:00 +00:00
#32-len(x[0])len(text)
2023-08-23 14:21:56 +00:00
2023-08-23 16:46:39 +00:00
# self.p.text(t + "\n")
2023-08-23 14:13:34 +00:00
2023-08-23 16:47:53 +00:00
# print(output)
# for l in output:
# print(l)
# self.p.text(l + "\n")
2023-08-23 16:46:39 +00:00
self . p . text ( " \n " )
2023-08-23 16:31:51 +00:00
self . p . set ( align = " left " )
2023-08-23 16:46:39 +00:00
self . p . text ( " \n " )
2023-08-23 14:13:34 +00:00
# self.p.set(align="right")
# self.p.textln(x[1])
# self.p.set(align="left")
# self.p.text("\n")
2023-08-22 23:51:39 +00:00
def print_sudoku ( self ) :
2023-08-23 14:27:00 +00:00
# self.p.close()
2023-08-23 02:32:59 +00:00
# self.p = Usb(0x0416, 0x5011, None, 4, 0x81, 0x02)
2023-08-23 14:27:00 +00:00
# self.p = Usb(idVendor=0x0416, idProduct=0x5011, usb_args=None, timeout=4000 , in_ep=0x81, out_ep=0x02, profile="POS-5890")
2023-08-24 00:02:31 +00:00
# convert_to_image(generate_sudoku())
sudokus = generate_sudoku ( )
convert_to_image ( sudokus [ 0 ] , size = 16 , flipped = False )
2023-08-24 00:09:06 +00:00
convert_to_image ( sudokus [ 1 ] , size = 10 , flipped = True )
2023-08-23 03:32:04 +00:00
self . p . set ( align = " center " )
2023-08-24 00:02:31 +00:00
self . p . image ( " sudoku_16.png " )
2023-08-24 00:11:05 +00:00
self . p . set ( align = " right " )
self . p . image ( " sudoku_10.png " )
self . p . set ( align = " left " )
2023-08-23 14:27:00 +00:00
# self.p.close()
# self.p = Usb(idVendor=0x0416, idProduct=0x5011, usb_args=None, timeout=4 , in_ep=0x81, out_ep=0x02, profile="POS-5890")
2023-08-23 03:32:04 +00:00
2023-08-22 23:51:39 +00:00
def print_random_quote ( self ) :
q = self . database . get_random_quote ( )
2023-08-23 13:29:20 +00:00
self . p . text ( " \n \n " )
2023-08-22 23:51:39 +00:00
self . p . set ( align = " left " )
2023-08-24 02:42:27 +00:00
# self.p.text(q[0])
for l in textwrap . wrap ( q [ 0 ] , 32 ) :
self . p . text ( l )
2023-08-23 02:47:20 +00:00
self . p . text ( " \n " )
2023-08-22 23:51:39 +00:00
self . p . set ( align = " right " )
self . p . text ( q [ 1 ] )
2023-08-23 02:47:20 +00:00
self . p . text ( " \n " )
2023-08-23 03:32:04 +00:00
self . p . set ( align = " left " )
2023-08-22 23:51:39 +00:00
# pass #TODO: parse https://zenquotes.io/api/quotes (api limit is 5 req per 30 seconds)
def print_wordsearch ( self ) :
# words = ("gugu,gaga")
2023-08-23 03:09:00 +00:00
r = RandomWord ( )
2023-08-24 00:11:05 +00:00
words = r . random_words ( 7 , include_parts_of_speech = [ " nouns " , " verbs " , " adjectives " ] , word_max_length = 16 )
2023-08-23 17:14:27 +00:00
w = WordSearch ( ' , ' . join ( words ) , 16 , 16 )
2023-08-22 23:51:39 +00:00
2023-08-23 03:32:04 +00:00
self . p . set ( align = " left " )
2023-08-23 13:29:20 +00:00
self . p . text ( " Word Search: " )
2023-08-23 13:09:31 +00:00
self . p . text ( " \n \n " )
2023-08-23 03:32:04 +00:00
self . p . set ( align = " center " )
2023-08-23 03:23:31 +00:00
2023-08-22 23:51:39 +00:00
for x in w . grid :
2023-08-23 17:14:27 +00:00
self . p . text ( ' ' . join ( x ) + " \n " )
2023-08-22 23:51:39 +00:00
2023-08-23 13:09:31 +00:00
self . p . set ( align = " left " )
2023-08-23 03:20:34 +00:00
self . p . text ( " \n Words: \n " )
2023-08-23 03:17:40 +00:00
wwords = textwrap . wrap ( ' ' . join ( words ) , 32 )
for word in wwords :
2023-08-23 03:23:31 +00:00
# self.p.set(align="left")
self . p . text ( word + " \n " )
2023-08-23 03:17:40 +00:00
2023-08-22 23:51:39 +00:00
# Defaults.NOUNS: Represents a list of nouns
# Defaults.VERBS: Represents a list of verbs
# Defaults.ADJECTIVES: Represents a list of adjectives
# def printGrid(grid):
# for row in grid:
# for column in row:
# print("%s" % column, end='')
# print()
# printGrid(w.grid)
# w.findWords(words.split(','))
# print(w.wordPosition)
def print_default ( self ) :
2023-08-23 17:14:27 +00:00
# print('start 1')
2023-08-22 23:51:39 +00:00
self . print_greeting ( )
2023-08-23 17:14:27 +00:00
# print('end 1')
2023-08-23 14:25:46 +00:00
2023-08-23 17:14:27 +00:00
# print('start 2')
2023-08-23 16:31:51 +00:00
self . print_todos ( )
2023-08-23 17:14:27 +00:00
# print('end 2')
2023-08-23 14:25:46 +00:00
2023-08-23 17:14:27 +00:00
# print('start 3')
self . print_sudoku ( )
# print('end 3')
2023-08-23 14:25:46 +00:00
2023-08-23 17:14:27 +00:00
# print('start 4')
self . print_wordsearch ( )
# print('end 4')
2023-08-23 14:25:46 +00:00
2023-08-23 17:14:27 +00:00
# print('start 5')
self . print_random_quote ( )
# print('end 5')
2023-08-22 23:51:39 +00:00
self . finished_printing ( )
def _parse_todos ( self , data ) :
out = [ ]
for x in data :
2023-08-23 17:01:29 +00:00
out . append ( [ " O " + x [ " time " ] , x [ " text " ] ] )
2023-08-22 23:51:39 +00:00
return out
#Return: ["□ 1:00pm to 2:00pm", "Read a book"]
def finished_printing ( self ) :
self . p . cut ( ) #move paper up enough to tear off (probably a better way, but this works.)
# class SyncData():
# def __init__(self):
# pass
# #TODO: Obtain calendar data and save to local database, parse into printable content
# class GetTodos:
# def on_post(self, req, resp):
# pass #Handle web interface requesting current todos
# class SaveTodos:
# def on_post(self, req, resp):
# pass #Handle web interface sending updated todo list
# class PrintTodos:
# def on_post(self, req, resp):
# pass #Handle web interface requesting a print action
def main ( ) :
data = TodoDatabase ( )
tp = ThermalPrinter ( data )
# tp.print_todos()
tp . print_default ( )
if __name__ == ' __main__ ' :
main ( )
# def main():
# app = falcon.App()
# # app.add_route('/stories', things)
# app.add_sink(apex, prefix='^/$')
# app.add_static_route('/', os.path.abspath(os.path.join(WORKING_DIRECTORY, STATIC)))
# # print(os.path.abspath(''))
# # app.add_static_route('/', os.path.abspath(''))
# httpd = simple_server.make_server('127.0.0.1', 8000, app)
# httpd.serve_forever()
# if __name__ == '__main__':
# main()