Browse Source

add:添加界面盒插拔提醒

wulianwei 1 week ago
parent
commit
08d4799f3e

+ 10 - 10
code/algorithm/emg_util.c

@@ -10,8 +10,8 @@
 // SOS coefficients (20-450 Hz bandpass, fs=1000Hz)
 // 将常量数组类型从 double 改为 float,并添加 'f' 后缀
 const float sos[2][6] = { // double -> float
-    {1.0000000000f,  2.0000000000f, 1.0000000000f, 1.0f,  -1.750100000f, 0.7797000000f}, // 所有数值添加 f 后缀
-    {1.0000000000f, -2.0000000000f, 1.0000000000f, 1.0f, -1.9912000000f, 0.9912000000f}  // 所有数值添加 f 后缀
+    {1.0000000000f,  2.0000000000f, 1.0000000000f, 1.0f,  1.5591f, 0.6418f}, // 所有数值添加 f 后缀
+    {1.0000000000f, -2.0000000000f, 1.0000000000f, 1.0f, -1.7340f, 0.7663f}  // 所有数值添加 f 后缀
 };
 
 // Notch filter parameters (50Hz notch, fs=1000Hz)
@@ -367,7 +367,7 @@ void denoise_emg_wavelab(float *data, int N, float fs, float *denoised) { // dou
     sigma = med_abs / 0.6745f/4.2; // 0.6745 -> 0.6745f
     // thr = sigma * sqrt(2.0 * log((double)N));
     thr = sigma * sqrtf(2.0f * logf((float)N)); // sqrt -> sqrtf, log -> logf, (double)N -> (float)N, 2.0 -> 2.0f
-    printf("Threshold: %.15f  sigma:%.15f\n", thr,sigma);
+    //printf("Threshold: %.15f  sigma:%.15f\n", thr,sigma);
     free(abs_d6);
 
     // === 3. Soft thresholding ===
@@ -439,18 +439,18 @@ void emg_denoised(float *emg_raw, int length, int fs, float *emg_denoised) { //
     notchFilterReset(&filter);
 
     // Remove DC offset
-    remove_dc_offset(emg_raw, emg_denoised, length);
+   // remove_dc_offset(emg_raw, emg_denoised, length);
 
     // Notch filtering (50Hz or 60Hz)
     //notchFilterProcessArray(&filter, emg_denoised, emg_denoised, length);
 
     // Bidirectional second-order section filtering (zero-phase bandpass filtering)
-    filtfilt_sos(emg_denoised, emg_denoised, length, sos, 2); // 确保 sos 系数数组在 emg_util.h 中也是 float 类型
-   for(i=0;i<length;i++)
-   {
-       emg_denoised[i]=emg_denoised[i]/1000;
-   }
+    filtfilt_sos(emg_raw, emg_denoised, length, sos, 2); // 确保 sos 系数数组在 emg_util.h 中也是 float 类型
+//   for(i=0;i<length;i++)
+//   {
+//       emg_denoised[i]=emg_denoised[i]/1000;
+//   }
     // Wavelet denoising (using sym4)
-    //denoise_emg_wavelab(emg_denoised, length, fs, emg_denoised);
+   // denoise_emg_wavelab(emg_denoised, length, fs, emg_denoised);
 }
 

+ 152 - 0
code/business/handle_bus.c

@@ -0,0 +1,152 @@
+/*
+ * handle_bus.c
+ *
+ *  Created on: 2025年11月26日
+ *      Author: wulianwei
+ */
+
+#include <stdlib.h>
+#include "tr_protocol.h"
+#include "nim_config.h"
+
+/**
+ * 处理采集信号
+ */
+
+#define COUNT_SIZE 75
+#define CHANNEL_COUNT 4 //采集通道总数
+
+/**
+ * 处理波形数据并上报到GUI
+ */
+void handleSign(unsigned char *s, unsigned short sl)
+{
+    ConsolePuts("handle signal data from usb\n", -1);
+
+    struct Frame
+    {
+        unsigned char sign;
+        unsigned char d1[3];
+        unsigned char d2[3];
+        unsigned char d3[3];
+        unsigned char d4[3];
+    };
+    short FRAME_LEN = sizeof(struct Frame);
+    if (sl % FRAME_LEN > 0)
+    {
+        ConsolePuts("长度不匹配\n", -1);
+        return;
+    }
+
+    int dataCount = sl / FRAME_LEN; //每个通道数据量, 每三个字节代表一个采集信号. 大端模式.
+    float *const fdata = malloc(sizeof(float) * dataCount*CHANNEL_COUNT*2);
+    memset(fdata,0,sizeof(float) * dataCount*CHANNEL_COUNT*2);
+    float *const cd1 = fdata+dataCount*0;
+    float *const cd2 = fdata+dataCount*1;
+    float *const cd3 = fdata+dataCount*2;
+    float *const cd4 = fdata+dataCount*3;
+    float *const fd1 = fdata+dataCount*4;
+    float *const fd2 = fdata+dataCount*5;
+    float *const fd3 = fdata+dataCount*6;;
+    float *const fd4 = fdata+dataCount*7;
+
+    float* sd1 = cd1;
+    float* sd2 = cd2;
+    float* sd3 = cd3;;
+    float* sd4 = cd4;
+
+    const int FULL = 0x007fffff;
+    const float FFULL = (float) FULL;
+    struct Frame *fm = NULL;
+    int i = 0;
+    for (i = 0; i < dataCount; i++)
+    {
+        fm = (struct Frame*) (s + FRAME_LEN * i);
+        int origenData1 = fm->d1[0] << 24 | fm->d1[1] << 16 | fm->d1[2] << 8;
+        *(cd1 + i) = ((float) (origenData1 >> 8) / FFULL) * 5000000+530; //将采集数据转成uV
+        //ConsolePrintf("%f,\n",*(cd1 + i));
+
+        int origenData2 = fm->d2[0] << 24 | fm->d2[1] << 16 | fm->d2[2] << 8;
+        *(cd2 + i) = ((float) (origenData2 >> 8) / FFULL) * 5000000+470; //将采集数据转成uV
+
+        int origenData3 = fm->d3[0] << 24 | fm->d3[1] << 16 | fm->d3[2] << 8;
+        *(cd3 + i) = ((float) (origenData3 >> 8) / FFULL) * 5000000+450; //将采集数据转成uV
+
+        int origenData4 = fm->d4[0] << 24 | fm->d4[1] << 16 | fm->d4[2] << 8;
+        *(cd4 + i) = ((float) (origenData4 >> 8) / FFULL) * 5000000+440; //将采集数据转成uV
+
+    }
+    if (!(fm->sign & 0x01))
+    {
+        emg_denoised(cd1, dataCount, 1000, fd1);
+        sd1 = fd1;
+    }
+    if (!(fm->sign & 0x02))
+    {
+        emg_denoised(cd2, dataCount, 1000, fd2);
+        sd2 = fd2;
+    }
+    if (!(fm->sign & 0x04))
+    {
+        emg_denoised(cd3, dataCount, 1000, fd3);
+        sd3 = fd3;
+    }
+    if (!(fm->sign & 0x08))
+    {
+        emg_denoised(cd4, dataCount, 1000, fd4);
+        sd4 = fd4;
+    }
+    lwip_udp_send_gui_signal2(cd1,fd1,cd3,cd4, fm->sign,dataCount);
+    //lwip_udp_send_vofa4(cd1, fd1 , cd3 ,cd4 , dataCount);
+    free(fdata);
+}
+
+
+/**
+ * 处理gui下发的数据
+ */
+void handleGUI(unsigned char *s, unsigned short sl)
+{
+    ConsolePuts("\n===handle from gui\n", -1);
+    ConsolePutsHexStr(s, sl);
+    unsigned char check = 0;
+    int a = ProtocolGetOption(s,sl,99,&check);
+    if(check == 1)
+    {
+        unsigned char udpAdd[8] = {0};
+        trProtocol verPro;
+        ProtocolInit(&verPro);
+        ProtocolSetDestAddress(&verPro,3);
+        ProtocolSetSourceAddress(&verPro,1);
+        ProtocolSetType(&verPro,2);
+        ProtocolSetOptionChar(&verPro, 100, 1);
+        ProtocolSetPayload(&verPro,sizeof(udpAdd),udpAdd);
+        ProtocolPackage(&verPro);
+        ConsolePuts("\n===dsp to gui version\n", -1);
+        ConsolePutsHexStr(verPro.message, verPro.length);
+        lwip_tcp_send_gui(verPro.message,verPro.length);
+    }
+    else if(check == 3)
+    {
+        touch_closegui();
+    }
+}
+
+/**
+ * 上报界面盒在线状态
+ */
+void handleUSBLineStatus(unsigned char status)
+{
+    trProtocol verPro;
+    ProtocolInit(&verPro);
+    ProtocolSetDestAddress(&verPro,3);
+    ProtocolSetSourceAddress(&verPro,1);
+    ProtocolSetType(&verPro,2);
+    ProtocolSetOptionChar(&verPro, 99, 4);
+    ProtocolSetOptionChar(&verPro, 123, status);
+    ProtocolPackage(&verPro);
+    ConsolePuts("\n===dsp to gui usb line status\n", -1);
+    ConsolePutsHexStr(verPro.message, verPro.length);
+    lwip_tcp_send_gui(verPro.message,verPro.length);
+}
+

+ 17 - 0
code/business/handle_bus.h

@@ -0,0 +1,17 @@
+/*
+ * handle_bus.h
+ *
+ *  Created on: 2025Äê11ÔÂ26ÈÕ
+ *      Author: wulianwei
+ */
+
+#ifndef CODE_BUSINESS_HANDLE_BUS_H_
+#define CODE_BUSINESS_HANDLE_BUS_H_
+
+
+void handleSign(unsigned char *s, unsigned short sl);
+void handleSign2(unsigned char *s, unsigned short sl);
+void handleGUI(unsigned char *s, unsigned short sl);
+void handleUSBLineStatus(unsigned char status);
+
+#endif /* CODE_BUSINESS_HANDLE_BUS_H_ */

+ 1 - 1
code/device/lwip.c

@@ -31,7 +31,7 @@ void init_ip(void);
 void lwip_device_init(void)
 {
     EthernetInit(); //ÒÔÌ«ÍøÍâÉèÅäÖÃ
-    init_ip();
+    //init_ip();
     //udp_bind(gui_udp_pcb, IP_ADDR_ANY, 3000);
 }
 

+ 16 - 2
code/device/touch.c

@@ -25,16 +25,19 @@ void touch_deivce_init()
  */
 void touch_switch(int len)
 {
+    TocuhTime = TOUCH_TIME;
     if(len)
     {
         TocuhTrigger = 0;
-        TocuhTime = TOUCH_TIME;
         ConsolePuts("swith 1", -1);
     }
     else
     {
         TocuhTrigger = 1;
-        TocuhTime = TOUCH_TIME;
+        if(gui_power_status == 0)
+        {
+            TocuhTime = 500;
+        }
         ConsolePuts("swith 0", -1);
     }
 }
@@ -76,6 +79,17 @@ void touch_closegui()
     ConsolePuts("\n===close gui\n", -1);
     GPIOPinWrite(SOC_GPIO_0_REGS, 86, GPIO_PIN_LOW);
     gui_power_status = 0;
+
+    trProtocol verPro;
+    ProtocolInit(&verPro);
+    ProtocolSetDestAddress(&verPro,2);
+    ProtocolSetSourceAddress(&verPro,1);
+    ProtocolSetType(&verPro,2);
+    ProtocolSetOptionChar(&verPro, 99, 2);
+    ProtocolPackage(&verPro);
+    ConsolePuts("\n===dsp to usb close\n", -1);
+    ConsolePutsHexStr(verPro.message, verPro.length);
+    USBHSPECIFICWrite(verPro.message,verPro.length);
 }
 
 void touch_task()

+ 1 - 200
code/hlx/tr_queue_handler.c

@@ -11,7 +11,6 @@
 #include "uartStdio.h"
 #include "usbhspecific.h"
 #include <stdlib.h>
-#include "tr_protocol.h"
 
 extern struct udp_pcb *upcb;
 void handleSign(unsigned char *s, unsigned short sl);
@@ -24,7 +23,7 @@ void handleGUI(unsigned char *s, unsigned short sl);
 void dataForwardedHandle(unsigned char *s, unsigned short sl, unsigned char da)
 {
 //    printf("进入到数据转发!\r\n");
-    ConsolePuts("\n===dataForwardedHandle\n", -1);
+    ConsolePuts("\n===dataForwardedHandle====\n", -1);
     ConsolePutsHexStr(s, sl);
     if (da == F4_ADDRESS)
     {
@@ -86,202 +85,4 @@ void dataNotForwardedHandle(unsigned char *s, unsigned short sl,
 
 }
 
-/**
- * 处理采集信号
- */
-
-#define COUNT_SIZE 75
-#define CHANNEL_COUNT 4 //采集通道总数
-void handleSign(unsigned char *s, unsigned short sl)
-{
-    ConsolePuts("handle signal data from usb\n", -1);
-
-    struct Frame
-    {
-        unsigned char sign;
-        unsigned char d1[3];
-        unsigned char d2[3];
-        unsigned char d3[3];
-        unsigned char d4[3];
-    };
-    short FRAME_LEN = sizeof(struct Frame);
-    if (sl % FRAME_LEN > 0)
-    {
-        ConsolePuts("长度不匹配\n", -1);
-        return;
-    }
-
-    int dataCount = sl / FRAME_LEN; //每个通道数据量, 每三个字节代表一个采集信号. 大端模式.
-    float *const fdata = malloc(sizeof(float) * dataCount*CHANNEL_COUNT*2);
-    float *const cd1 = fdata+dataCount*0;
-    float *const cd2 = fdata+dataCount*1;
-    float *const cd3 = fdata+dataCount*2;
-    float *const cd4 = fdata+dataCount*3;
-    float *const fd1 = fdata+dataCount*4;
-    float *const fd2 = fdata+dataCount*5;
-    float *const fd3 = fdata+dataCount*6;;
-    float *const fd4 = fdata+dataCount*7;
-
-    float* sd1 = cd1;
-    float* sd2 = cd2;
-    float* sd3 = cd3;;
-    float* sd4 = cd4;
-
-    const int FULL = 0x007fffff;
-    const float FFULL = (float) FULL;
-    struct Frame *fm = NULL;
-    int i = 0;
-    for (i = 0; i < dataCount; i++)
-    {
-        fm = (struct Frame*) (s + FRAME_LEN * i);
-        int origenData1 = fm->d1[0] << 24 | fm->d1[1] << 16 | fm->d1[2] << 8;
-        *(cd1 + i) = ((float) (origenData1 >> 8) / FFULL) * 5000000+530; //将采集数据转成uV
-
-        int origenData2 = fm->d2[0] << 24 | fm->d2[1] << 16 | fm->d2[2] << 8;
-        *(cd2 + i) = ((float) (origenData2 >> 8) / FFULL) * 5000000+470; //将采集数据转成uV
-
-        int origenData3 = fm->d3[0] << 24 | fm->d3[1] << 16 | fm->d3[2] << 8;
-        *(cd3 + i) = ((float) (origenData3 >> 8) / FFULL) * 5000000+450; //将采集数据转成uV
-
-        int origenData4 = fm->d4[0] << 24 | fm->d4[1] << 16 | fm->d4[2] << 8;
-        *(cd4 + i) = ((float) (origenData4 >> 8) / FFULL) * 5000000+440; //将采集数据转成uV
-
-    }
-//    memcpy(fd1, cd1, dataCount);
-//    memcpy(fd2, cd2, dataCount);
-//    memcpy(fd3, cd3, dataCount);
-//    memcpy(fd4, cd4, dataCount);
-    if (fm->sign & 0x01)
-    {
-        emg_denoised(cd1, dataCount, 1000, fd1);
-        sd1 = fd1;
-    }
-    if (fm->sign & 0x02)
-    {
-        emg_denoised(cd2, dataCount, 1000, fd2);
-        sd2 = fd2;
-    }
-    if (fm->sign & 0x04)
-    {
-        emg_denoised(cd3, dataCount, 1000, fd3);
-        sd3 = fd3;
-    }
-    if (fm->sign & 0x08)
-    {
-        emg_denoised(cd4, dataCount, 1000, fd4);
-        sd4 = fd4;
-    }
-
-    lwip_udp_send_gui_signal2(sd1,sd2,sd3,sd4, fm->sign,dataCount);
-    free(fdata);
-}
-
-void handleSign2(unsigned char *s, unsigned short sl)
-{
-    ConsolePuts("===handle signal 2 data from usb\n", -1);
-    struct Frame
-    {
-        unsigned char sign;
-        unsigned char d1[3];
-        unsigned char d2[3];
-        unsigned char d3[3];
-        unsigned char d4[3];
-    };
-    short FRAME_LEN = sizeof(struct Frame);
-    if (sl % FRAME_LEN > 0)
-    {
-        ConsolePuts("长度不匹配\n", -1);
-        return;
-    }
-
-    int dataCount = sl / FRAME_LEN; //每个通道数据量, 每三个字节代表一个采集信号. 大端模式.
-    float *const fdata = malloc(sizeof(float) * dataCount*CHANNEL_COUNT*2);
-    float *const cd1 = fdata+dataCount*0;
-    float *const cd2 = fdata+dataCount*1;
-    float *const cd3 = fdata+dataCount*2;
-    float *const cd4 = fdata+dataCount*3;
-    float *const fd1 = fdata+dataCount*4;
-    float *const fd2 = fdata+dataCount*5;
-    float *const fd3 = fdata+dataCount*6;;
-    float *const fd4 = fdata+dataCount*7;
-
-    float* sd1 = cd1;
-    float* sd2 = cd2;
-    float* sd3 = cd3;;
-    float* sd4 = cd4;
-
-    const int FULL = 0x007fffff;
-    const float FFULL = (float) FULL;
-    struct Frame *fm = NULL;
-    int i = 0;
-    for (i = 0; i < dataCount; i++)
-    {
-        fm = (struct Frame*) (s + FRAME_LEN * i);
-        int origenData1 = fm->d1[0] << 24 | fm->d1[1] << 16 | fm->d1[2] << 8;
-        *(cd1 + i) = ((float) (origenData1 >> 8) / FFULL)  * 5000000+530; //将采集数据转成mV
-
-        int origenData2 = fm->d2[0] << 24 | fm->d2[1] << 16 | fm->d2[2] << 8;
-        *(cd2 + i) = ((float) (origenData2 >> 8) / FFULL) * 5000000+470; //将采集数据转成mV
-
-        int origenData3 = fm->d3[0] << 24 | fm->d3[1] << 16 | fm->d3[2] << 8;
-        *(cd3 + i) = ((float) (origenData3 >> 8) / FFULL) * 5000000+450; //将采集数据转成mV
-
-        int origenData4 = fm->d4[0] << 24 | fm->d4[1] << 16 | fm->d4[2] << 8;
-        *(cd4 + i) = ((float) (origenData4 >> 8) / FFULL) * 5000000+440; //将采集数据转成mV
-    }
-//    memcpy(fd1, cd1, dataCount);
-//    memcpy(fd2, cd2, dataCount);
-//    memcpy(fd3, cd3, dataCount);
-//    memcpy(fd4, cd4, dataCount);
-    if (fm->sign & 0x01)
-    {
-        emg_denoised(cd1, dataCount, 1000, fd1);
-        sd1 = fd1;
-    }
-    if (fm->sign & 0x02)
-    {
-        emg_denoised(cd2, dataCount, 1000, fd2);
-        sd2 = fd2;
-    }
-    if (fm->sign & 0x04)
-    {
-        emg_denoised(cd3, dataCount, 1000, fd3);
-        sd3 = fd3;
-    }
-    if (fm->sign & 0x08)
-    {
-        emg_denoised(cd4, dataCount, 1000, fd4);
-        sd4 = fd4;
-    }
-    lwip_udp_send_vofa4(sd1, sd2 , sd3 ,sd4 , dataCount);
-    free(fdata);
-
-}
-
-void handleGUI(unsigned char *s, unsigned short sl)
-{
-    ConsolePuts("\n===handle from gui\n", -1);
-    ConsolePutsHexStr(s, sl);
-    unsigned char check = 0;
-    int a = ProtocolGetOption(s,sl,99,&check);
-    if(check == 1)
-    {
-        unsigned char udpAdd[8] = {0};
-        trProtocol verPro;
-        ProtocolInit(&verPro);
-        ProtocolSetDestAddress(&verPro,3);
-        ProtocolSetSourceAddress(&verPro,1);
-        ProtocolSetType(&verPro,2);
-        ProtocolSetOptionChar(&verPro, 100, 1);
-        ProtocolSetPayload(&verPro,sizeof(udpAdd),udpAdd);
-        ProtocolPackage(&verPro);
-        ConsolePuts("\n===dsp to gui version\n", -1);
-        ConsolePutsHexStr(verPro.message, verPro.length);
-        lwip_tcp_send_gui(verPro.message,verPro.length);
-    }
-    else if(check == 3)
-    {
-        touch_closegui();
-    }
-}
 

+ 3 - 0
code/hlx/usbhspecific.c

@@ -242,11 +242,13 @@ unsigned int USBHSPECIFICWrite(unsigned char *pucData, unsigned int ulSize)
 //*****************************************************************************
 #include <stdio.h>
 #include "device/console.h"
+#include "nim_config.h"
 unsigned char DataBuffer1[64] = {'\0'};
 static void *
 SPECIFICDriverOpen(tUSBHostDevice *pDevice, unsigned int ulInstance)
 {
     ConsolePuts("SPECIFICDriverOpen...\r\n",-1);
+    handleUSBLineStatus(1);
     printf("SPECIFICDriverOpen...\r\n");
     unsigned int ulIndex = 0;
 
@@ -285,6 +287,7 @@ SPECIFICDriverClose(void *pvInstance)
 {    
     g_SPECIFICInstance.isConnected = false;
     ConsolePuts("SPECIFICDriverClose...\r\n",-1);
+    handleUSBLineStatus(0);
     printf("SPECIFICDriverClose...\r\n");
 //    tSPECIFICInstance *pSPECIFICInstance;
 //    pSPECIFICInstance = (tSPECIFICInstance *)pvInstance;

+ 2 - 1
nim_config.h

@@ -27,6 +27,7 @@
 #include "device/lwip.h"
 #include "device/touch.h"
 #include "algorithm/emg_util.h"
+#include "business/handle_bus.h"
 
 
 #define SYSCLK_1_FREQ     (456000000)
@@ -45,7 +46,7 @@
 
 #define UI_UDP_PORT                 8000
 
-#define TOUCH_TIME                  3000    //触控时间 3s
+#define TOUCH_TIME                  2000    //触控时间 3s
 
 #define WATCH_TIME                  10000    //看门时间 10s