项目中硬件模块的开关状态,由独立的状态线程进行控制管理。
#ifndef _SL2000_SW_IPC_H
#define _SL2000_SW_IPC_H
void* sw_pro(void* arg);//状态更新线程
void area_sw_update(void);//更新区域状态
int dev_sw_update(struct sys_dev *head);//更新设备开关状态
#endif
#include "config.h"
#include "sw_pro.h"
extern struct sys_all g_dev[1]; //系统配置结构体全局变量
extern struct sys_dev *head_node; // 系统设备动态链表头指针
extern struct dev_fd g_dev_fd;//系统设备文件
extern int area_update_flag;
extern unsigned char net_cmd;
void* sw_pro(void* arg)
{
char light_da[2];
struct sys_dev* new_dev;
struct sys_dev* find_dev;
light_da[0] = 0;
new_dev = (struct sys_dev *)malloc(sizeof(struct sys_dev));
while(1){
//光感 处理
if(find_id(DEV_T_RAY,head_node,new_dev)==1){
if(g_dev_fd.light_fd>0){
read(g_dev_fd.light_fd,light_da,1);
}
if(new_dev->bind_dev>0 && new_dev->bind_dev<10)
if((find_dev = get_dev_id(new_dev->bind_dev,head_node)) != NULL)
find_dev->sw_sta=light_da[0];
}
if(area_update_flag ==1)
area_sw_update();
dev_sw_update(head_node);//灯光开关切换
sleep(1);
}
}
int dev_sw_update(struct sys_dev *head)
{
struct sys_dev * p;
p=head;
char da[8];
if(NULL==head){
printf("列表为空!\n");
return -1;
}
while(p!=NULL){
switch(p->node.type)
{
case DEV_T_LIGHT1:
if(p->sw_sta == 1){
da[0] = J1_ON;
}else{
da[0] = J1_OFF;
}
write(g_dev_fd.relay1_fd,da,1);
break;
case DEV_T_LIGHT2:
if(p->sw_sta == 1){
da[0] = J2_ON;
}else{
da[0] = J2_OFF;
}
write(g_dev_fd.relay2_fd,da,1);
break;
case DEV_T_LIG_NET:
if(p->sw_sta == 1){
net_cmd =0x31;
}else{
net_cmd = 0x32;
}
break;
default:
break;
}
p=p->next;
}
return 0;
}
//区域处理,可以使用信号
void area_sw_update(void)
{
int area_loop,dev_loop;
struct sys_area area_val;
struct sys_dev* find_dev;
for(area_loop=0;area_loop<MAX_DEV;area_loop++){
memcpy(&area_val,&(g_dev->sys_area[area_loop]),sizeof(struct sys_area));
if(area_val.enable_flag == 1)
if(area_val.sw_sta == 1){
for(dev_loop=0;dev_loop<MAX_DEV;dev_loop++){
if((find_dev = get_dev_id(area_val.dev_id[dev_loop],
head_node)) != NULL)
find_dev->sw_sta=ON;
}
}else{
for(dev_loop=0;dev_loop<MAX_DEV;dev_loop++){
if((find_dev = get_dev_id(area_val.dev_id[dev_loop],
head_node)) != NULL)
find_dev->sw_sta=OFF;
}
}
}
area_update_flag = 0;
}
原文:http://blog.51cto.com/91arm/2061738