修改:信号包独立发包,周期100ms;
布局切换按键,增加开机读取电平状态
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
#include <time.h> /* 新增:clock_gettime / clock_nanosleep */
|
||||
#include <pthread.h>
|
||||
#include "serial.h"
|
||||
#include "uartinf.h"
|
||||
@@ -49,6 +50,7 @@
|
||||
|
||||
|
||||
static int *shm_msg = NULL;
|
||||
static int *buffer_param = NULL; /* 新增:/param.txt 共享内存,供信号线程读 RSSI */
|
||||
|
||||
char _rx_data[200];
|
||||
|
||||
@@ -88,29 +90,6 @@ static int *switch_memory_open(void) {
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* 读 GPIO71(0/1),失败返回默认值 1 */
|
||||
static int read_gpio72(void)
|
||||
{
|
||||
char val = 0;
|
||||
int fd = open("/sys/class/gpio/gpio71/value", O_RDONLY);
|
||||
|
||||
if (fd < 0)
|
||||
return 0; /* 失败默认 0 */
|
||||
if (read(fd, &val, 1) != 1) {
|
||||
close(fd);
|
||||
return 0; /* 失败默认 0 */
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return (val == '0') ? 1 : 0; /* 反相:GPIO=1 → 0,GPIO=0 → 1 */
|
||||
}
|
||||
|
||||
static long elapsed_ms(struct timeval *start, struct timeval *end)
|
||||
{
|
||||
return (end->tv_sec - start->tv_sec) * 1000L
|
||||
+ (end->tv_usec - start->tv_usec) / 1000L;
|
||||
}
|
||||
|
||||
|
||||
int init_net_recv(int port){
|
||||
|
||||
@@ -274,40 +253,90 @@ void *net_recv(void *arg)
|
||||
|
||||
#endif
|
||||
|
||||
/* 独立信号包线程:与串口收包完全解耦,固定 SIGNAL_INTERVAL_MS 周期发送。
|
||||
* 用绝对时间 clock_nanosleep 对齐发送时刻,避免 sendto/printf 开销累积成漂移。 */
|
||||
void *signal_send(void *arg)
|
||||
{
|
||||
char sig_frame[NET_FRAME_LEN];
|
||||
struct timespec next;
|
||||
int k, sum;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &next);
|
||||
|
||||
while (1) {
|
||||
memset(sig_frame, 0, NET_FRAME_LEN);
|
||||
sig_frame[0] = (char)FRAME_HEAD; /* byte0 = 0x11 */
|
||||
|
||||
/* byte25/26 = 视频流0/1无信号,byte27 = 布局开关(light_pwm_lvds 的 0x68 按键) */
|
||||
sig_frame[BYTE_NOSIG0] = (shm_msg && shm_msg[2] == 0x55) ? 1 : 0;
|
||||
sig_frame[BYTE_NOSIG1] = (shm_msg && shm_msg[3] == 0x55) ? 1 : 0;
|
||||
sig_frame[27] = (shm_msg && shm_msg[1]) ? 1 : 0;
|
||||
printf("layout byte27 = %d\n", sig_frame[27]);
|
||||
|
||||
if (buffer_param) {
|
||||
sig_frame[BYTE_RSSI1] = (char)buffer_param[1];
|
||||
sig_frame[BYTE_RSSI2] = (char)buffer_param[2];
|
||||
}
|
||||
sig_frame[PAYLOAD_LEN] = (char)FRAME_TAIL; /* byte30 = 0x22 */
|
||||
|
||||
sum = 0;
|
||||
for (k = 0; k <= CHECKSUM_END; k++)
|
||||
sum += (unsigned char)sig_frame[k];
|
||||
sig_frame[CHECKSUM_END + 1] = (char)(sum & 0xFF);
|
||||
|
||||
sendto(socket_descriptor, sig_frame, NET_FRAME_LEN, 0,
|
||||
(struct sockaddr *)&address, sizeof(address));
|
||||
|
||||
/* 下一个发送时刻 = 本次时刻 + 周期,与实际执行耗时无关 */
|
||||
next.tv_nsec += (long)SIGNAL_INTERVAL_MS * 1000000L;
|
||||
while (next.tv_nsec >= 1000000000L) {
|
||||
next.tv_sec++;
|
||||
next.tv_nsec -= 1000000000L;
|
||||
}
|
||||
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int pthread_init()
|
||||
{
|
||||
int ret;
|
||||
pthread_t sig_thread;
|
||||
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||
ret = pthread_create(&pkts_thread, &attr, net_recv, NULL);
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
|
||||
if(ret){
|
||||
printf("create pthread1 error\n");
|
||||
pthread_attr_destroy(&attr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = pthread_create(&sig_thread, &attr, signal_send, NULL);
|
||||
if(ret){
|
||||
printf("create pthread2 error\n");
|
||||
pthread_attr_destroy(&attr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int n_read;
|
||||
int i;
|
||||
int count=0;
|
||||
int *buffer_param;
|
||||
|
||||
char buffer_send[100];
|
||||
int buffer_len;
|
||||
|
||||
struct timeval tv_sig = {0, 0}, tv_now;
|
||||
char sig_frame[NET_FRAME_LEN];
|
||||
|
||||
n_read = uart_init(ROUTE_UART, "/dev/ttyAMA4", 115200, 'N');
|
||||
if(n_read <=0 ) {
|
||||
printf("open /dev/ttyAMA4 failed\n");
|
||||
@@ -325,7 +354,11 @@ int main (int argc, char *argv[])
|
||||
shm_msg = switch_memory_open();
|
||||
buffer_param = param_memory_open();
|
||||
|
||||
pthread_init();
|
||||
if (pthread_init()) {
|
||||
printf("pthread_init failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
while(1){
|
||||
@@ -366,36 +399,6 @@ int main (int argc, char *argv[])
|
||||
sendto(socket_descriptor, _rx_data, NET_FRAME_LEN, 0, (struct sockaddr *)&address, sizeof(address));
|
||||
}
|
||||
|
||||
|
||||
/* 独立信号包:与串口数据无关,每 SIGNAL_INTERVAL_MS 发一次;其余字节填 0 */
|
||||
gettimeofday(&tv_now, NULL);
|
||||
if (tv_sig.tv_sec == 0 || elapsed_ms(&tv_sig, &tv_now) >= SIGNAL_INTERVAL_MS) {
|
||||
int k, sum = 0;
|
||||
|
||||
memset(sig_frame, 0, NET_FRAME_LEN);
|
||||
sig_frame[0] = (char)FRAME_HEAD; /* byte0 = 0x11 */
|
||||
|
||||
/* byte25/26 = 视频流0/1无信号,byte27 = GPIO */
|
||||
sig_frame[BYTE_NOSIG0] = (shm_msg && shm_msg[2] == 0x55) ? 1 : 0;
|
||||
sig_frame[BYTE_NOSIG1] = (shm_msg && shm_msg[3] == 0x55) ? 1 : 0;
|
||||
sig_frame[27] = (char)read_gpio72();
|
||||
|
||||
if (buffer_param) {
|
||||
sig_frame[BYTE_RSSI1] = (char)buffer_param[1];
|
||||
sig_frame[BYTE_RSSI2] = (char)buffer_param[2];
|
||||
//打印发出的信号值
|
||||
printf("signal: RSSI1=%d RSSI2=%d\n", buffer_param[1], buffer_param[2]);
|
||||
}
|
||||
sig_frame[PAYLOAD_LEN] = (char)FRAME_TAIL; /* byte30 = 0x22 */
|
||||
|
||||
for (k = 0; k <= CHECKSUM_END; k++)
|
||||
sum += (unsigned char)sig_frame[k];
|
||||
sig_frame[CHECKSUM_END + 1] = (char)(sum & 0xFF);
|
||||
|
||||
sendto(socket_descriptor, sig_frame, NET_FRAME_LEN, 0, (struct sockaddr *)&address, sizeof(address));
|
||||
tv_sig = tv_now;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user