初始提交
This commit is contained in:
70
application/ws63/user_main/spotlight/device_module.c
Normal file
70
application/ws63/user_main/spotlight/device_module.c
Normal 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;
|
||||
}
|
||||
|
||||
|
7
application/ws63/user_main/spotlight/spotlight.h
Normal file
7
application/ws63/user_main/spotlight/spotlight.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef __SPOTLIGHT_H__
|
||||
#define __SPOTLIGHT_H__
|
||||
|
||||
#include <hsf.h>
|
||||
int spotlight_main(void);
|
||||
#endif
|
||||
|
120
application/ws63/user_main/spotlight/spotlight_main.c
Normal file
120
application/ws63/user_main/spotlight/spotlight_main.c
Normal 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 PWM,50%%占空比,周期值: %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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user