优化充电逻辑 LED显示逻辑,优化传感器概率读取不成功

This commit is contained in:
helloyifa 2025-04-10 20:23:26 +08:00
parent 9cdd799881
commit ccd1b683bf

View File

@ -20,18 +20,18 @@
#include "examples/trans_data/ble_trans_profile.h" #include "examples/trans_data/ble_trans_profile.h"
//#include "ble_trans_profile.h" //#include "ble_trans_profile.h"
static int led_state = 0; static int led_state = 0;
static u16 temer_temperature_detect = 0;
void temperature_blink_led() void temperature_blink_led()
{ {
if (led_state) if (led_state)
{ {
gpio_direction_output(IO_PORTA_07, 0); gpio_direction_output(BLE_TEMPERATURE_WORK_LED, 0);
led_state = 0; led_state = 0;
} }
else else
{ {
led_state = 1; led_state = 1;
gpio_direction_output(IO_PORTA_07, 1); gpio_direction_output(BLE_TEMPERATURE_WORK_LED, 1);
} }
} }
@ -210,10 +210,10 @@ static uint8_t reversal(uint8_t data)
//--------------send start convert ----------------------------- //--------------send start convert -----------------------------
gpio_direction_output(DQ_PIN, 0);//pin_write(DQ_PIN, PIN_LOW); // DQ low gpio_direction_output(DQ_PIN, 0);//pin_write(DQ_PIN, PIN_LOW); // DQ low
//printf("send start convert out 0\r\n"); //printf("send start convert out 0\r\n");
delay_ms(5); delay_us_by_timer0(300);
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH); gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH);
//printf("send start convert out 1\r\n"); //printf("send start convert out 1\r\n");
udelay(20); delay_us_by_timer0(20);
//-----------------check DQ pin status should be high--------------------------------------- //-----------------check DQ pin status should be high---------------------------------------
gpio_set_direction(DQ_PIN,1); gpio_set_direction(DQ_PIN,1);
@ -236,9 +236,9 @@ static uint8_t reversal(uint8_t data)
do do
{ {
bit0 = pin_read(DQ_PIN); bit0 = pin_read(DQ_PIN);
delay_us(2); delay_us_by_timer0(2);
retry++; retry++;
if (retry > 20000) // time out 40ms if (retry > 30000) // time out 40ms
{ {
*pcal = (uint16_t)(ERR_DONE * 128); *pcal = (uint16_t)(ERR_DONE * 128);
printf("wait for done pulse fail \r\n"); printf("wait for done pulse fail \r\n");
@ -246,7 +246,7 @@ static uint8_t reversal(uint8_t data)
} }
} while (bit0); } while (bit0);
printf("wait for done pulse succ \r\n"); printf("wait for done pulse succ \r\n");
delay_us(10); delay_us_by_timer0(10);
//--------------read 24bit data--------------- //--------------read 24bit data---------------
gpio_set_direction(DQ_PIN,0); gpio_set_direction(DQ_PIN,0);
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH); gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH);
@ -281,7 +281,8 @@ static uint8_t reversal(uint8_t data)
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH); // DQ low gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH); // DQ low
//pin_mode(DQ_PIN, PIN_MODE_OUTPUT_OD); //pin_mode(DQ_PIN, PIN_MODE_OUTPUT_OD);
delay_us(60); //delay_us(60);
delay_us_by_timer0(60);
} }
//------------------- NST1002 Big Endian,but mcu Little Endian //------------------- NST1002 Big Endian,but mcu Little Endian
swap[0] = data[1]; swap[0] = data[1];
@ -313,7 +314,7 @@ static uint8_t reversal(uint8_t data)
{ {
if (ei < 4) if (ei < 4)
ei = -1 - err; ei = -1 - err;
printf("NST1002 err %s\r\n", err_code[ei]); printf("NST1002 err code :%d err %s\r\n", err,err_code[ei]);
return; return;
} }
@ -332,6 +333,88 @@ void temperature_detect(void)
} }
#endif #endif
u16 charge_led_hander = 0;
void temperature_charge_led(u8 on){
if(charge_led_hander){
sys_timer_del(charge_led_hander);
}
if(on){
charge_led_hander = sys_timer_add(NULL, temperature_blink_led, 500);
}else{
sys_timer_del(charge_led_hander);
}
}
void temperature_work_led(u8 on){
if(on){
gpio_direction_output(BLE_TEMPERATURE_WORK_LED, 1);
}else{
gpio_direction_output(BLE_TEMPERATURE_WORK_LED, 0);
}
}
void temperature_poweron_led_timeout(){
temperature_work_led(0);
}
void temperature_set_soft_poweroff(void)
{
printf("temperature_set_soft_poweroff\n");
//VCM_DET_EN(0);//关闭VCM复位
//gpio_shortpress_reset_config(0);//1--enable 0--disable
//P33_CON_SET(P3_PINR_CON, 0, 1, 0);//关闭长按复位
//P33_CON_SET(P3_PINR_CON, 0, 1, 1);//打开长按复位
gpio_longpress_pin0_reset_config(IO_PORTA_09, 0, 0);
//必须先主动断开蓝牙链路,否则要等链路超时断开
#if TCFG_USER_BLE_ENABLE
btstack_ble_exit(0);
//延时300ms确保BT退出链路断开
sys_timeout_add(NULL, power_set_soft_poweroff, 300);
#else
power_set_soft_poweroff();
#endif
}
void temperature_charge_state(int state){
switch (state)
{
//接入充电
case BLE_TEMPERATURE_CHARGE_STATE_CHARGING :
printf("BLE_TEMPERATURE_CHARGE_STATE_CHARGING\n");
sys_timer_del(temer_temperature_detect);
//体温计指标灯闪烁3次后转灭
for(int i = 0; i< 3 ; i++){
temperature_work_led(1);
delay_ms_by_timer0(150);
temperature_work_led(0);
delay_ms_by_timer0(150);
}
//spple_power_event_to_user(POWER_EVENT_POWER_SOFTOFF);
//printf("!!!!!!!!!!!!!! CHARGING POWER OFF!!!!!!!!!!!!!!!!\n");
//printf("BLE_TEMPERATURE_CHARGE_STATE_CHARGING 2\n");
break;
//充电完成
case BLE_TEMPERATURE_CHARGE_STATE_FULL :
//printf("BLE_TEMPERATURE_CHARGE_STATE_FULL\n");
//spple_power_event_to_user(POWER_EVENT_POWER_SOFTOFF);
//printf("!!!!!!!!!!!!!! POWER OFF!!!!!!!!!!!!!!!!\n");
temperature_set_soft_poweroff();
break;
//拔出充电
case BLE_TEMPERATURE_CHARGE_STATE_IDLE :
printf("BLE_TEMPERATURE_CHARGE_STATE_IDLE\n");
temer_temperature_detect = sys_timer_add(NULL, temperature_detect, 300);
temperature_charge_led(0);
temperature_work_led(1);
sys_s_hi_timerout_add(NULL,temperature_poweron_led_timeout,2000);
break;
default:
break;
}
}
void temperature_ble_init(){ void temperature_ble_init(){
printf(">>>>>>>>>>>>>>>>> temperature_ble_init ...\n"); printf(">>>>>>>>>>>>>>>>> temperature_ble_init ...\n");
@ -350,41 +433,50 @@ void temperature_ble_init(){
} }
void temperature_init(void) void temperature_init(void)
{ {
printf(">>>>>>>>>>>>>>>>> temperature_init ...\n"); printf(">>>>>>>>>>>>>>>>> temperature_init ...\n");
delay_us(60);
udelay(20);
printf(">>>>>>>>>>>>>>>>> temperature_init2 ...\n"); printf(">>>>>>>>>>>>>>>>> temperature_init2 ...\n");
udelay(1000*200);
printf("mdelay 200ms s...\n");
delay_ms(200);
printf("mdelay 200ms e...\n");
extern u16 get_vbat_trim(); extern u16 get_vbat_trim();
extern u16 get_vbg_trim(); extern u16 get_vbg_trim();
printf("vbat_trim:%d...vbg_trim:%d", get_vbat_trim(), get_vbg_trim()); printf("vbat_trim:%d...vbg_trim:%d", get_vbat_trim(), get_vbg_trim());
// 添加以上代码看打印值如果为15跟63那就要用烧录器先烧录一遍。 // 添加以上代码看打印值如果为15跟63那就要用烧录器先烧录一遍。
//从收纳仓取出时自动开机指示灯亮2S后灭
temperature_work_led(1);
sys_s_hi_timerout_add(NULL,temperature_poweron_led_timeout,2000);
sys_s_hi_timer_add(NULL, temperature_blink_led, 200); //sys_s_hi_timer_add(NULL, temperature_blink_led, 200);
//sys_s_hi_timer_add(NULL, temperature_detect, 2000); //sys_s_hi_timer_add(NULL, temperature_detect, 2000);
//sys_timer_add 方式添加定时任务 任务里边可以使用udelay delay_ms 等延时 sys_s_hi_timer_add 则延时无效
sys_timer_add(NULL, temperature_detect, 200); temer_temperature_detect = sys_timer_add(NULL, temperature_detect, 300);
// NTC VDD // NTC VDD
gpio_set_direction(IO_PORTB_05,0); //gpio_set_direction(IO_PORTB_05,0);
gpio_direction_output(IO_PORTB_05, 1); //gpio_direction_output(IO_PORTB_05, 1);
/* /*
delay_ms(100); //test delay
gpio_set_direction(IO_PORTB_04,1); gpio_set_direction(DQ_PIN,0);
gpio_set_die(IO_PORTB_04,1); gpio_direction_output(DQ_PIN, 1);
delay_ms(100); delay_us_by_timer0(20);
printf("check DQ pin status cur dq:%d:\r\n" ,pin_read(IO_PORTB_04)); gpio_direction_output(DQ_PIN, 0);
gpio_set_pull_up(IO_PORTB_04, 1); delay_us_by_timer0(20);
delay_ms(100); gpio_direction_output(DQ_PIN, 1);
printf("check DQ pin status cur dq:%d:\r\n" ,pin_read(IO_PORTB_04)); delay_us_by_timer0(20);
gpio_direction_output(DQ_PIN, 0);
delay_ms(200);
gpio_direction_output(DQ_PIN, 1);
delay_ms(200);
gpio_direction_output(DQ_PIN, 0);
delay_ms(200);
gpio_direction_output(DQ_PIN, 1);
*/ */
} }