#!/usr/bin/python

#I am 'getting old' and sometimes have trouble remembering to do important things when run my CNC.
#Things like turning the VFD on before executing a program.  (NOT a good situation!)
#I wanted an 'in your face' messaging system that I could not ignore and that is easy to read froma distance.
#I also wanted to be able to create and edit messages easily.
#The 'Continue' and 'Stop Executing' buttons are overly large. (Sometimes I 'mouse' left handedly and I'm not very accurate :)

#Well, this is it!
#With very little effort you can make it work for any M code from M100 - M199

#Enjoy
# --Jim Womeldorf

#Place this file in your linuxcnc/nc_files/ folder
#Name it Mxxx where xxx is 100 through 199
#Set the permissions on Mxxx so it is executable (Allow it to run as a program)

#In the same folder create a corresponding message file named Mxxxmessages.ini
#The first line of the .ini file must be     [Messages]
#You may place comments in the messagefile by starting the line with   #
#For each message begin a line with an integer (0 - 999999 the P number to be passed in), then an equals sign
# E.g.
#     100=Turn VFR on
#The message 'Turn VFR on' will be displayed when   Mxxx P100   is called in your code.

#if you wish to have a message appear on more than one line, place \n wherever the break is to occur as follows:
#     103=This is line 1\nThis is line2\nThis is line3\nEtc.

#You must restart AXIS after creating these files so it knows they are there

#When a message is displayed:
#    Clicking 'Continue'or pressing [Enter/Return] will close the message and allow the program to continue.
#    Clicking 'Stop Executing' or pressing [Esc] will close the message and stop execution of the program.

#change the following line to alter the title of the message window
title="Jim's Messaging System"

#If you  would like to change the font size of the message do it here
fontsize = 16

import sys
import os
import ConfigParser
import Tkinter as tk

MessageFilePath = __file__ + "messages.ini"

proceed = "Y"

if not os.path.isfile(MessageFilePath):
	instruction = MessageFilePath + "    does not exist"
	proceed = "N"
else:

	if len(sys.argv) == 1:
		instruction = "M100 was called with no P value"
		proceed = "N"
	else:
		P=sys.argv[1]

		#The following allows running from a terminal or from Linuxcnc which adds .000000 to the P parameter
	
		if len(P)<7:
			ps=P
		else:
			ps=P[0:len(P)-7]

		cfg = ConfigParser.ConfigParser()

		#Present errors raised while reading Messages.ini
		try:
			cfg.read(MessageFilePath)	
			instruction = cfg.get("Messages", ps)
			instruction = instruction.decode("unicode_escape") 
		except (ConfigParser.MissingSectionHeaderError), Argument:
			proceed = "N"
			instruction = "First line of\n" + MessageFilePath + "\nmust be:\n[Messages]"
			pass
		except (ConfigParser.MissingSectionHeaderError, ConfigParser.NoOptionError), Argument:
			proceed = "N"
			instruction = "There is no message for number\n" + P + "\n in the file\n" + MessageFilePath

if instruction == "":
	instruction = "The message for P" + P + " is blank"
	proceed = "N"

mess = os.linesep + instruction.replace("\n", os.linesep) + os.linesep

window = tk.Tk()

window.title(title)
window.geometry("600x300")

msg = tk.Message(window, text = mess,width=600)
msg.config(bg="Yellow", fg="Blue", font=("times", fontsize, "bold"))

#pressing the [Esc] key is the same as clicking Continue
def close(event):
	exit(1)
window.bind("<Escape>",close)

if proceed == "Y":

	#pressing the [Return] key is the same as clicking Continue
	def go_on(event):
		exit(0)
	window.bind("<Return>",go_on)
	
	#handles clicking on the Continue button
	def Continue_clicked():
		exit(0)
	
	btnContinue = tk.Button(window,text="Continue\n[Enter]",command=Continue_clicked,width=60,height=2,font=("Helvetica", "14"))
	btnContinue.configure(bg = "green")

#handles clicking on the Stop button
def Stop_clicked():
	exit(1)

btnStop = tk.Button(window,text="Stop Executing\n[Esc]",command=Stop_clicked,width=60,height=2,font=("Helvetica", "14"))
btnStop.configure(bg = "red")

msg.pack()

if proceed == "Y":
	btnContinue.pack()

btnStop.pack()

window.mainloop()
