增加可以设置基准电压值

This commit is contained in:
dianke3yu 2025-07-18 16:19:18 +08:00
parent c08c3d0604
commit d0cc549048
3 changed files with 361 additions and 329 deletions

View File

@ -268,13 +268,14 @@ int battery_get_percent(){
return batt_percent;
}
//AT+BAT_CALIBRTATION
int battery_calibration(){
int battery_calibration(int calibration_value){
bk_err_t ret = prvStartBatteryAdcOneTime();
if (ret == BK_OK)
{
bk_printf("BAT_CALIBRTATION calibration_value: %d \r\n", calibration_value);
ret = 1;
bat_offset = 3980-vol;
bat_offset = calibration_value-vol;
bk_config_write("bat_offset", (void *)&bat_offset, 4);
bk_config_sync_flash_safely();

View File

@ -25,7 +25,7 @@ void init_bat_timer();
void poweron_voice();
void poweroff_voice();
int battery_calibration();
int battery_calibration(int calibration_value);
/**
*
*/

View File

@ -38,16 +38,27 @@ static bk_gatt_if_t s_gatts_if = 0;
#define SYNC_CMD_TIMEOUT_MS 4000
#define ADV_HANDLE 0
#define BK_GATT_ATTR_TYPE(iuuid) {.len = BK_UUID_LEN_16, .uuid = {.uuid16 = iuuid}}
#define BK_GATT_ATTR_CONTENT(iuuid) {.len = BK_UUID_LEN_16, .uuid = {.uuid16 = iuuid}}
#define BK_GATT_ATTR_TYPE(iuuid) \
{ \
.len = BK_UUID_LEN_16, .uuid = {.uuid16 = iuuid } \
}
#define BK_GATT_ATTR_CONTENT(iuuid) \
{ \
.len = BK_UUID_LEN_16, .uuid = {.uuid16 = iuuid } \
}
#define BK_GATT_ATTR_VALUE(ilen, ivalue) {.attr_max_len = ilen, .attr_len = ilen, .attr_value = ivalue}
#define BK_GATT_ATTR_TYPE_128(iuuid) {.len = BK_UUID_LEN_128, .uuid = {.uuid128 = {iuuid[0], iuuid[1], iuuid[2], iuuid[3], iuuid[4], \
iuuid[5], iuuid[6], iuuid[7], iuuid[8], iuuid[9], iuuid[10], iuuid[11], iuuid[12], iuuid[13], iuuid[14], iuuid[15]}}}
#define BK_GATT_ATTR_TYPE_128(iuuid) \
{ \
.len = BK_UUID_LEN_128, .uuid = {.uuid128 = {iuuid[0], iuuid[1], iuuid[2], iuuid[3], iuuid[4], \
iuuid[5], iuuid[6], iuuid[7], iuuid[8], iuuid[9], iuuid[10], iuuid[11], iuuid[12], iuuid[13], iuuid[14], iuuid[15]} } \
}
#define BK_GATT_ATTR_CONTENT_128(iuuid) {.len = BK_UUID_LEN_128, .uuid = {.uuid128 = {iuuid[0], iuuid[1], iuuid[2], iuuid[3], iuuid[4], \
iuuid[5], iuuid[6], iuuid[7], iuuid[8], iuuid[9], iuuid[10], iuuid[11], iuuid[12], iuuid[13], iuuid[14], iuuid[15]}}}
#define BK_GATT_ATTR_CONTENT_128(iuuid) \
{ \
.len = BK_UUID_LEN_128, .uuid = {.uuid128 = {iuuid[0], iuuid[1], iuuid[2], iuuid[3], iuuid[4], \
iuuid[5], iuuid[6], iuuid[7], iuuid[8], iuuid[9], iuuid[10], iuuid[11], iuuid[12], iuuid[13], iuuid[14], iuuid[15]} } \
}
#define BK_GATT_PRIMARY_SERVICE_DECL(iuuid) \
.att_desc = \
@ -60,8 +71,7 @@ static bk_gatt_if_t s_gatts_if = 0;
.att_desc = \
{ \
.attr_type = BK_GATT_ATTR_TYPE(BK_GATT_UUID_PRI_SERVICE), \
.attr_content = BK_GATT_ATTR_CONTENT_128(iuuid)\
}
.attr_content = BK_GATT_ATTR_CONTENT_128(iuuid)}
#define BK_GATT_CHAR_DECL(iuuid, ilen, ivalue, iprop, iperm, irsp) \
.att_desc = \
@ -111,7 +121,6 @@ static uint8_t s_password[64];
static uint8_t s_cmd[64];
static uint16_t s_conn_ind = ~0;
static const bk_gatts_attr_db_t s_gatts_attr_db_service_boarding[] =
{
{
@ -191,15 +200,19 @@ static uint16_t *const s_boarding_attr_handle_list[sizeof(s_gatts_attr_db_servic
&s_char_cmd_char_handle,
};
static boarding_state m_state = BOARDING_STATE_IDLE;
int ble_notify_boarding_state(boarding_state state){
if(m_state == state){
int ble_notify_boarding_state(boarding_state state)
{
if (m_state == state)
{
return 0;
}
// 在没有配置wifi情况下不发送状态AP_NOFOUND PASSWORD_ERROR
if(state== BOARDING_STATE_AP_NOFOUND && m_state != BOARDING_STATE_SETWIFI){
if (state == BOARDING_STATE_AP_NOFOUND && m_state != BOARDING_STATE_SETWIFI)
{
return 0;
}
if(state== BOARDING_STATE_PASSWORD_ERROR && m_state != BOARDING_STATE_SETWIFI){
if (state == BOARDING_STATE_PASSWORD_ERROR && m_state != BOARDING_STATE_SETWIFI)
{
return 0;
}
@ -230,7 +243,8 @@ int ble_notify_boarding_state(boarding_state state){
break;
}
if(strlen((char*)&str_respone) > 0){
if (strlen((char *)&str_respone) > 0)
{
os_printf("notify:%s\r\n", str_respone);
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
@ -458,7 +472,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
wboard_logi("password: %s", s_ble_boarding_info->password_value);
}
// <20>ص<EFBFBD>
if (s_ble_boarding_info && s_ble_boarding_info->set_wifi_cb)
{
@ -471,13 +484,15 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
else if (s_char_cmd_char_handle == param->handle)
{
bk_ble_gatts_get_attr_value(param->handle, &buff_size, &tmp_buff);
if(param->len ==0 || param->value == NULL) return ret;
if (param->len == 0 || param->value == NULL)
return ret;
uint8_t recv[param->len + 1];
os_memset(recv, 0, sizeof(recv));
os_memcpy((uint8_t *)recv, param->value, param->len);
wboard_loge("recv: %s", recv);
char *ptr;
if (NULL != (ptr = strstr((char *)recv, "AT+SSID"))){
if (NULL != (ptr = strstr((char *)recv, "AT+SSID")))
{
wboard_loge("start scan wifi!!");
// <20>ص<EFBFBD>
if (s_ble_boarding_info && s_ble_boarding_info->scan_wifi_cb)
@ -485,7 +500,8 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
s_ble_boarding_info->scan_wifi_cb();
}
}
if (NULL != (ptr = strstr((char *)recv, "AT+SCAN"))){
if (NULL != (ptr = strstr((char *)recv, "AT+SCAN")))
{
wboard_loge("start scan wifi!!");
// <20>ص<EFBFBD>
if (s_ble_boarding_info && s_ble_boarding_info->scan_wifi_cb)
@ -494,7 +510,8 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
}
}
if (NULL != (ptr = strstr((char *)recv, "AT+WIFI"))){
if (NULL != (ptr = strstr((char *)recv, "AT+WIFI")))
{
wboard_loge("recv wifi info!!");
char m_ssid[64] = {0};
@ -504,7 +521,8 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
sscanf((char *)recv, "AT+WIFI=%[^,],%[^,]", m_ssid, m_pwd);
wboard_loge("%s %s ", m_ssid, m_pwd);
// <20><><EFBFBD><EFBFBD>SSID
if(strlen(m_ssid) > 0){
if (strlen(m_ssid) > 0)
{
if (s_ble_boarding_info->ssid_value)
{
os_free(s_ble_boarding_info->ssid_value);
@ -527,10 +545,13 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
s_ble_boarding_info->password_length = 0;
}
if(strlen(m_pwd) > 0){
if (strlen(m_pwd) > 0)
{
s_ble_boarding_info->password_length = 0;
s_ble_boarding_info->password_value = "";
}else{
}
else
{
s_ble_boarding_info->password_length = strlen(m_pwd);
s_ble_boarding_info->password_value = os_malloc(strlen(m_pwd) + 1);
@ -549,7 +570,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
s_ble_boarding_info->set_wifi_cb(m_ssid, strlen(m_ssid), m_pwd, strlen(m_pwd));
}
}
/**
* 7.
@ -580,7 +600,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
sprintf((char *)str_respone, "%s", "AT+VOLUME=OK");
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
}
}
/**
*
@ -631,9 +650,12 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
else
{
int open = atoi(enable);
if(open){
if (open)
{
ws2812_open();
}else{
}
else
{
ws2812_close();
}
sprintf((char *)str_respone, "%s", "AT+BRIGHT_EN=OK");
@ -661,9 +683,12 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
else
{
int open = atoi(enable);
if(open){
if (open)
{
sleep_helper_open();
}else{
}
else
{
sleep_helper_close();
}
sprintf((char *)str_respone, "%s", "AT+SLEEP_EN=OK");
@ -690,7 +715,8 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
else
{
int v = atoi(mode);
switch(v){
switch (v)
{
case 1:
sleep_helper_set_mode(SLEEP_MODE_1);
break;
@ -706,7 +732,8 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
}
else if(NULL != (ptr = strstr((char *)recv, "AT+RESET"))){
else if (NULL != (ptr = strstr((char *)recv, "AT+RESET")))
{
extern int demo_erase_network_auto_reconnect_info();
demo_erase_network_auto_reconnect_info();
}
@ -737,7 +764,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
sprintf((char *)str_respone, "%s", "AT+SLEEP_TIMEOUT=OK");
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
}
}
else if (NULL != (ptr = strstr((char *)recv, "AT+BRIGHT_TIMEOUT=")))
{
@ -760,7 +786,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
sprintf((char *)str_respone, "%s", "AT+BRIGHT_TIMEOUT=OK");
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
}
}
/*
*11.
@ -800,7 +825,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
sprintf((char *)str_respone, "AT+VERSION=%s", APPLICATION_VERSION);
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
}
else if (NULL != (ptr = strstr((char *)recv, "AT+SETAIURL")))
@ -823,7 +847,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
sprintf((char *)str_respone, "%s", "AT+SETAIURL=OK");
}
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
}
/**
* APP
@ -834,8 +857,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
ws2812_led_start_ota();
rtos_delay_milliseconds(10000);
ws2812_led_stop_ota();
}
/**
* APP
@ -844,20 +865,34 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
{
wboard_loge("AT+BAT_CALIBRTATION!!");
int ret = battery_calibration();
int value = 0;
char *eq_pos = strchr(ptr, '='); // 查找等号位置
if (eq_pos != NULL)
{
// 从等号后解析整数
if (sscanf(eq_pos + 1, "%d", &value) == 1)
{
wboard_loge("AT+BAT_CALIBRTATION: %d", value);
int ret = battery_calibration(value);
bk_printf("AT+BAT_CALIBRTATION ret: %d\r\n", ret);
uint8_t str_respone[32] = {0};
memset(str_respone, 0, sizeof(str_respone));
if(ret == 1){
if (ret == 1)
{
sprintf((char *)str_respone, "%s", "AT+BAT_CALIBRTATION=OK");
}else{
}
else
{
sprintf((char *)str_respone, "%s", "AT+BAT_CALIBRTATION=FAIL");
}
wifi_boarding_notify(str_respone, strlen((char *)&str_respone));
}
}
}
/**
* 12.
: AT+STATE=
@ -885,7 +920,8 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
os_printf("get sta link status fail!\n");
}
int connect_state = 0;
if(link_status.state == WIFI_LINKSTATE_STA_GOT_IP){
if (link_status.state == WIFI_LINKSTATE_STA_GOT_IP)
{
connect_state = 1;
}
os_printf("link_status.state:%d\n", link_status.state);
@ -938,10 +974,13 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
// lamp_http_ota_start(ota_url);
// 电量低于25 不允许执行OTA
int battery = battery_get_percent();
if(battery > 25){
if (battery > 25)
{
// iot_lamp_check_ota();
lamp_http_ota_start(ota_url);
}else{
}
else
{
app_event_send_msg(APP_EVT_LOW_VOLTAGE, 0);
}
free(ota_url);
@ -957,10 +996,13 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
memset(str_respone, 0, sizeof(str_respone));
// 电量低于25 不允许执行OTA
int battery = battery_get_percent();
if(battery > 25){
if (battery > 25)
{
sprintf((char *)str_respone, "%s", "AT+CHECKOTA=1");
iot_lamp_check_ota();
}else{
}
else
{
app_event_send_msg(APP_EVT_LOW_VOLTAGE, 0);
sprintf((char *)str_respone, "%s", "AT+CHECKOTA=2");
}
@ -970,7 +1012,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
// wifi_boarding_notify(param->value,param->len);
}
else
{
wboard_loge("invalid write handle %d", param->handle);
@ -1043,8 +1084,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
wifi_boarding_notify(test_data, sizeof(test_data));
#endif
}
}
break;
@ -1115,8 +1154,7 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
param->remote_bda[2],
param->remote_bda[1],
param->remote_bda[0],
param->conn_id
);
param->conn_id);
s_conn_ind = ~0;
@ -1150,7 +1188,6 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga
return ret;
}
static void dm_ble_gap_common_cb(bk_ble_gap_cb_event_t event, bk_ble_gap_cb_param_t *param)
{
wboard_logd("event %d", event);
@ -1170,8 +1207,7 @@ static void dm_ble_gap_common_cb(bk_ble_gap_cb_event_t event, bk_ble_gap_cb_para
evt->remote_bda[0],
evt->status,
evt->link_role,
evt->hci_handle
);
evt->hci_handle);
}
break;
@ -1189,9 +1225,7 @@ static void dm_ble_gap_common_cb(bk_ble_gap_cb_event_t event, bk_ble_gap_cb_para
evt->remote_bda_type,
evt->status,
evt->reason,
evt->hci_handle
);
evt->hci_handle);
}
break;
@ -1296,7 +1330,6 @@ static void dm_ble_gap_common_cb(bk_ble_gap_cb_event_t event, bk_ble_gap_cb_para
default:
break;
}
}
int wifi_boarding_init(ble_boarding_info_t *info)
@ -1617,7 +1650,6 @@ int wifi_boarding_adv_start(void)
error:
return 0;
}
int wifi_boarding_adv_stop(void)
@ -1664,4 +1696,3 @@ int wifi_boarding_notify(uint8_t *data, uint16_t length)
return BK_OK;
}
}