hx3918驱动修改
authorp15692025051 <846603105@qq.com>
Fri, 11 Aug 2023 01:31:41 +0000 (09:31 +0800)
committerp15692025051 <846603105@qq.com>
Fri, 11 Aug 2023 01:31:41 +0000 (09:31 +0800)
2.sdk/Helios_SDK/components/GH301x/gh3011_example_port.c
2.sdk/Helios_SDK/components/HX3918/hx3918.c
2.sdk/Helios_SDK/components/HX3918/hx3918.h
2.sdk/Helios_SDK/components/HX3918/hx3918_factory_test.c
2.sdk/Helios_SDK/components/HX3918/hx3918_prox.h
2.sdk/Helios_SDK/services/microPython/ports/quectel/core/source/audio_record.c
2.sdk/Helios_SDK/services/microPython/ports/quectel/core/source/modGsensor_SC7A20.c
2.sdk/Helios_SDK/services/microPython/ports/quectel/core/source/modHsensor_HX3918.c [new file with mode: 0644]

index 10a7361..65c478a 100644 (file)
@@ -156,7 +156,10 @@ void hal_gh30x_delay_us(uint16_t us_cnt)
        //      i = cnt;
        //      while (i--);
        // }
-       Helios_msleep(us_cnt/1000);
+       //Helios_msleep(us_cnt/1000);
+       if (us_cnt > 0) {
+        mp_hal_delay_us(us_cnt);
+    }
        //code implement by user
 }
 
index edf89aa..ff0f7bc 100644 (file)
 #include "helios_debug.h"
 
 #define HX3918_ADDR 0x44
-#define HX3918_DEBUG  1
-#if HX3918_DEBUG == 1
-#define HX3918_LOG(msg, ...)      custom_log("HX3918", msg, ##__VA_ARGS__)
-#endif
+
 //#include "tyhx_hrs_custom.h"
 #ifdef SAR_ALG_LIB
 #include "hx3918_prox.h"
@@ -62,15 +59,13 @@ int32_t PPG_buf_vec[8];
 #include "hrv_testvec.h"
 #endif
 
-#ifdef GSENSER_DATA
-//#include "lis3dh_drv.h"
-#endif
 tyhx_hrsresult_t hrsresult;
 
 #ifdef GSENSER_DATA
 volatile int16_t gsen_fifo_x[32];  //ppg time 330ms..330/40 = 8.25
 volatile int16_t gsen_fifo_y[32];
 volatile int16_t gsen_fifo_z[32];
+int16_t gsen_fifo_count = 0;
 #else
 int16_t gen_dummy[32] = {0};
 #endif
@@ -145,12 +140,10 @@ void display_refresh(void)
 }
 void hx3918_delay_us(uint32_t us)
 {
-#ifdef  TYHX_DEMO
-    nrf_delay_us(us);
-#endif
-    if(us < 1000)
-        us =1000;
-    Helios_msleep(us/1000);
+
+    if (us > 0) {
+        mp_hal_delay_us(us);
+    }
 }
 
 void hx3918_delay(uint32_t ms)
@@ -651,29 +644,92 @@ void hx3918_gesensor_Int_handle(void)
 {
     //readFifoByHrsModule();
 #ifdef GSENSER_DATA
-    uint8_t ii = 0;
-    AxesRaw_t gsen_buf;
-    if(work_mode_flag == WEAR_MODE)
+    // uint8_t ii = 0;
+    // AxesRaw_t gsen_buf;
+    // if(work_mode_flag == WEAR_MODE)
+    // {
+    //     return;
+    // }
+
+    // //LIS3DH_GetAccAxesRaw(&gsen_buf);
+
+    // for(ii=0; ii<9; ii++)
+    // {
+    //     gsen_fifo_x[ii] = gsen_fifo_x[ii+1];
+    //     gsen_fifo_y[ii] = gsen_fifo_y[ii+1];
+    //     gsen_fifo_z[ii] = gsen_fifo_z[ii+1];
+    // }
+    //     gsen_fifo_x[9] = gsen_buf.AXIS_X>>1;
+    //     gsen_fifo_y[9] = gsen_buf.AXIS_Y>>1;
+    //     gsen_fifo_z[9] = gsen_buf.AXIS_Z>>1;
+    // //SEGGER_RTT_printf(0,"gsen_x=%d gsen_y=%d gsen_z=%d\r\n", \
+    // gsen_fifo_x[9],gsen_fifo_y[9],gsen_fifo_z[9]);
+#endif
+}
+// volatile int16_t gsen_fifo_x[32];  //ppg time 330ms..330/40 = 8.25
+// volatile int16_t gsen_fifo_y[32];
+// volatile int16_t gsen_fifo_z[32];
+void hx3918_set_gsen_fifo(short *x,short *y,short *z, int count)
+{
+    
+    if(count > sizeof(gsen_fifo_y))
     {
-        return;
+        count = sizeof(gsen_fifo_y);
     }
+    for(int i = 0;i<count;i++)
+    {
 
-    LIS3DH_GetAccAxesRaw(&gsen_buf);
-
-    for(ii=0; ii<9; ii++)
+        gsen_fifo_x[i] =x[i];
+        gsen_fifo_y[i] =y[i];
+        gsen_fifo_z[i] =z[i]; 
+        //HX3918_LOG("count: %d %03d gsen_fifo_x:%d gsen_fifo_y:%d gsen_fifo_z:%d\n",count,i,gsen_fifo_x[i],gsen_fifo_y[i],gsen_fifo_z[i]);
+    }
+    gsen_fifo_count = count;
+}
+void hx3918_get_gsen_fifo(short *x, short *y,short *z,int count)
+{
+    int16_t gsen_fifo_x_tmp[32]= {0};
+    int16_t gsen_fifo_y_tmp[32]= {0};
+    int16_t gsen_fifo_z_tmp[32]= {0};
+    
+    if(gsen_fifo_count > sizeof(gsen_fifo_x_tmp))
+        gsen_fifo_count = sizeof(gsen_fifo_x_tmp);
+    for(int i = 0;i<gsen_fifo_count;i++)//备份
     {
-        gsen_fifo_x[ii] = gsen_fifo_x[ii+1];
-        gsen_fifo_y[ii] = gsen_fifo_y[ii+1];
-        gsen_fifo_z[ii] = gsen_fifo_z[ii+1];
+        gsen_fifo_x_tmp[i] = gsen_fifo_x[i] ;
+        gsen_fifo_y_tmp[i] = gsen_fifo_y[i];
+        gsen_fifo_z_tmp[i] = gsen_fifo_z[i]; 
     }
-        gsen_fifo_x[9] = gsen_buf.AXIS_X>>1;
-        gsen_fifo_y[9] = gsen_buf.AXIS_Y>>1;
-        gsen_fifo_z[9] = gsen_buf.AXIS_Z>>1;
-    //SEGGER_RTT_printf(0,"gsen_x=%d gsen_y=%d gsen_z=%d\r\n", \
-    gsen_fifo_x[9],gsen_fifo_y[9],gsen_fifo_z[9]);
-#endif
+    if(count > sizeof(gsen_fifo_y))
+        count = sizeof(gsen_fifo_y);
+    
+    for(int i = 0;i<count;i++)//复制
+    {
+        if(i<gsen_fifo_count)
+        {
+            x[i] = gsen_fifo_x[i];
+            y[i] = gsen_fifo_y[i];
+            z[i] = gsen_fifo_z[i];
+        }else
+        {
+            x[i] = gsen_fifo_x[gsen_fifo_count-1];
+            y[i] = gsen_fifo_y[gsen_fifo_count-1];
+            z[i] = gsen_fifo_z[gsen_fifo_count-1];
+        }
+         
+    }
+    if(gsen_fifo_count > count)//移位
+    {
+        for(int i = 0;i<gsen_fifo_count-count;i++)
+        {
+            gsen_fifo_x[i] = gsen_fifo_x_tmp[count+i];
+            gsen_fifo_y[i] = gsen_fifo_y_tmp[count+i];
+            gsen_fifo_z[i] = gsen_fifo_z_tmp[count+i];
+        }
+        gsen_fifo_count -= count;
+    }
+    
 }
-
 void agc_timeout_handler(void * p_context)
 {
 #ifdef TIMMER_MODE
@@ -827,14 +883,16 @@ void hx3918_hrs_ppg_Int_handle(void)
 #ifndef GSEN_40MS_TIMMER
     readFifoByHrsModule();
 #endif
+    hx3918_get_gsen_fifo(gsen_fifo_x_send,gsen_fifo_y_send,gsen_fifo_z_send,*count);
     for(ii=0; ii<*count; ii++)
     {
 #ifdef GSENSER_DATA
-        gsen_fifo_x_send[ii] = gsen_fifo_x[32-*count+ii];
-        gsen_fifo_y_send[ii] = gsen_fifo_y[32-*count+ii];
-        gsen_fifo_z_send[ii] = gsen_fifo_z[32-*count+ii];
+        // gsen_fifo_x_send[ii] = gsen_fifo_x[32-*count+ii];
+        // gsen_fifo_y_send[ii] = gsen_fifo_y[32-*count+ii];
+        // gsen_fifo_z_send[ii] = gsen_fifo_z[32-*count+ii];
+        
 #endif
-        //HX3918_LOG("%d %d %d %d %d %d\r\n", PPG_buf[ii],ir_buf[ii],gsen_fifo_x[ii],gsen_fifo_y[ii],gsen_fifo_z[ii],hrs_s_dat.agc_green);
+        //HX3918_LOG("ppg_Int_handle:%d %d %d %d %d %d\r\n", PPG_buf[ii],ir_buf[ii],gsen_fifo_x_send[ii],gsen_fifo_y_send[ii],gsen_fifo_z_send[ii]);
     }
     hrs_wear_status = hx3918_hrs_get_wear_status();
        hrsresult.wearstatus = hrs_wear_status;
@@ -1069,6 +1127,21 @@ void hx3918_hrv_ppg_Int_handle(void)
 
 
 #ifdef CHECK_TOUCH_LIB
+uint8_t hx3918_get_wear_ppg_Int_handle(void)
+{
+    uint8_t count = 0;
+    hx3918_wear_msg_code_t hx3918_check_mode_status = MSG_NO_WEAR;
+    count = hx3918_check_touch_read(&ppg_s_dat);
+    hx3918_check_mode_status = hx3918_check_touch(ppg_s_dat.s_buf,count);
+    hrsresult.wearstatus = hx3918_check_mode_status;
+    HX3918_LOG("wearstatus:%d\n",hrsresult.wearstatus);
+    if((count)&&(hrsresult.wearstatus == 0))
+    {
+        HX3918_LOG("count = %d, ir = %d\r\n",count,ppg_s_dat.s_buf[count-1]);
+        return 0;
+    }else
+        return 1;
+}
 void hx3918_wear_ppg_Int_handle(void)
 {
     uint8_t count = 0;
index 35fbfed..93cf642 100644 (file)
@@ -2,8 +2,13 @@
 #define _hx3918_H_
 #include <stdint.h>
 #include <stdbool.h>
-
-
+#include "helios_debug.h"
+#define HX3918_DEBUG  1
+#if HX3918_DEBUG == 1
+#define HX3918_LOG(msg, ...)      custom_log("HX3918", msg, ##__VA_ARGS__)
+#else
+#define HX3918_LOG(msg, ...)  
+#endif
 //#include "tyhx_mems.h"
 //#define DEBUG_PPG_ONLY     // 只用IR 判断佩带和非佩戴, 靠近感应功能关闭,  但是HX3918_PROX.C要合入编译
 /********************* alg_swi *********************
@@ -39,7 +44,7 @@ NEW_GSEN_SCHME     DEMO调试使用, 客户关闭
 //#define INT_MODE                                                     //fifo_all_most_full
 //#define TYHX_DEMO           // for ty demo
 //****************** gsen_cgf *********************//
-//#define GSENSER_DATA
+#define GSENSER_DATA
 #define GSEN_40MS_TIMMER
 #define NEW_GSEN_SCHME
 
@@ -306,6 +311,7 @@ void hx3918_gesensor_Int_handle(void);
 void hx3918_spo2_ppg_init(void);
 void hx3918_spo2_ppg_Int_handle(void);
 void hx3918_wear_ppg_Int_handle(void);
+uint8_t hx3918_get_wear_ppg_Int_handle(void);
 void hx3918_ft_hrs_Int_handle(void);
 void hx3918_ft_spo2_Int_handle(void);
 void hx3918_hrs_ppg_init(void);
@@ -347,4 +353,5 @@ int16_t hx3918xxx_read_two_reg_low_triple(uint8_t addh, uint8_t addl);
 void display_refresh(void);
 extern char hx3918_ppg_timerout_flag ;
 extern char hx3918_agc_timerout_flag ;
+void hx3918_set_gsen_fifo(short *x,short *y,short *z, int count);
 #endif
index 9652f6a..76478c3 100644 (file)
@@ -336,7 +336,6 @@ bool hx3918_chip_check_id(void)
     }
     return false;
 }
-
 factory_result_t hx3918_factroy_test(uint32_t  test_mode)
 {
     factory_result_t factory_reslts = {0,0,0};
@@ -494,6 +493,6 @@ factory_result_t hx3918_factroy_test(uint32_t  test_mode)
     factory_reslts.green_data_final = green_final;
     factory_reslts.red_data_final = red_final;
     factory_reslts.ir_data_final = ir_final;
-    DEBUG_PRINTF("%d %d %d\r\n",factory_reslts.green_data_final, factory_reslts.red_data_final,factory_reslts.ir_data_final);
+    HX3918_LOG("%d %d %d\r\n",factory_reslts.green_data_final, factory_reslts.red_data_final,factory_reslts.ir_data_final);
     return factory_reslts;
 }
index 1b2d331..9f4eeed 100644 (file)
 
 #define PROX_CHIPID         0x27
 
-#define HX3918_IR_AIR       6000                  //СÓÚÕâ¸öÖµ´ú±íÍÑÍó
+#define HX3918_IR_AIR       6000                  //小于这个值代表脱腕
 
-#define NORMALWEARDIFF      12000                 //Õý³£Åå´÷²âÊÔµ½µÄƽ¾ùÖµ, ¿íËÉÅå´÷
-#define WEARTHRES              NORMALWEARDIFF/2      //ÆÕͨÅå´÷ãÐÖµ
-#define SLEEPWEARTHRES      NORMALWEARDIFF*0.3    //˯ÃßÅå´÷ãÐÖµ
+#define NORMALWEARDIFF      12000                 //正常佩戴测试到的平均值, 宽松佩戴
+#define WEARTHRES              NORMALWEARDIFF/2      //普通佩戴阈值
+#define SLEEPWEARTHRES      NORMALWEARDIFF*0.3    //睡眠佩戴阈值
 
-#define AUTOCALITHRES       -3000                 //ºǫ́×Ô¶¯Ð£×¼LPºÍBLãÐÖµ
-#define ONAIRTHRESLOW       -2500                  //¶Ô¿ÕµÄLP BL ×îСֵ
-#define ONAIRTHRESHIGH       1300                  //¶Ô¿ÕµÄLP BL ×î´óÖµ
+#define AUTOCALITHRES       -3000                 //后台自动校准LP和BL阈值
+#define ONAIRTHRESLOW       -2500                  //对空的LP BL 最小值
+#define ONAIRTHRESHIGH       1300                  //对空的LP BL 最大值
 
-#define DIFFDETECT          200                   //BL ºÍ NV_BASE ±È½ÏµÄ²îÖµ
+#define DIFFDETECT          200                   //BL 和 NV_BASE 比较的差值
 
 #define CH0_HIGH_DIFF          SLEEPWEARTHRES
 #define CH0_LOW_DIFF           SLEEPWEARTHRES*0.7
@@ -68,8 +68,8 @@
 
 typedef enum
 {
-    prox_no_detect = 0,   // ¿¿½ü¸ÐӦûÓмì²âµ½
-    prox_detect =1,       // ¿¿½ü¸ÐÓ¦¼ì²âµ½
+    prox_no_detect = 0,   // 靠近感应没有检测到
+    prox_detect =1,       // 靠近感应检测到
 } prox_result_t;
 
 typedef enum {
@@ -87,8 +87,8 @@ extern int16_t nv_base_data[2];
 extern uint32_t ai_tune_cnt[2];
 extern int16_t nv_wear_offset[2];
 
-extern uint32_t fds_read[6]; //flash ¶Á
-extern uint32_t fds_write[6];//flash д
+extern uint32_t fds_read[6]; //flash 读
+extern uint32_t fds_write[6];//flash 写
 extern uint8_t wear_detect_mode;
 
 
index a117799..f161743 100644 (file)
@@ -833,7 +833,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_helios_record_file_end_callback_obj, helios_
 
 
 
-#ifdef PLAT_ASR
+
 STATIC mp_obj_t helios_audio_record_gain(size_t n_args, const mp_obj_t *args)
 {
        //audio_record_obj_t *self = MP_OBJ_TO_PTR(args[0]);
@@ -854,7 +854,7 @@ STATIC mp_obj_t helios_audio_record_gain(size_t n_args, const mp_obj_t *args)
        return mp_obj_new_int(0);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_helios_record_file_gain_obj,3,3, helios_audio_record_gain);
-
+#ifdef PLAT_ASR
 /*=============================================================================*/
 /* FUNCTION: helios_amrenc_dtx_enable                                          */
 /*=============================================================================*/
@@ -978,8 +978,9 @@ STATIC const mp_rom_map_elem_t audio_locals_dict_table[] = {
     { MP_ROM_QSTR(MP_QSTR_exists),             MP_ROM_PTR(&mp_helios_record_file_exists_obj)   },
     { MP_ROM_QSTR(MP_QSTR_isBusy),             MP_ROM_PTR(&mp_helios_record_file_isBusy_obj)   },
     { MP_ROM_QSTR(MP_QSTR_end_callback),               MP_ROM_PTR(&mp_helios_record_file_end_callback_obj)     },
+       { MP_ROM_QSTR(MP_QSTR_gain),            MP_ROM_PTR(&mp_helios_record_file_gain_obj)     },
 #ifdef PLAT_ASR
-    { MP_ROM_QSTR(MP_QSTR_gain),               MP_ROM_PTR(&mp_helios_record_file_gain_obj)     },
+    //{ MP_ROM_QSTR(MP_QSTR_gain),             MP_ROM_PTR(&mp_helios_record_file_gain_obj)     },
     { MP_ROM_QSTR(MP_QSTR_WAV), MP_ROM_INT(HELIOS_AUDIO_FORMAT_WAVPCM) },
     { MP_ROM_QSTR(MP_QSTR_AMRNB), MP_ROM_INT(HELIOS_AUDIO_FORMAT_AMRNB) },
     { MP_ROM_QSTR(MP_QSTR_AMRWB), MP_ROM_INT(HELIOS_AUDIO_FORMAT_AMRWB) },
index 9659342..f3307f9 100644 (file)
@@ -37,7 +37,9 @@
 
 // #endif
 
-
+#if CONFIG_HX3918
+#include "hx3918.h"
+#endif
 #include "helios_debug.h"
 #define SC7A20_LOG(msg, ...)      custom_log("SC7A20", msg, ##__VA_ARGS__)
 #include "callbackdeal.h"
@@ -392,7 +394,7 @@ unsigned int  SL_SC7A20_PEDO_KCAL_WRIST_SLEEP_SWAY_L_ALGO(void)
        //SC7A20_LOG("SC7A20_FIFO_NUM :%d  SC7A20_XYZ_Buf_tmp_len: %d SC7A20_XYZ_Buf_tmp[0]len:%d!!!!!!!!!!!!\n", SC7A20_FIFO_NUM,SC7A20_XYZ_Buf_tmp_len,sizeof(SC7A20_XYZ_Buf_tmp[0]));
        for(i = 0; i < SC7A20_FIFO_NUM; i++)
        {
-               if(i+SC7A20_XYZ_Buf_tmp_len < 31)
+               if(i+SC7A20_XYZ_Buf_tmp_len < 32)
                {
                        SC7A20_XYZ_Buf_tmp[0][i+SC7A20_XYZ_Buf_tmp_len] = SC7A20_XYZ_Buf[0][i];
                        SC7A20_XYZ_Buf_tmp[1][i+SC7A20_XYZ_Buf_tmp_len] = SC7A20_XYZ_Buf[1][i];
@@ -408,7 +410,20 @@ unsigned int  SL_SC7A20_PEDO_KCAL_WRIST_SLEEP_SWAY_L_ALGO(void)
        //memcpy(SC7A20_XYZ_Buf_tmp+SC7A20_XYZ_Buf_tmp_len*6,SC7A20_XYZ_Buf,SC7A20_FIFO_NUM);
        
        SC7A20_XYZ_Buf_tmp_len += SC7A20_FIFO_NUM;
-       //dump2
+
+       #if CONFIG_HX3918
+       if(SC7A20_XYZ_Buf_tmp_len >= 25)
+       {
+               for(i = 0; i < SC7A20_XYZ_Buf_tmp_len; i++)
+               {
+                       SC7A20_XYZ_Buf_tmp[0][i] = SC7A20_XYZ_Buf_tmp[0][i]/2;
+                       SC7A20_XYZ_Buf_tmp[1][i] = SC7A20_XYZ_Buf_tmp[1][i]/2;
+                       SC7A20_XYZ_Buf_tmp[2][i] = SC7A20_XYZ_Buf_tmp[2][i]/2;
+               }
+               hx3918_set_gsen_fifo(SC7A20_XYZ_Buf_tmp[0],SC7A20_XYZ_Buf_tmp[1],SC7A20_XYZ_Buf_tmp[2],SC7A20_XYZ_Buf_tmp_len);
+               SC7A20_XYZ_Buf_tmp_len = 0;
+       }
+       #endif
        #if CONFIG_GH301x
        // s_sc7a20_read_data_counter++;  
        if(SC7A20_XYZ_Buf_tmp_len >= 25)
diff --git a/2.sdk/Helios_SDK/services/microPython/ports/quectel/core/source/modHsensor_HX3918.c b/2.sdk/Helios_SDK/services/microPython/ports/quectel/core/source/modHsensor_HX3918.c
new file mode 100644 (file)
index 0000000..81590a2
--- /dev/null
@@ -0,0 +1,529 @@
+/**************************************************************************
+ @file Hsensor_HX3918.c
+ @brief        Gsensor.
+
+
+**************************************************************************/
+
+//////////////////////////////////////////////////////////////////////////////
+// Includes
+//////////////////////////////////////////////////////////////////////////////
+#include "autoconf.h"
+#include <stdio.h>
+#include "runtime.h"
+#include <stdlib.h>
+#include <math.h>
+#include "mphalport.h"
+#include "compile.h"
+#include "repl.h"
+#include "mperrno.h"
+#include "binary.h"
+
+#include <string.h>
+
+#if CONFIG_HX3918
+
+#include "helios_gpio.h"
+#include "helios_iic.h"
+#include "helios_lcd.h"
+#include "helios_fs.h"
+#include "helios_pin.h"
+#include "helios_timer.h"
+
+
+
+#include "helios_debug.h"
+#include "hx3918.h"
+#include "autoconf.h"
+extern void sensor_thread_entry(void * argv);
+extern Helios_ThreadAttr ThreadAttr;
+extern Helios_Thread_t sensor_thread_id ;
+#define HX3918_LOG(msg, ...)      custom_log("HX3918", msg, ##__VA_ARGS__)
+//////////////////////////////////////////////////////////////////////////////
+typedef struct _HX3918_obj_t
+{
+    mp_obj_base_t base;
+
+    uint8_t i2c_no;
+       uint8_t i2c_mode;
+    uint8_t irq;
+       uint8_t power;
+
+} HX3918_obj_t;
+
+STATIC HX3918_obj_t *g_HX3918 = NULL;
+static unsigned char is_HX3918 = 0;//是否存在IC
+static unsigned char is_open_HX3918 = 0; //是否打开测试
+static unsigned char s_HX3918_heart_rate = 0x0;//心律
+static unsigned char s_HX3918_high_blood = 0x0;//血压/高压
+static unsigned char s_HX3918_low_blood = 0x0;//血压/低压
+static unsigned char s_HX3918_blood_oxygen = 0x0;//血氧
+static unsigned char s_HX3918_HRV = 0x0;//hrv
+static unsigned char s_HX3918_wear_status = 0;//佩戴状态
+#define HEART_RATE_BUFF_LEN 10
+unsigned char s_HX3918_heart_rate_buff[HEART_RATE_BUFF_LEN]={0};
+unsigned short s_HX3918_heart_rate_buff_count = 0;
+
+char HX3918_sleep_flag =  0;
+#include "helios_extint.h"
+
+int sensor_timer = 0;
+#define TIMER_TIME_MS  100 //10ms
+#define IS_CYCLICAL 1
+uint8_t HX3918_irq_flag = 0;
+uint32_t HX3918_run_mode = NULL_MODE;
+uint32_t HX3918_jobs_mode = NULL_MODE;
+#define RUN_MODE_CLOSE 0xffff
+static uint32_t timer_Gsensor_count = 0;
+#if CONFIG_SC7A20
+extern int SC7A20_init(void);
+extern signed char SL_SC7A20_PEDO_KCAL_WRIST_SLEEP_SWAY_L_ALGO(void);
+extern unsigned char Gsensor_init_flag ;
+
+#endif
+int ir_wear = 0;//红外佩戴状态
+int cap_wear = 0;//电容佩戴状态
+int wear_time = 0;
+// int test_count1 = 0;
+// int test_count2 = 0;
+void HX3918_test(uint32_t mode);
+void HX3918_close(void);
+void sensor_timerout_cb(void);
+
+void HX3918_thread_do(void)
+{
+       if(HX3918_run_mode == close_MODE)
+       {
+               HX3918_close();
+               //HX3918_LOG("HX3918_irq_cb: 1111111111111!!!!!!!!\r\n");
+               HX3918_run_mode = NULL_MODE;
+               HX3918_jobs_mode = NULL_MODE;
+               hx3918_ppg_timerout_flag = 0;
+               hx3918_agc_timerout_flag = 0;
+               return;
+       }
+       if(HX3918_run_mode != NULL_MODE )
+       {
+               if(HX3918_jobs_mode != NULL_MODE)//没关闭就打开其他模式,先关闭一次
+               {
+                       HX3918_close();
+                       Helios_msleep(20);
+                       HX3918_jobs_mode = NULL_MODE;
+               }
+               HX3918_test(HX3918_run_mode);
+               HX3918_jobs_mode = HX3918_run_mode;
+               HX3918_run_mode = NULL_MODE;
+       }
+       if(is_HX3918 == 1 && is_open_HX3918 == 1)//处理Hsensor定时数据
+       {
+
+               if(hx3918_ppg_timerout_flag)
+               {
+                       hx3918_ppg_Int_handle();
+                       hx3918_ppg_timerout_flag = 0;
+               }
+               if(hx3918_agc_timerout_flag)
+               {
+                       agc_timeout_handler(NULL);
+                       hx3918_agc_timerout_flag =0;
+               }
+               if(timer_Gsensor_count>(500/TIMER_TIME_MS))//0.5秒定时,轮询更新Gsensor数据
+               {
+                       timer_Gsensor_count = 0;
+                       //Helios_msleep(5);
+                       #if CONFIG_SC7A20
+                       SL_SC7A20_PEDO_KCAL_WRIST_SLEEP_SWAY_L_ALGO();
+                       #endif
+
+               }
+       }
+       else
+       {
+               // if(hx3918_ppg_timerout_flag)//测试模式
+               // {
+               //      hx3918_ppg_Int_handle();
+               //      hx3918_ppg_timerout_flag = 0;
+               // }
+               wear_time++;
+               if(wear_time > 3)
+               {
+                       wear_time = 0;
+                       ir_wear = hx3918_get_wear_ppg_Int_handle();
+                       cap_wear = hx3918_report_prox_status();
+                       HX3918_LOG("ir_wear:%d cap_wear%d\n",ir_wear,cap_wear);
+               }
+               
+               #if CONFIG_SC7A20
+               if(ir_wear == 1 || cap_wear==1)
+                       SL_SC7A20_PEDO_KCAL_WRIST_SLEEP_SWAY_L_ALGO();
+               #endif
+               Helios_msleep(1000);
+               
+               if(HX3918_sleep_flag == 1)
+               {
+                       HX3918_sleep_flag = 0;
+                       Helios_msleep(3000);
+               }
+       }
+       Helios_msleep(5);
+
+}
+void HX3918_result_set_heart_rate(unsigned char heart_rate)
+{
+//    s_HX3918_heart_rate_buff[HEART_RATE_BUFF_LEN]={0};
+// uint16 s_HX3918_heart_rate_buff_count = 0;
+   uint32_t i,tmp = 0;
+   s_HX3918_heart_rate_buff[s_HX3918_heart_rate_buff_count] = heart_rate;
+   s_HX3918_heart_rate_buff_count++;
+   for(i=0;i<s_HX3918_heart_rate_buff_count;i++)//算总和,取平均值
+   {
+      tmp+=s_HX3918_heart_rate_buff[i];
+   }
+   s_HX3918_heart_rate = tmp/s_HX3918_heart_rate_buff_count;//取平均值
+   //s_HX3918_heart_rate = heart_rate; 
+   if(s_HX3918_heart_rate_buff_count >=  HEART_RATE_BUFF_LEN)
+      s_HX3918_heart_rate_buff_count = 0;
+   s_HX3918_high_blood = (uint8_t)(heart_rate * 1.5);
+   
+   s_HX3918_low_blood = (uint8_t)(heart_rate * 0.95);
+}
+void HX3918_result_set_wear_status(unsigned char wear_status)
+{
+   s_HX3918_wear_status = wear_status; 
+}
+void HX3918_result_set_blood_oxygen(unsigned char blood_oxygen)
+{
+   s_HX3918_blood_oxygen = blood_oxygen;    
+}
+ 
+void HX3918_result_set_hrv(unsigned char hrv)
+{
+   s_HX3918_HRV = hrv;    
+}
+STATIC mp_obj_t mp_activate_HX3918(mp_obj_t self_in)
+{
+    HX3918_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+    g_HX3918 = self;
+    return mp_const_none;
+}
+
+STATIC mp_obj_t HX3918_make_new(const mp_obj_type_t *type,
+                               size_t n_args,
+                               size_t n_kw,
+                               const mp_obj_t *all_args)
+{
+    enum{
+        ARG_i2c_no,
+        ARG_i2c_mode,
+        ARG_irq,
+               ARG_power,
+    };
+
+    static const mp_arg_t allowed_args[] = {
+        { MP_QSTR_i2c_no, MP_ARG_INT, {.u_int = 0}},
+        { MP_QSTR_i2c_mode, MP_ARG_INT, {.u_int = 1}},
+        { MP_QSTR_irq, MP_ARG_INT, {.u_int = 27}},
+               { MP_QSTR_power, MP_ARG_INT, {.u_int = 30}},
+    };
+
+    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+    mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+    HX3918_obj_t *self = m_new_obj(HX3918_obj_t);
+    self->base.type = type;
+
+    self->i2c_no = args[ARG_i2c_no].u_int;
+       self->i2c_mode = args[ARG_i2c_mode].u_int;
+    self->irq = args[ARG_irq].u_int;
+
+       self->power = args[ARG_power].u_int;
+    return MP_OBJ_FROM_PTR(self);
+}
+
+STATIC mp_obj_t mp_HX3918_init(mp_obj_t self_in);
+STATIC mp_obj_t mp_HX3918_deinit(mp_obj_t self_in);
+
+
+
+
+void sensor_timerout_cb(void)
+{
+       timer_Gsensor_count++;  
+}
+void HX3918_irq_cb(void)
+{
+       HX3918_irq_flag = 1;
+       //HX3918_LOG("HX3918_irq_cb: 1111111111111!!!!!!!!\r\n");
+       Helios_ExtInt_Enable(g_HX3918->irq);
+}
+/*
+**函数名:HX3918_Init
+**功能:初始化HX3918
+*/
+mp_obj_t mp_HX3918_init(mp_obj_t self_in)
+{
+    HX3918_obj_t *self = MP_OBJ_TO_PTR(self_in);
+       g_HX3918 = self;
+    mp_activate_HX3918(self_in);
+       uint8_t index = 0;
+       uint8_t device_id = 0;
+       Helios_GPIOInitStruct gpio_struct = {0};
+       gpio_struct.dir = HELIOS_GPIO_INPUT;
+       gpio_struct.pull = HELIOS_PULL_UP;
+       gpio_struct.value = HELIOS_LVL_LOW;
+
+       // ret = Helios_GPIO_Init((Helios_GPIONum) self->irq, &gpio_struct);
+       // HX3918_LOG("cst816 irq=%d, irq ret: %d\n", self->irq, ret);
+
+       gpio_struct.pull = HELIOS_PULL_NONE;
+       gpio_struct.dir = HELIOS_GPIO_OUTPUT;
+       Helios_GPIO_Init(self->power, &gpio_struct);//ldo
+
+       gpio_struct.dir = HELIOS_GPIO_OUTPUT;
+       gpio_struct.pull = HELIOS_PULL_NONE;
+       gpio_struct.value = HELIOS_LVL_LOW;
+       //Helios_GPIO_Init(25, &gpio_struct);//体温
+       Helios_GPIO_Init(31, &gpio_struct);//复位
+
+       gpio_struct.dir = HELIOS_GPIO_INPUT;
+       gpio_struct.pull = HELIOS_PULL_NONE;
+       gpio_struct.value = HELIOS_LVL_LOW;
+       Helios_GPIO_Init(self->irq, &gpio_struct);//IRQ
+
+       Helios_ExtInt_Deinit(self->irq);//关闭中断
+       Helios_ExtIntStruct extint_struct1 = {0};
+       // Helios_TriggerMode gpio_trigger;     //GPIO interrupt trigger mode  (0-EDGE_TRIGGER  1-LEVEL_TRIGGER)
+       // Helios_DebounceMode gpio_debounce;   //GPIO interrupt trigger debounce enable/disable  (0-disable  1-enable)
+       // Helios_EdgeMode gpio_edge;                   //GPIO interrupt edge/level trigger configure  (0-EDGE_RISING 1-EDGE_FALLING 2-EDGE_BOTH)
+       // Helios_PullMode gpio_pull;                   //GPIO get input pull (0-PULL_NONE 1-PULL_UP 2-PULL_DOWN)
+       // void *eint_cb;                                               //GPIO interrupt cb
+       // void *wakeup_eint_cb;                                //GPIO interrupt cb_ctx
+       extint_struct1.gpio_trigger = HELIOS_EDGE_TRIGGER;
+       extint_struct1.gpio_edge = HELIOS_EDGE_RISING;
+       extint_struct1.gpio_debounce = HELIOS_DEBOUNCE_EN;
+       extint_struct1.gpio_pull = HELIOS_PULL_NONE;
+       extint_struct1.eint_cb = HX3918_irq_cb;
+       extint_struct1.wakeup_eint_cb = NULL;
+       
+       
+       Helios_I2C_Init((Helios_I2CEnum)self->i2c_no, HELIOS_FAST_MODE);
+       Helios_GPIO_SetLevel(self->power, HELIOS_LVL_LOW);
+       Helios_msleep(10);
+       Helios_GPIO_SetLevel(self->power, HELIOS_LVL_HIGH);
+       Helios_GPIO_SetLevel(31, HELIOS_LVL_LOW);
+       Helios_msleep(20);
+       Helios_GPIO_SetLevel(31, HELIOS_LVL_HIGH);
+               
+       if(sensor_timer == 0)
+       {
+               sensor_timer = Helios_Timer_init( (void* )sensor_timerout_cb, NULL);
+       }
+       if(sensor_thread_id == 0)
+       {
+               HX3918_LOG("mp_HX3918_open_heart Helios_Thread_Create\r\n");
+               sensor_thread_id = Helios_Thread_Create(&ThreadAttr);
+       }       
+       
+       
+       bool ret = 0;
+       ret = hx3918_chip_check();
+       if(!ret){
+               //mp_raise_ValueError("i2c init fail ");
+               HX3918_LOG("mp_HX3918_init: i2c init fail\r\n");
+               is_HX3918 = 0;
+       }else
+       {
+               HX3918_LOG("mp_HX3918_init: OK\r\n");
+               is_HX3918 = 1;
+               Helios_ExtInt_Init(self->irq,  &extint_struct1);//初始化中断
+               Helios_ExtInt_Enable(self->irq);//打开中断
+               hx3918_init(WEAR_MODE);//打开佩戴检测
+               #if CONFIG_SC7A20
+               if(Gsensor_init_flag == 0)//初始化Gsensor
+                       SC7A20_init();
+               #endif
+       }
+       
+    return mp_obj_new_int(1);
+}
+
+STATIC mp_obj_t mp_HX3918_deinit(mp_obj_t self_in)
+{
+    HX3918_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+       HX3918_close();
+       Helios_GPIO_Deinit((Helios_GPIONum)self->irq);
+       Helios_ExtInt_Deinit(self->irq);
+       // Helios_I2C_Deinit((Helios_I2CEnum) self->i2c_no);
+       Helios_GPIO_SetLevel(self->power, HELIOS_LVL_LOW);
+
+    return mp_const_none;
+}
+void HX3918_test(uint32_t mode)
+{
+       s_HX3918_heart_rate = 0x0;
+       s_HX3918_heart_rate_buff_count = 0; 
+       s_HX3918_high_blood = 0x0;
+       s_HX3918_low_blood = 0x0;
+       s_HX3918_HRV = 0;
+       s_HX3918_wear_status = 0;
+       s_HX3918_blood_oxygen = 0x0;
+       
+       if(hx3918_init(mode) == true)
+       {
+               is_open_HX3918 = 1;
+               if(sensor_timer== 0)
+               {
+                       sensor_timer = Helios_Timer_init( (void* )sensor_timerout_cb, NULL);
+               }
+               
+               Helios_Timer_Start(sensor_timer, TIMER_TIME_MS, IS_CYCLICAL);
+       }
+               
+       
+}
+//打开心律测试
+STATIC mp_obj_t mp_HX3918_open_heart(mp_obj_t self_in)
+{
+
+       HX3918_run_mode = HRS_MODE;
+    return mp_obj_new_int(1);
+}
+//打开血氧测试
+STATIC mp_obj_t mp_HX3918_open_blood_oxygen(mp_obj_t self_in)
+{
+       HX3918_run_mode = SPO2_MODE     ;
+    return mp_obj_new_int(1);
+}
+//打开HRV测试
+STATIC mp_obj_t mp_HX3918_open_hrv(mp_obj_t self_in)
+{
+
+       //HX3918_run_mode = HRV_MODE;
+    return mp_obj_new_int(1);
+}
+void HX3918_close(void)
+{
+       if(is_open_HX3918 == 1)
+       {
+               
+               //hx3918_hrs_disable
+               if(HX3918_jobs_mode == HRS_MODE)
+               {
+                       hx3918_hrs_disable();
+               }else if(HX3918_jobs_mode == SPO2_MODE)
+               {
+                       hx3918_spo2_disable();
+               }else if(HX3918_jobs_mode == HRV_MODE)
+               {
+                       //hx3918_hrv_disable();
+               }
+               if(sensor_timer != 0)
+               {
+                       Helios_Timer_Stop(sensor_timer);
+               }
+       }
+       is_open_HX3918 = 0;
+       Helios_GPIO_SetLevel(g_HX3918->power, HELIOS_LVL_LOW);
+       Helios_msleep(5);
+       Helios_GPIO_SetLevel(g_HX3918->power, HELIOS_LVL_HIGH);
+       Helios_msleep(20);
+       hx3918_init(WEAR_MODE);//打开佩戴检测
+
+}
+//关闭hsensor
+STATIC mp_obj_t mp_HX3918_close(mp_obj_t self_in)
+{
+    HX3918_obj_t *self = MP_OBJ_TO_PTR(self_in);
+       
+       HX3918_run_mode = close_MODE;
+       //Helios_GPIO_SetLevel(self->power, HELIOS_LVL_LOW);
+    return mp_obj_new_int(1);
+}
+//获取心律
+STATIC mp_obj_t mp_HX3918_get_heart(mp_obj_t self_in)
+{
+    return mp_obj_new_int(s_HX3918_heart_rate);
+}
+//获取血氧
+STATIC mp_obj_t mp_HX3918_get_blood_oxygen(mp_obj_t self_in)
+{
+    return mp_obj_new_int(s_HX3918_blood_oxygen);
+}
+//获取血压
+STATIC mp_obj_t mp_HX3918_get_blood_pressure(mp_obj_t self_in)
+{
+    mp_obj_t coordinate_list[2] = {s_HX3918_high_blood, s_HX3918_low_blood,};
+    return mp_obj_new_list(2, coordinate_list);
+}
+//获取hrv
+STATIC mp_obj_t mp_HX3918_get_hrv(mp_obj_t self_in)
+{
+    return mp_obj_new_int(s_HX3918_HRV);
+}
+//hx3918_report_prox_status();获取电容佩戴状态
+//SENSOR_ERROR_T hx3918_check_touch_enable(void);获取红外佩戴状态
+//获取佩戴状态
+STATIC mp_obj_t mp_HX3918_get_wear(mp_obj_t self_in)
+{
+       
+    return mp_obj_new_int(s_HX3918_wear_status);
+}
+//获取所有数据  心律、血氧、高压、低压、HRV、佩戴状态
+STATIC mp_obj_t mp_HX3918_get_all_data(mp_obj_t self_in)
+{
+    mp_obj_t coordinate_list[6] = {mp_obj_new_int((int)s_HX3918_heart_rate),mp_obj_new_int((int)s_HX3918_blood_oxygen),\
+                                                                  mp_obj_new_int((int)s_HX3918_high_blood),mp_obj_new_int((int)s_HX3918_low_blood),\
+                                                                  mp_obj_new_int((int)s_HX3918_HRV),mp_obj_new_int((int)s_HX3918_wear_status)};
+    return mp_obj_new_list(6, coordinate_list);
+}
+//设置sleep
+STATIC mp_obj_t mp_HX3918_set_sleep(mp_obj_t self_in)
+{
+       HX3918_sleep_flag = 1;
+       HX3918_LOG("mp_HX3918_set_sleep\r\n");
+    return mp_obj_new_int(1);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_init_HX3918_obj, mp_HX3918_init);//初始化
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_deinit_HX3918_obj, mp_HX3918_deinit);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_activate_HX3918_obj, mp_activate_HX3918);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_get_blood_oxygen_obj, mp_HX3918_get_blood_oxygen);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_get_heart_obj, mp_HX3918_get_heart);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_get_blood_pressure_obj, mp_HX3918_get_blood_pressure);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_get_weart_obj, mp_HX3918_get_wear);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_open_heart_obj, mp_HX3918_open_heart);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_open_blood_oxygen_obj, mp_HX3918_open_blood_oxygen);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_open_hrv_obj, mp_HX3918_open_hrv);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_get_hrv_obj, mp_HX3918_get_hrv);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_get_all_data_obj, mp_HX3918_get_all_data);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_close_obj, mp_HX3918_close);//
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_HX3918_set_sleep_obj, mp_HX3918_set_sleep);//
+
+STATIC const mp_rom_map_elem_t HX3918_locals_dict_table[] = {
+    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mp_init_HX3918_obj) },
+    { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&mp_deinit_HX3918_obj) },
+    { MP_ROM_QSTR(MP_QSTR_activate), MP_ROM_PTR(&mp_activate_HX3918_obj) },
+       { MP_ROM_QSTR(MP_QSTR_get_blood_oxygen), MP_ROM_PTR(&mp_HX3918_get_blood_oxygen_obj)},
+       { MP_ROM_QSTR(MP_QSTR_get_heart), MP_ROM_PTR(&mp_HX3918_get_heart_obj)},
+       { MP_ROM_QSTR(MP_QSTR_get_blood_pressure), MP_ROM_PTR(&mp_HX3918_get_blood_pressure_obj)},
+       { MP_ROM_QSTR(MP_QSTR_get_weart), MP_ROM_PTR(&mp_HX3918_get_weart_obj)},
+       { MP_ROM_QSTR(MP_QSTR_open_heart), MP_ROM_PTR(&mp_HX3918_open_heart_obj)},
+       { MP_ROM_QSTR(MP_QSTR_open_blood_oxygen), MP_ROM_PTR(&mp_HX3918_open_blood_oxygen_obj)},
+       { MP_ROM_QSTR(MP_QSTR_open_hrv), MP_ROM_PTR(&mp_HX3918_open_hrv_obj)},
+       { MP_ROM_QSTR(MP_QSTR_get_hrv), MP_ROM_PTR(&mp_HX3918_get_hrv_obj)},
+       { MP_ROM_QSTR(MP_QSTR_get_all_data), MP_ROM_PTR(&mp_HX3918_get_all_data_obj)},
+       { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_HX3918_close_obj)},
+       { MP_ROM_QSTR(MP_QSTR_set_sleep), MP_ROM_PTR(&mp_HX3918_set_sleep_obj)},
+};
+
+STATIC MP_DEFINE_CONST_DICT(HX3918_locals_dict, HX3918_locals_dict_table);
+
+const mp_obj_type_t Hsensor_HX3918_type = {
+    { &mp_type_type },
+    .name = MP_QSTR_HX3918,
+    .make_new = HX3918_make_new,
+    .locals_dict = (mp_obj_dict_t*)&HX3918_locals_dict,
+};
+
+#endif 
+