module CTopDrivePanel
    use CTopDrivePanelVariables
    use SimulationVariables
    use CLog3
    implicit none    
    public    
    contains
    
    ! Input routines
    subroutine SetTopDriveTdsPowerState(v)
    !DEC$ ATTRIBUTES DLLEXPORT :: SetTopDriveTdsPowerState
    !DEC$ ATTRIBUTES ALIAS: 'SetTopDriveTdsPowerState' :: SetTopDriveTdsPowerState
	    implicit none
	    integer, intent(in) :: v
	    data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState = v
#ifdef deb
	    call Log_3( 'TopDriveTdsPowerState=', data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState)
#endif
    end subroutine

    subroutine SetTopDriveTorqueWrench(v)
    !DEC$ ATTRIBUTES DLLEXPORT :: SetTopDriveTorqueWrench
    !DEC$ ATTRIBUTES ALIAS: 'SetTopDriveTorqueWrench' :: SetTopDriveTorqueWrench
	    implicit none
	    logical, intent(in) :: v
	    data%EquipmentControl%TopDrivePanel%TopDriveTorqueWrench = v
#ifdef deb
	    call Log_3( 'TopDriveTorqueWrench=', data%EquipmentControl%TopDrivePanel%TopDriveTorqueWrench)
#endif
    end subroutine

    subroutine SetTopDriveDrillTorqueState(v)
    !DEC$ ATTRIBUTES DLLEXPORT :: SetTopDriveDrillTorqueState
    !DEC$ ATTRIBUTES ALIAS: 'SetTopDriveDrillTorqueState' :: SetTopDriveDrillTorqueState
	    implicit none
	    integer, intent(in) :: v
	    data%EquipmentControl%TopDrivePanel%TopDriveDrillTorqueState = v
#ifdef deb
	    call Log_3( 'TopDriveDrillTorqueState=', data%EquipmentControl%TopDrivePanel%TopDriveDrillTorqueState)
#endif
    end subroutine

    subroutine SetTopDriveLinkTiltState(v)
    !DEC$ ATTRIBUTES DLLEXPORT :: SetTopDriveLinkTiltState
    !DEC$ ATTRIBUTES ALIAS: 'SetTopDriveLinkTiltState' :: SetTopDriveLinkTiltState
	    implicit none
	    integer, intent(in) :: v
	    data%EquipmentControl%TopDrivePanel%TopDriveLinkTiltState = v
#ifdef deb
	    call Log_3( 'TopDriveLinkTiltState=', data%EquipmentControl%TopDrivePanel%TopDriveLinkTiltState)
#endif
    end subroutine

    subroutine SetTopDriveIbop(v)
    !DEC$ ATTRIBUTES DLLEXPORT :: SetTopDriveIbop
    !DEC$ ATTRIBUTES ALIAS: 'SetTopDriveIbop' :: SetTopDriveIbop
	    implicit none
	    logical, intent(in) :: v
	    data%EquipmentControl%TopDrivePanel%TopDriveIbop = v
#ifdef deb
	    call Log_3( 'TopDriveIbop=', data%EquipmentControl%TopDrivePanel%TopDriveIbop)
#endif
    end subroutine
    
    subroutine SetTopDriveTorqueLimitKnob(v)
    !DEC$ ATTRIBUTES DLLEXPORT :: SetTopDriveTorqueLimitKnob
    !DEC$ ATTRIBUTES ALIAS: 'SetTopDriveTorqueLimitKnob' :: SetTopDriveTorqueLimitKnob
        ! use CSimulationVariables, only: IsPortable
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: data%EquipmentControl%DrillingConsole%RTSwitch, data%EquipmentControl%DrillingConsole%RTTorqueLimitKnob
        use CWarnings, only: Activate_TopdriveRotaryTableConfilict
        use CScaleRange
	    implicit none
	    real, intent(in) :: v
        if (IsPortable) then
            if(data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState /= 0 .and. data%EquipmentControl%DrillingConsole%RTSwitch /= 0) call Activate_TopdriveRotaryTableConfilict()
            if(data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState /= 0 .and. data%EquipmentControl%DrillingConsole%RTSwitch == 0) then
                data%EquipmentControl%DrillingConsole%RTTorqueLimitKnob = 0
                data%EquipmentControl%TopDrivePanel%TopDriveTorqueLimitKnob = v
#ifdef deb
	            call Log_3( 'RTTorqueLimitKnob=', data%EquipmentControl%DrillingConsole%RTTorqueLimitKnob )
	            call Log_3( 'TopDriveTorqueLimitKnob=', data%EquipmentControl%TopDrivePanel%TopDriveTorqueLimitKnob )
#endif
            endif
            if(data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState == 0 .and. data%EquipmentControl%DrillingConsole%RTSwitch /= 0) then
                data%EquipmentControl%TopDrivePanel%TopDriveTorqueLimitKnob = 0
                data%EquipmentControl%DrillingConsole%RTTorqueLimitKnob = real(ScaleRange(v, 0.0, 10.0, 0.0, 6000.0), 8)
#ifdef deb
	            call Log_3( 'RTTorqueLimitKnob=', data%EquipmentControl%DrillingConsole%RTTorqueLimitKnob )
	            call Log_3( 'TopDriveTorqueLimitKnob=', data%EquipmentControl%TopDrivePanel%TopDriveTorqueLimitKnob )
#endif
            endif
        else
            data%EquipmentControl%TopDrivePanel%TopDriveTorqueLimitKnob = v
#ifdef deb
	        call Log_3( 'TopDriveTorqueLimitKnob=', data%EquipmentControl%TopDrivePanel%TopDriveTorqueLimitKnob)
#endif
        endif

    end subroutine
    
    subroutine SetRpmKnob(v)
    !DEC$ ATTRIBUTES DLLEXPORT :: SetRpmKnob
    !DEC$ ATTRIBUTES ALIAS: 'SetRpmKnob' :: SetRpmKnob
	    ! use CSimulationVariables, only: IsPortable
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables!, only: data%EquipmentControl%DrillingConsole%RTSwitch, data%EquipmentControl%DrillingConsole%RTThrottle
        use CWarnings, only: Activate_TopdriveRotaryTableConfilict
        use CScaleRange
	    implicit none
	    real, intent(in) :: v
        if (IsPortable) then
            if(data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState /= 0 .and. data%EquipmentControl%DrillingConsole%RTSwitch /= 0) call Activate_TopdriveRotaryTableConfilict()
            if(data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState /= 0 .and. data%EquipmentControl%DrillingConsole%RTSwitch == 0) then
                data%EquipmentControl%DrillingConsole%RTThrottle = 0
                data%EquipmentControl%TopDrivePanel%RpmKnob = v
#ifdef deb
	            call Log_3( 'RTThrottle=', data%EquipmentControl%DrillingConsole%RTThrottle )
	            call Log_3( 'RpmKnob=', data%EquipmentControl%TopDrivePanel%RpmKnob )
#endif
            endif
            if(data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerState == 0 .and. data%EquipmentControl%DrillingConsole%RTSwitch /= 0) then
                data%EquipmentControl%TopDrivePanel%RpmKnob = 0
                data%EquipmentControl%DrillingConsole%RTThrottle = real(ScaleRange(v, 0.0, 965.0, 0.0, 250.0), 8)
#ifdef deb
	            call Log_3( 'RpmKnob=', data%EquipmentControl%TopDrivePanel%RpmKnob )
	            call Log_3( 'RTThrottle=', data%EquipmentControl%DrillingConsole%RTThrottle )
#endif
            endif
        else
            data%EquipmentControl%TopDrivePanel%RpmKnob = v
        endif
    
    
    
        if (IsPortable) then
            !
        else
	        data%EquipmentControl%TopDrivePanel%RpmKnob = v
#ifdef deb
	    call Log_3( 'RpmKnob=', data%EquipmentControl%TopDrivePanel%RpmKnob )
#endif
        endif

    end subroutine
    
    
    ! Output routines
    integer function GetTopDriveOperationFaultLed()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveOperationFaultLed
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveOperationFaultLed' :: GetTopDriveOperationFaultLed
	    implicit none
	    GetTopDriveOperationFaultLed = data%EquipmentControl%TopDrivePanel%TopDriveOperationFaultLed
	    !GetTopDriveOperationFaultLed = 1
    end function

    integer function GetTopDriveTdsPowerLed()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveTdsPowerLed
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveTdsPowerLed' :: GetTopDriveTdsPowerLed
	    implicit none
	    GetTopDriveTdsPowerLed = data%EquipmentControl%TopDrivePanel%TopDriveTdsPowerLed
	    !GetTopDriveTdsPowerLed = 1
    end function

    integer function GetTopDriveTorqueWrenchLed()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveTorqueWrenchLed
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveTorqueWrenchLed' :: GetTopDriveTorqueWrenchLed
	    implicit none
	    GetTopDriveTorqueWrenchLed = data%EquipmentControl%TopDrivePanel%TopDriveTorqueWrenchLed
	    !GetTopDriveTorqueWrenchLed = 1
    end function

    integer function GetTopDriveLinkTiltLed()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveLinkTiltLed
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveLinkTiltLed' :: GetTopDriveLinkTiltLed
	    implicit none
	    GetTopDriveLinkTiltLed = data%EquipmentControl%TopDrivePanel%TopDriveLinkTiltLed
	    !GetTopDriveLinkTiltLed = 1
    end function

    integer function GetTopDriveIbopLed()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveIbopLed
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveIbopLed' :: GetTopDriveIbopLed
	    implicit none
	    GetTopDriveIbopLed = data%EquipmentControl%TopDrivePanel%TopDriveIbopLed
	    !GetTopDriveIbopLed = 1
    end function
    
    real function GetTopDriveTorqueGauge()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveTorqueGauge
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveTorqueGauge' :: GetTopDriveTorqueGauge
	    implicit none
	    GetTopDriveTorqueGauge = data%EquipmentControl%TopDrivePanel%TopDriveTorqueGauge
	    !GetTopDriveTorqueGauge = 340
    end function
    
    real function GetTopDriveTorqueLimitGauge()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveTorqueLimitGauge
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveTorqueLimitGauge' :: GetTopDriveTorqueLimitGauge
	    implicit none
	    GetTopDriveTorqueLimitGauge = data%EquipmentControl%TopDrivePanel%TopDriveTorqueLimitGauge
	    !GetTopDriveTorqueLimitGauge = 442
    end function
    
    real function GetTopDriveRpmGauge()
    !DEC$ ATTRIBUTES DLLEXPORT :: GetTopDriveRpmGauge
    !DEC$ ATTRIBUTES ALIAS: 'GetTopDriveRpmGauge' :: GetTopDriveRpmGauge
	    implicit none
	    GetTopDriveRpmGauge = data%EquipmentControl%TopDrivePanel%TopDriveRpmGauge
	    !GetTopDriveRpmGauge = 67
    end function

    
 end module CTopDrivePanel
