实现电机,按键,虚拟串口,未验证

This commit is contained in:
ranchuan
2023-06-30 14:23:54 +08:00
parent edd3010a20
commit b15db16b91
14 changed files with 1130 additions and 368 deletions

109
source/task/commend.c Normal file
View File

@@ -0,0 +1,109 @@
#include "commend.h"
#include "string.h"
#include "mystring.h"
#include "bytearray.h"
//#include "mystdlib.h"
#include "debug.h"
#include "board.h"
#define CMD_RETURN_BUFF_SIZE 4096
typedef struct{
char *data;
}self_def;
static self_def g_self;
void *tappend(void *p,void *del);
static commend_def *cmd_find(char *name)
{
extern const int cmdstruct$$Base;
extern const int cmdstruct$$Limit;
commend_def *start=(commend_def *)&cmdstruct$$Base;
commend_def *end=(commend_def *)&cmdstruct$$Limit;
for(commend_def *t=start;t<end;t++)
{
if(strcmp(t->name,name)==0)
{
return t;
}
}
return 0;
}
void cmd_recv_slot(void *obj,const char *coder,uint8_t cmd,array_def *data,char *err_str)
{
self_def *s=&g_self;
commend_def *commd;
if((strcmp(err_str,"ok")!=0)||(cmd!=0)){
return;
}
list_def *argv=str_split(str_simplified((const char *)arr_data(data)),' ');
DBG_LOG("list_str=%s",str_temp(list_string(argv)));
commd=cmd_find(list_get_str(argv,0));
if(commd&&commd->fun) commd->fun(argv);
else cmd_print("unknown cmd of \"%s\"",list_get_str(argv,0));
}
static int cmd_init(void)
{
self_def *s=&g_self;
if(s->data==0){
s->data=calloc(CMD_RETURN_BUFF_SIZE,sizeof(char));
}
app_variable("cmd",s,0);
return 0;
}
app_init_export(cmd_init);
int cmd_print(const char *fmt,...)
{
self_def *s=&g_self;
if(s->data==0) return 0;
va_list args;
size_t length=0;
va_start(args, fmt);
length += vsnprintf(&s->data[length], CMD_RETURN_BUFF_SIZE - length - 1, fmt, args);
if (length > CMD_RETURN_BUFF_SIZE - 1)
length = CMD_RETURN_BUFF_SIZE - 1;
va_end(args);
memcpy(&s->data[length],"\r\n",2);
length+=2;
s->data[length]=0;
array_def *arr=arr_creat();
arr_appends(arr,s->data,length);
emit cmd_reply_signal(s,0,arr_temp(arr));
return length;
}
static int cmd_help(list_def *argv)
{
extern const int cmdstruct$$Base;
extern const int cmdstruct$$Limit;
commend_def *start=(commend_def *)&cmdstruct$$Base;
commend_def *end=(commend_def *)&cmdstruct$$Limit;
cmd_print("help ->");
for(commend_def *t=start;t<end;t++)
{
cmd_print("%-15s: %s",t->name,t->help);
}
return 0;
}
commend_export(help,cmd_help,"print the help str of cmds.")

49
source/task/commend.h Normal file
View File

@@ -0,0 +1,49 @@
#ifndef commend_h__
#define commend_h__
#include "list.h"
#include "bytearray.h"
#include "signal.h"
// 执行命令函数不能调用延时函数
typedef int (*cmd_fun_def)(list_def *argv /* str */);
typedef struct{
const char *name;
const char *help;
cmd_fun_def fun;
}commend_def;
#define commend_export(name_,fun_,help_) \
const static char __cmd_##name_##_name[] SECTION(".rodata.cmdstr") = #name_; \
const static char __cmd_##name_##_help[] SECTION(".rodata.cmdstr") = help_; \
RT_USED static commend_def _cmd_##name_ SECTION("cmdstruct")= \
{\
.name=__cmd_##name_##_name,\
.help=__cmd_##name_##_help,\
.fun=fun_,\
};
int cmd_print(const char *fmt,...);
signal cmd_reply_signal(void *obj,uint8_t cmd,array_def *data);
void cmd_recv_slot(void *obj,const char *codec_name,uint8_t cmd,array_def *data,char *err_str);
#endif

95
source/task/key.c Normal file
View File

@@ -0,0 +1,95 @@
#include "board.h"
#include "rtthread.h"
#include "debug.h"
#include "string.h"
#include "dev_flash.h"
#include "commend.h"
#include "key.h"
#include "mystdlib.h"
// 作为批检仪使用时读取按键
typedef struct{
int inited;
int run;
int input;
int input_lock;
void *tran;
}self_def;
static self_def g_self;
static void request_check(void *p);
static void key_thread(void *arg)
{
self_def *s=arg;
gpioin_def *in1;
in1=dev_get("key");
in1->init(in1);
s->input=0;
s->input_lock=1;
while (s->run)
{
rt_thread_mdelay(20);
s->input=(in1->state(in1));
if(s->input!=s->input_lock)
{
DBG_LOG("key=%08X",s->input);
s->input_lock=s->input;
if((s->input&1)==0)
{
// 发送按键按下消息
cmd_print("key pressed");
}
}
}
}
// 获取实时按键状态,1按下
int key_pressed(void)
{
self_def *s=&g_self;
return !s->input;
}
// cmd=0x37
static void request_check(void *p)
{
const sys_param_def *par=sys_param();
array_def *d=arr_creat();
emit key_send_signal(p,0x37,arr_temp(d));
//DBG_LOG("tcp liver:%s",str_temp(arr_string(d)));
}
static int init_thread(void)
{
if(strcmp(sys_param()->device_type,"checker")==0)
{
self_def *s=&g_self;
s->inited=1;
s->run=1;
rt_thread_t rt_t=rt_thread_create("key_t",key_thread,s,1024,15,20);
rt_thread_startup(rt_t);
DBG_LOG("key thread created.");
}
return 0;
}
app_init_export(init_thread)

28
source/task/key.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef key_h__
#define key_h__
#include "rtthread.h"
#include "signal.h"
#include "bytearray.h"
signal key_send_signal(void *obj,uint8_t cmd,array_def *data);
int key_pressed(void);
#endif

203
source/task/moter.c Normal file
View File

@@ -0,0 +1,203 @@
#include "moter.h"
#include "dev_flash.h"
#include "debug.h"
#include "commend.h"
#include "mymisc.h"
#include "board.h"
// <20><><EFBFBD><EFBFBD>״̬
typedef enum{
STOP=0,
INITING=1,
DOWNING=2,
UPING=3,
}moter_stat;
typedef struct{
int inited;
pwm_def *pwm;
rt_event_t event;
int run;
int want_count;
int count;
int max_count;
moter_stat stat;
void (*fun_in_end)(void *t);
void *t;
}self_def;
self_def g_self;
#define MOTER_MAX_COUNT 22000
#define MOTER_END_EVENT 0x1
static void end_irq(void *t)
{
self_def *s=t;
if(s->inited)
rt_event_send(s->event,MOTER_END_EVENT);
}
static void moter_run(void *t)
{
self_def *s=t;
uint32_t ev;
while(s->run)
{
rt_event_recv(s->event,0xffffffff,RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&ev);
if(ev&MOTER_END_EVENT)
{
if(s->stat==INITING)
{
s->count=0;
}else if(s->stat==DOWNING)
{
s->count+=s->want_count;
}else if(s->stat==UPING)
{
// <20><><EFBFBD><EFBFBD>ʱ s->want_count Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD>Ǽ<EFBFBD>
s->count+=s->want_count;
if(s->count<0) s->count=0;
}
s->stat=STOP;
DBG_LOG("moter stoped.");
emit moter_end_signal(s);
if(s->fun_in_end){
s->fun_in_end(s->t);
s->fun_in_end=0;
s->t=0;
}
}
ev=0;
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ص<EFBFBD>
void moter_down(void (*fun)(void *t),void *t)
{
self_def *s=&g_self;
if(fun){
s->fun_in_end=fun;
s->t=t;
}
moter_start(0,s->max_count-s->count);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ص<EFBFBD>
void moter_up(void (*fun)(void *t),void *t)
{
self_def *s=&g_self;
if(fun){
s->fun_in_end=fun;
s->t=t;
}
moter_start(0,-s->count-100);
}
void moter_start(int fre,int step_count)
{
self_def *s=&g_self;
if(s->inited==0)
{
DBG_WARN("moter object is not inited.");
return;
}
moter_start_slot(s,fre,step_count);
}
void moter_start_slot(void *t,int fre,int step_count)
{
self_def *s=t;
if(s->stat!=STOP)
{
DBG_WARN("moter is running!");
return ;
}
if(s->count+step_count>s->max_count)
{
DBG_WARN("steps count exceeded limit!,max=%d",s->max_count);
end_irq(s);
return ;
}
else{
DBG_LOG("s->count=%d,step_count=%d,s->max_count=%d",s->count,step_count,s->max_count);
}
if(step_count>0){
s->stat=DOWNING;
}else if(step_count<0){
s->stat=UPING;
}else{
DBG_WARN("step_count can not == 0");
return ;
}
s->want_count=step_count;
pwm_def *pwm=s->pwm;
pwm->set_fre(pwm,fre);
pwm->start(pwm,step_count);
}
void moter_init(void *t)
{
self_def *s=t;
DBG_LOG("moter init to zero.");
pwm_def *pwm=s->pwm;
pwm->set_fre(pwm,2000);
pwm->start(pwm,-22000);
}
int init_moter(void)
{
self_def *s=&g_self;
const sys_param_def *par=sys_param();
if(strcmp(par->device_type,"checker")==0){
pwm_def *pwm=dev_get("pwm1");
if(pwm==0){
cmd_print("can not find device \"pwm1\"");
return -1;
}
pwm->init(pwm);
pwm->set_irq_fun(pwm,end_irq,s);
s->pwm=pwm;
s->stat=INITING;
if((par->moter_max_count>25000)||(par->moter_max_count<1000))
{
s->max_count=MOTER_MAX_COUNT;
}else{
s->max_count=par->moter_max_count;
}
s->count=s->max_count;
s->event=rt_event_create("moter_e",RT_IPC_FLAG_FIFO);
s->inited=1;
s->run=1;
rt_thread_t rt_t=rt_thread_create("moter_t",moter_run,s,2048,3,20);
rt_thread_startup(rt_t);
later_execute(moter_init,s,500);
app_variable("moter",s,0);
}
return 0;
}
app_init_export(init_moter)

31
source/task/moter.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef moter_h__
#define moter_h__
#include "signal.h"
#include "bytearray.h"
#include "list.h"
#include "board.h"
signal moter_end_signal(void *m);
void moter_start_slot(void *t,int fre,int step_count);
void moter_start(int fre,int step_count);
void moter_down(void (*fun)(void *t),void *t);
void moter_up(void (*fun)(void *t),void *t);
#endif