Files
checker_m4/source/main/mymain.c

285 lines
5.3 KiB
C
Raw Normal View History

2023-07-14 18:52:42 +08:00
#include "mymain.h"
#include "main.h"
#include "board.h"
#include "debug.h"
#include "core_delay.h"
#include "string.h"
#include "stdio.h"
typedef struct{
pwm_def *pwm;
int key_old;
int key;
int sen;
char str_buff[200];
}mymain_def;
static mymain_def g_m={0};
void *mymain_init(void)
{
delay_init();
// m.pwm=dev_get("pwm");
// m.pwm->init(m.pwm);
//DBG_LOG("pwm start.");
// m.pwm->start(m.pwm,1);
GPIO_InitTypeDef init={0};
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
init.Mode = GPIO_MODE_OUTPUT_PP;
init.Pull = GPIO_PULLUP;
init.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
PERIPH_LOCK(GPIOF);
HAL_GPIO_Init(GPIOF, &init);
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9,GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOF);
// <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_6;
PERIPH_LOCK(GPIOI);
HAL_GPIO_Init(GPIOI, &init);
PERIPH_UNLOCK(GPIOI);
// <20><><EFBFBD><EFBFBD>
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_3;
PERIPH_LOCK(GPIOG);
HAL_GPIO_Init(GPIOG, &init);
PERIPH_UNLOCK(GPIOG);
// <20><><EFBFBD><EFBFBD>
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_1|GPIO_PIN_6;
PERIPH_LOCK(GPIOE);
HAL_GPIO_Init(GPIOE, &init);
PERIPH_UNLOCK(GPIOE);
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_8|GPIO_PIN_9;
PERIPH_LOCK(GPIOH);
HAL_GPIO_Init(GPIOH, &init);
PERIPH_UNLOCK(GPIOH);
return &g_m;
}
void send_str_to_a7(const char *str);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1=PF9;2=PF8;3=PF7
void out_set(int index,int power)
{
mymain_def *m=&g_m;
PERIPH_LOCK(GPIOF);
HAL_GPIO_WritePin(GPIOF,1<<(10-index),power?GPIO_PIN_SET:GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOF);
sprintf(m->str_buff,"out_set:%d,%d",index,power);
send_str_to_a7(m->str_buff);
}
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void moter_plused(int us)
{
// out3
//HAL_GPIO_TogglePin(GPIOF,1<<7);
// out1
HAL_GPIO_TogglePin(GPIOF,1<<9);
delay_us(us);
}
// PF7-PWM PF8-DST PI6_STOP
// <20>͵<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>
void moter_run(int pwm,int count)
{
int use_sen=0;
if(count>0){
HAL_GPIO_WritePin(GPIOF,1<<8,GPIO_PIN_RESET);
}else{
HAL_GPIO_WritePin(GPIOF,1<<8,GPIO_PIN_SET);
count=-count;
use_sen=1;
}
int max_=500;
int min_=35;
int temp=max_;
int add_count=0;
int step_sub=0;
for(int i=0;i<count;i++)
{
//<2F>ѵ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>˳<EFBFBD>
if((HAL_GPIO_ReadPin(GPIOI,1<<6)==RESET)&&(use_sen!=0)) return;
moter_plused(temp);
if(pwm==0){
// pwm==0ʱ <20>Զ<EFBFBD><D4B6>Ӽ<EFBFBD><D3BC><EFBFBD>
if(i<count/2){
if(temp>min_) {step_sub+=temp; if(step_sub>=1000) {temp--; step_sub=0;}}
else add_count++;
}else{
if(add_count>0) add_count--;
else {step_sub+=temp; if(step_sub>=1000) {temp++; step_sub=0;}}
}
}else{
// pwm!=0ʱ <20><><EFBFBD><EFBFBD>
temp=1000000/pwm;
}
}
// out3
//HAL_GPIO_WritePin(GPIOF,1<<7,GPIO_PIN_SET);
// out1
HAL_GPIO_WritePin(GPIOF,1<<9,GPIO_PIN_SET);
}
static int str_ainttoi(const char *s)
{
int ret=0;
int sig=1;
if(*s=='-'){
s++;
sig=-1;
}
while(*s)
{
if(*s>='0'&&*s<='9')
{
ret*=10;
ret+=*s-'0';
}
else return ret*sig;
s++;
}
return ret*sig;
}
void mymain_scan(void *mymain)
{
mymain_def *m=mymain;
m->key=HAL_GPIO_ReadPin(GPIOG,1<<3);
m->sen=HAL_GPIO_ReadPin(GPIOI,1<<6);
if(m->key_old!=m->key){
if(m->key==RESET)
{
send_str_to_a7("key press");
}else
{
send_str_to_a7("key up");
}
m->key_old=m->key;
}
}
int read_sw(void *mymain)
{
int ret=0;
ret|=HAL_GPIO_ReadPin(GPIOE,1<<6);
ret|=HAL_GPIO_ReadPin(GPIOH,1<<9)<<1;
ret|=HAL_GPIO_ReadPin(GPIOE,1<<1)<<2;
ret|=HAL_GPIO_ReadPin(GPIOH,1<<8)<<3;
return ret;
}
void mymain_loop(void *mymain,char *cmd_str)
{
mymain_def *m=mymain;
if(strncmp(cmd_str,"moter ",6)==0)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD>
int count=str_ainttoi(cmd_str+6);
if(count!=0){
send_str_to_a7(m->str_buff);
moter_run(0,count);
if(count>0){
send_str_to_a7("moter down");
}else{
send_str_to_a7("moter up");
}
}
}else if(strncmp(cmd_str,"moterinit",9)==0)
{
// <20><>ʼ<EFBFBD><CABC>
moter_run(2000,-25000);
send_str_to_a7("moterinited");
}
else if(strncmp(cmd_str,"motertest ",10)==0)
{
// <20><>ʼ<EFBFBD><CABC>
int count=str_ainttoi(cmd_str+10);
moter_run(2000,count);
send_str_to_a7("motertest end");
}
else if(strncmp(cmd_str,"readsw",6)==0)
{
int sw=read_sw(mymain);
sprintf(m->str_buff,"readsw %d",sw);
send_str_to_a7(m->str_buff);
}
else if(strncmp(cmd_str,"readkey",7)==0){
if(m->key)
send_str_to_a7("key up");
else
send_str_to_a7("key press");
}
else if(strncmp(cmd_str,"readsen",7)==0){
// <20><><EFBFBD><EFBFBD>
if(m->sen)
send_str_to_a7("sen high");
else
send_str_to_a7("sen low");
}
else if(strncmp(cmd_str,"out",3)==0){
if((cmd_str[3]>='1')&&(cmd_str[3]<='3')){
if(strncmp(cmd_str+5,"on",2)==0){
out_set(cmd_str[3]-'0',1);
}else if(strncmp(cmd_str+5,"off",3)==0){
out_set(cmd_str[3]-'0',0);
}
}
}
}
void mymain_loop__(void *mymain,char *cmd_str)
{
while(1){
HAL_GPIO_TogglePin(GPIOF,1<<7);
HAL_GPIO_TogglePin(GPIOF,1<<8);
HAL_GPIO_TogglePin(GPIOF,1<<9);
HAL_Delay(2000);
}
}