msReadEvent (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

NX-200, LX2

Firmware version:

1.50.00


Read the returned data from an event. This function also clears the event and should always be called after msWaitEvent, even when there is no data to read.

 

 

Input:

Data : PTR

A pointer to a variable to read the received data into, or omitted, depending on the type of event given.

Possible variable types include msReadWatermark, msReadShockEvent, msReadAccelEvent,

 

Output:

Event : DINT

The ID of the event received. Event ID 0 represents solicited events, e.g. events triggered by function calls and not registered events.

 

 

Returns: INT

1

- Success.

0

- This function is not supported.

-1

- Interface is not open. Call msOpen first.

-2

- Generic error.

-4

- Invalid input type.

-18

- No event to read.

-19

- Event handling error.

-20

- Invalid event data received.

 

 

Declaration

FUNCTION msReadEvent : INT;
VAR_INPUT
  Data       : PTR;

  Event      : ACCESS DINT;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
  logger       : ARRAY[1..2] OF SYSHANDLE;
  eventid      : ARRAY[1..4] OF DINT;
  accelid      : DINT;
  shockid      : DINT;
  timestamp    : msTimestamp;
  acc          : msAccData;
  gyr          : msGyrData;
  running      : BOOL;
END_VAR;
 
FUNCTION readLogger;
VAR_INPUT
  _logger : SYSHANDLE;
END_VAR;
VAR
  next         : INT;
  rc           : INT;
END_VAR;
  REPEAT
    next := msLoggerNext(logger:=_logger);
    CASE next OF
        _MS_LOGGER_NODATA:
          DebugMsg(message:="No more data in buffer!");
        _MS_LOGGER_TIMESTAMP:
          rc := msLoggerRead(logger:=_logger, data:=ADDR(timestamp));
          DebugFmt(message:="Timestamp: \4", v4:=timestamp.time);
          DebugMsg(message:="--------------------------------");
        _MS_LOGGER_ACCDATA:
          rc := msLoggerRead(logger:=_logger, data:=ADDR(acc));
          DebugMsg(message:="  ACC >>> X: "+floatToStr(v:=(acc.x*1000.0))+" mg  Y: "+
                        floatToStr(v:=(acc.y*1000.0))+" mg  Z: "+
                        floatToStr(v:=(acc.z*1000.0))+" mg");
        _MS_LOGGER_GYRDATA:
          rc := msLoggerRead(logger:=_logger, data:=ADDR(gyr));
          DebugMsg(message:="  GYR >>> X: "+floatToStr(v:=(gyr.x))+" dps  Y: "+
                        floatToStr(v:=(gyr.y))+" dps  Z: "+
                        floatToStr(v:=(gyr.z))+" dps");
    ELSE
        DebugFmt(message:="Invalid record ID \1", v1:=next);
    END_CASE;
    IF rc <> 1 THEN
        DebugFmt(message:="msLoggerRead (rc=\1)", v1:=rc);
    END_IF;
  UNTIL (next = _MS_LOGGER_NODATA);
  END_REPEAT;
END_FUNCTION;
 
THREAD_BLOCK msHandleEvents;
VAR
  rc           : INT;
  evid         : DINT;
  watermark    : msReadWatermark;
  shock        : msReadShockEvent;
  accel        : msReadAccelEvent;
  logger_lvl   : INT;
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:=evid, data:=ADDR(watermark));
          DebugFmt(message:="Watermark event received with id: \4", v4:=evid);
          DebugFmt(message:="Level of buffer was \1", v1:=watermark.level);
        _MS_EVENT_LOGGER_FULL:
          msReadEvent(event:=evid);
          DebugFmt(message:="Logger full event received with id: \4", v4:=evid);
        _MS_EVENT_LOGGER_STOPPED:
          msReadEvent(event:=evid);
          DebugFmt(message:="Logger stopped event received with id: \4", v4:=evid);
        _MS_EVENT_LOGGER_STARTED:
          msReadEvent(event:=evid);
          DebugFmt(message:="Logger started event received with id: \4", v4:=evid);
        _MS_EVENT_LOGGER_SWITCHED:
          msReadEvent(event:=evid);
          DebugFmt(message:="Logger switched event received with id: \4", v4:=evid);
        _MS_EVENT_SHOCK:
          msReadEvent(event:=evid, data:=ADDR(shock));
          DebugFmt(message:="Shock event received with id: \4", v4:=evid);
          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:=evid, data:=ADDR(accel));
          DebugFmt(message:="Acceleration event received with id: \4", v4:=evid);
          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;
    IF evid = shockid THEN
        logger_lvl := msLoggerLevel(logger:=logger[1]);
        DebugFmt(message:="Logger #1 level is at \1", v1:=logger_lvl);
        logger_lvl := msLoggerLevel(logger:=logger[2]);
        DebugFmt(message:="Logger #2 level is at \1", v1:=logger_lvl);
    END_IF;
    IF evid = eventid[4] THEN
        logger_lvl := msLoggerLevel(logger:=logger[2]);
        DebugFmt(message:="Logger #2 level is at \1", v1:=logger_lvl);
        rc := msLoggerStop(logger:=logger[2]);
        DebugFmt(message:="msLoggerStop (rc=\1)", v1:=rc);
        readLogger(_logger:=logger[1]);
        readLogger(_logger:=logger[2]);
        evid := 0;
        running := FALSE;
    END_IF;
  END_IF;
END_WHILE;
END_THREAD_BLOCK;
 
PROGRAM test;
VAR
  rc           : INT;
  ms_events    : msHandleEvents;
  accelcfg     : msEventAcceleration;
  shockcfg     : msEventShock;
  watermark    : msEventLogWatermark;
  switchLogger : msActionSwitchLogger;
  buf1         : ARRAY[1..10000] OF DINT;
  buf2         : ARRAY[1..10000] OF DINT;
END_VAR;
rc := msOpen();
DebugFmt(message:="msOpen (rc=\1)", v1:=rc);
 
ms_events();
 
rc := msAccEnable(enable:=TRUE, mode:=3);
DebugFmt(message:="accEnable ON (rc=\1)", v1:=rc);
rc := msGyrEnable(enable:=TRUE);
DebugFmt(message:="gyrEnable ON (rc=\1)", v1:=rc);
 
rc := msLoggerCreate(logger:=logger[1], buffer:=ADDR(buf1), size:=SIZEOF(buf1), stoponfull:=TRUE );
DebugFmt(message:="msLoggerCreate (rc=\1) buffer_size=\4, stoponfull=TRUE", v1:=rc, v4:=SIZEOF(buf1));
rc := msLoggerAddAcc(logger:=logger[1], downsample:=0, lowres:=FALSE);
DebugFmt(message:="msLoggerAddAcc (rc=\1) downsample=0, lowres=FALSE", v1:=rc);
rc := msLoggerAddGyr(logger:=logger[1], downsample:=10, lowres:=FALSE);
DebugFmt(message:="msLoggerAddGyr (rc=\1) downsample=10, lowres=FALSE", v1:=rc);
rc := msLoggerCreate(logger:=logger[2], buffer:=ADDR(buf2), size:=SIZEOF(buf2), stoponfull:=TRUE );
DebugFmt(message:="msLoggerCreate (rc=\1) buffer_size=\4, stoponfull=TRUE", v1:=rc, v4:=SIZEOF(buf2));
rc := msLoggerAddAcc(logger:=logger[2], downsample:=0, lowres:=FALSE);
DebugFmt(message:="msLoggerAddAcc (rc=\1) downsample=0, lowres=TRUE", v1:=rc);
rc := msLoggerAddGyr(logger:=logger[2], downsample:=0, lowres:=FALSE);
DebugFmt(message:="msLoggerAddGyr (rc=\1) downsample=0, lowres=FALSE", v1:=rc);
 
watermark.logger := logger[1];
watermark.level := 400;
eventid[1] := msEventRegister(event:=ADDR(watermark));
IF eventid[1] > 0 THEN
  DebugFmt(message:="Success, watermark event id is \4", v4:=eventid[1]);
ELSE
  DebugFmt(message:="msEventRegister  (rc=\4)", v4:=eventid[1]);
END_IF;
watermark.level := 800;
switchLogger.src := logger[1];
switchLogger.dst := logger[2];
eventid[2] := msEventRegister(event:=ADDR(watermark), action:=ADDR(switchLogger));
IF eventid[2] > 0 THEN
  DebugFmt(message:="Success, watermark event id is \4", v4:=eventid[2]);
ELSE
  DebugFmt(message:="msEventRegister  (rc=\4)", v4:=eventid[2]);
END_IF;
 
watermark.logger := logger[2];
watermark.level := 400;
eventid[3] := msEventRegister(event:=ADDR(watermark));
IF eventid[3] > 0 THEN
  DebugFmt(message:="Success, watermark event id is \4", v4:=eventid[3]);
ELSE
  DebugFmt(message:="msEventRegister  (rc=\4)", v4:=eventid[3]);
END_IF;
watermark.level := 800;
eventid[4] := msEventRegister(event:=ADDR(watermark));
IF eventid[4] > 0 THEN
  DebugFmt(message:="Success, watermark event id is \4", v4:=eventid[4]);
ELSE
  DebugFmt(message:="msEventRegister  (rc=\4)", v4:=eventid[4]);
END_IF;
 
shockcfg.threshold := 0.1;
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.2;
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:=logger[1], reset:=TRUE);
DebugFmt(message:="msLoggerStart (rc=\1) reset=TRUE", v1:=rc);
BEGIN
  WHILE running DO
    Sleep(delay:=1000);
  END_WHILE;
  rc := msEventUnregister(event:=shockid);
  DebugFmt(message:="msEventUnregister shock (rc=1)", v1:=rc);
  rc := msEventUnregister(event:=accelid);
  DebugFmt(message:="msEventUnregister accel (rc=1)", v1:=rc);
  rc := msClose();
  IF rc = 1 THEN
    DebugMsg(message:="Done");
  ELSE
    DebugFmt(message:="msClose (rc=\1)", v1:=rc);
  END_IF;
  EXIT;
END;
END_PROGRAM;