diff --git a/.gitignore b/.gitignore index c9dd391..8e8005e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,6 @@ __pycache__/ *.pid *.pkt *.spec +*.csv python/build/ python/dist/ \ No newline at end of file diff --git a/checker_gen1.uvoptx b/checker_gen1.uvoptx index c139aca..4857ea5 100644 --- a/checker_gen1.uvoptx +++ b/checker_gen1.uvoptx @@ -120,7 +120,7 @@ 0 DLGUARM - d + 7 0 @@ -1044,6 +1044,18 @@ 0 0 + + 4 + 47 + 1 + 0 + 0 + 0 + .\source\interface\if_gpioin.c + if_gpioin.c + 0 + 0 + @@ -1054,7 +1066,7 @@ 0 5 - 47 + 48 1 0 0 @@ -1066,7 +1078,7 @@ 5 - 48 + 49 1 0 0 @@ -1078,7 +1090,7 @@ 5 - 49 + 50 1 0 0 @@ -1098,7 +1110,7 @@ 0 6 - 50 + 51 1 0 0 @@ -1110,7 +1122,7 @@ 6 - 51 + 52 1 0 0 @@ -1130,7 +1142,7 @@ 0 7 - 52 + 53 1 0 0 @@ -1142,7 +1154,7 @@ 7 - 53 + 54 2 0 0 @@ -1154,7 +1166,7 @@ 7 - 54 + 55 1 0 0 @@ -1166,7 +1178,7 @@ 7 - 55 + 56 1 0 0 @@ -1178,7 +1190,7 @@ 7 - 56 + 57 1 0 0 @@ -1190,7 +1202,7 @@ 7 - 57 + 58 1 0 0 @@ -1202,7 +1214,7 @@ 7 - 58 + 59 1 0 0 @@ -1214,7 +1226,7 @@ 7 - 59 + 60 1 0 0 @@ -1226,7 +1238,7 @@ 7 - 60 + 61 1 0 0 @@ -1238,7 +1250,7 @@ 7 - 61 + 62 1 0 0 @@ -1250,7 +1262,7 @@ 7 - 62 + 63 1 0 0 @@ -1262,7 +1274,7 @@ 7 - 63 + 64 1 0 0 @@ -1274,7 +1286,7 @@ 7 - 64 + 65 1 0 0 @@ -1286,7 +1298,7 @@ 7 - 65 + 66 1 0 0 @@ -1298,7 +1310,7 @@ 7 - 66 + 67 1 0 0 @@ -1310,7 +1322,7 @@ 7 - 67 + 68 1 0 0 @@ -1322,7 +1334,7 @@ 7 - 68 + 69 1 0 0 @@ -1334,7 +1346,7 @@ 7 - 69 + 70 1 0 0 @@ -1346,7 +1358,7 @@ 7 - 70 + 71 1 0 0 @@ -1366,7 +1378,7 @@ 0 8 - 71 + 72 1 0 0 @@ -1378,7 +1390,7 @@ 8 - 72 + 73 1 0 0 @@ -1390,7 +1402,7 @@ 8 - 73 + 74 1 0 0 @@ -1402,7 +1414,7 @@ 8 - 74 + 75 1 0 0 @@ -1422,7 +1434,7 @@ 0 9 - 75 + 76 1 0 0 @@ -1434,7 +1446,7 @@ 9 - 76 + 77 1 0 0 @@ -1446,7 +1458,7 @@ 9 - 77 + 78 1 0 0 @@ -1458,7 +1470,7 @@ 9 - 78 + 79 1 0 0 @@ -1470,7 +1482,7 @@ 9 - 79 + 80 1 0 0 @@ -1482,7 +1494,7 @@ 9 - 80 + 81 1 0 0 @@ -1494,7 +1506,7 @@ 9 - 81 + 82 1 0 0 @@ -1506,7 +1518,7 @@ 9 - 82 + 83 1 0 0 @@ -1518,7 +1530,7 @@ 9 - 83 + 84 1 0 0 @@ -1530,7 +1542,7 @@ 9 - 84 + 85 1 0 0 @@ -1542,7 +1554,7 @@ 9 - 85 + 86 1 0 0 @@ -1554,7 +1566,7 @@ 9 - 86 + 87 1 0 0 @@ -1566,7 +1578,7 @@ 9 - 87 + 88 1 0 0 @@ -1578,7 +1590,7 @@ 9 - 88 + 89 1 0 0 @@ -1590,7 +1602,7 @@ 9 - 89 + 90 1 0 0 @@ -1602,7 +1614,7 @@ 9 - 90 + 91 1 0 0 @@ -1614,7 +1626,7 @@ 9 - 91 + 92 1 0 0 @@ -1626,7 +1638,7 @@ 9 - 92 + 93 1 0 0 @@ -1638,7 +1650,7 @@ 9 - 93 + 94 1 0 0 @@ -1650,7 +1662,7 @@ 9 - 94 + 95 1 0 0 @@ -1662,7 +1674,7 @@ 9 - 95 + 96 1 0 0 @@ -1674,7 +1686,7 @@ 9 - 96 + 97 1 0 0 @@ -1686,7 +1698,7 @@ 9 - 97 + 98 1 0 0 @@ -1698,7 +1710,7 @@ 9 - 98 + 99 1 0 0 @@ -1710,7 +1722,7 @@ 9 - 99 + 100 1 0 0 @@ -1722,7 +1734,7 @@ 9 - 100 + 101 1 0 0 @@ -1734,7 +1746,7 @@ 9 - 101 + 102 1 0 0 @@ -1746,7 +1758,7 @@ 9 - 102 + 103 1 0 0 @@ -1758,7 +1770,7 @@ 9 - 103 + 104 1 0 0 @@ -1770,7 +1782,7 @@ 9 - 104 + 105 1 0 0 @@ -1782,7 +1794,7 @@ 9 - 105 + 106 1 0 0 @@ -1794,7 +1806,7 @@ 9 - 106 + 107 1 0 0 @@ -1806,7 +1818,7 @@ 9 - 107 + 108 1 0 0 @@ -1818,7 +1830,7 @@ 9 - 108 + 109 1 0 0 @@ -1830,7 +1842,7 @@ 9 - 109 + 110 1 0 0 @@ -1850,7 +1862,7 @@ 0 10 - 110 + 111 1 0 0 @@ -1862,7 +1874,7 @@ 10 - 111 + 112 1 0 0 @@ -1874,7 +1886,7 @@ 10 - 112 + 113 1 0 0 @@ -1886,7 +1898,7 @@ 10 - 113 + 114 1 0 0 @@ -1898,7 +1910,7 @@ 10 - 114 + 115 1 0 0 @@ -1910,7 +1922,7 @@ 10 - 115 + 116 1 0 0 @@ -1922,7 +1934,7 @@ 10 - 116 + 117 1 0 0 @@ -1934,7 +1946,7 @@ 10 - 117 + 118 1 0 0 @@ -1946,7 +1958,7 @@ 10 - 118 + 119 1 0 0 @@ -1958,7 +1970,7 @@ 10 - 119 + 120 1 0 0 @@ -1970,7 +1982,7 @@ 10 - 120 + 121 1 0 0 @@ -1990,7 +2002,7 @@ 0 11 - 121 + 122 1 0 0 @@ -2002,7 +2014,7 @@ 11 - 122 + 123 1 0 0 @@ -2014,7 +2026,7 @@ 11 - 123 + 124 1 0 0 @@ -2026,7 +2038,7 @@ 11 - 124 + 125 1 0 0 @@ -2038,7 +2050,7 @@ 11 - 125 + 126 1 0 0 @@ -2050,7 +2062,7 @@ 11 - 126 + 127 1 0 0 @@ -2062,7 +2074,7 @@ 11 - 127 + 128 1 0 0 @@ -2074,7 +2086,7 @@ 11 - 128 + 129 1 0 0 @@ -2086,7 +2098,7 @@ 11 - 129 + 130 1 0 0 @@ -2098,7 +2110,7 @@ 11 - 130 + 131 1 0 0 @@ -2110,7 +2122,7 @@ 11 - 131 + 132 1 0 0 @@ -2122,7 +2134,7 @@ 11 - 132 + 133 1 0 0 @@ -2134,7 +2146,7 @@ 11 - 133 + 134 1 0 0 @@ -2146,7 +2158,7 @@ 11 - 134 + 135 1 0 0 @@ -2158,7 +2170,7 @@ 11 - 135 + 136 1 0 0 @@ -2168,6 +2180,18 @@ 0 0 + + 11 + 137 + 1 + 0 + 0 + 0 + .\source\task\key.c + key.c + 0 + 0 + @@ -2178,7 +2202,7 @@ 0 12 - 136 + 138 1 0 0 @@ -2198,7 +2222,7 @@ 0 13 - 137 + 139 1 0 0 @@ -2210,7 +2234,7 @@ 13 - 138 + 140 1 0 0 @@ -2222,7 +2246,7 @@ 13 - 139 + 141 1 0 0 @@ -2242,7 +2266,7 @@ 0 14 - 140 + 142 5 0 0 diff --git a/checker_gen1.uvprojx b/checker_gen1.uvprojx index 0ab2bcd..e9e6aed 100644 --- a/checker_gen1.uvprojx +++ b/checker_gen1.uvprojx @@ -729,6 +729,11 @@ 1 .\source\interface\if_uart_dma.c + + if_gpioin.c + 1 + .\source\interface\if_gpioin.c + @@ -1311,6 +1316,11 @@ 1 .\source\task\mod_signals.c + + key.c + 1 + .\source\task\key.c + @@ -2129,6 +2139,11 @@ + + if_gpioin.c + 1 + .\source\interface\if_gpioin.c + @@ -4078,6 +4093,11 @@ 1 .\source\task\mod_signals.c + + key.c + 1 + .\source\task\key.c + diff --git a/python/checker_save.py b/python/checker_save.py new file mode 100644 index 0000000..d2bd656 --- /dev/null +++ b/python/checker_save.py @@ -0,0 +1,35 @@ + + + + +class save: + def __init__(self) -> None: + pass + def save(self,data:bytearray): + d=data[1:] + for i in range(20): + self.save_item(d[i*28:i*28+28]) + def save_item(self,d:bytearray): + s='=\"'+d[0:8].hex()+'\"'+',' + s+='=\"'+self.hex2bit(d[8:10])+'\"'+',' + s+=self.hex2int(d[10:]) + print(d[8:10].hex(' ')) + print(s) + with open("./file/save.csv","+a") as f: + f.write(s+'\n') + def hex2int(self,d:bytearray): + s="" + for i in range(len(d)//2): + s+=str(d[i*2]|(d[i*2+1]<<8))+',' + return s + def hex2bit(self,d:bytearray): + s="" + for i in range(len(d)*8): + if(d[i//8]&(1<<(i%8))!=0): + s+='1' + else: + s+='0' + if((i>0) and ((i+1)%8==0) and ((i+1)0): + crc=0xffff + for i in range(len-offset): + crc=(crc^data[i+offset])&0xffff + for j in range(8): + if(crc&1)!=0: + crc=((crc>>1)^0xa001)&0xffff + else: + crc=(crc>>1)&0xffff + return crc&0xff,(crc>>8)&0xff + return 0,0 + + + + # 把tcp封装为串口 class utcp: @@ -27,7 +47,12 @@ class utcp: class port: def __init__(self) -> None: - pass + self.cmd=0 + self.cmd_no=0 + self.str_err="" + self.is_big_data=False + self.num_to_recv=0 + self.recv_data=bytearray() def open(self,name:str,bsp:int): if(name!="utcp"): self.ser = serial.Serial(port=name, baudrate=bsp,bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE, @@ -37,89 +62,116 @@ class port: def start_recv(self): self.thread_ = threading.Thread(target=self.recv, args=()) self.thread_.start() - def recv(self,num:int): - d=bytes() - try: - d=self.ser.read(num) - except Exception as a: - print("port closed") - return - # print("recv:",d.hex(" ")) - def send(self,data:bytearray): - self.ser.write(data) - # 测试上位机 - def ecode_test(self,times:int): - print("检测赋码系统耗时测试:") + def decode(self,data:bytearray): + self.str_err="ok" + if(len(data)<10): + print("recv data len too less.") + self.str_err="recv data len too less." + return bytearray() + if(data[0]!=0x59 or data[1]!=0x6d or data[2]!=0x43): + print("frame head not 0x59 0x6d.") + self.str_err="frame head not 0x59 0x6d." + return bytearray() + length=data[3]|(data[4]<<8) + if(length==65535): + # 重新设置数据长度 + length=data[7]|(data[8]<<8)|(data[9]<<16)|(data[10]<<24); + self.is_big_data=True + else: + self.is_big_data=False + if(length+7!=len(data)): + print("recv data have lossed") + self.str_err="recv data have lossed" + return bytearray() + a,b=crc16(data,3,length+5) + if(a!=data[-2] or b!=data[-1]): + print("recv data check error.h_crc=%02x %02x,crc=%02x %02x",a,b,data[-2],data[-1]) + self.str_err="recv data check error." + self.cmd_no=data[6]|(data[7]<<8) + self.cmd=data[5] + if(self.is_big_data==False): + return data[8:-2] + else: + return data[12:-2] + def recv(self): + data=bytearray() + while(True): + d=bytes() + try: + d=self.ser.read(1) + except Exception as a: + # print("err:",str(a)) + print("port closed") + return + data+=d + if(len(data)==3): + if(data[0]==0x59 and data[1]==0x6d and data[2]==0x43): + self.num_to_recv=5 + else: + data=data[1:] + self.num_to_recv=0 + elif(len(data)==5): + length=data[3]|(data[4]<<8) + if(length<65535): + self.num_to_recv+=length+2 + self.is_big_data=False + else: + self.num_to_recv=12 + self.is_big_data=True + elif(len(data)==12): + if(self.is_big_data==True): + length=data[8]|(data[9]<<8)|(data[10]<<16)|(data[11]<<24) + self.num_to_recv=5+length+2 + if(self.num_to_recv>0 and self.num_to_recv==len(data)): + # print("recv:",data.hex(" ")) + return self.decode(data) + def encode(self,data:bytearray): + t=bytearray() + length=len(data)+3 + t.append(0x59) + t.append(0x6d) + t.append(length&0xff) + t.append(length>>8) + t.append(self.cmd) + t.append(self.cmd_no&0xff) + t.append(self.cmd_no>>8) + t+=data + a,b=crc16(t,2,length+4) + t.append(a) + t.append(b) + # print("encode:",t.hex(",")) + return t + def send(self,cmd:int,data:bytearray): + self.cmd=cmd + self.cmd_no+=1 + # print("send:",data.hex(",")) + self.ser.write(self.encode(data)) + # 测试批检仪 + def checker_test(self,times:int): + print("批检仪测试:") tick=0 + save=checker_save.save() + data=bytearray() while(tickprivate_data) return 0; + GPIO_InitTypeDef init=GPIO_Initer(); + int index; + const gpioin_dtb *dtb=find(g->name,&index); + self_data *self=&g_self[index]; + self->dtb=dtb; + g->private_data=self; + + dtb->gpio_clock_fun(dtb->gpio_rcc,ENABLE); + init.GPIO_Pin = 1<gpio_pin; + GPIO_Init(dtb->gpio_base, &init); + return 0; +} + + +static int deinit(gpioin_def *g) +{ + return 0; +} + + +static int state(gpioin_def *g) +{ + param_check(g); + param_check(g->private_data); + self_data *self=g->private_data; + return *self->dtb->bitmap_pin; +} + + + +gpioin_init_export(gpioin1,init,deinit,state,0) +gpioin_init_export(gpioin2,init,deinit,state,0) +gpioin_init_export(gpioin3,init,deinit,state,0) +gpioin_init_export(gpioin4,init,deinit,state,0) + + diff --git a/source/main/compiler_info.h b/source/main/compiler_info.h index 5882632..37bbeb2 100644 --- a/source/main/compiler_info.h +++ b/source/main/compiler_info.h @@ -6,7 +6,7 @@ -#define BUILD_DATE "2023-06-25 17:31:20" +#define BUILD_DATE "2023-06-26 10:46:21" #define SOFT_VERSION "0.01" diff --git a/source/rt_thread/board.h b/source/rt_thread/board.h index adc13d2..8d77bf1 100644 --- a/source/rt_thread/board.h +++ b/source/rt_thread/board.h @@ -40,7 +40,13 @@ typedef struct __pwm_def{ }pwm_def; - +typedef struct __gpioin_def{ + const char *name; + int (*init)(struct __gpioin_def *u); + int (*deinit)(struct __gpioin_def *u); + int (*state)(struct __gpioin_def *u); + void *private_data; +}gpioin_def; @@ -88,7 +94,22 @@ typedef struct __pwm_def{ RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \ { \ __dev_##name_##_name, \ - &_pwm_##name_, \ + &_pwm_##name_,\ + }; + +#define gpioin_init_export(name_,init_,deinit_,state_,priv_) \ + const static char __dev_##name_##_name[] SECTION(".rodata.devstr") = #name_; \ + RT_USED static gpioin_def _gpioin_##name_={\ + .name=__dev_##name_##_name,\ + .init=init_,\ + .deinit=deinit_,\ + .state=state_,\ + .private_data=priv_,\ + };\ + RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \ + { \ + __dev_##name_##_name, \ + &_gpioin_##name_,\ }; diff --git a/source/task/key.c b/source/task/key.c new file mode 100644 index 0000000..380991f --- /dev/null +++ b/source/task/key.c @@ -0,0 +1,104 @@ + +#include "board.h" +#include "rtthread.h" +#include "debug.h" +#include "string.h" +#include "dev_flash.h" +#include "prot_uc.h" +#include "key.h" +#include "mystdlib.h" + + + +// 作为批检仪使用时读取按键 + + +typedef struct{ + int inited; + int run; +}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,*in2,*in3,*in4; + int input=0,input_lock=1; + in1=dev_get("gpioin1"); + in2=dev_get("gpioin2"); + in3=dev_get("gpioin3"); + in4=dev_get("gpioin4"); + in1->init(in1); + in2->init(in2); + in3->init(in3); + in4->init(in4); + + while (s->run) + { + rt_thread_mdelay(20); + //input=(in1->state(in1))|(in2->state(in2)<<1)|(in3->state(in3)<<2)|(in4->state(in4)<<3); + input=(in1->state(in1)); + if(input!=input_lock) + { + DBG_LOG("key=%08X",input); + input_lock=input; + if((input&1)==0) + { + request_check(s); + } + } + } + +} + + + + +// 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 void init_later(void *t) +{ + void *protu=app_variable("protu",0,0); + if(protu){ + protu_codec_set(protu,protu_find_codec("ym_checker")); + connect(t,key_send_signal,0,protu,protu_send_call); + DBG_LOG("key thread created"); + }else{ + DBG_WARN("can not fond variable \"protu\""); + } +} + + + + +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); + app_valid_call("protu",init_later,s); + } + return 0; +} +app_init_export(init_thread) + + + diff --git a/source/task/key.h b/source/task/key.h new file mode 100644 index 0000000..d8b30ef --- /dev/null +++ b/source/task/key.h @@ -0,0 +1,28 @@ +#ifndef key_h__ +#define key_h__ + + +#include "rtthread.h" +#include "transmit.h" +#include "signal.h" +#include "bytearray.h" + + + + + +signal key_send_signal(void *obj,uint8_t cmd,array_def *data); + + + + + + + + + + + + + +#endif diff --git a/source/task/mod_signals.c b/source/task/mod_signals.c index a7ed3c7..54b7b04 100644 --- a/source/task/mod_signals.c +++ b/source/task/mod_signals.c @@ -4,6 +4,7 @@ #include "handle.h" #include "handle_for_checker.h" #include "handle_for_coder.h" +#include "key.h" #include "log.h" #include "moter.h" #include "prot_mcu.h" @@ -59,6 +60,17 @@ signal_export(port_end_signal); +void key_send_signal(void *obj,uint8_t cmd,array_def *data) +{ + uint32_t param[2]; + param[0]=(uint32_t)cmd; + param[1]=(uint32_t)data; + _signal_emit(obj,key_send_signal,param,2); +} +signal_export(key_send_signal); + + + void moter_end_signal(void *m) { _signal_emit(m,moter_end_signal,0,0);