116 lines
3.1 KiB
C
116 lines
3.1 KiB
C
#include "hsf.h"
|
||
#include "spotlight.h"
|
||
#include "pinctrl.h"
|
||
#include "gpio.h"
|
||
#include "pinctrl.h"
|
||
#include "pwm.h"
|
||
#include "timer.h"
|
||
#include "soc_osal.h"
|
||
#include "systick.h"
|
||
|
||
extern device_control_t g_device_control;
|
||
extern void update_pwm_output(void);
|
||
extern void calculate_pwm_duty(void);
|
||
|
||
// AT指令:设置PWM占空比
|
||
// 格式:AT+PWM=<channel>,<duty>
|
||
// channel: 0-冷白LED, 1-暖白LED
|
||
// duty: 0-1000
|
||
int hf_atcmd_pwm(pat_session_t s, int argc, char *argv[], char *rsp, int len)
|
||
{
|
||
if (argc != 2) {
|
||
e_printf(rsp, "ERROR: Invalid parameters. Usage: AT+PWM=<channel>,<duty>");
|
||
return -1;
|
||
}
|
||
|
||
int channel = atoi(argv[0]);
|
||
int duty = atoi(argv[1]);
|
||
|
||
// 参数检查
|
||
if (channel < 0 || channel > 1) {
|
||
e_printf(rsp, "ERROR: Invalid channel. Must be 0(CW) or 1(WW)");
|
||
return -1;
|
||
}
|
||
|
||
if (duty < 0 || duty > PWM_DUTY_RATIO_MAX) {
|
||
e_printf(rsp, "ERROR: Invalid duty. Must be 0-%d", PWM_DUTY_RATIO_MAX);
|
||
return -1;
|
||
}
|
||
|
||
// 设置PWM占空比
|
||
if (channel == 0) {
|
||
g_device_control.duty_cw = duty;
|
||
} else {
|
||
g_device_control.duty_ww = duty;
|
||
}
|
||
|
||
// 更新PWM输出
|
||
update_pwm_output();
|
||
|
||
sprintf(rsp, "OK: Channel %d PWM duty set to %d", channel, duty);
|
||
return 0;
|
||
}
|
||
|
||
// AT指令:获取当前PWM占空比
|
||
// 格式:AT+PWM?
|
||
int hf_atcmd_pwm_query(pat_session_t s, int argc, char *argv[], char *rsp, int len)
|
||
{
|
||
sprintf(rsp, "+PWM: CW=%d,WW=%d", g_device_control.duty_cw, g_device_control.duty_ww);
|
||
return 0;
|
||
}
|
||
|
||
// AT指令:直接设置色温和亮度
|
||
// 格式:AT+LIGHT=<brightness>,<cct>
|
||
// brightness: 0-1000
|
||
// cct: 2700-6500
|
||
int hf_atcmd_light(pat_session_t s, int argc, char *argv[], char *rsp, int len)
|
||
{
|
||
if (argc != 2) {
|
||
e_printf(rsp, "ERROR: Invalid parameters. Usage: AT+LIGHT=<brightness>,<cct>");
|
||
return -1;
|
||
}
|
||
|
||
int brightness = atoi(argv[0]);
|
||
int cct = atoi(argv[1]);
|
||
|
||
// 参数检查
|
||
if (brightness < 0 || brightness > BRIGHTNESS_MAX) {
|
||
e_printf(rsp, "ERROR: Invalid brightness. Must be 0-%d", BRIGHTNESS_MAX);
|
||
return -1;
|
||
}
|
||
|
||
if (cct < CCT_MIN || cct > CCT_MAX) {
|
||
e_printf(rsp, "ERROR: Invalid CCT. Must be %d-%d", CCT_MIN, CCT_MAX);
|
||
return -1;
|
||
}
|
||
|
||
// 设置亮度和色温
|
||
g_device_control.brightness_local = BRIGHTNESS_REMOTE2LOCAL(brightness);
|
||
g_device_control.cct_local = CCT_REMOTE2LOCAL(cct);
|
||
|
||
// 计算PWM占空比并更新输出
|
||
calculate_pwm_duty();
|
||
update_pwm_output();
|
||
|
||
sprintf(rsp, "OK: Brightness=%d, CCT=%d, CW=%d%%%%, WW=%d%%%%",
|
||
brightness, cct, g_device_control.duty_cw, g_device_control.duty_ww);
|
||
return 0;
|
||
}
|
||
|
||
// AT指令:获取当前亮度和色温
|
||
// 格式:AT+LIGHT?
|
||
int hf_atcmd_light_query(pat_session_t s, int argc, char *argv[], char *rsp, int len)
|
||
{
|
||
sprintf(rsp, "+LIGHT: Brightness=%d, CCT=%d, CW=%d, WW=%d",
|
||
g_device_control.brightness_local,
|
||
g_device_control.cct_local,
|
||
g_device_control.duty_cw,
|
||
g_device_control.duty_ww);
|
||
return 0;
|
||
}
|
||
|
||
// 注册AT指令
|
||
const hfat_cmd_t spotlight_ut_cmds_table[] = {
|
||
|
||
{NULL, NULL, NULL, NULL}
|
||
};
|