From eaaa4b5979c52016a63f08b18cd59084e83028a9 Mon Sep 17 00:00:00 2001 From: SnickerSun Date: Mon, 11 Mar 2024 19:42:57 +0800 Subject: [PATCH] =?utf8?q?Vibrator=20=E4=BC=98=E5=8C=96=E8=B0=83=E6=95=B4?= =?utf8?q?=EF=BC=8C=E5=8A=9F=E8=83=BDok=EF=BC=8C=E7=94=B5=E6=B5=81?= =?utf8?q?=E4=B9=9F=E7=9B=B8=E5=AF=B9=E8=BE=83=E5=B0=8F=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../vibrator/service/vibrator_service.cpp | 49 ++++++++------- .../vibrator/service/vibrator_service.h | 10 +-- .../drivers/peripheral/vibrator/vibrator.c | 62 +++++++++++-------- .../drivers/peripheral/vibrator/vibrator.h | 6 +- .../peripheral/vibrator/vibrator_at_cmd.c | 12 ++-- .../peripheral/vibrator/vibrator_hal.c | 25 +++----- .../peripheral/vibrator/vibrator_hal.h | 8 +-- 7 files changed, 88 insertions(+), 84 deletions(-) diff --git a/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.cpp b/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.cpp index 88967fef4..1f0d02aa9 100644 --- a/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.cpp +++ b/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.cpp @@ -50,19 +50,29 @@ static bool g_batter_service_initialled = false; void custom_VibratorFunc(OHOS::VibratorType vibratorType) { + + vibrator_cmd_t mail = {0}; switch (vibratorType) { case OHOS::VibratorType::VIBRATOR_TYPE_ONE: - do_vibrator_cmd(VIB_CMD_MSG_ONE); + mail.cycle_time_ms=200; + mail.duty = 20; + mail.duration_time_ms = 500; break; case OHOS::VibratorType::VIBRATOR_TYPE_TWO: - do_vibrator_cmd(VIB_CMD_MSG_TWO); + mail.cycle_time_ms=200; + mail.duty = 20; + mail.duration_time_ms = 1000; break; case OHOS::VibratorType::VIBRATOR_TYPE_THREE: - do_vibrator_cmd(VIB_CMD_MSG_THREE); + mail.cycle_time_ms=200; + mail.duty = 20; + mail.duration_time_ms = 1500; break; } + do_vibrator_cmd(mail); + } #define MAG_CHECK_MS 20 @@ -78,14 +88,17 @@ void uapi_vibrator_service_deinit(void) -void do_vibrator_cmd(int cmd) + +void do_vibrator_cmd(vibrator_cmd_t cmd) { int ret; - common_notify_mail_t mail = {0}; - mail.comm.app = (app_type)cmd; + vibrator_cmd_t mail = {0}; + mail.cycle_time_ms = cmd.cycle_time_ms; + mail.duty = cmd.duty; + mail.duration_time_ms = cmd.duration_time_ms; ret = osal_msg_queue_write_copy(g_app_queue, (void *)&mail, - sizeof(common_notify_mail_t), 0); + sizeof(vibrator_cmd_t), 0); if (ret != OSAL_SUCCESS) { PRINT("Error mag Check MSG 0x%02X send failed! err=0x%x! \r\n",cmd, ret); } @@ -96,8 +109,8 @@ static void vibrator_service_check_task(void const *arg) { unused(arg); int32_t ret; - common_notify_mail_t mail; - uint32_t msgsize = (uint32_t)sizeof(common_notify_mail_t); + vibrator_cmd_t mail; + uint32_t msgsize = (uint32_t)sizeof(vibrator_cmd_t); while (1) { ret = osal_msg_queue_read_copy(g_app_queue, (void *)&mail, &msgsize, @@ -105,20 +118,8 @@ static void vibrator_service_check_task(void const *arg) if (ret != OSAL_SUCCESS) { continue; } - - - if (mail.comm.app == VIB_CMD_MSG_ONE) - { - g_VibratorFunc( 1 , 500, 50); - } - else if (mail.comm.app == VIB_CMD_MSG_TWO) - { - g_VibratorFunc( 2 , 500, 50); - } - else if (mail.comm.app == VIB_CMD_MSG_THREE) - { - g_VibratorFunc( 3 , 500, 50); - } + + g_VibratorFunc( mail.duty , mail.cycle_time_ms, mail.duration_time_ms); } } @@ -126,7 +127,7 @@ static void vibrator_service_check_task(void const *arg) errcode_t uapi_vibrator_service_init(void) { if (osal_msg_queue_create("vibrator_service_queue", APP_BOARD_MSG_COUNT, &g_app_queue, - 0, sizeof(common_notify_mail_t)) != ERRCODE_SUCC) { + 0, sizeof(vibrator_cmd_t)) != ERRCODE_SUCC) { PRINT("[mag][service] app create mail fail\n"); return ERRCODE_FAIL; } diff --git a/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.h b/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.h index d1d1d58dc..225898a4a 100644 --- a/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.h +++ b/code/sdk/drivers/peripheral/vibrator/service/vibrator_service.h @@ -20,14 +20,16 @@ extern "C" { #include "errcode.h" -#define VIB_CMD_MSG_ONE 0x21 -#define VIB_CMD_MSG_TWO 0x22 -#define VIB_CMD_MSG_THREE 0x23 +typedef struct { + uint32_t duty; //占空比 + uint32_t cycle_time_ms; //频率 + uint32_t duration_time_ms;//震动时间 +} vibrator_cmd_t; /** * 测试指令 参数cmd 选项: VIB_CMD_MSG_ONE VIB_CMD_MSG_TWO VIB_CMD_MSG_THREE */ -extern void do_vibrator_cmd(int cmd); +extern void do_vibrator_cmd(vibrator_cmd_t cmd); #ifdef __cplusplus #if __cplusplus diff --git a/code/sdk/drivers/peripheral/vibrator/vibrator.c b/code/sdk/drivers/peripheral/vibrator/vibrator.c index d928dc9fb..fe967593c 100644 --- a/code/sdk/drivers/peripheral/vibrator/vibrator.c +++ b/code/sdk/drivers/peripheral/vibrator/vibrator.c @@ -124,40 +124,50 @@ void g_VibratorFunc(uint8_t vibratorType , uint16_t vibtime_ms,uint16_t delaytim #define VIBRATORPIN_MODE HAL_PIO_FUNC_GPIO static pin_t pin = VIBRATOR_GPIOPPIN; static pin_mode_t mode = VIBRATORPIN_MODE; -#define VIBRATOR_VIB_HZ 200 -static void vib_once(uint32_t duration_time) -{ - uint32_t delaytime_ms = 1000/VIBRATOR_VIB_HZ; - uapi_pin_set_mode(VIBRATOR_GPIOPPIN, (pin_mode_t)HAL_PIO_FUNC_GPIO); - uapi_gpio_set_dir(VIBRATOR_GPIOPPIN,GPIO_DIRECTION_OUTPUT); - - while(duration_time) - { - duration_time-=delaytime_ms; - uapi_gpio_toggle(VIBRATOR_GPIOPPIN); - uapi_tcxo_delay_ms(delaytime_ms); - } - uapi_gpio_set_val(VIBRATOR_GPIOPPIN,GPIO_LEVEL_LOW); -} +static uint8_t vibrating = 0; -void g_VibratorFunc(uint8_t vibratorType , uint16_t vibtime_ms,uint16_t delaytime_ms) + +void g_VibratorFunc(uint16_t duty , uint16_t cycle_time_ms,uint16_t duration_time_ms) { - if(vibratorType >0) + if( duty>100 || duty ==0 ||cycle_time_ms == 0 || duration_time_ms == 0) { - PRINT("g_VibratorFunc vib %d times , vibtime_ms %d , delaytime_ms %d\r\n",vibratorType ,vibtime_ms, delaytime_ms); - uint8_t count = vibratorType; - while(count--) + PRINT("Not Supported param duty %u cycle_time_ms %u duration_time_ms %u !\r\n",duty,cycle_time_ms,duration_time_ms ); + }else{ + PRINT("g_VibratorFunc duration_time_ms %u , duty %u , cycle_time_ms %u\r\n",duration_time_ms ,duty, cycle_time_ms); + + if (vibrating == 1) { - // PRINT("g_VibratorFunc vib %d times \r\n",count ); - vib_once(vibtime_ms); - // PRINT("g_VibratorFunc vib %d times \r\n",count ); - uapi_tcxo_delay_ms(delaytime_ms); + PRINT("vibrating skip\r\n"); + return ; + } + vibrating = 1; + // PRINT("g_VibratorFunc vib %d times \r\n",count ); + uapi_pin_set_mode(VIBRATOR_GPIOPPIN, (pin_mode_t)HAL_PIO_FUNC_GPIO); + uapi_gpio_set_dir(VIBRATOR_GPIOPPIN,GPIO_DIRECTION_OUTPUT); + uint16_t delaytime_ms_high = duty*(1000/cycle_time_ms)/100; + uint16_t delaytime_ms_low = (100-duty)*(1000/cycle_time_ms)/100; + + + if (delaytime_ms_low == 0) delaytime_ms_low = 1; + if (delaytime_ms_high == 0) delaytime_ms_high = 1; + + PRINT("g_VibratorFunc delaytime_ms_high %u , delaytime_ms_low %u \r\n",delaytime_ms_high,delaytime_ms_low ); + + while(duration_time_ms) + { + uapi_gpio_set_val(VIBRATOR_GPIOPPIN,GPIO_LEVEL_HIGH); + uapi_tcxo_delay_ms(delaytime_ms_high); + uapi_gpio_set_val(VIBRATOR_GPIOPPIN,GPIO_LEVEL_LOW); + uapi_tcxo_delay_ms(delaytime_ms_low); + duration_time_ms-=delaytime_ms_high; + duration_time_ms-=delaytime_ms_low; + } - }else{ - PRINT("Not Supported Vibtype %d !\r\n",vibratorType ); + uapi_gpio_set_val(VIBRATOR_GPIOPPIN,GPIO_LEVEL_LOW); + vibrating = 0; } } diff --git a/code/sdk/drivers/peripheral/vibrator/vibrator.h b/code/sdk/drivers/peripheral/vibrator/vibrator.h index c23f1f96b..5ec1cf376 100644 --- a/code/sdk/drivers/peripheral/vibrator/vibrator.h +++ b/code/sdk/drivers/peripheral/vibrator/vibrator.h @@ -19,9 +19,11 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ /** - * vibtype 1 vib1 , 2 vib2, 3 vib3 + * duty 占空比 : 0-100 + * cycle_time_ms 频率 + * duration_time_ms 震动时间 */ -void g_VibratorFunc(uint8_t vibratorType , uint16_t vibtime_ms,uint16_t delaytime_ms); +void g_VibratorFunc(uint16_t duty , uint16_t cycle_time_ms,uint16_t duration_time_ms); /** * @} diff --git a/code/sdk/drivers/peripheral/vibrator/vibrator_at_cmd.c b/code/sdk/drivers/peripheral/vibrator/vibrator_at_cmd.c index 0b6cf4ea8..cdcd7fd0b 100644 --- a/code/sdk/drivers/peripheral/vibrator/vibrator_at_cmd.c +++ b/code/sdk/drivers/peripheral/vibrator/vibrator_at_cmd.c @@ -57,13 +57,15 @@ static uint32_t vibrator_do_test(uint8_t *para, uint32_t paraLen, char *strCommo return ERRCODE_FAIL; } - if (argc != 1) { - PRINT("VIBRATORTEST ATCMD Set Parameter Failed argc %d\n",argc); + if (argc != 3) { + PRINT("VIBRATORTEST ATCMD Set Parameter Failed argc %d, should be AT^VIBRATORTEST=duration cycle duty \n",argc); return ERRCODE_FAIL; } - uint8_t vibtype = (uint8_t)atoi((char*)argv[0]); - PRINT("VIBRATORTEST ATCMD custom_VibratorFunc VibType %d\n",vibtype); - hal_custom_VibratorFunc(vibtype); + uint16_t duration = (uint16_t)atoi((char*)argv[0]); + uint16_t cycle = (uint16_t)atoi((char*)argv[1]); + uint16_t duty = (uint16_t)atoi((char*)argv[2]); + PRINT("VIBRATORTEST ATCMD custom_VibratorFunc duration %u,cycle %u,duty %u\n",duration,cycle,duty); + hal_custom_VibratorFunc(duty,cycle,duration); return ERRCODE_SUCC; } diff --git a/code/sdk/drivers/peripheral/vibrator/vibrator_hal.c b/code/sdk/drivers/peripheral/vibrator/vibrator_hal.c index ebe90dca9..11f318efa 100644 --- a/code/sdk/drivers/peripheral/vibrator/vibrator_hal.c +++ b/code/sdk/drivers/peripheral/vibrator/vibrator_hal.c @@ -2,22 +2,15 @@ #include "vibrator_hal.h" #include "vibrator_service.h" #include "debug_print.h" -void hal_custom_VibratorFunc(uint8_t vibratorType) +void hal_custom_VibratorFunc(uint16_t duty , uint16_t cycle_time_ms,uint16_t duration_time_ms) { - switch (vibratorType) - { - case ENUM_VIBRATOR_TYPE_ONE: - PRINT("ENUM_VIBRATOR_TYPE_ONE\r\n"); - do_vibrator_cmd(VIB_CMD_MSG_ONE); - break; - case ENUM_VIBRATOR_TYPE_TWO: - PRINT("ENUM_VIBRATOR_TYPE_TWO\r\n"); - do_vibrator_cmd(VIB_CMD_MSG_TWO); - break; - case ENUM_VIBRATOR_TYPE_THREE: - PRINT("ENUM_VIBRATOR_TYPE_THREE\r\n"); - do_vibrator_cmd(VIB_CMD_MSG_THREE); - break; - } + vibrator_cmd_t cmd = {0}; + + PRINT("hal_custom_VibratorFunc\r\n"); + cmd.cycle_time_ms = cycle_time_ms; + cmd.duration_time_ms = duration_time_ms; + cmd.duty = duty; + do_vibrator_cmd(cmd); + } \ No newline at end of file diff --git a/code/sdk/drivers/peripheral/vibrator/vibrator_hal.h b/code/sdk/drivers/peripheral/vibrator/vibrator_hal.h index 9b2186425..12f47cd7a 100644 --- a/code/sdk/drivers/peripheral/vibrator/vibrator_hal.h +++ b/code/sdk/drivers/peripheral/vibrator/vibrator_hal.h @@ -21,14 +21,8 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -typedef enum { - ENUM_VIBRATOR_TYPE_ONE=1, - ENUM_VIBRATOR_TYPE_TWO=2, - ENUM_VIBRATOR_TYPE_THREE=3, -} VIB_TYPE_ENUM_T; - -void hal_custom_VibratorFunc(uint8_t vibratorType); +void hal_custom_VibratorFunc(uint16_t duty , uint16_t cycle_time_ms,uint16_t duration_time_ms); #ifdef __cplusplus #if __cplusplus } -- 2.17.1