初始提交

This commit is contained in:
2025-05-13 22:00:58 +08:00
commit e4c030b0c0
564 changed files with 78858 additions and 0 deletions

View File

@ -0,0 +1,73 @@
#===============================================================================
# @brief cmake file
# Copyright (c) ShangHai High-flying Electronics Technology Co.,Ltd 2024-2024. All rights reserved.
#===============================================================================
set(COMPONENT_NAME "user_main")
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/app_main.c
)
if (DEFINES MATCHES "HF_MCU_OTA")
list(APPEND SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/mcu_update.c)
endif()
set(PUBLIC_HEADER
${CMAKE_CURRENT_SOURCE_DIR}
)
set(PRIVATE_HEADER
)
set(PRIVATE_DEFINES
)
set(PUBLIC_DEFINES
)
# use this when you want to add ccflags like -include xxx
set(COMPONENT_PUBLIC_CCFLAGS #当前组件需要对外提供的编译选项
#-Wno-error=maybe-uninitialized #变量未初始化
#-Wno-error=pointer-sign #传递参数类型不匹配
#-Wno-error=char-subscripts #使用char类作为数组下标,char有符号
#-Wno-error=unused-label #未使用的标签比如用goto
#-Wno-error=strict-prototypes #函数的声明或定义没有参数
#-Wno-unused-parameter #未使用的函数参数
#-Wno-unused-variable #未使用的变量
#-Wno-unused-function #声明但未使用函数
#-Wno-unused-but-set-variable #设置了但未使用的变量
#-Wsign-compare #有符号与无符号比较
#-Wno-error=pointer-arith
#-Wno-error=sign-compare
#-Wno-error=jump-misses-init
#-Wno-error=incompatible-pointer-types
#-Wno-error=logical-op
#-Wno-error=empty-body
)
set(COMPONENT_CCFLAGS #当前组件内部生效的编译选项
-Wno-error=maybe-uninitialized #变量未初始化
-Wno-error=pointer-sign #传递参数类型不匹配
-Wno-error=char-subscripts #使用char类作为数组下标,char有符号
-Wno-error=unused-label #未使用的标签比如用goto
-Wno-error=strict-prototypes #函数的声明或定义没有参数
-Wno-unused-parameter #未使用的函数参数
-Wno-unused-variable #未使用的变量
-Wno-unused-function #声明但未使用函数
-Wno-unused-but-set-variable #设置了但未使用的变量
-Wsign-compare #有符号与无符号比较
-Wno-error=pointer-arith
-Wno-error=sign-compare
-Wno-error=jump-misses-init
-Wno-error=incompatible-pointer-types
-Wno-error=logical-op
-Wno-error=empty-body
)
set(WHOLE_LINK
true
)
set(MAIN_COMPONENT
true
)
build_component()

View File

@ -0,0 +1,258 @@
/* assis_thread.c
*
* Copyright (C) 2024 ShangHai High-flying Electronics Technology Co.,Ltd.
*
* This file is part of HSF.
*
*/
#include "hsf.h"
#include "spotlight.h"
#ifdef HF_MCU_OTA
#include "mcu_update.h"
#endif
int g_module_id = HFM_TYPE_LPT262;
const int hf_lpt_262_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
HFM_NOPIN, //HFGPIO_F_JTAG_TCK
HFM_NOPIN, //HFGPIO_F_JTAG_TDO
HFM_NOPIN, //HFGPIO_F_JTAG_TDI
HFM_NOPIN, //HFGPIO_F_JTAG_TMS
HFM_NOPIN, //HFGPIO_F_USBDP
HFM_NOPIN, //HFGPIO_F_USBDM
LPT26x_GPIO17, //HFGPIO_F_UART0_TX
HFM_NOPIN, //HFGPIO_F_UART0_RTS
LPT26x_GPIO18, //HFGPIO_F_UART0_RX
HFM_NOPIN, //HFGPIO_F_UART0_CTS
HFM_NOPIN, //HFGPIO_F_SPI_MISO
HFM_NOPIN, //HFGPIO_F_SPI_CLK
HFM_NOPIN, //HFGPIO_F_SPI_CS
HFM_NOPIN, //HFGPIO_F_SPI_MOSI
LPT26x_GPIO15, //HFGPIO_F_UART1_TX,
HFM_NOPIN, //HFGPIO_F_UART1_RTS,
LPT26x_GPIO16, //HFGPIO_F_UART1_RX,
HFM_NOPIN, //HFGPIO_F_UART1_CTS,
LPT26x_GPIO2, //HFGPIO_F_NLINK
LPT26x_GPIO10, //HFGPIO_F_NREADY
LPT26x_GPIO5, //HFGPIO_F_NRELOAD
HFM_NOPIN, //HFGPIO_F_SLEEP_RQ
HFM_NOPIN, //HFGPIO_F_SLEEP_ON
HFM_NOPIN, //HFGPIO_F_WPS
HFM_NOPIN, //HFGPIO_F_RESERVE1
HFM_NOPIN, //HFGPIO_F_RESERVE2
HFM_NOPIN, //HFGPIO_F_RESERVE3
HFM_NOPIN, //HFGPIO_F_RESERVE4
HFM_NOPIN, //HFGPIO_F_RESERVE5
HFM_NOPIN, //HFGPIO_F_USER_DEFINE
};
const int hf_lpt_263_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
HFM_NOPIN, //HFGPIO_F_JTAG_TCK
HFM_NOPIN, //HFGPIO_F_JTAG_TDO
HFM_NOPIN, //HFGPIO_F_JTAG_TDI
HFM_NOPIN, //HFGPIO_F_JTAG_TMS
HFM_NOPIN, //HFGPIO_F_USBDP
HFM_NOPIN, //HFGPIO_F_USBDM
LPT26x_GPIO17, //HFGPIO_F_UART0_TX
HFM_NOPIN, //HFGPIO_F_UART0_RTS
LPT26x_GPIO18, //HFGPIO_F_UART0_RX
HFM_NOPIN, //HFGPIO_F_UART0_CTS
HFM_NOPIN, //HFGPIO_F_SPI_MISO
HFM_NOPIN, //HFGPIO_F_SPI_CLK
HFM_NOPIN, //HFGPIO_F_SPI_CS
HFM_NOPIN, //HFGPIO_F_SPI_MOSI
LPT26x_GPIO15, //HFGPIO_F_UART1_TX,
HFM_NOPIN, //HFGPIO_F_UART1_RTS,
LPT26x_GPIO16, //HFGPIO_F_UART1_RX,
HFM_NOPIN, //HFGPIO_F_UART1_CTS,
LPT26x_GPIO7, //HFGPIO_F_NLINK
LPT26x_GPIO8, //HFGPIO_F_NREADY
LPT26x_GPIO9, //HFGPIO_F_NRELOAD
HFM_NOPIN, //HFGPIO_F_SLEEP_RQ
HFM_NOPIN, //HFGPIO_F_SLEEP_ON
HFM_NOPIN, //HFGPIO_F_WPS
HFM_NOPIN, //HFGPIO_F_RESERVE1
HFM_NOPIN, //HFGPIO_F_RESERVE2
HFM_NOPIN, //HFGPIO_F_RESERVE3
HFM_NOPIN, //HFGPIO_F_RESERVE4
HFM_NOPIN, //HFGPIO_F_RESERVE5
HFM_NOPIN, //HFGPIO_F_USER_DEFINE
};
const int hf_lpt_162_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
HFM_NOPIN, //HFGPIO_F_JTAG_TCK
HFM_NOPIN, //HFGPIO_F_JTAG_TDO
HFM_NOPIN, //HFGPIO_F_JTAG_TDI
HFM_NOPIN, //HFGPIO_F_JTAG_TMS
HFM_NOPIN, //HFGPIO_F_USBDP
HFM_NOPIN, //HFGPIO_F_USBDM
LPT26x_GPIO17, //HFGPIO_F_UART0_TX
HFM_NOPIN, //HFGPIO_F_UART0_RTS
LPT26x_GPIO18, //HFGPIO_F_UART0_RX
HFM_NOPIN, //HFGPIO_F_UART0_CTS
HFM_NOPIN, //HFGPIO_F_SPI_MISO
HFM_NOPIN, //HFGPIO_F_SPI_CLK
HFM_NOPIN, //HFGPIO_F_SPI_CS
HFM_NOPIN, //HFGPIO_F_SPI_MOSI
LPT26x_GPIO15, //HFGPIO_F_UART1_TX,
HFM_NOPIN, //HFGPIO_F_UART1_RTS,
LPT26x_GPIO16, //HFGPIO_F_UART1_RX,
HFM_NOPIN, //HFGPIO_F_UART1_CTS,
LPT26x_GPIO7, //HFGPIO_F_NLINK
LPT26x_GPIO8, //HFGPIO_F_NREADY
LPT26x_GPIO9, //HFGPIO_F_NRELOAD
HFM_NOPIN, //HFGPIO_F_SLEEP_RQ
HFM_NOPIN, //HFGPIO_F_SLEEP_ON
HFM_NOPIN, //HFGPIO_F_WPS
HFM_NOPIN, //HFGPIO_F_RESERVE1
HFM_NOPIN, //HFGPIO_F_RESERVE2
HFM_NOPIN, //HFGPIO_F_RESERVE3
HFM_NOPIN, //HFGPIO_F_RESERVE4
HFM_NOPIN, //HFGPIO_F_RESERVE5
HFM_NOPIN, //HFGPIO_F_USER_DEFINE
};
#ifdef ENABLE_BLE_SCAN
void hf_ble_scan_callback(gap_scan_result_data_t *scan_result_data)
{
// todo
return ;
}
int hfble_start_scan(void)
{
int ret = 0;
gap_ble_scan_params_t ble_scan_param = {
.scan_interval = 0x40, //30*8/5*2*2,
.scan_window = 0x20, //30*8/5/2,
.scan_type = 0x00,
.scan_phy = GAP_BLE_PHY_1M,
.scan_filter_policy = 0x00,
};
gap_ble_extern_scan_params_t ble_extern_scan_param = {
.filter_duplicate = 0,
.duration = 0,
.period = 0,
};
ret = gap_ble_set_scan_parameters(&ble_scan_param);
if(ret != 0){
HILINK_SAL_DEBUG("gap_ble_set_scan_parameters failed, errcode = 0x%X\r\n", ret);
return -1;
}
ret = gap_ble_set_scan_extern_parameters(&ble_extern_scan_param);
if(ret != 0){
HILINK_SAL_DEBUG("gap_ble_set_scan_extern_parameters failed, errcode = 0x%X\r\n", ret);
return -1;
}
ret = gap_ble_start_scan();
if(ret != 0){
HILINK_SAL_DEBUG("gap_ble_start_scan failed, errcode = 0x%X\r\n", ret);
return -1;
}
HILINK_SAL_DEBUG("*****************ble start scan*****************\r\n");
return 0;
}
#endif
static int USER_FUNC uart_recv_callback(uint32_t event,char *data,uint32_t len,uint32_t buf_len)
{
if(data == NULL)
return 0;
if(event == HFNET_UART0_DATA_READY)
HF_Debug(DEBUG_LEVEL_LOW,"[%d]uart 0 recv %d bytes data %d\n",event,len,buf_len);
else if(event == HFNET_UART1_DATA_READY)
HF_Debug(DEBUG_LEVEL_LOW,"[%d]uart 1 recv %d bytes data %d\n",event,len,buf_len);
if(hfsys_get_run_mode() == HFSYS_STATE_RUN_CMD)
return len;
return len;
}
void user_app_init(void)
{
u_printf("user app init\r\n");
}
#define APPVER "1.0.0"
int hf_atcmd_appver(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc == 0)
{
sprintf(rsp,"=LPT262_Harmonyos_%s_%s",APPVER,"20250227");
return 0;
}
return -3;
}
const hfat_cmd_t user_define_at_cmds_table[]=
{
{"APPVER", hf_atcmd_appver, "AT+APPVER: get version. \r\n",NULL},
#ifdef HF_MCU_OTA
{"MCUOTA", hf_atcmd_mcu, "AT+MCUOTA: start mcu ota. \r\n",NULL},
{"MCUVER", hf_atcmd_mcuver, "AT+MCUVER: get mcu version. \r\n",NULL},
{"OTAWAITTIME", hf_atcmd_ota_wait_time, "AT+OTAWAITTIME: set/get uart send mcu ota time\r\n", NULL},
#endif
{NULL, NULL, NULL, NULL} //the last item must be null
};
int USER_FUNC user_app_main(void)
{
//AT+UART uart0
if(hfnet_start_uart(HFTHREAD_PRIORITIES_LOW,(hfnet_callback_t)uart_recv_callback)!=HF_SUCCESS)
{
HF_Debug(DEBUG_WARN,"start uart fail!\r\n");
}
#ifdef HF_MCU_OTA
if(hf_mcu_init() != HF_SUCCESS)
HF_Debug(DEBUG_WARN,"init mcu ota fail!\r\n");
#endif
//See Wi-Fi Config tools APP for detailed usage of this thread, only debug mode open
if(hfdbg_get_level())
{
while(!hfnet_wifi_is_active())
{
msleep(50);
}
if(hfnet_start_assis(ASSIS_PORT)!=HF_SUCCESS)
{
HF_Debug(DEBUG_WARN,"start assis fail\r\n");
}
}
e_printf("start assis success!\r\n");
// hfgpio_pwm_enable(HFGPIO_F_SPI_CLK, 1000, 50);
spotlight_main();
return 1;
}

View File

@ -0,0 +1,259 @@
#include "mcu_update.h"
#include "string.h"
#include "hilink_open_ota_mcu_adapter.h"
#include "hilink_device.h"
mcu_info_t hf_mcu_info = {0};
unsigned char hf_mcu_step = OTA_STEP_NULL;
static int ota_wait_time=100;
int hf_atcmd_mcu(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc==0)
{
if(hf_mcu_info.ota == 0)
sprintf(rsp,"=%s","off");
else if(hf_mcu_info.ota == 1)
sprintf(rsp,"=%s","on");
return 0;
}
else if(argc==1)
{
if(strcasecmp(argv[0],"on") == 0)
{
if(hf_mcu_info.ota == 0)
{
hf_mcu_info.ota=1;
hf_save_mcu_info();
}
return 0;
}
else if(strcasecmp(argv[0],"off") == 0)
{
if(hf_mcu_info.ota == 1)
{
hf_mcu_info.ota=0;
hf_save_mcu_info();
}
return 0;
}
return -4;
}
return -3;
}
int hf_atcmd_mcuver(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc == 1)
{
if(strlen(argv[0]) > 10)
return -4;
hf_set_mcu_version((char *)argv[0]);
return 0;
}
return -3;
}
int hf_atcmd_ota_wait_time(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc == 0)
{
sprintf(rsp,"=%d",ota_wait_time);
return 0;
}
else if(argc == 1)
{
int time_wait=atoi(argv[0]);
if(time_wait<SEND_UART_WAIT_TIME_MIN || time_wait>SEND_UART_WAIT_TIME_MAX)
return -4;
ota_wait_time=time_wait;
if(hf_mcu_info.wait_time != time_wait)
{
hf_mcu_info.wait_time=ota_wait_time;
hf_save_mcu_info();
}
return 0;
}
return -3;
}
const char *hf_get_mcu_ver(void)
{
if(hf_mcu_info.head != MCU_INFO_HEAD)
{
memset(&hf_mcu_info, 0, sizeof(hf_mcu_info));
strcpy((char *)hf_mcu_info.ver, MCU_VER);
hf_save_mcu_info();
}
return (const char *)hf_mcu_info.ver;
}
void hf_set_mcu_version(char *ver)
{
if(strlen(ver) >= MCU_VER_MAX_SIZE)
return ;
strcpy((char *)hf_mcu_info.ver,ver);
if(hf_mcu_info.head != MCU_INFO_HEAD || strcmp((char *)hf_mcu_info.ver,ver) != 0)
{
memset((char *)hf_mcu_info.ver,0,sizeof(hf_mcu_info.ver));
strcpy((char *)hf_mcu_info.ver,ver);
hf_save_mcu_info();
}
u_printf("get mcu ver:%s\r\n",hf_mcu_info.ver);
}
int hf_save_mcu_info(void)
{
hf_mcu_info.head=MCU_INFO_HEAD;
hfuflash_erase_page(MCU_INFO_FILE,1);
hfuflash_write(MCU_INFO_FILE,&hf_mcu_info,sizeof(mcu_info_t));
return 0;
}
static int hf_mcu_ota_is_vaild(void)
{
return hf_mcu_info.ota;
}
void hf_save_mcu_status(unsigned char flag, int len)
{
if(flag == START_SEND_DATA)
{
u_printf("ready to recv mcu firmware...\n");
}
else if(flag == STOP_SEND_DATA)
{
u_printf("recv mcu firmware over...\n");
hf_mcu_info.updateFlag = 1;
hf_mcu_info.len = len;
hf_save_mcu_info();
}
else if(flag == SEND_DATA_ERROR)
{
u_printf("recv mcu firmware error...\n");
}
}
static void mcu_upgrade_thread(void *arg)
{
int i = 0;
int ret = 0;
int seq = 0;
int offset = 0;
unsigned char *buff = hfmem_malloc(512);
unsigned char *data = hfmem_malloc(1024);
if(data == NULL || buff == NULL)
return ;
u_printf("mcu ver:%s\r\n",hf_mcu_info.ver);
if((hf_mcu_info.updateFlag > 0) && (hf_mcu_info.len > 0))
hf_mcu_step = OTA_STEP_START;
if(hf_mcu_info.wait_time >= SEND_UART_WAIT_TIME_MIN && hf_mcu_info.wait_time <= SEND_UART_WAIT_TIME_MAX)
ota_wait_time=hf_mcu_info.wait_time;
else
ota_wait_time=100;
while(1)
{
switch (hf_mcu_step)
{
case OTA_STEP_START:
{
memset(buff, 0, 512);
if(HILINK_GetDevStatus() == HILINK_M2M_CLOUD_ONLINE)
{
sprintf((char *)buff, "AT+OTASTART=%d\r\n", hf_mcu_info.len);
hfuart_send(HFUART0,buff, strlen((char *)buff),0);
u_printf("mcu ota OTA_STEP_START\r\n");
hf_mcu_step += 0x01;
}
break;
}
case OTA_STEP_TRANS:
{
memset(buff, 0, 512);
memset(data, 0, 1024);
if(hf_mcu_info.len - offset >= OTA_READ_LEN)
{
flash_read(UPGRADE_ADDRESS + offset, buff, OTA_READ_LEN,0);
offset += OTA_READ_LEN;
seq += 0x01;
u_printf("mcu send data count :%d\r\n",offset/OTA_READ_LEN);
sprintf((char *)data, "AT+OTADATA=%d,%d,", seq, OTA_READ_LEN);
ret = strlen((char *)data);
for(i = 0; i < OTA_READ_LEN; i++)
sprintf((char *)data + ret + 2 * i, "%02x", buff[i]);
strncat((char *)data, "\r\n", 2);
hfuart_send(HFUART0,data, ret + (OTA_READ_LEN * 2) + 2,0);
}
else
{
flash_read(UPGRADE_ADDRESS + offset, buff, hf_mcu_info.len - offset,0);
seq += 0x01;
sprintf((char *)data, "AT+OTADATA=%d,%d,", seq, hf_mcu_info.len - offset);
ret = strlen((char *)data);
for(i = 0; i < (hf_mcu_info.len - offset); i++)
sprintf((char *)data + ret + 2 * i, "%02x", buff[i]);
strncat((char *)data, "\r\n", 2);
hfuart_send(HFUART0,data, ret + ((hf_mcu_info.len - offset) * 2) + 2,0);
offset = hf_mcu_info.len;
hf_mcu_step += 0x01;
}
break;
}
case OTA_STEP_END:
{
hfuart_send(HFUART0,(unsigned char *)"AT+OTAEND\r\n", strlen("AT+OTAEND\r\n"),0);
seq = 0;
offset = 0;
hf_mcu_step = OTA_STEP_NULL;
hf_mcu_info.updateFlag = 0;
hf_save_mcu_info();
goto EXIT;
break;
}
case OTA_STEP_ERROR:
{
hfuart_send(HFUART0,(unsigned char *)"AT+OTAERR\r\n", strlen("AT+OTAERR\r\n"),0);
hf_mcu_step = OTA_STEP_NULL;
goto EXIT;
break;
}
default:
break;
}
msleep(ota_wait_time);
}
EXIT:
hfmem_free(data);
hfmem_free(buff);
hfthread_destroy(0);
}
void hf_mcu_thread(void)
{
hfthread_create(mcu_upgrade_thread, "hf-mcu-thread", 1024, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL);
}
int hf_mcu_init(void)
{
hfuflash_read(MCU_INFO_FILE,&hf_mcu_info,sizeof(mcu_info_t));
if(hf_mcu_ota_is_vaild())
{
return -1;
}
char *get_mcu_ver="AT+GETMCUVER\r\n";
hfuart_send(HFUART0,(unsigned char *)get_mcu_ver, strlen(get_mcu_ver),0);
if(hf_mcu_info.updateFlag && hf_mcu_info.len)
hf_mcu_thread();
return HF_SUCCESS;
}

View File

@ -0,0 +1,49 @@
#ifndef MCU_UPDATE_H
#define MCU_UPDATE_H
#include <stdint.h>
#include "hsf.h"
#define MCU_INFO_HEAD 0xA5A5
#define MCU_INFO_FILE 0
#define MCU_VER "1.0.0"
#define OTA_READ_LEN 256
#define OTA_STEP_NULL 0
#define OTA_STEP_START 1
#define OTA_STEP_TRANS 2
#define OTA_STEP_END 3
#define OTA_STEP_ERROR 4
#define MCU_VER_MAX_SIZE 8
#define SEND_UART_WAIT_TIME_MIN 20
#define SEND_UART_WAIT_TIME_MAX 500
#pragma pack(push)
#pragma pack(1)
typedef struct{
uint16_t head;
unsigned char ota;
unsigned char updateFlag;
uint16_t wait_time;
unsigned char ver[MCU_VER_MAX_SIZE];
unsigned int len;
unsigned char reserve[5];
}mcu_info_t;
#pragma pack(pop)
const char *hf_get_mcu_ver(void);
void hf_save_mcu_status(unsigned char flag, int len);
void hf_mcu_thread(void);
void hf_set_mcu_version(char *ver);
int hf_mcu_init(void);
int hf_save_mcu_info(void);
int hf_atcmd_mcu(pat_session_t s,int argc,char *argv[],char *rsp,int len);
int hf_atcmd_mcuver(pat_session_t s,int argc,char *argv[],char *rsp,int len);
int hf_atcmd_ota_wait_time(pat_session_t s,int argc,char *argv[],char *rsp,int len);
#endif

View File

@ -0,0 +1,70 @@
#include "spotlight.h"
#include "cJSON.h"
#define CHECK_JSON_ITEM(condition) if(condition) { \
goto lab_end; \
}
#define MAX_BRIGHTNESS 100
#define MIN_BRIGHTNESS 0
#define MAX_CCT 6500
#define MIN_CCT 2700
typedef struct device_control_s {
bool on;
int32_t colorTemperature;
int32_t brightness;
} device_control_t;
device_control_t g_device_control = {
.on = false,
.colorTemperature = MAX_CCT,
.brightness = MAX_BRIGHTNESS / 2,
};
int device_module_control(const char *svcId, const char *payload, unsigned int len)
{
if ((svcId == NULL) || (payload == NULL)) {
return -1;
}
cJSON *item = NULL;
cJSON *json = cJSON_Parse(payload);
CHECK_JSON_ITEM(json == NULL);
e_printf("put char sid:%s,payload:%s\r\n",svcId,payload);
if(strcmp(svcId, "brightness") == 0)
{
item = cJSON_GetObjectItem(json,"brightness");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
}
else if(strcmp(svcId, "cct") == 0)
{
item = cJSON_GetObjectItem(json,"colorTemperature");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
} else if(strcmp(svcId, "lightMode") == 0) // scense mode
{
item = cJSON_GetObjectItem(json,"mode");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
}
else if(strcmp(svcId, "switch") == 0) // device open/close
{
item = cJSON_GetObjectItem(json,"on");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
}
lab_end:
if(json != NULL)
{
cJSON_Delete(json);
json = NULL;
}
return 0;
}

View File

@ -0,0 +1,7 @@
#ifndef __SPOTLIGHT_H__
#define __SPOTLIGHT_H__
#include <hsf.h>
int spotlight_main(void);
#endif

View File

@ -0,0 +1,120 @@
#include "hsf.h"
#if defined(CONFIG_PWM_SUPPORT_LPM)
#include "pm_veto.h"
#endif
#include "pinctrl.h"
#include "gpio.h"
#include "pwm.h"
// #include "tcxo.h"
#include "spotlight.h"
#define BRIGHTNESS_PIN GPIO_00
#define CCT_PIN GPIO_04
#define SWITCH_PIN GPIO_13
#define BRIGHTNESS_PWM_CHANNEL 0
#define CCT_PWM_CHANNEL 4
#define CONFIG_PWM_GROUP_ID 0
static errcode_t pwm_sample_callback(uint8_t channel)
{
e_printf("PWM %d, cycle done. \r\n", channel);
return ERRCODE_SUCC;
}
static void gpio_init(pin_t pin)
{
/* PINCTRL init. */
uapi_pin_init();
uapi_pin_set_mode(pin, HAL_PIO_FUNC_GPIO);
uapi_gpio_set_dir(pin, GPIO_DIRECTION_OUTPUT);
uapi_pin_set_ds(pin, PIN_DS_3);
uapi_pin_set_pull(pin, PIN_PULL_TYPE_DOWN);
uapi_gpio_set_val(pin, GPIO_LEVEL_LOW);
}
static void pwm_sys_init() {
uapi_pwm_deinit();
uapi_pwm_init();
}
static void pwm_init(pin_t pin, pin_t pin1, uint8_t channel, uint channel1)
{
pwm_config_t cfg_no_repeat = {
0,
0,
0,
0,
true
};
uint32_t frequency = uapi_pwm_get_frequency(channel);
e_printf("pwm基础时钟频率为: %dHZ\r\n", frequency);
// 计算1KHz PWM所需的周期值高电平+低电平)
uint32_t total_period = frequency / 1000;
// 50%占空比,高低电平时间相等
cfg_no_repeat.high_time = total_period / 2;
cfg_no_repeat.low_time = total_period / 2;
e_printf("设置1KHz PWM50%%占空比,周期值: %d\r\n", total_period);
uapi_pin_set_mode(pin, PIN_MODE_3); //set pin as pwm output
uapi_pin_set_mode(pin1, PIN_MODE_3); //set pin as pwm output
uapi_pwm_open(channel, &cfg_no_repeat);
uapi_pwm_open(channel1, &cfg_no_repeat);
// uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
// uapi_pwm_unregister_interrupt(channel);
// uapi_pwm_register_interrupt(channel, pwm_sample_callback);
#ifdef CONFIG_PWM_USING_V151
uint8_t channel_id[2] = {channel, channel1};
/* channel_id can also choose to configure multiple channels, and the third parameter also needs to be adjusted
accordingly. */
uapi_pwm_set_group(CONFIG_PWM_GROUP_ID, channel_id, 2);
#else
uapi_pwm_start(channel);
#endif
}
void pwm_start()
{
#ifdef CONFIG_PWM_USING_V151
/* Here you can also call the uapi_pwm_start interface to open each channel individually. */
uapi_pwm_start_group(CONFIG_PWM_GROUP_ID);
#endif
}
void pwm_sys_deinit(void)
{
// uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
#ifdef CONFIG_PWM_USING_V151
uapi_pwm_close(CONFIG_PWM_GROUP_ID);
#else
uapi_pwm_close(CONFIG_PWM_CHANNEL);
#endif
// uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
uapi_pwm_deinit();
}
static void pwm_entry(void)
{
pwm_sys_init();
pwm_init(BRIGHTNESS_PIN, BRIGHTNESS_PWM_CHANNEL, CCT_PIN, CCT_PWM_CHANNEL);
pwm_start();
}
int spotlight_main(void) {
gpio_init(SWITCH_PIN);
pwm_entry();
while (1) {
e_printf("spotlight_main\r\n");
uapi_gpio_toggle(SWITCH_PIN);
msleep(1000);
}
return 0;
}