Files
coder_stm32f1/source/task/input.c

187 lines
3.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "board.h"
#include "rtthread.h"
#include "debug.h"
#include "string.h"
#include "dev_flash.h"
#include "prot_uc.h"
#include "input.h"
#include "mystdlib.h"
#include "transmit.h"
/*
读取所有输入通道
*/
// 定义输入通道数
#define INPUT_CHANNEL_NUM 10
typedef struct{
gpioin_def *dev;
int input;
int input_lock;
int turn_true_call_always;// 为0时单次回调为1时永久回调
int turn_false_call_always;// 同上
void (*turn_true_to_call)(void *turn_true_obj);
void (*turn_false_to_call)(void *turn_false_obj);
void *turn_true_obj;
void *turn_false_obj;
}input_def;
// 判断此通道状态是否发生了翻转
// 0翻转为false1翻转为true-1未翻转
static int input_check_state(input_def *s)
{
s->input=(s->dev->state(s->dev));
if(s->input!=s->input_lock){
DBG_LOG("%s=%d",s->dev->name, s->input);
s->input_lock=s->input;
if((s->input)==0){
return 0;
}else{
return 1;
}
}else{
return -1;
}
}
// 根据输入通道的状态调用回调函数
static void input_run(input_def *s)
{
int state=input_check_state(s);
if(state==1)
{
if(s->turn_true_to_call)
s->turn_true_to_call(s->turn_true_obj);
irq_disable();
if(s->turn_true_call_always==0)
{
s->turn_true_to_call=0;
s->turn_true_obj=0;
}
irq_enable();
}else if(state==0)
{
if(s->turn_false_to_call)
s->turn_false_to_call(s->turn_false_obj);
irq_disable();
if(s->turn_false_call_always==0)
{
s->turn_false_to_call=0;
s->turn_false_obj=0;
}
irq_enable();
}
}
typedef struct{
int inited;
int run;
void *tran;
input_def input[INPUT_CHANNEL_NUM];
}self_def;
static self_def g_self;
static void input_thread(void *arg)
{
self_def *s=arg;
char gpioin_name[]="gpioin0";
for(int i=0;i<INPUT_CHANNEL_NUM;i++)
{
gpioin_name[6]='0'+i;
s->input[i].dev=dev_get(gpioin_name);
s->input[i].dev->init(s->input[i].dev);
}
while (s->run)
{
rt_thread_mdelay(20);
for(int i=0;i<INPUT_CHANNEL_NUM;i++)
{
input_run(&s->input[i]);
}
}
}
static int init_thread(void)
{
self_def *s=&g_self;
s->inited=1;
s->run=1;
rt_thread_t rt_t=rt_thread_create("input_t",input_thread,s,1024,15,20);
rt_thread_startup(rt_t);
return 0;
}
app_init_export(init_thread)
static int input_set_callback(int always,int channel,int state_to_call,void (*fun)(void *t),void *t)
{
self_def *s=&g_self;
input_def *in=0;
if(channel<0||channel>=INPUT_CHANNEL_NUM)
return -1;
irq_disable();
in=&s->input[channel];
if(state_to_call){
in->turn_true_to_call=fun;
in->turn_true_obj=t;
in->turn_true_call_always=always;
}else{
in->turn_false_to_call=fun;
in->turn_false_obj=t;
in->turn_false_call_always=always;
}
irq_enable();
return 0;
}
// 设置永久回调函数
// channel:通道(0~9);state_to_call:回调调用时的输入状态;fun:回调函数
// 返回0成功
int input_set_callback_always(int channel,int state_to_call,void (*fun)(void *t),void *t)
{
return input_set_callback(1,channel,state_to_call,fun,t);
}
// 设置单次回调函数
int input_set_callback_once(int channel,int state_to_call,void (*fun)(void *t),void *t)
{
return input_set_callback(0,channel,state_to_call,fun,t);
}