2,204
社区成员
发帖
与我相关
我的任务
分享
#include "cgi_head.h"
int msgid;
char *radio[]={"led_on","led_off","beep_on","beep_off","mpu6050"};
int radio_serult;
void shm_read(void)
{
kill(sh->pid,SIGUSR1);
fprintf(cgiOut,"gyro x=%d,gyro y=%d,gyro z=%d\n",sh->data.gyro.x,sh->data.gyro.y,sh->data.gyro.z);
fprintf(cgiOut,"accel x=%d,accel y=%d,accel z=%d\n",sh->data.accel.x,sh->data.accel.y,sh->data.accel.z);
fprintf(cgiOut,"temp=%d\n",sh->data.temp);
}
void cmd_send(struct msg info)
{
int ret;
fprintf(cgiOut,"已选中%s,cmd=%d\n",radio[radio_serult],info.text[0]);
ret=msgsnd(msgid,&info,sizeof(info)-sizeof(info.type),0);
if(ret<0)
{
fprintf(cgiOut,"msgsnd fail\n");
exit(EXIT_FAILURE);
}
}
int cgiMain(void)
{
key_t key;
int ret;
struct msg info;
cgiHeaderContentType("text/html\n");
fprintf(cgiOut,"<html><title>welcome to page2</title>\n");
fprintf(cgiOut,"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
fprintf(cgiOut,"<body>\n");
key=ftok("/www",'a');
msgid=msgget(key,IPC_CREAT|0666);
info.type=100;
if(msgid==-1)
{
fprintf(cgiOut,"msgget fail\n");
exit(EXIT_FAILURE);
}
if(cgiFormRadio("radio",radio,5,&radio_serult,0)==cgiFormSuccess)
{
switch(radio_serult)
{
case 0:
info.text[0]=1;
cmd_send(info);
break;
case 1:
info.text[0]=2;
cmd_send(info);
break;
case 2:
info.text[0]=3;
cmd_send(info);
break;
case 3:
info.text[0]=4;
cmd_send(info);
break;
default:
fprintf(cgiOut,"hello world\n");
if(shm_create()!=0)
{
fprintf(cgiOut,"shm_create\n");
exit(EXIT_FAILURE);
}
shm_read();
break;
}
}
fprintf(cgiOut,"</body>\n</html>\n");
return 0;
}
#include "head.h"
void handler(int sig)
{
}
void *pthread_for_mpu6050(void *arg)
{
key_t key;
pid_t shmpid;
int shmid;
int fd;
struct shared *sh;
union mpu6050_data msg;
key=ftok("/www",500);
signal(SIGUSR1,handler);
if(key==-1)
{
perror("shm ftok");
exit(EXIT_FAILURE);
}
shmid=shmget(key,64,0666|IPC_CREAT|IPC_EXCL);
if(shmid<0)
{
if(errno==EEXIST)
{
shmid=shmget(key,sizeof(struct shared),0666);
}
else
{
perror("shmget");
exit(EXIT_FAILURE);
}
}
printf("shmid=%d\n",shmid);
if((void *)-1==(sh=(struct shared *)shmat(shmid,NULL,0)))
{
perror("shmat");
exit(EXIT_FAILURE);
}
printf("-----------------------\n");
printf("sh=%p\n",sh);
sh->pid=getpid();
printf("sh->pid=%d\n",sh->pid);
// pause();
printf("***********************\n");
#if 1
sh->data.gyro.x=msg.gyro.x=10;
sh->data.gyro.y=msg.gyro.y=10;
sh->data.gyro.z=msg.gyro.z=10;
sh->data.accel.x=msg.accel.x=20;
sh->data.accel.y=msg.accel.y=20;
sh->data.accel.z=msg.accel.z=20;
sh->data.temp=msg.temp=30;
// fd=open("/dev/mpu6050",O_RDWR);
// while(1)
// {
//写shm
// ioctl(fd,GET_GYRO,&(sh->data));
printf("gyro x=%d,gyro y=%d,gyro z=%d\n",sh->data.gyro.x,sh->data.gyro.y,sh->data.gyro.z);
sleep(1);
// ioctl(fd,GET_ACCEL,&(sh->data));
printf("accel x=%d,accel y=%d,accel z=%d\n",sh->data.accel.x,sh->data.accel.y,sh->data.accel.z);
sleep(1);
// ioctl(fd,GET_TEMP,&(sh->data));
printf("temp=%d\n",sh->data.temp);
sleep(1);
// pause();
// }
#endif
}