styles: optimize tanle

This commit is contained in:
esma(马璐)
2020-12-15 16:09:50 +08:00
parent 3b9dceb829
commit 7702f7f3cb
4 changed files with 149 additions and 149 deletions

View File

@@ -19,32 +19,32 @@
void iot_explorer_handle_power_switch(int power_switch) void iot_explorer_handle_power_switch(int power_switch)
{ {
if (0 == power_switch){ if (0 == power_switch){
if(LED_IS_OPEN){ if(LED_IS_OPEN){
printf("iot-explorer close the light\r\n"); printf("iot-explorer close the light\r\n");
LED_CLOSE; LED_CLOSE;
}else{ }else{
printf("the light already closed\r\n"); printf("the light already closed\r\n");
} }
}else{ }else{
if(LED_IS_OPEN){ if(LED_IS_OPEN){
printf("iot-explorer already opened\r\n"); printf("iot-explorer already opened\r\n");
}else{ }else{
printf("the light closed already\r\n"); printf("the light closed already\r\n");
LED_OPEN; LED_OPEN;
} }
} }
} }
void key1_handle_power_switch(void) void key1_handle_power_switch(void)
{ {
if(LED_IS_OPEN){ if(LED_IS_OPEN){
printf("key1 close the light\r\n"); printf("key1 close the light\r\n");
LED_CLOSE; LED_CLOSE;
}else{ }else{
printf("key1 open the light\r\n"); printf("key1 open the light\r\n");
LED_OPEN; LED_OPEN;
} }
} }
/*************************************************************** /***************************************************************
@@ -55,7 +55,7 @@ void key1_handle_power_switch(void)
***************************************************************/ ***************************************************************/
static void Init_BH1750(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); 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_Init(&TIM3_Handler); //时基初始化
//HAL_TIM_Base_Start_IT(&TIM3_Handler); //使能定时器3和定时器3更新中断TIM_IT_UPDATE //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); //TIM3_Init(1, 719);
@@ -138,9 +138,9 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{ {
if(htim->Instance==TIM3) if(htim->Instance==TIM3)
{ {
__HAL_RCC_TIM3_CLK_ENABLE(); //使能TIM3时钟 __HAL_RCC_TIM3_CLK_ENABLE(); //使能TIM3时钟
HAL_NVIC_SetPriority(TIM3_IRQn,1,3); //设置中断优先级抢占优先级1子优先级3 HAL_NVIC_SetPriority(TIM3_IRQn,1,3); //设置中断优先级抢占优先级1子优先级3
HAL_NVIC_EnableIRQ(TIM3_IRQn); //开启ITM3中断 HAL_NVIC_EnableIRQ(TIM3_IRQn); //开启ITM3中断
} }
} }
@@ -153,17 +153,17 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{ {
if(htim==(&TIM3_Handler)) if(htim==(&TIM3_Handler))
{ {
if(pwm_count < light_pwm) if(pwm_count < light_pwm)
{ {
set_light(true); set_light(true);
} else { } else {
set_light(false); set_light(false);
} }
if(pwm_count >= 100) if(pwm_count >= 100)
{ {
pwm_count = 0; pwm_count = 0;
} }
pwm_count++; pwm_count++;
} }
} }
#endif #endif

View File

@@ -30,15 +30,6 @@
#define BH1750_Addr 0x46 #define BH1750_Addr 0x46
#define LIGHT_MAX_NAME_LEN 32 #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); int e53_sc1_init(void);
float e53_scl_read_data(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 iot_explorer_handle_power_switch(int power_switch);
void key1_handle_power_switch(void); 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_ */ #endif /* _E53_SC1_H_ */

View File

@@ -11,12 +11,12 @@ extern void stm32g0xx_key_init(void);
#define DEVICE_NAME "techo_test01" #define DEVICE_NAME "techo_test01"
#define DEVICE_KEY "UOgIWiO8kcVkgif6FO3tJQ==" #define DEVICE_KEY "UOgIWiO8kcVkgif6FO3tJQ=="
#define YOUR_WIFI_SSID "iot-explorer" #define YOUR_WIFI_SSID "iot-explorer"
#define YOUR_WIFI_PWD "iot-explorer" #define YOUR_WIFI_PWD "iot-explorer"
// 数据模板支持数据增量上报 // 数据模板支持数据增量上报
#define REPORT_LX_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"lx\\\":%.0f}}" #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_POWER_SWITCH_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"power_switch\\\":%d}}"
#define MAIL_SIZE 256 #define MAIL_SIZE 256
#define MAIL_MAX 10 #define MAIL_MAX 10
@@ -33,7 +33,7 @@ void key1_handler_callback(void)
static void default_message_handler(mqtt_message_t* msg) static void default_message_handler(mqtt_message_t* msg)
{ {
int payload_len = 0; int payload_len = 0;
printf("callback:\r\n"); printf("callback:\r\n");
printf("---------------------------------------------------------\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("\tpayload:%s\r\n", msg->payload);
printf("---------------------------------------------------------\r\n"); printf("---------------------------------------------------------\r\n");
payload_len = strlen(msg->payload); payload_len = strlen(msg->payload);
if (payload_len > MAIL_SIZE){ if (payload_len > MAIL_SIZE){
printf("too long payload\r\n"); printf("too long payload\r\n");
return; 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}; 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; device_info_t dev_info;
memset(&dev_info, 0, sizeof(device_info_t)); memset(&dev_info, 0, sizeof(device_info_t));
float e53_value = 0; float e53_value = 0;
char e53_str[16] = {0}; char e53_str[16] = {0};
OLED_ShowString(0, 2, (uint8_t*)"connecting...", 16); OLED_ShowString(0, 2, (uint8_t*)"connecting...", 16);
@@ -120,28 +120,28 @@ static void mqtt_demo_task(void)
} }
while (1) { while (1) {
e53_value = e53_scl_read_data(); e53_value = e53_scl_read_data();
printf("e53 value %.0f lx\r\n", e53_value); printf("e53 value %.0f lx\r\n", e53_value);
memset(e53_str, 0, sizeof(e53_str)); memset(e53_str, 0, sizeof(e53_str));
sprintf(e53_str, "%.0f lx(lm/m2)", e53_value); sprintf(e53_str, "%.0f lx(lm/m2)", e53_value);
OLED_ShowString(0, 2, (uint8_t*)e53_str, 16); OLED_ShowString(0, 2, (uint8_t*)e53_str, 16);
if (key_pressed_flag){ if (key_pressed_flag){
key_pressed_flag = false; key_pressed_flag = false;
memset(payload, 0, sizeof(payload)); memset(payload, 0, sizeof(payload));
snprintf(payload, sizeof(payload), REPORT_POWER_SWITCH_DATA_TEMPLATE, LED_IS_OPEN); snprintf(payload, sizeof(payload), REPORT_POWER_SWITCH_DATA_TEMPLATE, LED_IS_OPEN);
if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) { if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) {
printf("module mqtt pub fail\n"); printf("module mqtt pub fail\n");
} else { } else {
printf("module mqtt pub success\n"); printf("module mqtt pub success\n");
} }
} }
// 请在此处添加光照强度数据上报物联网开发平台的代码 // 请在此处添加光照强度数据上报物联网开发平台的代码
tos_sleep_ms(5000); tos_sleep_ms(5000);
} }
} }
@@ -155,44 +155,44 @@ static void iot_explorer_data_handle(void *arg)
cJSON *method; cJSON *method;
cJSON *p; cJSON *p;
while(1){ while(1){
memset(buf, 0, MAIL_SIZE); memset(buf, 0, MAIL_SIZE);
if (K_ERR_NONE == tos_mail_q_pend(&mail_q, buf, &mail_size, TOS_TIME_FOREVER)){ if (K_ERR_NONE == tos_mail_q_pend(&mail_q, buf, &mail_size, TOS_TIME_FOREVER)){
//printf("mail content: %s\r\n", buf); //printf("mail content: %s\r\n", buf);
//字符串首尾的引号丢弃后才是标准的json数据格式 //字符串首尾的引号丢弃后才是标准的json数据格式
buf[strlen(buf) - 1] = '\0'; buf[strlen(buf) - 1] = '\0';
root = cJSON_Parse(buf + 1); root = cJSON_Parse(buf + 1);
if (NULL == root){ if (NULL == root){
printf("Invalid json root\r\n"); printf("Invalid json root\r\n");
continue; continue;
} }
method = cJSON_GetObjectItem(root, "method"); method = cJSON_GetObjectItem(root, "method");
if (NULL == method){ if (NULL == method){
printf("Invalid json method\r\n"); printf("Invalid json method\r\n");
cJSON_Delete(root); cJSON_Delete(root);
continue; continue;
} }
// 仅处理云端下发的 control 数据 // 仅处理云端下发的 control 数据
if (0 != strncmp(method->valuestring, "control", sizeof("control") - 1)){ if (0 != strncmp(method->valuestring, "control", sizeof("control") - 1)){
//printf("Invalid method\r\n"); //printf("Invalid method\r\n");
cJSON_Delete(root); cJSON_Delete(root);
continue; continue;
} }
// 若需要回复报文,需要携带数据报文的 clitenToken 字段 // 若需要回复报文,需要携带数据报文的 clitenToken 字段
//token = cJSON_GetObjectItem(root, "clientToken"); //token = cJSON_GetObjectItem(root, "clientToken");
params = cJSON_GetObjectItem(root, "params"); params = cJSON_GetObjectItem(root, "params");
if (NULL == params){ if (NULL == params){
printf("Invalid json params\r\n"); printf("Invalid json params\r\n");
cJSON_Delete(root); cJSON_Delete(root);
continue; continue;
} }
// 请在此处添加物联网开发平台对智能灯的控制代码 // 请在此处添加物联网开发平台对智能灯的控制代码
cJSON_Delete(root); cJSON_Delete(root);
} }
} }
} }
#define TASK_HANDLE_MAIL_PRIORITY 3 #define TASK_HANDLE_MAIL_PRIORITY 3
@@ -204,26 +204,26 @@ void application_entry(void *arg)
{ {
char *str = "TencentOS-tiny"; char *str = "TencentOS-tiny";
// 初始化tencent-os tiny定时器模块 // 初始化tencent-os tiny定时器模块
timer_init(); timer_init();
// 初始化按键检测模块 // 初始化按键检测模块
stm32g0xx_key_init(); stm32g0xx_key_init();
// OLED 显示屏初始化 // OLED 显示屏初始化
OLED_Init(); OLED_Init();
OLED_Clear(); OLED_Clear();
OLED_ShowString(0, 0, (uint8_t*)str, 16); OLED_ShowString(0, 0, (uint8_t*)str, 16);
// 创建tencent-os tiny邮箱使用邮箱机制进行任务间通信 // 创建tencent-os tiny邮箱使用邮箱机制进行任务间通信
tos_mail_q_create(&mail_q, mail_pool, MAIL_MAX, MAIL_SIZE); tos_mail_q_create(&mail_q, mail_pool, MAIL_MAX, MAIL_SIZE);
// 创建tencent-os tiny任务任务遍历邮箱处理iot-explorer下发的数据 // 创建tencent-os tiny任务任务遍历邮箱处理iot-explorer下发的数据
(void)tos_task_create(&task_handle_mail, "iot-explorer", iot_explorer_data_handle, (void)tos_task_create(&task_handle_mail, "iot-explorer", iot_explorer_data_handle,
(void *)NULL, TASK_HANDLE_MAIL_PRIORITY, (void *)NULL, TASK_HANDLE_MAIL_PRIORITY,
stack_task_handle_mail, TASK_HANDLE_MAIL_STACK_SIZE, 0); stack_task_handle_mail, TASK_HANDLE_MAIL_STACK_SIZE, 0);
mqtt_demo_task(); mqtt_demo_task();
while (1) { while (1) {
printf("This is a demo\r\n"); printf("This is a demo\r\n");
tos_task_delay(10000); tos_task_delay(10000);
} }
} }

View File

@@ -30,28 +30,28 @@ static bool key_pressed = false;
static void key_check_timer_cb(void *arg) static void key_check_timer_cb(void *arg)
{ {
if (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_0)){ if (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_0)){
key_press_count++; key_press_count++;
key_release_count = 0; key_release_count = 0;
} else { } else {
key_press_count = 0; key_press_count = 0;
key_release_count++; key_release_count++;
} }
// 支持按键消抖 // 支持按键消抖
if (key_press_count > KEY_CHECK_SHAKING_COUNT) { if (key_press_count > KEY_CHECK_SHAKING_COUNT) {
// 按键按下时功能即生效,且只生效一次 // 按键按下时功能即生效,且只生效一次
if (!key_pressed) { if (!key_pressed) {
//printf("key 1 pressed\r\n"); //printf("key 1 pressed\r\n");
key_pressed = true; key_pressed = true;
key1_handler_callback(); key1_handler_callback();
} }
} }
if (key_release_count > KEY_CHECK_SHAKING_COUNT){ if (key_release_count > KEY_CHECK_SHAKING_COUNT){
if (key_pressed) { if (key_pressed) {
key_pressed = false; key_pressed = false;
//printf("key 1 release\r\n"); //printf("key 1 release\r\n");
} }
} }
} }
void stm32g0xx_key_init(void) void stm32g0xx_key_init(void)