| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 | /** * @FileName     : aem_adapter_dev.c * @Author       : AEM Team * @CreateDate   : 2024/04/01 17:57:10 * @Description  : **/#include <string.h>#include <property_manager.h>#ifndef CONFIG_SIMULATOR#include <acts_bluetooth/host_interface.h>#include <bt_manager_ble.h>#endif#include "aem_adapter_dev.h"#include "aem_adapter_motor.h"#include "aem_log.h"#include "power_manager.h"#include "aem_app_err_if.h"#include "device_info_cfg.h"#ifdef CONFIG_SIMULATOR#include <time.h>#include <windows.h>#endif#ifdef CONFIG_RTC_ACTS#include <drivers/rtc.h>#endif#ifndef CONFIG_SIMULATOR#include <drivers/power_supply.h>#include <drivers/hrtimer.h>#endifstatic uint8_t s_brightness_value = 0;static void aem_disp_set_brightness(uint8_t value){#ifndef CONFIG_SIMULATOR    extern int aem_lv_display_set_brightness(uint8_t brightness);    aem_lv_display_set_brightness(value);    AEM_LOG_I("%s %d", __func__, value);#endif    s_brightness_value = value;}static uint8_t aem_disp_get_brightness(void){    return s_brightness_value;}static int get_ble_mac_addr(uint8_t *mac, uint8_t len){    int ret;#ifndef CONFIG_SIMULATOR    bt_addr_le_t addr_le;    if (len != 6)    {        return -EINVAL;    }    bt_manager_get_ble_mac(&addr_le);    for (int i = 0; i < 6; i++)    {        mac[i] = addr_le.a.val[5 - i];    }#endif    return 0;}static int get_sn_str(uint8_t *sn, uint8_t len){    int ret;#ifndef CONFIG_SIMULATOR    if (len != 16 || NULL == sn)    {        return -EINVAL;    }    sn = "123456789abcde";#endif    return 0;}#define MAC_STR_LEN (12 + 1)static int get_bt_mac_addr(uint8_t *mac, uint8_t len){    int ret = 0;    char mac_str[MAC_STR_LEN];#ifndef CONFIG_SIMULATOR    if (len != 6)    {        return -EINVAL;    }    ret = property_get(CFG_BT_MAC, mac_str, (MAC_STR_LEN - 1));    if (ret > 0)    {        hex2bin(mac_str, 12, mac, len);    }#endif    return ret;}#ifndef CONFIG_SIMULATORextern float f_Power[2];#endifstatic uint8_t get_battery_percentage(void){    int percentage = 0;#ifndef CONFIG_SIMULATOR    //取大的功率来显示,功率范围为0~60W,显示范围0~100%,按比例转换    if (f_Power[0] >= f_Power[1])      {        percentage = (int)f_Power[0];    }    else    {        percentage = (int)f_Power[1];    }    if (percentage > 100)    {        percentage = 100;    }    if (percentage < 0)    {        percentage = 0;    }#endif    return percentage;}static uint16_t get_battery_vol(void){    uint16_t value = 0;    int vol = 0;#ifndef CONFIG_SIMULATOR    vol = power_manager_get_battery_vol();    if (vol < 0)    {        value = 0;    }    else    {        value = vol / 1000;    }#endif    return value;}static uint8_t get_charge_status(void){    int status = 0;#ifndef CONFIG_SIMULATOR    status = power_manager_get_dc5v_status();    if (status < 0)    {        status = 0;    }#endif    return status;}static uint8_t cnt_1s = 0;#ifndef CONFIG_SIMULATORstatic struct hrtimer g_wait_for_set_time;static void htimer_fun(struct hrtimer *ttimer, void *expiry_fn_arg){	if (cnt_1s > 0)    {        cnt_1s--;    }}static void htimer_wait_for_set_time(unsigned int ms){	hrtimer_init(&g_wait_for_set_time, htimer_fun, NULL);	hrtimer_start(&g_wait_for_set_time, 1000*ms, 1000*ms);}#endifextern uint8_t bySetHour;extern uint16_t wSetYear;extern uint8_t bySetTimeStep;extern uint8_t read_time_data[7];static uint8_t get_rtc_time(aem_time_t *time){    uint8_t ret = AEM_OK;    if (time == NULL)    {        return AEM_ERR_INVALID_PARAM;    }#ifdef CONFIG_RTC_ACTS    struct rtc_time rtc_time;    const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);    if (rtc)    {        //rtc_get_time(rtc, &rtc_time);        //time->year = rtc_time.tm_year + 1900;        //time->month = rtc_time.tm_mon + 1;        //time->day = rtc_time.tm_mday;        //time->wday = rtc_time.tm_wday;        //time->hour = rtc_time.tm_hour;        //time->min = rtc_time.tm_min;        //time->second = rtc_time.tm_sec;        //time->ms = rtc_time.tm_ms;    #ifndef CONFIG_SIMULATOR          if (bySetTimeStep == 1)        {             cnt_1s = 255;            //k_msleep(3000);  //延时1.5s跳转画面,保证时间或日期能够更新             bySetTimeStep = 2;          }        else if (bySetTimeStep == 3)        {            if (cnt_1s == 255)            {                 cnt_1s = 2;                 //k_msleep(2000);  //延时1.5s跳转画面,保证时间或日期能够更新                 if (hrtimer_is_running(&g_wait_for_set_time))                {                    hrtimer_restart(&g_wait_for_set_time);                }                else                    htimer_wait_for_set_time(750);            }            if (cnt_1s == 0)            {                hrtimer_stop(&g_wait_for_set_time);                if (bySetTimeStep == 3)  //防止bySetTimeStep在其他地方被修改                    bySetTimeStep = 0;            }        }    #endif            if (bySetTimeStep == 0)        {            time->year = read_time_data[6] + 2000;            time->month = read_time_data[5];            time->wday = read_time_data[4];            time->day = read_time_data[3];            time->hour = read_time_data[2];            time->min = read_time_data[1];            time->second = read_time_data[0];        }    }#elif defined(CONFIG_SIMULATOR)    SYSTEMTIME lt;    GetLocalTime(<);    time->year = lt.wYear;    time->month = lt.wMonth;    time->day = lt.wDay;    time->wday = lt.wDayOfWeek;    time->hour = lt.wHour;    time->min = lt.wMinute;    time->second = lt.wSecond;    time->ms = lt.wMilliseconds;#endif /* CONFIG_RTC_ACTS */    return ret;}static int set_rtc_time(aem_time_t *time){    int ret = 0;    if (time == NULL)    {        return AEM_ERR_INVALID_PARAM;    }#ifdef CONFIG_RTC_ACTS    struct rtc_time rtc_time;    const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);    if (rtc)    {        rtc_time.tm_year = time->year;        rtc_time.tm_mon = time->month;        rtc_time.tm_mday = time->day;        rtc_time.tm_wday = time->wday;        rtc_time.tm_hour = time->hour;        rtc_time.tm_min = time->min;        rtc_time.tm_sec = time->second;        rtc_time.tm_ms = time->ms;        rtc_time.tm_year -= 1900;        rtc_time.tm_mon -= 1;        ret = rtc_set_time(rtc, &rtc_time);        if (ret)        {            AEM_LOG_E("set time error ret=%d", ret);        }    }#endif /* CONFIG_RTC_ACTS */    return ret;}aem_dev_info_t get_device_info(void){    aem_dev_info_t info = { 0 };    //info.device_id = DEVICE_ID;    sprintf(info.device_id, "%s", DEVICE_ID);    info.major_v = MAJOR_VERSION;    info.minor_v = MINOR_VERSION;    info.micro_v = MICRO_VERSION;    sprintf(info.device_name, "%s", DEVICE_NAME);    return info;}static aem_bat_charge_upate_evt_api_t s_bat_charge_upate_evt_cb = NULL;uint8_t aem_bat_charge_upate_evt_cb_register(aem_bat_charge_upate_evt_api_t cb){    s_bat_charge_upate_evt_cb = cb;    return 0;}void aem_adapter_dev_update_bat_charge_event(uint16_t cmd){    AEM_LOG_I("update_bat_charge_event %d \r\n", cmd);#ifndef CONFIG_SIMULATOR    switch (cmd)    {    case BAT_CHG_EVENT_CHARGE_START:    case BAT_CHG_EVENT_CHARGE_STOP:    case BAT_CHG_EVENT_CHARGE_FULL:    case BAT_CHG_EVENT_VOLTAGE_CHANGE:        if (s_bat_charge_upate_evt_cb != NULL)        {            s_bat_charge_upate_evt_cb(cmd);        }        break;    default:        break;    }#endif // !CONFIG_SIMULATOR}static bool check_bat_is_lowpower(void){#ifndef CONFIG_SIMULATOR    return power_manager_check_bat_is_lowpower();#else    return 0;#endif // !CONFIG_SIMULATOR}static const aem_device_ops_t s_device_ops = {    .disp_set_brightness = aem_disp_set_brightness,    .disp_get_brightness = aem_disp_get_brightness,    .get_bt_mac_addr = get_ble_mac_addr,    .get_ble_mac_addr = get_ble_mac_addr,    .get_sn_str = get_sn_str,    .get_battery_percentage = get_battery_percentage,    .get_battery_vol = get_battery_vol,    .get_charge_status = get_charge_status,    .get_rtc_time = get_rtc_time,    .set_rtc_time = set_rtc_time,#ifndef CONFIG_SIMULATOR    .motor_get_level = aem_dev_motor_get_level,    .motor_set_level = aem_dev_motor_set_level,    .motor_start = aem_dev_motor_start,    .motor_stop = aem_dev_motor_stop,    .motor_control = aem_dev_motor_control,    .motor_set_freq = aem_dev_motor_set_freq,#endif    .get_device_info = get_device_info,    .check_bat_is_lowpower = check_bat_is_lowpower,};const aem_device_ops_t *aem_get_device_ops(void){#ifndef CONFIG_SIMULATOR    aem_motor_ctx_init();#endif    return &s_device_ops;}
 |