Examples - NMP Example

Top  Previous  Next

//-----------------------------------------------------------------------------
// example.vpl, created 2012-03-30 10:01
//
// This application demonstrates the use of the NMP.
//
// It creates two NMP buttons, one red with the text "Panic!" and a green button
// with the text "Enter vehicle".
// The panic button will start flashing when pressed and print the text
// "!! ALERT !!" to the debug window.
// The text of the enter vehicle button will alternate between "Exit vehicle" and
// "Enter vehicle" when pressed, and a message is printed to the debug window.
//
// The MDT is enabled, and by pressing the MDT button in the menu, the MDT will be
// shown, with the text "Welcome!" on the display.
//
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
 
VAR
  navMsgAck     : navMessageReplyReceive;
  navMsg        : navMessageReceive;
  navMsgStatus  : navMessageStatusReceive;
  navETA        : navETAReceive;
  navStop       : navStopReceive;
  navUserID     : navUserIDReceive;
  navUserStatus : navUserStatusReceive;
  nmpButtonRec  : nmpButtonPressedReceive;
  updProg       : nmpUpdateProgressReceive;
  gps           : gpsFix;
  mdtInfo       : mdtProfile;
  // flag to keep track of the location of the driver, according to the
  // enter/exit vehicle button
  driverInVehicle: BOOL;
 
END_VAR;
 
 
//-----------------------------------------------------------------------------
// FUNCTION   : setupNMP
//    Handles the creation of buttons, etc. on the NMP when it is connected.
//-----------------------------------------------------------------------------
FUNCTION setupNMP : INT;
VAR
  rc:INT;
END_VAR;
  //Add NMP buttons
  rc := nmpButtonsDefine(count:=2);
  IF rc <> 0 THEN    
    DebugFmt(message:="Failed to define NMP buttons (nmpButtonsDefine=\1)", v1:=rc);
    setupNMP := rc;
    RETURN;
  END_IF;  
  rc := nmpButtonCreate(id := 1, weight:= 2, text := "Panic!", visible := TRUE,
enable := TRUE, color := nmpRGBToDint(r:= 16#FF, g := 16#00, b := 16#00));
  IF(rc <> 0 ) THEN      
    DebugFmt(message:="Failed to create panic button (nmpButtonCreate=\1)", v1:=rc);
    setupNMP := rc;
    RETURN;
  END_IF;  
 
  IF driverInVehicle THEN
    rc := nmpButtonCreate(id := 2, weight:= 3, text := "Exit vehicle", visible := TRUE,
enable := TRUE, color := nmpRGBToDint(r:= 16#66, g := 16#FF, b := 16#66));
  ELSE
    rc := nmpButtonCreate(id := 2, weight:= 3, text := "Enter vehicle", visible := TRUE,
enable := TRUE, color := nmpRGBToDint(r:= 16#66, g := 16#FF, b := 16#66));            
  END_IF;
  IF(rc <> 0 ) THEN      
    DebugFmt(message:="Failed to create enter/exit button (nmpButtonCreate=\1)", v1:=rc);
    setupNMP := rc;
    RETURN;
  END_IF;  
 
// Turn MDT on  
  rc := mdtPower(power := ON);  
  IF(rc = 0) THEN
    DebugMsg(message:="mdt powered on");
    mdtInfo();
    IF mdtInfo.type > 0 THEN
        DebugMsg(message:="MDT connected!");
        DebugFmt(message:="  Type=\1",v1:=mdtInfo.type);
        DebugFmt(message:="  Ver= \1",v1:=mdtInfo.version);
        DebugFmt(message:="  MaxX=\1",v1:=mdtInfo.pos_max_x);
        DebugFmt(message:="  MaxY=\1",v1:=mdtInfo.pos_max_y);
    END_IF;
     
  ELSE
    DebugFmt(message:="Failed to power MDT on (mdtPower=\1)", v1:=rc);
  END_IF;
 
 
  setupNMP:= rc;
END_FUNCTION;
 
//-----------------------------------------------------------------------------
// THREAD    : navMonitor
//    monitor events from the navigation device
//-----------------------------------------------------------------------------
THREAD_BLOCK navMonitor;
VAR
  event : INT := 0;
  rc    : INT;
END_VAR;
 
WHILE event <> -1 DO
  event := navWaitEvent(timeout := -1);
  CASE event OF
    1:   // A reply for a text message is received
          navMsgAck();
          DebugMsg(message := "MON: message reply received");
          DebugFmt(message := "MON: -ID=\1", v1 := navMsgAck.ID);
          DebugFmt(message := "MON: -reply=\1", v1 := navMsgAck.reply);
       
    2:  // A text message is received
          navMsg();
          DebugMsg(message := "MON: message received");
          DebugMsg(message := "MON: -text=" + navMsg.message);
         
    3:   // The status for a text message is received
          navMsgStatus();
          DebugMsg(message := "MON: message status received");
          DebugFmt(message := "MON: -ID=\1", v1 := navMsgStatus.ID);
          CASE navMsgStatus.status OF
              1: DebugMsg(message := "MON: -status=Unread");
              2: DebugMsg(message := "MON: -status=Read");
              3: DebugMsg(message := "MON: -status=Deleted/Not found");
          END_CASE;
 
    4:  // An ETA update is received
          navETA();
          DebugMsg(message := "MON: ETA received");
          DebugFmt(message := "MON: -stop=\1", v1 := navETA.id);
          DebugFmt(message := "MON: -distance=\4", v4 := navETA.distance);
          DebugFmt(message := "MON: -latitude=\4", v4 := navETA.latitude);
          DebugFmt(message := "MON: -longitude=\4", v4 := navETA.longitude);
       
    5:   // The status of a destination is received
          navStop();
          DebugMsg(message := "MON: stop status received");
          DebugFmt(message := "MON: -ID=\1", v1 := navStop.ID);
          DebugFmt(message := "MON: -index=\1", v1 := navStop.index);
          CASE navStop.status OF
              1: DebugMsg(message := "MON: -status=Active");
              2: DebugMsg(message := "MON: -status=Done");
              3: DebugMsg(message := "MON: -status=Inactive - Unread");
              4: DebugMsg(message := "MON: -status=Inactive - Read");
              5: DebugMsg(message := "MON: -status=Deleted/Not found");
          END_CASE;
       
    6:  // An user ID is received
          navUserID();
          DebugMsg(message := "MON: user ID received");
          DebugMsg(message := "MON: -user=" + navUserID.user);
         
    7:   // The status of an user is received
          navUserStatus();
          DebugMsg(message := "MON: user status received");
          DebugFmt(message := "MON: -status=\1", v1 := navUserStatus.status);
 
    10:   // NMP update status received    
          updProg();  
          IF updProg.ready THEN  
              DebugMsg(message := "*** Update progress received ***");  
              DebugFmt(message := "-status=\1", v1 := updProg.status);  
              DebugFmt(message := "-progress=\1", v1 := updProg.progress);  
              IF updProg.status = 0 THEN
                DebugMsg(message:="Update transferred to NMP");
              ELSIF updProg.status = 2 THEN
                DebugMsg(message:="Update ready to install");
              ELSIF updProg.status < 0 THEN
                DebugMsg(message:="Failed to transfer update");
              END_IF;
          END_IF;  
           
    11: // NMP button pressed
          nmpButtonRec();
          IF nmpButtonRec.ready THEN
              DebugMsg(message := "*** Button Pressed ***");
              DebugFmt(message := "-button=\1", v1 := nmpButtonRec.id);
              CASE nmpButtonRec.id OF
                1: // Panic!
                    rc:=nmpButtonFlash(id:=1,flash:=ON,duration:=30,period:=250,
color := nmpRGBToDint(r:=16#FF, g:=16#FF, b:=16#FF));
                    DebugMsg(message:="!! ALERT !!");
                       
                2: // Enter/exit
                    driverInVehicle := NOT driverInVehicle;
                    IF driverInVehicle THEN
                      DebugMsg(message:="Driver entered vehicle");
                      rc := nmpButtonText(id := 2, text := "Exit vehicle");
                    ELSE
                      DebugMsg(message:="Driver exited vehicle");
                      rc := nmpButtonText(id := 2, text := "Enter vehicle");
                    END_IF;  
              END_CASE;  
          END_IF;
         
    129: // A request to refresh the Quick messages
          DebugMsg(message := "MON: refresh of quick messages received, sending
defaults");
          navMessageQuickDefine(id := 1, text := "I have arrived at the destination");
          navMessageQuickDefine(id := 2, text := "There are no persons to receive
the packages at destination");
 
    130: // A request to refresh the message replies.
          DebugMsg(message := "MON: refresh of message replies received, sending
defaults");
          navMessageReplyDefine(id := 1, text := "Yes");
          navMessageReplyDefine(id := 2, text := "No");
 
    131: // A request to refresh the user status list.  
          DebugMsg(message := "MON: refresh of drivers status, sending defaults");
          navUserStatusDefine(id := 1, text := "Awaiting new destination");
          navUserStatusDefine(id := 2, text := "On the way to a destination");
 
    132: // Connection to navigation device established
          DebugMsg(message := "MON: navigation device present");
          DebugFmt(message := "MON: -serial=" + navDeviceSerial());
           
          IF nmpPresent() THEN
              DebugMsg(message := "NMP present");
              IF setupNMP() = 0 THEN
                DebugMsg(message := "NMP set up");
              ELSE
                DebugMsg(message := "Failed to set up NMP");
              END_IF;  
          ELSE
              DebugMsg(message := "Not an NMP");
          END_IF;
                 
    133: // Connection to navigation device lost
          DebugMsg(message := "MON: navigation device not present");
  ELSE
    DebugFmt(message := "MON: unhandled event form device '\1'", v1 := event);
  END_CASE;
END_WHILE;
END_THREAD_BLOCK;
 
 
//-----------------------------------------------------------------------------
// THREAD    : gpsRefresh
//    calls gpsFix repeatedly to send gps data to the NMP.
//-----------------------------------------------------------------------------
THREAD_BLOCK gpsRefresh;
  WHILE TRUE DO
    gps();
    Sleep(delay:=100);
  END_WHILE;
END_THREAD_BLOCK;
 
//-----------------------------------------------------------------------------
// THREAD    : mdtMonitor
//    Monitors the MDT for pressed buttons.
//-----------------------------------------------------------------------------
THREAD_BLOCK mdtMonitor;
VAR
  key:INT;
  rc:INT;
END_VAR;
  WHILE TRUE DO
    key := mdtGetKey(timeout := 10000);  
    CASE key OF
        127: // PowerUP key
              rc:=mdtStandby(enable :=OFF);
              DebugFmt(message:="mdtStandby(OFF) = \1", v1:=rc);              
             
              rc:=mdtGotoXY(x:=10,y:=5);
              DebugFmt(message:="mdtGotoXY() = \1", v1:=rc);              
              rc:=mdtWrite(message:="Welcome!");
              DebugFmt(message:="mdtWrite() = \1", v1:=rc);              
             
        126: // Power key
              rc:=mdtStandby(enable :=ON);
              DebugFmt(message:="mdtStandby(ON) = \1", v1:=rc);              
 
    END_CASE;
  END_WHILE;
END_THREAD_BLOCK;
 
 
PROGRAM example;
// These are the local variables of the program block
VAR
  navMon : navMonitor;
  gpsRefr: gpsRefresh;
  mdtMon : mdtMonitor;
  rc     : INT;
 
END_VAR;
  // Initializing
  gpsPower(power:=TRUE);
 
  rc := navOpen(port := 1);
  IF rc = 0 THEN
    DebugMsg(message := "navigation open");
  ELSE
    DebugFmt(message := "navigation error (navOpen=\1)", v1 := rc);
  END_IF;
  // Power on NMP
  rc := nmpPower( power:= ON);
  IF rc = 0 THEN
    DebugMsg(message := "NMP powered on");
  ELSE
    DebugFmt(message := "failed to power on NMP (nmpPower=\1)", v1 := rc);
  END_IF;
  // Monitor for events from NMP
  navMon();
  // Start sending GPS data to NMP
  gpsRefr();
  // Open MDT  
  rc := mdtOpen(port := 1);  
  // Monitor MDT keys
  mdtMon();
 
BEGIN
  // Code from this point until END will be executed repeatedly
  Sleep(delay:=100);  
END;
 
END_PROGRAM;