• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <pthread.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/time.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <termios.h>
25 #include <poll.h>
26 #include "hisignalling.h"
27 
28 #define MSG(args...) printf(args)
29 pthread_t hisignallingMsgHandleID;
30 #define HISIGNALLING_HEAD_1 (0xaa)
31 #define HISIGNALLING_HEAD_2 (0x55)
32 
33 /**
34     @berf GPIO enable of key
35     @param pin: gpio port
36 */
37 
GpioExport(int pin)38 static int GpioExport(int pin)
39 {
40     char buffer[64] = {0};
41     int len = -1;
42     int fd = -1;
43 
44     fd = open("/sys/class/gpio/export", O_WRONLY);
45     if (fd < 0) {
46         MSG("Failed to open export for writing!\n");
47         close(fd);
48         return -1;
49     }
50 
51     len = snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, "%d", pin);
52     if (len < 0) {
53         MSG("printf msg failed\r\n");
54     }
55     if (write(fd, buffer, len) < 0) {
56         MSG("Failed to export gpio!");
57         close(fd);
58         return -1;
59     }
60 
61     close(fd);
62     return 0;
63 }
64 /**
65     @berf GPIO disable of key
66     @param pin: gpio port
67 */
GpioUnexport(int pin)68 static int GpioUnexport(int pin)
69 {
70     char buffer[64] = {0};
71     int len = -1;
72     int fd = -1;
73 
74     fd = open("/sys/class/gpio/unexport", O_WRONLY);
75     if (fd < 0) {
76         MSG("Failed to open unexport for writing!\n");
77         close(fd);
78         return -1;
79     }
80 
81     len = snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, "%d", pin);
82     if (len < 0) {
83         MSG("printf msg failed\r\n");
84     }
85     if (write(fd, buffer, len) < 0) {
86         MSG("Failed to unexport gpio!");
87         close(fd);
88         return -1;
89     }
90 
91     close(fd);
92     return 0;
93 }
94 
95 // dir: 0-->IN, 1-->OUT
GpioDirection(int pin,int dir)96 static int GpioDirection(int pin, int dir)
97 {
98     static const char dirStr[] = "in\0out";
99     char path[64] = {0};
100     int fd = -1;
101 
102     int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/direction", pin);
103     if (len < 0) {
104         MSG("printf msg failed\r\n");
105     }
106     fd = open(path, O_WRONLY);
107     if (fd < 0) {
108         MSG("Failed to open gpio direction for writing!\n");
109         close(fd);
110         return -1;
111     }
112 
113     if (write(fd, &dirStr[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0) { /* 3, 2, 3 gpio register */
114         MSG("Failed to set direction!\n");
115         close(fd);
116         return -1;
117     }
118 
119     close(fd);
120     return 0;
121 }
122 
123 // value: 0-->LOW, 1-->HIGH
GpioWrite(int pin,int value)124 static int GpioWrite(int pin, int value)
125 {
126     static const char valuesStr[] = "01";
127     char path[64] = {0};
128     int fd = -1;
129 
130     int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/value", pin);
131     if (len < 0) {
132         MSG("printf Msg failed\r\n");
133     }
134     fd = open(path, O_WRONLY);
135     if (fd < 0) {
136         MSG("Failed to open gpio value for writing!\n");
137         close(fd);
138         return -1;
139     }
140 
141     if (write(fd, &valuesStr[value == 0 ? 0 : 1], 1) < 0) { /* 1, 1 gpio register */
142         MSG("Failed to write value!\n");
143         close(fd);
144         return -1;
145     }
146 
147     close(fd);
148     return 0;
149 }
150 /**
151     @berf Read GPIO port level change
152     @param int pin: gpio port
153 */
GpioRead(int pin)154 static int GpioRead(int pin)
155 {
156     char path[64] = {0};
157     char value_str[3] = {0};
158     int fd = -1;
159 
160     int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/value", pin);
161     if (len < 0) {
162         MSG("printf msg failed\r\n");
163     }
164     fd = open(path, O_RDONLY);
165     if (fd < 0) {
166         MSG("Failed to open gpio value for reading!\n");
167         close(fd);
168         return -1;
169     }
170 
171     if (read(fd, value_str, 3) < 0) { /* 3: gpio register */
172         MSG("Failed to read value!\n");
173         close(fd);
174         return -1;
175     }
176 
177     close(fd);
178     return (atoi(value_str));
179 }
180 
181 // none表示引脚为输入,不是中断引脚
182 // rising表示引脚为中断输入,上升沿触发
183 // falling表示引脚为中断输入,下降沿触发
184 // both表示引脚为中断输入,边沿触发
185 // 0-->none, 1-->rising, 2-->falling, 3-->both
GpioEdge(int pin,int edge)186 static int GpioEdge(int pin, int edge)
187 {
188     const char dirStr[] = "none\0rising\0falling\0both";
189     char ptr = 0;
190     char path[64] = {0};
191     int fd = -1;
192 
193     switch (edge) {
194         case EAGE_0:
195             ptr = 0;
196             break;
197         case EAGE_1:
198             ptr = 5; /* 5: gpio register */
199             break;
200         case EAGE_2:
201             ptr = 12; /* 12: gpio register */
202             break;
203         case EAGE_3:
204             ptr = 20; /* 20: gpio register */
205             break;
206         default:
207             ptr = 0;
208     }
209     int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/edge", pin);
210     if (len < 0) {
211         MSG("printf Msg failed\r\n");
212     }
213 
214     fd = open(path, O_WRONLY);
215     if (fd < 0) {
216         MSG("Failed to open gpio edge for writing!\n");
217         close(fd);
218         return -1;
219     }
220 
221     if (write(fd, &dirStr[ptr], strlen(&dirStr[ptr])) < 0) {
222         MSG("Failed to set edge!\n");
223         close(fd);
224         return -1;
225     }
226 
227     close(fd);
228     return 0;
229 }
230 
231 // 初始化按键1
InitGpio1(void)232 void InitGpio1(void)
233 {
234     MSG("\n =============== InitGpio1 start ========== \n");
235     GpioUnexport(1); /* 1: gpio pin */
236     GpioExport(1); /* 1: gpio pin */
237     GpioDirection(1, 0); /* 1, 0: gpio pin */
238     GpioEdge(1, 2); /* 1, 2: gpio pin */
239     MSG("\n =============== InitGpio1 end ========== \n");
240 }
241 
242 // 初始化按键2
InitGpio2(void)243 void InitGpio2(void)
244 {
245     MSG("\n =============== InitGpio2 start ========== \n");
246     GpioUnexport(2); /* 2: gpio pin */
247     GpioExport(2); /* 2: gpio pin */
248     GpioDirection(2, 0); /* 2, 0: gpio pin */
249     GpioEdge(2, 2); /* 2, 2: gpio pin */
250     MSG("\n =============== InitGpio2 end ========== \n");
251 }
252 
253 /* 串口设置 */
Uart1Config(int fd)254 int Uart1Config(int fd)
255 {
256     struct termios newtio = {0}, oldtio = {0};
257     /* 获取原有串口配置 */
258     if (tcgetattr(fd, &oldtio) != 0) {
259         perror("SetupSerial 1");
260         return -1;
261     }
262     (void)memset_s(&newtio, sizeof(newtio), 0, sizeof(newtio));
263     /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
264     newtio.c_cflag  |=  CLOCAL | CREAD;
265 
266     /* 设置数据位8 */
267     newtio.c_cflag &= ~CSIZE;
268     newtio.c_cflag |= CS8;
269     /* 设置奇偶校验位 */
270     newtio.c_cflag &= ~PARENB; // 无奇偶校验
271     /* 设置波特率 115200 */
272     cfsetispeed(&newtio, B115200);
273     cfsetospeed(&newtio, B115200);
274 
275     /* 设置停止位 */
276     newtio.c_cflag &=  ~CSTOPB; /* 默认为一位停止位 */
277     /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时 */
278     newtio.c_cc[VTIME]  = 0; /* 非规范模式读取时的超时时间 */
279     newtio.c_cc[VMIN] = 0; /* 非规范模式读取时的最小字符数 */
280     /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
281     tcflush(fd, TCIFLUSH);
282     if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
283         perror("com set error");
284         return -1;
285     }
286     return 0;
287 }
288 
289 /**
290     @berf uart send
291     @param int fd: uart file descriptor
292     @param void *buf:send data buf
293     @param int len:data buf len
294 */
UartSend(int fd,char * buf,int len)295 int UartSend(int fd, char *buf, int len)
296 {
297     int ret = 0;
298     int count = 0;
299     char *sendBuf = buf;
300     int sendLen = len;
301 
302     tcflush(fd, TCIFLUSH);
303 
304     while (sendLen > 0) {
305         ret = write(fd, (char*)sendBuf + count, sendLen);
306         if (ret < 1) {
307             printf("write data below 1 byte % d\r\n", ret);
308             break;
309         }
310         count += ret;
311         sendLen = sendLen - ret;
312     }
313 
314     return count;
315 }
316 /**
317     @berf uart read
318     @param int uart_fd: uart file descriptor
319     @param void *buf:read data buf
320     @param int len:data buf len
321     @param int timeoutMs: read data time
322 */
UartRead(int uartFd,char * buf,int len,int timeoutMs)323 int UartRead(int uartFd, char *buf, int len, int timeoutMs)
324 {
325     int ret = 0;
326     size_t  rsum = 0;
327     ret = 0;
328     fd_set rset;
329     struct timeval time;
330     int timeout = timeoutMs;
331     char *readBuf = buf;
332     int readLen = len;
333 
334     while (rsum < readLen) {
335         time.tv_sec = timeout / 1000; /* 1000:转换成秒 */
336         time.tv_usec = (timeout - time.tv_sec * 1000) * 1000; /* 1000, 1000:转换为微秒 */
337         FD_ZERO(&rset);
338         FD_SET(uartFd, &rset);
339         ret = select(uartFd + 1, &rset, NULL, NULL, &time);
340         if (ret <= 0) {
341             if (ret == 0) {
342                 printf("time over!\r\n");
343                 return -1;
344             }
345             if (ret == -1) {
346                 printf("select error!\r\n");
347                 // 信号中断
348                 continue;
349             }
350             return -1;
351         } else {
352             ret = read(uartFd, (char *)readBuf + rsum, readLen - rsum);
353             if (ret < 0) {
354                 printf("read data failed\r\n");
355                 return ret;
356             } else {
357                 rsum += ret;
358             }
359         }
360     }
361     return rsum;
362 }
363 
364 /*
365 * crc32 Verification implementation
366 */
367 static const unsigned int crc32table[] = {
368 };
369 
370 /**
371     @berf CRC check
372     @param const unsigned char *buf: Data to be verified buff
373     @param unsigned int size: Data to be verified length
374 */
crc32(const unsigned char * buf,unsigned int size)375 static unsigned int crc32(const unsigned char *buf, unsigned int size)
376 {
377     unsigned int  i, crc = 0xFFFFFFFF;
378 
379     for (i = 0; i < size; i++) {
380         crc = crc32table[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); /* 8: 右移8bit */
381     }
382     return crc ^ 0xFFFFFFFF;
383 }
384 
385 #define RIGHT_MOVE_8_BIT (8)
386 #define RIGHT_MOVE_16_BIT (16)
387 #define RIGHT_MOVE_24_BIT (24)
388 /* hisignal Hi3516 message send */
HisignallingDataPackage(HisignallingProtocalType * buf,unsigned int len,unsigned char * hisignallingDataBuf)389 static unsigned int HisignallingDataPackage(HisignallingProtocalType *buf,
390     unsigned int len, unsigned char *hisignallingDataBuf)
391 {
392     unsigned int crcCheckSend = 0x1010;
393     unsigned int packageLen = 0;
394     unsigned int DataPackLen = len;
395 
396     (void)memcpy_s(hisignallingDataBuf, HISGNALLING_MSG_FRAME_HEADER_LEN,
397         buf->frameHeader, HISGNALLING_MSG_FRAME_HEADER_LEN);
398     (void)memcpy_s(&hisignallingDataBuf[HISGNALLING_MSG_FRAME_HEADER_LEN],
399         DataPackLen, buf->hisignallingMsgBuf, DataPackLen);
400     (void)memcpy_s(&hisignallingDataBuf[HISGNALLING_MSG_FRAME_HEADER_LEN + DataPackLen],
401         HISIGNALLING_MSG_HEADER_LEN, &(buf->endOfFrame), HISIGNALLING_MSG_HEADER_LEN);
402     crcCheckSend = crc32(hisignallingDataBuf, (DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN));
403     hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN] =
404         (unsigned char)((crcCheckSend & 0xff000000) >> RIGHT_MOVE_24_BIT);
405     hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 1] = /* 1: addr offset */
406         (unsigned char)((crcCheckSend & 0x00ff0000) >> RIGHT_MOVE_16_BIT);
407     hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 2] = /* 2: addr offset */
408         (unsigned char)((crcCheckSend & 0x0000ff00) >> RIGHT_MOVE_8_BIT);
409     hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 3] = /* 3: addr offset */
410         (unsigned char)crcCheckSend;
411     packageLen = DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 4; /* 4: crc check lenght */
412     return packageLen;
413 }
414 
415 /* hisignalling Hi3561 message recevice */
HisignallingMsgReceive(int fd,unsigned char * buf,unsigned int len)416 static HisignallingErrorType HisignallingMsgReceive(int fd, unsigned char *buf, unsigned int len)
417 {
418     unsigned int crcCheckReceived = 0;
419     int i = 0, readLen = 0;
420     unsigned int RecvLen = len;
421 
422     /* Hi3516dv300 uart read */
423     readLen = UartRead(fd, buf, RecvLen, 1000); /* 1000 :time out */
424     if (readLen <= 0) {
425         printf("uart_read data failed\r\n");
426         return HISIGNALLING_RET_VAL_ERROR;
427     }
428     printf("read_len=%d\r\n", readLen);
429     /* 输出收到的数据 */
430     for (i = 0; i < RecvLen; i++) {
431         printf("0x%x ", buf[i]);
432     }
433     printf("\r\n");
434 
435     return HISIGNALLING_RET_VAL_CORRECT;
436 }
437 
438 /**
439     @berf hisignalling protocol send msg
440     @param void *buf: send data buff
441     @param unsigned int data_len: send data length
442 */
HisignallingMsgSend(int fd,char * buf,unsigned int dataLen)443 static unsigned int HisignallingMsgSend(int fd, char *buf, unsigned int dataLen)
444 {
445     unsigned int ret = 0;
446     HisignallingProtocalType hisignallingMsg = {0};
447     unsigned char hisignallingSendBuf[HISIGNALLING_MSG_BUFF_LEN] = {0};
448     unsigned int hisignallingPackageLen = 0;
449     unsigned int writeDataLen = 0;
450 
451     hisignallingMsg.frameHeader[0]= 0xAA; /* Protocol head data 1 */
452     hisignallingMsg.frameHeader[1]= 0x55; /* Protocol head data 2 */
453     (void)memcpy_s(hisignallingMsg.hisignallingMsgBuf, dataLen, buf, dataLen);
454     hisignallingMsg.endOfFrame = 0xff; /* Protocol tail data */
455 
456     hisignallingPackageLen = HisignallingDataPackage(&hisignallingMsg, dataLen, hisignallingSendBuf);
457     if (!hisignallingPackageLen) {
458         printf("hisignalling_data_package failed\r\n");
459         return -1;
460     }
461     if (*hisignallingSendBuf == 0) {
462         printf("hisignalling send buf is null!\r\n");
463         return -1;
464     }
465 
466     ret = UartSend(fd, hisignallingSendBuf, hisignallingPackageLen);
467     if (ret < 0) {
468         printf("write data failed\r\n");
469         return -1;
470     }
471 
472     for (int i = 0; i < hisignallingPackageLen; i++) {
473         printf("send data = 0x%x \r\n", hisignallingSendBuf[i]);
474     }
475 
476     return 0;
477 }
478 
479 /* hisignalling message handle */
HisignallingMsgHandle(char * param)480 void *HisignallingMsgHandle(char *param)
481 {
482     unsigned int err = 0;
483 
484     while (1) {
485         printf("hisignalling_msg_handle\r\n");
486         usleep(HISGNALLING_FREE_TASK_TIME);
487     }
488     err = pthread_join(hisignallingMsgHandleID, NULL);
489     if (err != 0) {
490         printf("Failed to delete hisignalling msg task\r\n");
491     }
492 }
493 
494 #define HISIGNALLING_TASK_STACK_SIZE (2048)
495 /* hisignalling message task */
HisignallingMsgTask(void)496 unsigned int HisignallingMsgTask(void)
497 {
498     unsigned int  ret = 0;
499     int err = 0, stacksize = HISIGNALLING_TASK_STACK_SIZE;
500 
501     pthread_attr_t hisignallingAttr = {0};
502     err = pthread_attr_init(&hisignallingAttr);
503     err = pthread_attr_setstacksize(&hisignallingAttr, stacksize);
504     ret = pthread_create(&hisignallingMsgHandleID, &hisignallingAttr, HisignallingMsgHandle, NULL);
505     if (ret != 0) {
506         printf("Failed to create hisignalling msg task\r\n");
507         return -1;
508     }
509     return 0;
510 }
511 
UartProcess(int uartFd,int Gpio1Fd,int Gpio2Fd,struct pollfd fdS1,struct pollfd fdS2)512 static void UartProcess(int uartFd, int Gpio1Fd, int Gpio2Fd, struct pollfd fdS1, struct pollfd fdS2)
513 {
514     int ret = 0;
515     char buff[10] = {0};
516     unsigned char writeBuffer[4]  = {0, 2, 0, 3};
517     unsigned char writeBuffer2[4] = {0, 2, 0, 4};
518     unsigned char writeBuffer3[4] = {0, 2, 0, 5};
519     unsigned char readBuff[16] = {0};
520 
521     Uart1Config(uartFd);
522     while (1) {
523         /* 按键操作 */
524         ret = read(Gpio1Fd, buff, 10); /* 10:read data lenght */
525         if (ret == -1) {
526             MSG("gpio1 read error\n");
527         }
528         ret = poll(&fdS1, 1, 0); /* 1: 监视一个文件描述符 */
529         if (ret == -1) {
530             MSG("gpio1 poll error\n");
531         }
532         if (fdS1.revents & POLLPRI) {
533             ret = lseek(Gpio1Fd, 0, SEEK_SET);
534             if (ret == -1) {
535                 MSG("gpio1 lseek error\n");
536             }
537             MSG("sw2 Pressed \n");
538             /* 按键触发发送 */
539             HisignallingMsgSend(uartFd, writeBuffer2, sizeof(writeBuffer2) / sizeof(writeBuffer2[0]));
540         }
541         ret = read(Gpio2Fd, buff, 10); /* 10:read data lenght */
542         if (ret == -1) {
543             MSG("gpio2 read error\n");
544         }
545         ret = poll(&fdS2, 1, 0); /* 1: 监视一个文件描述符 */
546         if (ret == -1) {
547             MSG("gpio2 poll error\n");
548         }
549         if (fdS2.revents & POLLPRI) {
550             ret = lseek(Gpio2Fd, 0, SEEK_SET);
551             if (ret == -1) {
552                 MSG("gpio1 lseek error\n");
553             }
554             MSG("sw1 Pressed \n");
555             /* 按键触发发送 */
556 #ifdef  EXPANSION_BOARD
557             HisignallingMsgSend(uartFd, writeBuffer3, sizeof(writeBuffer3) / sizeof(writeBuffer3[0]));
558 #elif defined (ROBOT_BOARD)
559             HisignallingMsgSend(uartFd, writeBuffer, sizeof(writeBuffer) / sizeof(writeBuffer[0]));
560 #endif
561         }
562         /* 串口读写操作 */
563         HisignallingMsgReceive(uartFd, readBuff, HISIGNALLING_MSG_MOTOR_ENGINE_LEN);
564         usleep(HISGNALLING_FREE_TASK_TIME);
565     }
566 }
567 
UartOpenInit(void)568 unsigned int UartOpenInit(void)
569 {
570     int fd;
571     char *uart1 = "/dev/ttyAMA1";
572 
573     if ((fd = open(uart1, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
574         printf("open %s is failed", uart1);
575         return -1;
576     } else {
577         Uart1Config(fd);
578     }
579     return fd;
580 }
581 
UartSendRead(int fd,refuseClassification refuseType)582 void UartSendRead(int fd, refuseClassification refuseType)
583 {
584     /* test buffer */
585     unsigned char writeBuffer2[4] = {0, 2, 0, 1};
586     unsigned char writeBuffer3[4] = {0, 2, 0, 2};
587     unsigned char writeBuffer4[4] = {0, 2, 0, 3};
588     unsigned char writeBuffer5[4] = {0, 2, 0, 4};
589     unsigned char writeBuffer6[4] = {0, 2, 0, 5};
590     unsigned char writeBuffer7[4] = {0, 2, 0, 6};
591     unsigned char writeBuffer8[4] = {0, 2, 0, 7};
592     unsigned char writeBuffer9[4] = {0, 2, 0, 8};
593     unsigned char readBuff[16] = {0};
594 
595 #ifdef  EXPANSION_BOARD
596     switch (refuseType) {
597         case FistGesture:
598             HisignallingMsgSend(fd, writeBuffer2, sizeof(writeBuffer2)/sizeof(writeBuffer2[0]));
599             printf("send gesture status:FistGesture\r\n");
600             break;
601         case ForefingerGesture:
602             HisignallingMsgSend(fd, writeBuffer3, sizeof(writeBuffer3)/sizeof(writeBuffer3[0]));
603             printf("send gesture status:ForefingerGesture\r\n");
604             break;
605         case OkGesture:
606             HisignallingMsgSend(fd, writeBuffer4, sizeof(writeBuffer4)/sizeof(writeBuffer4[0]));
607             printf("send gesture status:OkGesture\r\n");
608             break;
609         case PalmGesture:
610             HisignallingMsgSend(fd, writeBuffer5, sizeof(writeBuffer5)/sizeof(writeBuffer5[0]));
611             printf("send gesture status:PalmGesture\r\n");
612             break;
613         case YesGesture:
614             HisignallingMsgSend(fd, writeBuffer6, sizeof(writeBuffer6)/sizeof(writeBuffer6[0]));
615             printf("send gesture status:YesGesture\r\n");
616             break;
617         case ForefingerAndThumbGesture:
618             HisignallingMsgSend(fd, writeBuffer7, sizeof(writeBuffer7)/sizeof(writeBuffer7[0]));
619             printf("send gesture status:ForefingerAndThumbGesture\r\n");
620             break;
621         case LittleFingerAndThumbGesture:
622             HisignallingMsgSend(fd, writeBuffer8, sizeof(writeBuffer8)/sizeof(writeBuffer8[0]));
623             printf("send gesture status:LittleFingerAndThumbGesture\r\n");
624             break;
625         case InvalidGesture:
626             HisignallingMsgSend(fd, writeBuffer9, sizeof(writeBuffer9)/sizeof(writeBuffer9[0]));
627             printf("send gesture status:InvalidGesture\r\n");
628             break;
629     }
630 #endif
631     /* 串口读操作 */
632     if (readBuff[0] == HISIGNALLING_HEAD_1 && readBuff[1] == HISIGNALLING_HEAD_2) {
633         HisignallingMsgReceive(fd, readBuff, HISIGNALLING_MSG_MOTOR_ENGINE_LEN);
634     }
635 }
636 
AiUartTransmit(void)637 int AiUartTransmit(void)
638 {
639     /* 按键初始化定义 */
640     int gpio1Fd = -1;
641     int gpio2Fd = -1;
642     int ret1     = -1;
643     int ret2     = -1;
644     struct pollfd fds1;
645     struct pollfd fds2;
646     /* 串口初始化定义 */
647     int fd = 0;
648     char *uart1 = "/dev/ttyAMA1";
649 
650     printf("hisignal task start\r\n");
651     /* 按键操作 */
652     InitGpio1();
653     InitGpio2();
654     gpio1Fd = open("/sys/class/gpio/gpio1/value", O_RDONLY);
655     if (gpio1Fd < 0) {
656         MSG("Failed to open gpio1 !\n");
657         return -1;
658     }
659     fds1.fd     = gpio1Fd;
660     fds1.events = POLLPRI;
661 
662     gpio2Fd = open("/sys/class/gpio/gpio2/value", O_RDONLY);
663     if (gpio2Fd < 0) {
664         MSG("Failed to open gpio1 !\n");
665         return -1;
666     }
667     fds2.fd     = gpio2Fd;
668     fds2.events = POLLPRI;
669     /* 串口读写 */
670     if ((fd = open(uart1, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
671         printf("open %s is failed", uart1);
672         return -1;
673     } else {
674         UartProcess(fd, gpio1Fd, gpio2Fd, fds1, fds2);
675     }
676     return 0;
677 }
678