.

     

MPI Application Template
template.c
 

notify1.c -- Create notify object and wait for events.
/* notify1.c */

/* Copyright(c) 1991-2006 by Motion Engineering, Inc.  All rights reserved.
 *
 * This software  contains proprietary and  confidential information  of
 * Motion Engineering Inc., and its suppliers.  Except as may be set forth
 * in the license agreement under which  this software is supplied, use,
 * disclosure, or  reproduction is prohibited without the prior express
 * written consent of Motion Engineering, Inc.
 */

/*

:Create notify object and wait for events.

Create a thread that uses a notify object to wait for user-defined events
 passed to the notify object from the main thread.

Warning!  This is a sample program to assist in the integration of an
 MEI motion controller with your application.  It may not contain all
 of the logic and safety features that your application requires.

*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "stdmpi.h"
#include "stdmei.h"
#include "apputil.h"

#if defined(ARG_MAIN_RENAME)
#define main    notify1Main

argMainRENAME(main, notify1)
#endif

/* Command line arguments and defaults */

Arg argList[] = {
    {   NULL,       ArgTypeINVALID, NULL,   }
};

Static  long    NotifyWaitAwake;

Static long
    notifyWait(MPINotify notify);

int
    main(int    argc,
         char   *argv[])
{
    MPIControl  control;        /* Motion controller handle */
    MPINotify   notify;         /* Event notification object */

    MPIEventMask    eventMask;

    long    returnValue;    /* Return value from library */

    long    eventDone;

    MPIControlType      controlType;
    MPIControlAddress   controlAddress;

    long    argIndex;

    Thread          thread;
    ThreadStatus    status;

    argIndex =
        argControl(argc,
                   argv,
                   &controlType,
                   &controlAddress);

    /* Parse command line for application-specific arguments */
    while (argIndex < argc) {
        long    argIndexNew;

        argIndexNew = argSet(argList, argIndex, argc, argv);

        if (argIndexNew <= argIndex) {
            argIndex = argIndexNew;
            break;
        }
        else {
            argIndex = argIndexNew;
        }
    }

    /* Check for unknown/invalid command line arguments */
    if (argIndex < argc) {
        mpiPlatformConsole("usage: %s %s\n",
                            argv[0],
                            ArgUSAGE);
        exit(MPIMessageARG_INVALID);
    }

    /* Create motion controller object */
    control =
        mpiControlCreate(controlType,
                         &controlAddress);
    msgCHECK(mpiControlValidate(control));

    /* Initialize motion controller */
    returnValue = mpiControlInit(control);
    msgCHECK(returnValue);

    /* Request notification of ALL events */
    mpiEventMaskCLEAR(eventMask);
    mpiEventMaskALL(eventMask);

    /* Create event notification object */
    notify =
        mpiNotifyCreate(eventMask,
                        NULL);
    msgCHECK(mpiNotifyValidate(notify));

    if (returnValue == MPIMessageOK) {
        thread = threadCreate(NULL);

        returnValue = threadValidate(thread);
    }

    if (returnValue == MPIMessageOK) {
        returnValue =
            threadStart(thread,
                        (THREAD_FUNCTION)notifyWait,
                        notify);
    }

    eventDone = FALSE;

    while ((returnValue == MPIMessageOK) &&
           (eventDone == FALSE)) {
        char    buffer[128];
        long    index;
        long    haveEventNumber;

        /* Wait for notifyWait() to call mpiNotifyEventWait() */
        while (NotifyWaitAwake != FALSE) {
            mpiPlatformSleep(1);
        }

        /* Make sure notifyWait() thread is still executing */
        returnValue =
            threadStatus(thread,
                         &status);

        if (returnValue != MPIMessageOK) {
            break;
        }

        if (status.active == FALSE) {
            break;
        }

        mpiPlatformConsole("Enter an event number, or ESC to exit ...\n");

        memset(buffer, 0, sizeof(buffer));
        index = 0;

        haveEventNumber = FALSE;
        while (haveEventNumber == FALSE) {
            MPIEventStatus  eventStatus;

            long    key;
            long    eventNumber = 0;

            key = mpiPlatformKey(MPIWaitFOREVER);


            if (key <= 0) {
                mpiPlatformKey(MPIWaitFOREVER);
                continue;
            }

            switch (key) {
                case 0x1b: {    /* ESC */
                    eventNumber = key;
                    haveEventNumber = TRUE;
                    eventDone = TRUE;
                    break;
                }
                case '\r':
                case '\n': {
                    mpiPlatformConsole("\n");

                    eventNumber = mpiPlatformAtol(buffer);

                    haveEventNumber = TRUE;
                    break;
                }
                case '\b': {
                    if (index > 0) {
                        mpiPlatformConsole("\b \b");
                        buffer[--index] = '\0';
                    }
                    break;
                }
                case '-': {
                    if (index == 0) {
                        buffer[index++] = (char)key;
                        mpiPlatformConsole("%c", key);
                    }
                    break;
                }
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9': {
                    buffer[index++] = (char)key;
                    mpiPlatformConsole("%c", key);
                    break;
                }
                default: {
                    break;
                }
            }

            if (haveEventNumber != FALSE) {
                NotifyWaitAwake = TRUE;

                eventStatus.type    = MPIEventTypeEXTERNAL;
                eventStatus.source  = NULL;
                eventStatus.info[0] = eventNumber;

                returnValue =
                    mpiNotifyEventWake(notify,
                                       &eventStatus);
            }
        }
    }

    if (thread != NULL) {
        while (returnValue == MPIMessageOK) {
            returnValue =
                threadStatus(thread,
                             &status);
            msgCHECK(returnValue);

            if (status.active == FALSE) {
                returnValue = status.returnValue;
                break;
            }

            mpiPlatformSleep(1);
        }

        returnValue = threadDelete(thread);
        msgCHECK(returnValue);
    }

    fprintf(stderr,
            "%s exiting: returnValue 0x%x: %s\n",
            argv[0],
            returnValue,
            mpiMessage(returnValue, NULL));

    returnValue = mpiNotifyDelete(notify);
    msgCHECK(returnValue);

    returnValue = mpiControlDelete(control);
    msgCHECK(returnValue);

    return ((int)returnValue);
}

Static long
    notifyWait(MPINotify notify)
{
    long    returnValue;

    MPIWait timeout;
    long    eventDone;

    /* Validate the notify object and its handle */
    returnValue = mpiNotifyValidate(notify);

    timeout = MPIWaitFOREVER;

    eventDone = FALSE;

    while ((returnValue == MPIMessageOK) &&
           (eventDone == FALSE)) {
        MPIEventStatus  eventStatus;

        NotifyWaitAwake = FALSE;

        /* Wait for notify event */
        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               timeout);

        mpiPlatformConsole("mpiNotifyEventWait(0x%x, 0x%x, %d) returns 0x%x: %s\n",
                            notify,
                            &eventStatus,
                            timeout,
                            returnValue,
                            mpiMessage(returnValue, NULL));

        switch (returnValue) {
            case MPIMessageOK: {
                mpiPlatformConsole("\teventStatus: type %d number %d\n",
                                    eventStatus.type,
                                    eventStatus.info[0]);

                if (eventStatus.info[0] == 0x1b) {
                    eventDone = TRUE;
                }
                break;
            }
            case MPIMessageTIMEOUT: {
                mpiPlatformConsole("\tTIMEOUT\n");
                break;
            }
            default: {
                break;
            }
        }
    }

    mpiPlatformConsole("notifyWait(0x%x) returning 0x%x: %s\n",
                        notify,
                        returnValue,
                        mpiMessage(returnValue, NULL));

    return (returnValue);
}


      
       Legal Notice  |  Tech Email  |  Feedback
      
Copyright ©
2001-2009 Motion Engineering