&ACCESS RV1
&REL 7
&PARAM TEMPLATE = C:\KRC\Roboter\Template\vorgabe
&PARAM EDITMASK = *
DEF SAKRL_EventNotice( )
END

;FOLD SAKRL_DisplayMessage(MODE:IN, DisplayMsg[]:IN)
GLOBAL DEF SAKRL_DisplayMessage(MODE:IN, DisplayMsg[]:IN)
  INT  MODE
  CHAR DisplayMsg[]

  DECL KrlMsg_T msg
  DECL KrlMsgPar_T par[3]
  DECL KrlMsgOpt_T opt
  
  INT nHandle
  BOOL bRet
  INT answer


  ; MODE integrity check - force to be valid

  IF (MODE < 1) THEN
    MODE = Abs(MODE) + SAKRL_LOG
  ENDIF

  msg.modul[]   = "SAKRL"
  msg.Nr        = MODE
  msg.msg_txt[] = DisplayMsg[]

  SWITCH (MODE)
    CASE SAKRL_INFO
      opt = {vl_stop FALSE, clear_p_reset TRUE, clear_p_SAW FALSE, log_to_DB FALSE}
      nHandle = Set_KrlMsg(#notify, msg, par[], opt)

    CASE SAKRL_WAIT
      opt = {vl_stop TRUE, clear_p_reset TRUE, clear_p_SAW FALSE, log_to_DB FALSE}
      nHandle = Set_KrlMsg(#quit, msg, par[], opt)

      IF (nHandle > 0) THEN
        WHILE (Exists_KrlMsg(nHandle))
          WAIT SEC 0.1
        ENDWHILE
      ENDIF

    CASE SAKRL_QUIT
      opt = {vl_stop TRUE, clear_p_reset TRUE, clear_p_SAW FALSE, log_to_DB TRUE}
      nHandle = Set_KrlMsg(#quit, msg, par[], opt)
      
      IF (nHandle > 0) THEN
        WHILE (Exists_KrlMsg(nHandle))
          WAIT SEC 0.1
        ENDWHILE
      ENDIF
      
      answer = SAKRL_PromptDialog(1, "Stop program and restart to continue", 1, "Ack")

      ; Loop forever

      LOOP
        HALT
      ENDLOOP

    DEFAULT ; Everything else gets logged
      opt = {vl_stop FALSE, clear_p_reset TRUE, clear_p_SAW FALSE, log_to_DB TRUE}
      nHandle = Set_KrlMsg(#notify, msg, par[], opt)
      bRet = Clear_KrlMsg(nHandle)
  ENDSWITCH
END
;ENDFOLD SAKRL_DisplayMessage(MsgId:IN, DisplayMsg[]:IN)

;FOLD SAKRL_PromptDialog(CallerId:IN, Prompt[]:IN, OptCnt:IN, Opt1[]:IN, Opt2[]:IN, Opt3[]:IN, Opt4[]:IN, Opt5[]:IN, Opt6[]:IN, Opt7[]:IN)
GLOBAL DEFFCT INT SAKRL_PromptDialog(CallerId:IN, Prompt[]:IN, OptCnt:IN, Opt1[]:IN, Opt2[]:IN, Opt3[]:IN, Opt4[]:IN, Opt5[]:IN, Opt6[]:IN, Opt7[]:IN)
  INT CallerId
  CHAR Prompt[]
  INT OptCnt
  CHAR Opt1[]
  CHAR Opt2[]
  CHAR Opt3[]
  CHAR Opt4[]
  CHAR Opt5[]
  CHAR Opt6[]
  CHAR Opt7[]

  DECL KrlMsg_T msg
  DECL KrlMsgPar_T par[3]
  DECL KrlMsgOpt_T opt
  DECL KrlMsgDlgSK_T sk[7] ; Softkeys

  INT nHandle
  INT keynumber
  INT i

  FOR i = 1 TO OptCnt
    SWITCH (i)
      CASE 1
        sk[1].sk_Type = #value
        sk[1].sk_txt[] = Opt1[]

      CASE 2
        sk[2].sk_Type = #value
        sk[2].sk_txt[] = Opt2[]

      CASE 3
        sk[3].sk_Type = #value
        sk[3].sk_txt[] = Opt3[]

      CASE 4
        sk[4].sk_Type = #value
        sk[4].sk_txt[] = Opt4[]

      CASE 5
        sk[5].sk_Type = #value
        sk[5].sk_txt[] = Opt5[]

      CASE 6
        sk[6].sk_Type = #value
        sk[6].sk_txt[] = Opt6[]

      CASE 7
        sk[7].sk_Type = #value
        sk[7].sk_txt[] = Opt7[]

      DEFAULT
    ENDSWITCH
  ENDFOR

  msg.modul[] = "SAKRL"
  msg.Nr      = CallerId
  msg.msg_txt[] = Prompt[]

  nHandle = Set_KrlDlg(msg, par[], sk[], opt)
  
  IF (nHandle > 0) THEN
    WHILE (Exists_KrlDlg(nHandle, keynumber))
      WAIT SEC 0.1
    ENDWHILE

    RETURN (keynumber)
  ENDIF

  RETURN (0)
ENDFCT
;ENDFOLD SAKRL_PromptDialog(CallerId:IN, Prompt[]:IN, OptCnt:IN, Opt1[]:IN, Opt2[]:IN, Opt3[]:IN, Opt4[]:IN, Opt5[]:IN, Opt6[]:IN, Opt7[]:IN)

;FOLD SAKRL_EKIDisplayStatus(MODE:IN, Status:IN)
GLOBAL DEF SAKRL_EKIDisplayStatus(MODE:IN, Status:IN)
  INT   MODE
  DECL EKI_STATUS Status
  CHAR  StatusStr[81]
  DECL  STATE_T state
  INT   offset
  BOOL  b
  
  offset = 0
  b = STRCLEAR(StatusStr[])
  
  IF (Status.Connected) THEN
    SWRITE(StatusStr[], state, offset, "(Connected) Buff %d, Read %d, Msg_No %d", Status.Buff, Status.Read, Status.Msg_No)
  ELSE
    SWRITE(StatusStr[], state, offset, "(Disconnected) Buff %d, Read %d, Msg_No %d", Status.Buff, Status.Read, Status.Msg_No)
  ENDIF

  SAKRL_DisplayMessage(MODE, StatusStr[])
END
;ENDFOLD SAKRL_EKIDisplayStatus(MODE:IN, Status:IN)

;FOLD SAKRL_DisplayIntValue(MODE:IN, MsgStr[]:IN, iVal:IN)
GLOBAL DEF SAKRL_DisplayIntValue(MODE:IN, MsgStr:IN, iVal:IN)
  INT  MODE
  CHAR MsgStr[]
  INT  iVal
  CHAR DisplayStr[81]
  DECL STATE_T state
  INT  offset
  BOOL b
  
  offset = 0
  b = STRCLEAR(DisplayStr[])
  
  SWRITE(DisplayStr[], state, offset, "%s: %d", MsgStr[], iVal)
  SAKRL_DisplayMessage(MODE, DisplayStr[])
END
;ENDFOLD SAKRL_DisplayIntStatus(MODE:IN, Status:IN)
