# PYTHON WRAPPER TO AUTOMATE INDUSTRIAL AUTOMATION DEMONSTRATION
# SENIOR DESIGN 2018-2019
# AUTHOR: Phil Parisi
# REVISION: 1.1 on 15Apr19

# THIS IS PURELY THE ENGRAVEMENT MACRO FILE - CLOSED FILE 22APR AS FINAL VERSION

######## CODE SETUP, PARAMETERS, PACKAGE LOADING ############
import pyautogui #these are for our clicks and keyboard strokes!
import os #required for any os commands
import time #required to call any time commands (i.e. delays)

#Laser Engraver coordinates, settings (as STRINGS so macro can type these later)
x_coord = '200'
y_coord = '292'
MinPower = '30'
MaxPower = '30'
Speed = '200' #mm/s
Interval = '0.067' #mm

######################## BEGIN ACTUAL EXECUTION ############################

######## PART 1: OPEN PROGRAMS, RECEIVE USER INPUT, START DEMO #############

# prompt for opening all programs vs. direct to robot program
pyautogui.click(550,750)
#run_startup = input('Do you want to run startup? Type y or n... ')


# open up programs (if specified)
#if run_startup == 'y':
    #print('...Starting up programs...')
    #os.startfile('EngraveFile.rld')
    #os.startfile('INTERRA FILE NAME HERE.ext')
    #os.startfile("ElevateAutoRuns_swift.lnk")
    #os.startfile('run_it.docx')
    #print('...All programs opened...')

# go directly to get text inputs
#else:
    #print('...Bypassing program start-up...')

# prompt text input & ensure text input for engraver is </= specified length
for x in range(1,10):
    # prepare typing in PyCharm command window
    time.sleep(1)
    pyautogui.click(800, 785)
    engrave_text = input('What text do you want to engrave onto the flashlight? ')
    if len(engrave_text) >= 24:
        print('...Sorry, text string is too long. Maximum 23 characters...')
    else:
        break
print('...Great! We will engrave -> ',engrave_text, ' <- onto your flashlight! Enjoy the demo...')


#START SAWYER PROGRAM NOW!


######## PART 2: WAITING FOR LIGHTCHECK INPUT #############

#print('...Execute James code here...')


######## PART 3: ENGRAVE, PREPARE FOR FUTURE RUNS #############

# PREPARE ENGRAVING FILE, step by step what to do in LaserWorks
print('...Running Engraving Macro...')
#pyautogui.click(1455,1054) #minimize pycharm
os.startfile("ElevateAutoRuns.lnk") #this bypasses security access when opening :)
time.sleep(5)
os.startfile("distract.txt")
time.sleep(1)
pyautogui.click(1578, 1053) #enter laserworks
pyautogui.click(18,445) #click 'font' button
#time.sleep(.5)
pyautogui.click(650,600) #place 'font' in workspace
time.sleep(.5)
pyautogui.click(1031,317) #click dropdown arrow for font
#time.sleep(.5)
pyautogui.typewrite('t') #select technic font
pyautogui.typewrite('t') #select technic font
#time.sleep(.5)
pyautogui.press('enter')
pyautogui.click(930,387)
for x in range(1,30):
    pyautogui.press('backspace')
time.sleep(.5)
pyautogui.typewrite(engrave_text) #add engraver text
pyautogui.click(1143,752) #hit ok to close box
pyautogui.click(523,413) #click away
pyautogui.click(650,600) #click object
pyautogui.click(50,108) #click x coord box
pyautogui.click(50,108) #click x coord box twice!
pyautogui.press('backspace')
pyautogui.typewrite(x_coord) #enter x coord
pyautogui.press('tab')
pyautogui.press('backspace')
pyautogui.typewrite(y_coord) #enter y coord
pyautogui.press('enter')

#adjust power and speed settings
pyautogui.click(1823,430) #edit MinPower
for x in range(1,5):
    pyautogui.press('backspace')
pyautogui.typewrite(MinPower)
pyautogui.click(1823,452) #edit MaxPower
for x in range(1,5):
    pyautogui.press('backspace')
pyautogui.typewrite(MaxPower)
pyautogui.click(1806,483) #edit Speed
for x in range(1,7):
    pyautogui.press('backspace')
pyautogui.typewrite(Speed)

#adjust scan and interval
pyautogui.doubleClick(1740,224,.01) #double click 'laser output' to open settings
time.sleep(.5)
pyautogui.click(868,500)
time.sleep(.5)
pyautogui.click(868,518) #click 'scan'
pyautogui.click(1202,526)
for x in range(1,7):
    pyautogui.press('backspace')
pyautogui.typewrite(Interval) #edit interval between rows of engravement
pyautogui.click(1168,725) #click OK to close box
pyautogui.click(1852,792) #click the coordinate positioning dropdown box
time.sleep(.5)
pyautogui.click(1852,862) #select 'absolute coordinate'
pyautogui.moveTo(1648,723) #hover over 'start' button
print('...Engraving File Prepared Successfully...')

#WAIT BEFORE ENGRAVEMENT


#START THE ENGRAVEMENT
#pyautogui.click(1647,728)



#pyautogui.click()3time.sleep(.5)
#pyautogui.click()
#time.sleep(.5)
#time.sleep(.5)
#pyautogui.click()
#time.sleep(.5)
#pyautogui.click()
#time.sleep(.5)






# microsoft word screwing around
#pyautogui.click(893,1051)
#time.sleep(1)
#pyautogui.click(1200,700)
#time.sleep(1)
#pyautogui.typewrite(engrave_text)
#pyautogui.hotkey('ctrl','v')
#time.sleep(2)

#if len(engrave_text) >= 24
#print('Thank you! We will engrave -> ',engrave_text, ' <- onto your flashlight. Enjoy the demo!')

#import os
#name of file you want to open, must be in same folder/directory that THIS file is in
#command = "run_it.docx"
#executes or opens a file
#os.system(command)
#print("Command Executed")

################################################

# HOW TO TYPE LETTERS! using pynput module/library/package

#from pynput.keyboard import Key, Controller
#keyboard = Controller()

# a single press
#keyboard.press('a')
#keyboard.release('a')

# a whole sentence!
#keyboard.type("This is your mother talkin!")

###################################################

# HOW TO READ OUPUTS FROMS A OPENED FILE

#how to read a file (i.e. read name to engrave)
#f = open('EngraveName.txt','r') #r indicates reading from file
#print(f.name)
#f.close() #close files after using them
#print('oh yeah it worked!')

# OR
#with open('EngraveName.txt','r') as f:
    #f_contents = f.read()
    #print(f_contents)

###############################################

# HOW TO PROMPT/RECEIVE USER INPUT
#engrave_text = input('What text do you want to engrave onto the flashlight? ')
#print('Thank you! We will engrave -> ',engrave_text, ' <- onto your flashlight. Enjoy the demo!')

###############################################

# HOW TO ADD A TIME DELAY

#import time
#time.sleep(amt of seconds)


############################################

#import pynput.mouse as ms #create mouse module to call clicks and positions with pynput package
#import pynput.keyboard as kb #create keyboard module to call key strokes and type strings with pynput package
#mouse = ms.Controller()
#keyboard = kb.Controller()