| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #define LOG_TAG "control"
- #include "sys_config.h"
- #include <board.h>
- extern uint32_t month;
- extern uint32_t day;
- extern uint32_t year;
- extern uint32_t hour;
- extern uint32_t min;
- extern uint32_t sec;
- /*
- 控制的参数有阈值,时间范围,使能标志等
- 同时上述参数要能保存在flash中,开机读取,便于重启后重新运行
- 阈值与定时谁优先?
- */
- #define output0 GET_PIN(E, 2)
- #define output1 GET_PIN(E, 3)
- #define input0 GET_PIN(D, 14)
- #define input1 GET_PIN(D, 15)
- //阈值参数及定时参数初始化
- Threshold_Contol S_Threshold_Contol[Number_JK] = {{0, 0, 32767, 32767, 255},{1, 0, 32767, 32767, 255}};
- Timing_Contol S_Timing_Contol[Number_JK] = {{0, 20, 4 },{1, 21, 2 }};
- JK_Status S_JK_Status[Number_JK] = {{0, 0},{1, 0}};
- //阈值/定时控制选择,0:阈值控制,1:定时控制,3:手动控制,默认手动控制
- uint8_t Control_Option = 3;
- //控制IO输出状态
- void CONTROL_OUTPUT(uint8_t jk, uint8_t ctr)
- {
- if(jk == 0)
- {
- if(ctr == 0)
- rt_pin_write(output0, PIN_LOW);
- else if(ctr == 1)
- rt_pin_write(output0, PIN_HIGH);
- else return;
-
- }
- else if(jk == 1)
- {
- if(ctr == 0)
- rt_pin_write(output1, PIN_LOW);
- else if(ctr == 1)
- rt_pin_write(output1, PIN_HIGH);
- else return;
- }
- else return;
- }
- //读取输出状态
- //num: 输出点位号 (取值范围0、1)
- void READ_OUTPUT_STATUS(void)
- {
- S_JK_Status[0].status = rt_pin_read(output0);
- S_JK_Status[1].status = rt_pin_read(output1);
- }
- //读取输入引脚的电平,每次只能读取一个引脚的电平
- //num: 引脚取值范围 0~1
- //ret: 输入脚的电平值,这里取反值,和硬件电路设计有关,
- // 这样输入值等于读取值
- uint8_t READ_INTPUT_STATUS(uint8_t num)
- {
- if(num == 0)
- return !rt_pin_read(input0);
- else if(num ==1)
- return !rt_pin_read(input1);
- else return 255;
- }
- /*定时控制处理*/
- /*输入参数:通道号,定时开始时间,定时工作时长*/
- /*返回控制结果,1,0*/
- void Timing_Contol_Handle(uint32_t jk ,uint16_t starttime,uint16_t duration)
- {
- if((starttime == 255) || (duration == 32767)) return ;//定时未启用
- if(duration > 12)return ;//定时时长不可超过12小时
- if(starttime <= 23){ //检查起始时间是否在24小时范围,否则返回错误
-
- if((starttime + duration) > 23 ) { //工作时间跨越午夜0时
- duration = starttime + duration - 24;//取得午夜0时后的停止工作时间
- if( ((hour >= starttime)&&(hour <= 23))||(hour < duration )){ //满足0时前条件/满足0后条件
- //判断当前时间在0时前及0时候是否落在延时范围内,是则通道置一
- CONTROL_OUTPUT(jk, 1);
- }
- else { //不在延时范围内,通道置零
- CONTROL_OUTPUT(jk, 0);
- }
- return ;
- }
- else { //工作时间不跨越0时
- if( (hour >= starttime)&&(hour < starttime + duration) ){ //判断当前时间在0时前及0时候是否落在延时范围内,是则通道置一
- CONTROL_OUTPUT(jk, 1);
- }
- else if (hour >= starttime + duration){ //大于等于起始时间+工作时长(小时),通道置零
- CONTROL_OUTPUT(jk, 0);
- }
- return ;
- }
- }
- else return ; //参数超过范围返回0
- }
- //输出通道定时控制扫描
- void Timing_Contol_Run(void)
- {
- Timing_Contol *pStruct;
- pStruct = S_Timing_Contol;
- int index;
- for(index=0; index < Number_JK; index ++, pStruct ++)
- {
- Timing_Contol_Handle(pStruct->JK, pStruct->StartTime, pStruct->Duration);
- rt_thread_mdelay(500);
- }
- }
- /*阈值控制处理*/
- /*输入参数:通道号,关联传感器通道,阈值上限,阈值下限,控制方式*/
- /*返回控制结果,1,0*/
- void Threshold_Contol_Handle(uint32_t jk, uint32_t ekey, uint32_t upper,uint32_t lower, uint32_t method)
- {
- if((upper == 32767) || (lower == 32767) ||(method == 255)) return ;//阈值不生效
-
- uint16_t temp = 0;
-
- /*相应通道在线才能进行控制操作,不在线直接关闭跳出*/
- if(sensor[ekey].online_flag != 0)
- temp = atoi(sensor[ekey].eValue);
- else
- {
- CONTROL_OUTPUT(jk, 0);
- return;
- }
- if(method == 0)
- {
- if((temp > upper) && (temp < lower))CONTROL_OUTPUT(jk, 1);
- else CONTROL_OUTPUT(jk, 0);
- }
- else if(method == 1)
- {
- if((temp > upper) && (temp < lower))CONTROL_OUTPUT(jk, 0);
- else if(temp < lower)CONTROL_OUTPUT(jk, 1);
- }
- else return ;
- }
- //输出通道阈值控制扫描
- void Threshold_Contol_Run(void)
- {
- Threshold_Contol *pStruct;
- pStruct = S_Threshold_Contol;
- int index;
- for(index=0; index < Number_JK; index ++, pStruct ++)
- {
- Threshold_Contol_Handle(pStruct->JK, pStruct->eKey, pStruct->upper, pStruct->lower, pStruct->method);
- rt_thread_mdelay(300);
- }
- }
- static void iocontrol_thread_entry(void *parameter)
- {
- rt_pin_mode(output0, PIN_MODE_OUTPUT);
- rt_pin_mode(output1, PIN_MODE_OUTPUT);
- rt_pin_mode(input0, PIN_MODE_INPUT_PULLDOWN);
- rt_pin_mode(input1, PIN_MODE_INPUT_PULLDOWN);
- while(1)
- {
- if(Control_Option == 1)
- Timing_Contol_Run();
- else if(Control_Option == 0)
- Threshold_Contol_Run();
- else return;
- }
- }
- // IO控制线程线程
- static int iocontrol_thread(int argc, char *argv[])
- {
- rt_err_t ret = RT_EOK;
- /* 创建 serial 线程 */
- rt_thread_t thread1 = rt_thread_create("io_ctr", iocontrol_thread_entry, RT_NULL, 256, 17, 10);
- /* 创建成功则启动线程 */
- if (thread1 != RT_NULL)
- {
- rt_thread_startup(thread1);
- }
- else
- {
- ret = RT_ERROR;
- }
- return ret;
- }
- INIT_APP_EXPORT(iocontrol_thread);
|