styles: optimize tanle
This commit is contained in:
@@ -19,32 +19,32 @@
|
||||
|
||||
void iot_explorer_handle_power_switch(int power_switch)
|
||||
{
|
||||
if (0 == power_switch){
|
||||
if(LED_IS_OPEN){
|
||||
printf("iot-explorer close the light\r\n");
|
||||
LED_CLOSE;
|
||||
}else{
|
||||
printf("the light already closed\r\n");
|
||||
}
|
||||
}else{
|
||||
if(LED_IS_OPEN){
|
||||
printf("iot-explorer already opened\r\n");
|
||||
}else{
|
||||
printf("the light closed already\r\n");
|
||||
LED_OPEN;
|
||||
}
|
||||
}
|
||||
if (0 == power_switch){
|
||||
if(LED_IS_OPEN){
|
||||
printf("iot-explorer close the light\r\n");
|
||||
LED_CLOSE;
|
||||
}else{
|
||||
printf("the light already closed\r\n");
|
||||
}
|
||||
}else{
|
||||
if(LED_IS_OPEN){
|
||||
printf("iot-explorer already opened\r\n");
|
||||
}else{
|
||||
printf("the light closed already\r\n");
|
||||
LED_OPEN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void key1_handle_power_switch(void)
|
||||
{
|
||||
if(LED_IS_OPEN){
|
||||
printf("key1 close the light\r\n");
|
||||
LED_CLOSE;
|
||||
}else{
|
||||
printf("key1 open the light\r\n");
|
||||
LED_OPEN;
|
||||
}
|
||||
if(LED_IS_OPEN){
|
||||
printf("key1 close the light\r\n");
|
||||
LED_CLOSE;
|
||||
}else{
|
||||
printf("key1 open the light\r\n");
|
||||
LED_OPEN;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
@@ -55,7 +55,7 @@ void key1_handle_power_switch(void)
|
||||
***************************************************************/
|
||||
static void Init_BH1750(void)
|
||||
{
|
||||
uint8_t t_Data = 0x01;
|
||||
uint8_t t_Data = 0x01;
|
||||
HAL_I2C_Master_Transmit(&hi2c2,BH1750_Addr,&t_Data,1,0xff);
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ void TIM3_Init(uint16_t arr,uint16_t psc) //arr:自动重装值 psc:时钟
|
||||
HAL_TIM_Base_Init(&TIM3_Handler); //时基初始化
|
||||
|
||||
//HAL_TIM_Base_Start_IT(&TIM3_Handler); //使能定时器3和定时器3更新中断:TIM_IT_UPDATE
|
||||
//HAL_TIM_Base_Stop_IT(&TIM3_Handler);
|
||||
//HAL_TIM_Base_Stop_IT(&TIM3_Handler);
|
||||
}
|
||||
|
||||
//TIM3_Init(1, 719);
|
||||
@@ -138,9 +138,9 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
if(htim->Instance==TIM3)
|
||||
{
|
||||
__HAL_RCC_TIM3_CLK_ENABLE(); //使能TIM3时钟
|
||||
HAL_NVIC_SetPriority(TIM3_IRQn,1,3); //设置中断优先级,抢占优先级1,子优先级3
|
||||
HAL_NVIC_EnableIRQ(TIM3_IRQn); //开启ITM3中断
|
||||
__HAL_RCC_TIM3_CLK_ENABLE(); //使能TIM3时钟
|
||||
HAL_NVIC_SetPriority(TIM3_IRQn,1,3); //设置中断优先级,抢占优先级1,子优先级3
|
||||
HAL_NVIC_EnableIRQ(TIM3_IRQn); //开启ITM3中断
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,17 +153,17 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
if(htim==(&TIM3_Handler))
|
||||
{
|
||||
if(pwm_count < light_pwm)
|
||||
{
|
||||
set_light(true);
|
||||
} else {
|
||||
set_light(false);
|
||||
}
|
||||
if(pwm_count >= 100)
|
||||
{
|
||||
pwm_count = 0;
|
||||
}
|
||||
pwm_count++;
|
||||
if(pwm_count < light_pwm)
|
||||
{
|
||||
set_light(true);
|
||||
} else {
|
||||
set_light(false);
|
||||
}
|
||||
if(pwm_count >= 100)
|
||||
{
|
||||
pwm_count = 0;
|
||||
}
|
||||
pwm_count++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@@ -30,15 +30,6 @@
|
||||
#define BH1750_Addr 0x46
|
||||
#define LIGHT_MAX_NAME_LEN 32
|
||||
|
||||
#if 0
|
||||
typedef enum{
|
||||
LIGHT_LEVEL_LOW = 0,
|
||||
LIGTH_LEVEL_MID = 1,
|
||||
LIGTH_LEVEL_HIGH = 2,
|
||||
LIGTH_LEVEL_BUTT,
|
||||
}E_LIGTH_LEVEL;
|
||||
#endif
|
||||
|
||||
int e53_sc1_init(void);
|
||||
|
||||
float e53_scl_read_data(void);
|
||||
@@ -46,4 +37,13 @@ float e53_scl_read_data(void);
|
||||
void iot_explorer_handle_power_switch(int power_switch);
|
||||
|
||||
void key1_handle_power_switch(void);
|
||||
|
||||
#if 0
|
||||
typedef enum{
|
||||
LIGHT_LEVEL_LOW = 0,
|
||||
LIGTH_LEVEL_MID = 1,
|
||||
LIGTH_LEVEL_HIGH = 2,
|
||||
LIGTH_LEVEL_BUTT,
|
||||
}E_LIGTH_LEVEL;
|
||||
#endif
|
||||
#endif /* _E53_SC1_H_ */
|
||||
|
@@ -11,12 +11,12 @@ extern void stm32g0xx_key_init(void);
|
||||
#define DEVICE_NAME "techo_test01"
|
||||
#define DEVICE_KEY "UOgIWiO8kcVkgif6FO3tJQ=="
|
||||
|
||||
#define YOUR_WIFI_SSID "iot-explorer"
|
||||
#define YOUR_WIFI_PWD "iot-explorer"
|
||||
#define YOUR_WIFI_SSID "iot-explorer"
|
||||
#define YOUR_WIFI_PWD "iot-explorer"
|
||||
|
||||
// 数据模板支持数据增量上报
|
||||
#define REPORT_LX_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"lx\\\":%.0f}}"
|
||||
#define REPORT_POWER_SWITCH_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"power_switch\\\":%d}}"
|
||||
#define REPORT_LX_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"lx\\\":%.0f}}"
|
||||
#define REPORT_POWER_SWITCH_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"power_switch\\\":%d}}"
|
||||
|
||||
#define MAIL_SIZE 256
|
||||
#define MAIL_MAX 10
|
||||
@@ -33,7 +33,7 @@ void key1_handler_callback(void)
|
||||
|
||||
static void default_message_handler(mqtt_message_t* msg)
|
||||
{
|
||||
int payload_len = 0;
|
||||
int payload_len = 0;
|
||||
|
||||
printf("callback:\r\n");
|
||||
printf("---------------------------------------------------------\r\n");
|
||||
@@ -41,13 +41,13 @@ static void default_message_handler(mqtt_message_t* msg)
|
||||
printf("\tpayload:%s\r\n", msg->payload);
|
||||
printf("---------------------------------------------------------\r\n");
|
||||
|
||||
payload_len = strlen(msg->payload);
|
||||
if (payload_len > MAIL_SIZE){
|
||||
printf("too long payload\r\n");
|
||||
return;
|
||||
}
|
||||
payload_len = strlen(msg->payload);
|
||||
if (payload_len > MAIL_SIZE){
|
||||
printf("too long payload\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
tos_mail_q_post(&mail_q, msg->payload, MAIL_SIZE);
|
||||
tos_mail_q_post(&mail_q, msg->payload, MAIL_SIZE);
|
||||
}
|
||||
|
||||
static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0};
|
||||
@@ -66,7 +66,7 @@ static void mqtt_demo_task(void)
|
||||
device_info_t dev_info;
|
||||
memset(&dev_info, 0, sizeof(device_info_t));
|
||||
|
||||
float e53_value = 0;
|
||||
float e53_value = 0;
|
||||
char e53_str[16] = {0};
|
||||
|
||||
OLED_ShowString(0, 2, (uint8_t*)"connecting...", 16);
|
||||
@@ -120,28 +120,28 @@ static void mqtt_demo_task(void)
|
||||
}
|
||||
|
||||
while (1) {
|
||||
e53_value = e53_scl_read_data();
|
||||
printf("e53 value %.0f lx\r\n", e53_value);
|
||||
e53_value = e53_scl_read_data();
|
||||
printf("e53 value %.0f lx\r\n", e53_value);
|
||||
|
||||
memset(e53_str, 0, sizeof(e53_str));
|
||||
sprintf(e53_str, "%.0f lx(lm/m2)", e53_value);
|
||||
OLED_ShowString(0, 2, (uint8_t*)e53_str, 16);
|
||||
|
||||
if (key_pressed_flag){
|
||||
key_pressed_flag = false;
|
||||
memset(payload, 0, sizeof(payload));
|
||||
snprintf(payload, sizeof(payload), REPORT_POWER_SWITCH_DATA_TEMPLATE, LED_IS_OPEN);
|
||||
|
||||
if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) {
|
||||
printf("module mqtt pub fail\n");
|
||||
} else {
|
||||
printf("module mqtt pub success\n");
|
||||
}
|
||||
}
|
||||
|
||||
// 请在此处添加光照强度数据上报物联网开发平台的代码
|
||||
|
||||
tos_sleep_ms(5000);
|
||||
if (key_pressed_flag){
|
||||
key_pressed_flag = false;
|
||||
memset(payload, 0, sizeof(payload));
|
||||
snprintf(payload, sizeof(payload), REPORT_POWER_SWITCH_DATA_TEMPLATE, LED_IS_OPEN);
|
||||
|
||||
if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) {
|
||||
printf("module mqtt pub fail\n");
|
||||
} else {
|
||||
printf("module mqtt pub success\n");
|
||||
}
|
||||
}
|
||||
|
||||
// 请在此处添加光照强度数据上报物联网开发平台的代码
|
||||
|
||||
tos_sleep_ms(5000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,44 +155,44 @@ static void iot_explorer_data_handle(void *arg)
|
||||
cJSON *method;
|
||||
cJSON *p;
|
||||
|
||||
while(1){
|
||||
memset(buf, 0, MAIL_SIZE);
|
||||
if (K_ERR_NONE == tos_mail_q_pend(&mail_q, buf, &mail_size, TOS_TIME_FOREVER)){
|
||||
//printf("mail content: %s\r\n", buf);
|
||||
//字符串首尾的引号丢弃后才是标准的json数据格式
|
||||
buf[strlen(buf) - 1] = '\0';
|
||||
root = cJSON_Parse(buf + 1);
|
||||
if (NULL == root){
|
||||
printf("Invalid json root\r\n");
|
||||
continue;
|
||||
}
|
||||
method = cJSON_GetObjectItem(root, "method");
|
||||
if (NULL == method){
|
||||
printf("Invalid json method\r\n");
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
// 仅处理云端下发的 control 数据
|
||||
if (0 != strncmp(method->valuestring, "control", sizeof("control") - 1)){
|
||||
//printf("Invalid method\r\n");
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
// 若需要回复报文,需要携带数据报文的 clitenToken 字段
|
||||
//token = cJSON_GetObjectItem(root, "clientToken");
|
||||
params = cJSON_GetObjectItem(root, "params");
|
||||
if (NULL == params){
|
||||
printf("Invalid json params\r\n");
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 请在此处添加物联网开发平台对智能灯的控制代码
|
||||
|
||||
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
}
|
||||
while(1){
|
||||
memset(buf, 0, MAIL_SIZE);
|
||||
if (K_ERR_NONE == tos_mail_q_pend(&mail_q, buf, &mail_size, TOS_TIME_FOREVER)){
|
||||
//printf("mail content: %s\r\n", buf);
|
||||
//字符串首尾的引号丢弃后才是标准的json数据格式
|
||||
buf[strlen(buf) - 1] = '\0';
|
||||
root = cJSON_Parse(buf + 1);
|
||||
if (NULL == root){
|
||||
printf("Invalid json root\r\n");
|
||||
continue;
|
||||
}
|
||||
method = cJSON_GetObjectItem(root, "method");
|
||||
if (NULL == method){
|
||||
printf("Invalid json method\r\n");
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
// 仅处理云端下发的 control 数据
|
||||
if (0 != strncmp(method->valuestring, "control", sizeof("control") - 1)){
|
||||
//printf("Invalid method\r\n");
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
// 若需要回复报文,需要携带数据报文的 clitenToken 字段
|
||||
//token = cJSON_GetObjectItem(root, "clientToken");
|
||||
params = cJSON_GetObjectItem(root, "params");
|
||||
if (NULL == params){
|
||||
printf("Invalid json params\r\n");
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 请在此处添加物联网开发平台对智能灯的控制代码
|
||||
|
||||
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define TASK_HANDLE_MAIL_PRIORITY 3
|
||||
@@ -204,26 +204,26 @@ void application_entry(void *arg)
|
||||
{
|
||||
char *str = "TencentOS-tiny";
|
||||
|
||||
// 初始化tencent-os tiny定时器模块
|
||||
timer_init();
|
||||
// 初始化按键检测模块
|
||||
stm32g0xx_key_init();
|
||||
|
||||
// OLED 显示屏初始化
|
||||
// 初始化tencent-os tiny定时器模块
|
||||
timer_init();
|
||||
// 初始化按键检测模块
|
||||
stm32g0xx_key_init();
|
||||
|
||||
// OLED 显示屏初始化
|
||||
OLED_Init();
|
||||
OLED_Clear();
|
||||
OLED_ShowString(0, 0, (uint8_t*)str, 16);
|
||||
|
||||
// 创建tencent-os tiny邮箱,使用邮箱机制进行任务间通信
|
||||
tos_mail_q_create(&mail_q, mail_pool, MAIL_MAX, MAIL_SIZE);
|
||||
// 创建tencent-os tiny任务,任务遍历邮箱,处理iot-explorer下发的数据
|
||||
|
||||
// 创建tencent-os tiny邮箱,使用邮箱机制进行任务间通信
|
||||
tos_mail_q_create(&mail_q, mail_pool, MAIL_MAX, MAIL_SIZE);
|
||||
// 创建tencent-os tiny任务,任务遍历邮箱,处理iot-explorer下发的数据
|
||||
(void)tos_task_create(&task_handle_mail, "iot-explorer", iot_explorer_data_handle,
|
||||
(void *)NULL, TASK_HANDLE_MAIL_PRIORITY,
|
||||
stack_task_handle_mail, TASK_HANDLE_MAIL_STACK_SIZE, 0);
|
||||
|
||||
|
||||
mqtt_demo_task();
|
||||
while (1) {
|
||||
printf("This is a demo\r\n");
|
||||
tos_task_delay(10000);
|
||||
printf("This is a demo\r\n");
|
||||
tos_task_delay(10000);
|
||||
}
|
||||
}
|
||||
|
@@ -30,28 +30,28 @@ static bool key_pressed = false;
|
||||
static void key_check_timer_cb(void *arg)
|
||||
{
|
||||
if (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_0)){
|
||||
key_press_count++;
|
||||
key_release_count = 0;
|
||||
} else {
|
||||
key_press_count = 0;
|
||||
key_release_count++;
|
||||
}
|
||||
|
||||
// 支持按键消抖
|
||||
if (key_press_count > KEY_CHECK_SHAKING_COUNT) {
|
||||
// 按键按下时功能即生效,且只生效一次
|
||||
if (!key_pressed) {
|
||||
//printf("key 1 pressed\r\n");
|
||||
key_pressed = true;
|
||||
key1_handler_callback();
|
||||
}
|
||||
}
|
||||
if (key_release_count > KEY_CHECK_SHAKING_COUNT){
|
||||
if (key_pressed) {
|
||||
key_pressed = false;
|
||||
//printf("key 1 release\r\n");
|
||||
}
|
||||
}
|
||||
key_press_count++;
|
||||
key_release_count = 0;
|
||||
} else {
|
||||
key_press_count = 0;
|
||||
key_release_count++;
|
||||
}
|
||||
|
||||
// 支持按键消抖
|
||||
if (key_press_count > KEY_CHECK_SHAKING_COUNT) {
|
||||
// 按键按下时功能即生效,且只生效一次
|
||||
if (!key_pressed) {
|
||||
//printf("key 1 pressed\r\n");
|
||||
key_pressed = true;
|
||||
key1_handler_callback();
|
||||
}
|
||||
}
|
||||
if (key_release_count > KEY_CHECK_SHAKING_COUNT){
|
||||
if (key_pressed) {
|
||||
key_pressed = false;
|
||||
//printf("key 1 release\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stm32g0xx_key_init(void)
|
||||
|
Reference in New Issue
Block a user