msEventRegister (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

1.50.00


Create an event handler consisting of an event configuration and a possible action to follow.

 

Shock and acceleration events are treated separately, and will trigger as long as they are registered and the logger is running.

For performance reasons, there can only be one active configuration for each. See limitations.

It is always the last configuration registered that is used.

If the last configuration is unregistered, the detection will be disabled and a new must be registered for the detection to continue.

In case an action is connected to a shock or acceleration event, it will only be done once.

 

When any of the other events has been triggered, it will automatically be deactivated and unregistered.

 

Input:

Event : PTR

A mandatory pointer to a structure containing the event configuration.

See msEventLogWatermark, msEventLogFull, msEventShock and msEventAcceleration.

 

Action : PTR

An optional pointer to a structure containing the possible action configuration.

See msActionStopLogger, msActionStartLogger and msActionSwitchLogger.

 

Returns: DINT

>0


- The ID of the event handler.

0


- This function is not supported.

-1


- Interface is not open. Call msOpen first.

-2


- Generic error.

-3


- Invalid configuration.

-4


- Invalid input type.

-5


- The logger is not present.

-17


- Switch-logger sensor setup is invalid.

 

 

Declaration

FUNCTION msEventRegister : DINT;
VAR_INPUT
   Event        : MANDATORY PTR;
   Action       : PTR;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
   logger      : SYSHANDLE;
   buf         : ARRAY[1..2000OF DINT;
   timestamp   : msTimestamp;
   acc         : msAccData;
   gyr         : msGyrData;
   running     : BOOL;
END_VAR;
 
THREAD_BLOCK msHandleEvents;
VAR
   rc        : INT;
   eventID   : DINT;
   watermark : msReadWatermark;
   shock     : msReadShockEvent;
   accel     : msReadAccelEvent;
END_VAR;
DebugMsg(message:="Event Handler started!");
running := TRUE;
WHILE running DO
   rc := msWaitEvent();
   IF rc < 0 THEN
      DebugFmt(message:="msWaitEvent error (rc=\1)", v1:=rc);
   ELSE
      CASE rc OF
         _MS_EVENT_TIMEOUT:
            DebugMsg(message:="msWaitEvent - Timeout!");
         _MS_EVENT_LOGGER_LEVEL:
            msReadEvent(event:=eventIDdata:=ADDR(watermark));
            DebugFmt(message:="Watermark event received with id: \4", v4:=eventID);
            DebugFmt(message:="Level of buffer was \1", v1:=watermark.level);
         _MS_EVENT_LOGGER_FULL:
            msReadEvent(event:=eventID);
            DebugFmt(message:="Logger full event received with id: \4", v4:=eventID);
         _MS_EVENT_LOGGER_STOPPED:
            msReadEvent(event:=eventID);
            DebugFmt(message:="Logger stopped event received with id: \4", v4:=eventID);
         _MS_EVENT_LOGGER_STARTED:
            msReadEvent(event:=eventID);
            DebugFmt(message:="Logger started event received with id: \4", v4:=eventID);
         _MS_EVENT_LOGGER_SWITCHED:
            msReadEvent(event:=eventID);
            DebugFmt(message:="Logger switched event received with id: \4", v4:=eventID);
         _MS_EVENT_SHOCK:
            msReadEvent(event:=eventIDdata:=ADDR(shock));
            DebugFmt(message:="Shock event received with id: \4", v4:=eventID);
            DebugFmt(message:="Timestamp was \4", v4:=shock.time);
            IF shock.x THEN
               DebugMsg(message:="Shock event on X");
            END_IF;
            IF shock.y THEN
               DebugMsg(message:="Shock event on y");
            END_IF;
            IF shock.z THEN
               DebugMsg(message:="Shock event on Z");
            END_IF;
         _MS_EVENT_ACCELERATION:
            msReadEvent(event:=eventIDdata:=ADDR(accel));
            DebugFmt(message:="Acceleration event received with id: \4", v4:=eventID);
            DebugFmt(message:="Acc : X "+floatToStr(v:=accel.accx)+
                              ", Y "+floatToStr(v:=accel.accy)+
                              ", Z "+floatToStr(v:=accel.accz));
            IF accel.xabove THEN
               DebugMsg(message:="X was above threshold");
            END_IF;
            IF accel.yabove THEN
               DebugMsg(message:="Y was above threshold");
            END_IF;
            IF accel.zabove THEN
               DebugMsg(message:="Z was above threshold");
            END_IF;
            IF accel.xbelow THEN
               DebugMsg(message:="X was below threshold");
            END_IF;
            IF accel.ybelow THEN
               DebugMsg(message:="Y was below threshold");
            END_IF;
            IF accel.zbelow THEN
               DebugMsg(message:="Z was below threshold");
            END_IF;
      ELSE
         DebugFmt(message:="Unknown event received: \1", v1:=rc);
      END_CASE;
   END_IF;
END_WHILE;
END_THREAD_BLOCK;
 
 
PROGRAM test;
VAR
   ms_events  : msHandleEvents;
   rc         : INT;
   logger_lvl : INT;
   accelcfg   : msEventAcceleration;
   accelid    : DINT;
   shockcfg   : msEventShock;
   shockid    : DINT;
END_VAR;
rc := msOpen();
DebugFmt(message:="msOpen (rc=\1)", v1:=rc);
ms_events();
rc := msAccEnable(enable:=TRUEmode:=3);
DebugFmt(message:="accEnable ON (rc=\1)", v1:=rc);
rc := msGyrEnable(enable:=TRUE);
DebugFmt(message:="gyrEnable ON (rc=\1)", v1:=rc);
 
rc := msLoggerCreate(logger:=loggerbuffer:=ADDR(buf), size:=SIZEOF(buf), stoponfull:=FALSE);
DebugFmt(message:="msLoggerCreate (rc=\1) buffer_size=\4, stoponfull=FALSE", v1:=rcv4:=SIZEOF(buf));
rc := msLoggerAddAcc(logger:=loggerdownsample:=100, lowres:=TRUE);
DebugFmt(message:="msLoggerAddAcc (rc=\1) downsample=100, lowres=TRUE", v1:=rc);
rc := msLoggerAddGyr(logger:=loggerdownsample:=100, lowres:=TRUE);
DebugFmt(message:="msLoggerAddGyr (rc=\1) downsample=100, lowres=TRUE", v1:=rc);
 
shockid := msEventRegister(event:=ADDR(shockcfg));
IF shockid > 0 THEN
   DebugFmt(message:="Success, shock event id is \4", v4:=shockid);
ELSE
   DebugFmt(message:="msEventRegister  (rc=\4)", v4:=shockid);
END_IF;
 
accelcfg.threshold := 0.1;
accelid := msEventRegister(event:=ADDR(accelcfg));
IF accelid > 0 THEN
   DebugFmt(message:="Success, acceleration event id is \4", v4:=accelid);
ELSE
   DebugFmt(message:="msEventRegister  (rc=\4)", v4:=accelid);
END_IF;
 
rc := msLoggerStart(logger:=loggerreset:=TRUE);
DebugFmt(Message:="msLoggerStart (rc=\1) reset=TRUE", v1:=rc);
BEGIN
   Sleep(delay:=10000);
END;
END_PROGRAM;