⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagequeue.c

📁 嵌入式VxWorks开发所需典型例程源代码
💻 C
字号:
/* tdemo.c - demonstration program */

#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "msgQLib.h"
#include "taskLib.h"
#include "math.h"



#define PI	(3.1416)
#define WAIT_TIME 10
#define MSG_TIME_OUT 20


int MAX_CNT = 6000; /* change to macro after tuned */
#ifndef DELAY_MILLISECONDS
int DELAY_MILLISECONDS = 10;
#endif 

float		angle, sineVal, *nxtval, outputVal, val = 0;
MSG_Q_ID	dataQId;
int		tidProducer;
int		tidConsumer;
int		tDemoPrintFlag = 0;


STATUS		consumer();
STATUS		producer();


/*
 * tDemoStart() - start tdemo
 */



STATUS tDemoStart(int count) 
{
	


    /* Initialize Message Queue (dataQId) */
    dataQId = msgQCreate(4, sizeof(float *), MSG_Q_PRIORITY);

    /* Spawn Producer Task */
    tidProducer = taskSpawn("tProducer", 80, 0, 2000, (FUNCPTR)producer,
	      0,0,0,0,0,0,0,0,0,0);
    
    /* Spawn Consumer Task */
    tidConsumer = taskSpawn("tConsumer", 80, 0, 2000, (FUNCPTR)consumer,
	      0,0,0,0,0,0,0,0,0,0);
    
    return (OK);
}


/*
 * producer() - producer task
 */
STATUS producer (void) 
{
    register int	i = 0;
    float		*buf;


    FOREVER {
	/* Allocate Data Buffer */
	buf=malloc(10);
	if (i++ < 50)
	    angle = (float)((2 * PI) * (i / 50.0));
	else
	    angle = (float)(i = 0);

	/* Generate Sine Wave Data */
	*buf = sineVal = sin((double)angle);

	/* Send Pointer to Data through Message Queue */
	if (msgQSend(dataQId, (char *)&buf, sizeof(float *),
	       NO_WAIT, MSG_PRI_NORMAL) != OK)
	    	    printf("tProducer: msgQSend error\n");

	    
        free(buf);
        taskDelay(WAIT_TIME); /* wait a few milliseconds */

    }
}


/*
 * consumer() - consumer task
 */
STATUS consumer(void)
{
    FOREVER {
	/* Read Message Queue */
	tDemoPrintFlag = msgQReceive(dataQId, (char *)&nxtval, sizeof(float *),
		    MSG_TIME_OUT);

/*	taskSuspend(0);*/
	if (tDemoPrintFlag!=ERROR)
	    printf("Consumer: received a message!! (val=%f)\n", val);
	else printf("Msg time out\n");

	/* Condition Data */
	outputVal = (val * 0.85);

	/* Save Data */
	val = *nxtval;

    }
}

/*
 * tDemoStop() - Stop tdemo
 */
STATUS tDemoStop() 
{

    /* delete tasks */
    taskDelete (tidProducer);
    taskDelay(WAIT_TIME); /* just to make sure producer is done producing */
    taskDelete (tidConsumer);
    
return(OK);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -