
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
from PIL import Image
from PIL import ImageTk
import numpy
from numpy import asarray
from matplotlib import image
import sys
import serial
import time

global col
global line
global isready
global maxline
global maxcol



ser = serial.Serial(
  port=None,
  baudrate=57600,
  parity=serial.PARITY_ODD,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS,
  timeout=0.70,
  xonxoff=True 
)

i = 0

while True:
  while True:
    pn = str(i)
    ps = "COM" + pn
    if i < 200:
      try:
        ser.port = ps
        ser.open()
      except:
        i = i + 1
      else:
        break
    else:
      break

  ptsnd = 0
  ptrcv = b"/x00"

  srsnd = "0"

  while ptsnd < 30:
    ptbsnd = 0
    while ptbsnd < 121:
      try:
        ser.write(srsnd.encode())
      except:
        break
      ptbsnd = ptbsnd + 1
    try:
      ptrcv = ser.read(1)
    except:
      ptrcv = ptrcv
    if ptrcv == b'\xd0':
      break
    elif ptrcv == b'\x13':
      break
    elif ptrcv == b'\x11':
      break
    else:
      ptsnd = ptsnd + 1
  if ptrcv == b'\xd0':
    break
  elif ptrcv == b'\x13':
    break
  elif ptrcv == b'\x11':
    break
  if i >= 200:
    break

if i >= 185:
  print("Are you sure you connected the printer and did it properly?")
  raise SystemExit
else:
  print("Here is the port of the printer. Enjoy!")
  print(ps)

numpy.set_printoptions(threshold=sys.maxsize)

def open_image():
 filepath = filedialog.askopenfilename(title="Open 120px wide Image to print", filetypes=[("Bitmaps", "*.bmp")])
 if filepath:
  display_image(filepath)

def display_image(filepath):
 global image
 image = Image.open(filepath)
 photo = ImageTk.PhotoImage(image)
 image_label.config(image=photo)
 image_label.photo = photo
 global numpydata
 numpydata = numpy.asarray(image)

def goprint():
  ser.close()
  ser.open()
  ser.read(1)
  ser.read(1)
  ser.read(1)
  ser.read(1)
  ser.read(1)
  ser.read(1)
  ser.read(1)
  ser.read(1)
  ser.read(1)
  global prijem
  prijem = b'15'
  maxcol, maxline = image.size
  maxcol = maxcol
  print(maxcol)
  print(maxline)
  line = 0
  col = 0
  while line < maxline:
    while col < 120:
      if col > maxcol - 1:
        val = 0
      else:
        val = numpydata[line][col]
      val2 = str(val)
      try:
        ser.write(val2.encode())
      except:
        print("Why did you unplug it? That was rude! Right, I will quit now because you are impatient")
        raise SystemExit
      if col > 118:
        prijem = ser.read(1)
      if prijem == b'/xd0':
        col = 0
      elif prijem == b'/x11':
        col = 0
      elif prijem == b'41':
        col = 0
      else:
        col = col + 1
    print(prijem)
    print("Line:" + str(line))
    col = 0
    line = line + 1
  print("Look! I have finished printing your image!")


root = tk.Tk()
root.minsize(500, 200)
root.title("Espoon Bitmap Receipt Printing")
open_button = ttk.Button(root, text="Open Image", command=open_image)
open_button.place(x="20", y="5")
isPrint = ttk.Button(root, text="Print", command=goprint)
isPrint.place(x="110", y="5")
image_label = tk.Label(root)
image_label.pack(padx=50, pady=50)

#numpydata = asarray(image)

root.mainloop()
