INCLUDE "GLOBALS"

DIM lastmoveconsumed AS INTEGER
lastmoveconsumed = -1

DIM lastspeedset AS INTEGER
lastspeedset = -1

DIM linecount AS INTEGER
linecount = 0

DIM linetoread AS INTEGER
linetoread = 0

DIM oldz AS INTEGER
oldz = 0

DIM thismovetype AS INTEGER

DIM movex, movey, movez, moveiv AS FLOAT
DIM end1, end2, center1, center2, direction, axistype AS FLOAT
DIM consumed, readupto AS INTEGER

REPEAT
    WA(10)

    'FORCE_SPEED = SPEED
    'ENDMOVE_SPEED = SPEED


    consumed = TABLE(table_consumed)
    readupto = TABLE(table_readupto)


    IF readupto > consumed THEN

        WHILE readupto > consumed
            linetoread = consumed + table_firstmove
            thismovetype = TABLE(linetoread)
            IF thismovetype = 10 THEN
                consumed = consumed + num_moves_newspeed
                IF (lastspeedset <> TABLE(linetoread+1)) THEN
                    lastspeedset= TABLE(linetoread+1)
                    FOR axiscount = 0 TO 1
                        BASE(axiscount)
                        FORCE_SPEED = lastspeedset
                        ENDMOVE_SPEED = lastspeedset
                        STARTMOVE_SPEED = lastspeedset
                    NEXT axiscount
                    'BASE(2)
                    'FORCE_SPEED = lastspeedset/4
                    'ENDMOVE_SPEED = lastspeedset/4
                    'STARTMOVE_SPEED = lastspeedset/4
                ENDIF
                lastmoveconsumed = -1
                CONTINUE
            ELSEIF thismovetype = 0 OR thismovetype = 1 OR thismovetype = 5 OR thismovetype = 6 THEN
                consumed = consumed + num_moves_linear
                movex = TABLE(linetoread+1)
                movey = TABLE(linetoread+2)
                movez = TABLE(linetoread+3)
                'moveiv = TABLE(linetoread+4)

                ' IF (movex <> TABLE(linetoread+1+200000)) OR (movey <> TABLE(linetoread+2+200000)) OR (movez <> _
                '        TABLE(linetoread+3+200000)) THEN
                '    PRINT "error on line " , linetoread
                ' ENDIF



            ELSE
                consumed = consumed + num_moves_arc
                end1 = TABLE(linetoread+1)
                end2 = TABLE(linetoread+2)
                center1 = TABLE(linetoread+3)
                center2 = TABLE(linetoread+4)
                direction = TABLE(linetoread+5)
                axistype = TABLE(linetoread+6)
            ENDIF

            IF (thismovetype < 2) AND (oldz <> movez) THEN
                BASE(2)
                MOVEABSSP(movez)
                oldz = movez
                lastmoveconsumed = -1
            ENDIF



            IF lastmoveconsumed <> thismovetype THEN

                SELECT_CASE(thismovetype)
                    'mill linear
                    CASE 0 :
                        BASE(0,1,2)

                        'mill rapid
                    CASE 1 :
                        BASE(0,1,2)

                    CASE 2 :'XY
                        BASE(0,1)
                    CASE 3 :'YZ
                        BASE(1,2)
                    CASE 4 :'XZ
                        BASE(0,2)
                        'CASE 2,3,4 :
                        '   BASE(0,1,2,3)

                    CASE 5 :
                        'lathe linear
                        BASE(0,1)
                    CASE 6 :
                        'lathe rapid
                        BASE(0,1)

                END_CASE
                lastmoveconsumed = thismovetype
            ENDIF

            '  PRINT SPEED
            ' PRINT movex, movey, movez

            SELECT_CASE(thismovetype)
                CASE 0 :
                    MOVEABSSP(movex, movey, movez)
                CASE 1 :
                    MOVEABSSP(movex, movey, movez)
                    'WAIT IDLE
                CASE 2:'XY
                    TRIGGER
                    ' MOVECIRCSP(end1,end2,center1,center2,direction)
                    MHELICALSP(end1,end2,center1,center2,direction,0)
                    'MSPHERICALSP(end1,end2,center1,center2,direction,0)
                    'MSPHERICALSP(end1,end2,center1,center2,direction,0)
                    'PRINT end1,end2,center1,center2

                    'MSPHERICALSP(end1,end2,0,center1,center2,0,1,0,0)
                CASE 3:'YZ
                    TRIGGER
                    MHELICALSP(end1,end2,center1,center2,direction,0)
                    'MSPHERICALSP(0,end1,end2,0,center1,center2,1,0,0)
                CASE 4:'XZ
                    TRIGGER
                    MHELICALSP(end1,end2,center1,center2,direction,0)
                    'MSPHERICALSP(end1,0,end2,center1,0,center2,1,0,0)
                CASE 5 :
                    MOVEABSSP(movex, movey)
                CASE 6 :
                    MOVEABSSP(movex, movey)
                    WAIT IDLE

            END_CASE

            TABLE(table_idle, 0)

            IF TABLE(table_consumed) = 0 AND TABLE(table_readupto) = 0 THEN
                'Stop was pressed while this consumer was reading. So quit the loop
                EXIT_LOOP
            ELSE
                lastmoveconsumed = thismovetype
                movesmade = TABLE(table_movesmade) + 1
                TABLE(table_movesmade, movesmade)
            ENDIF

        WEND
        TABLE(table_consumed,readupto)
    ENDIF

UNTIL FALSE

