#!/usr/bin/python
#    This is a component of AXIS, a front-end for emc
#    Copyright 2004, 2005, 2006 Jeff Epler <jepler@unpythonic.net>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""jdi: Just do it

This program can be used as an emc "DISPLAY".  It resets estop, turns the
machine on, loads and runs the ngc file (which must be given as a positional
argument), printing the sequence number and location once per second.  When
it's done it punches the ESTOP button and exits emc.
"""

import sys, os
BASE = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))
sys.path.insert(0, os.path.join(BASE, "lib", "python"))

import emc
import time

print sys.argv
if len(sys.argv) != 4 or sys.argv[1] != "-ini":
    raise SystemExit, "Usage: jdi -ini <inifile> <gcodefile>"

ini = emc.ini(sys.argv[2])
emc.nmlfile = ini.find("EMC", "NML_FILE")
filename = sys.argv[3]

c = emc.command()
c.debug(-1)
s = emc.stat()

c.state(emc.STATE_ESTOP_RESET)
c.wait_complete()
c.state(emc.STATE_ON)
c.wait_complete()
c.mode(emc.MODE_AUTO)
c.wait_complete()
c.program_open(filename)
c.wait_complete()
c.auto(emc.AUTO_RUN, 0)
c.wait_complete()

def running(do_poll=True):
    if do_poll: s.poll()
    return s.task_mode == emc.MODE_AUTO and s.interp_state != emc.INTERP_IDLE

while running():
    print s.id, s.position
    time.sleep(1)

c.state(emc.STATE_ESTOP)

# vim:sw=4:sts=4:et:
