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
import wonderwords
from datetime import datetime
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
self . p = Usb ( idVendor = 0x0416 , idProduct = 0x5011 , usb_args = None , timeout = 4 , in_ep = 0x81 , out_ep = 0x02 )
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 . set ( invert = True )
self . p . text ( datetime . today ( ) . strftime ( ' % Y- % m- %d ' ) )
self . p . set ( align = " left " )
self . p . set ( invert = False )
2023-08-23 02:47:20 +00:00
self . p . text ( " \n " )
2023-08-22 23:51:39 +00:00
# pass #TODO: add other greetings?
def print_todos ( self , todos = [ { " time " : " 1:00pm to 2:00pm " , " text " : " Read a book " } ] ) :
for x in self . _parse_todos ( todos ) :
# print(x)
self . p . set ( align = " left " )
self . p . text ( x [ 0 ] )
self . p . set ( align = " right " )
self . p . textln ( x [ 1 ] )
self . p . set ( align = " left " )
2023-08-23 02:47:20 +00:00
self . p . text ( " \n " )
2023-08-22 23:51:39 +00:00
def print_sudoku ( self ) :
self . p . close ( )
2023-08-23 02:32:59 +00:00
# self.p = Usb(0x0416, 0x5011, None, 4, 0x81, 0x02)
self . p = Usb ( idVendor = 0x0416 , idProduct = 0x5011 , usb_args = None , timeout = 4 , in_ep = 0x81 , out_ep = 0x02 )
2023-08-22 23:51:39 +00:00
convert_to_image ( generate_sudoku ( ) )
self . p . image ( " sudoku.png " )
def print_random_quote ( self ) :
q = self . database . get_random_quote ( )
self . p . set ( align = " left " )
self . p . text ( q [ 0 ] )
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-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")
words = r . random_words ( 10 , include_parts_of_speech = [ " nouns " , " verbs " , " adjectives " ] )
w = WordSearch ( words , 32 , 32 )
for x in w . grid :
self . p . set ( align = " center " )
self . p . text ( x + " \n " )
# 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 ) :
self . print_greeting ( )
self . print_todos ( )
self . print_random_quote ( )
self . print_sudoku ( )
self . print_wordsearch ( )
self . finished_printing ( )
def _parse_todos ( self , data ) :
out = [ ]
for x in data :
out . append ( [ " ○ " + x [ " time " ] , x [ " text " ] ] )
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()