fix:优化rtc及sntp逻辑

fix:优化部分函数名
add:增加relay直接控制函数(未测试)
This commit is contained in:
Zip
2019-01-29 16:33:04 +08:00
parent fa570549a9
commit 882b4bb0a8
10 changed files with 105 additions and 75 deletions

View File

@@ -27,7 +27,7 @@ NAME := App_TC1
$(NAME)_SOURCES := main.c\ $(NAME)_SOURCES := main.c\
cJSON/cJSON.c\ cJSON/cJSON.c\
user_wifi.c\ user_wifi.c\
user_key.c\ user_gpio.c\
user_sntp.c\ user_sntp.c\
user_rtc.c\ user_rtc.c\
user_mqtt_client.c\ user_mqtt_client.c\

View File

@@ -1,6 +1,6 @@
#include "main.h" #include "main.h"
#include "user_key.h" #include "user_gpio.h"
#include "user_wifi.h" #include "user_wifi.h"
#include "user_rtc.h" #include "user_rtc.h"
#include "user_mqtt_client.h" #include "user_mqtt_client.h"
@@ -68,7 +68,7 @@ int application_start( void )
} }
MicoGpioInitialize( (mico_gpio_t) MICO_GPIO_5, OUTPUT_PUSH_PULL ); MicoGpioInitialize( (mico_gpio_t) MICO_GPIO_5, OUTPUT_PUSH_PULL );
led( 0 ); user_led_set( 0 );
if ( user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23 ) if ( user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23 )
{ {

View File

@@ -15,6 +15,9 @@
#define Led MICO_GPIO_5 #define Led MICO_GPIO_5
#define Button MICO_GPIO_23 #define Button MICO_GPIO_23
#define Relay_ON 1
#define Relay_OFF 0
#define Relay_0 MICO_GPIO_6 #define Relay_0 MICO_GPIO_6
#define Relay_1 MICO_GPIO_7 #define Relay_1 MICO_GPIO_7
#define Relay_2 MICO_GPIO_8 #define Relay_2 MICO_GPIO_8
@@ -30,6 +33,7 @@ typedef struct {
char hour; //Сʱ char hour; //Сʱ
char minute; //<2F><><EFBFBD><EFBFBD> char minute; //<2F><><EFBFBD><EFBFBD>
uint8_t repeat; //bit7:һ<><D2BB> bit6-0:<3A><><EFBFBD><EFBFBD>-<2D><>һ uint8_t repeat; //bit7:һ<><D2BB> bit6-0:<3A><><EFBFBD><EFBFBD>-<2D><>һ
char action; //<2F><><EFBFBD><EFBFBD>
char on; //<2F><><EFBFBD><EFBFBD> char on; //<2F><><EFBFBD><EFBFBD>
} user_plug_task_config_t; } user_plug_task_config_t;

View File

@@ -1,7 +1,8 @@
#define os_log(format, ...) custom_log("FUNCTION", format, ##__VA_ARGS__) #define os_log(format, ...) custom_log("FUNCTION", format, ##__VA_ARGS__)
#include "main.h" #include "main.h"
#include "user_key.h" #include "user_gpio.h"
#include "cJSON/cJSON.h" #include "cJSON/cJSON.h"
typedef struct _user_json_context_t typedef struct _user_json_context_t
@@ -32,7 +33,7 @@ void user_function_cmd_received( uint8_t *pusrdata )
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" ); cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
if ( p_nvalue ) if ( p_nvalue )
{ {
led( p_nvalue->valueint ); user_led_set( p_nvalue->valueint );
user_config->idx++; user_config->idx++;
sys_config->micoSystemConfig.name[0]++; sys_config->micoSystemConfig.name[0]++;

68
TC1/user_gpio.c Normal file
View File

@@ -0,0 +1,68 @@
#define os_log(format, ...) custom_log("KEY", format, ##__VA_ARGS__)
#include "main.h"
#include "user_gpio.h"
#include "user_mqtt_client.h"
mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 };
void user_led_set( char x )
{
if ( x == -1 )
MicoGpioOutputTrigger( Led );
else if ( x )
MicoGpioOutputHigh( Led );
else
MicoGpioOutputLow( Led );
}
void user_relay_set( char x, char y )
{
char onoff = (y == 1 ? Relay_ON : Relay_OFF);
OSStatus (* Gpiosetfunction)( mico_gpio_t );
if ( y == -1 )
Gpiosetfunction = MicoGpioOutputTrigger;
else if ( onoff )
Gpiosetfunction = MicoGpioOutputHigh;
else
Gpiosetfunction = MicoGpioOutputLow;
if ( x >= 0 && x < Relay_NUM )
{
(*Gpiosetfunction)( relay[x] );
} else if ( x == Relay_NUM )
{
OSStatus *setgpiofunction = MicoGpioOutputTrigger;
for ( int i = 0; i < Relay_NUM; i++ )
{
(*Gpiosetfunction)( relay[i] );
}
}
}
static void key_long_press( void )
{
os_log("key_long_press");
user_led_set(1);
user_mqtt_send( "mqtt test" );
}
static void key_short_press( void )
{
//os_log("test");
user_led_set(-1);
}
void key_init( void )
{
button_init_t button_config = {
.gpio = Button,
.long_pressed_func = key_long_press,
.pressed_func = key_short_press,
.long_pressed_timeout = 800,
};
button_init( IOBUTTON_USER_1, button_config );
}

View File

@@ -6,7 +6,7 @@
#include "mico.h" #include "mico.h"
#include "MiCOKit_EXT.h" #include "MiCOKit_EXT.h"
extern void led(char x); extern void user_led_set(char x);
extern void key_init(void); extern void key_init(void);

View File

@@ -1,39 +0,0 @@
#define os_log(format, ...) custom_log("KEY", format, ##__VA_ARGS__)
#include "main.h"
//#include "user_key.h"
#include "user_mqtt_client.h"
void led(char x)
{
if(x==-1)MicoGpioOutputTrigger(Led);
else if(x) MicoGpioOutputHigh(Led);
else MicoGpioOutputLow(Led);
}
static void key_long_press(void)
{
os_log("key_long_press");
MicoGpioOutputHigh(MICO_GPIO_5);
user_mqtt_send("mqtt test");
}
static void key_short_press(void)
{
//os_log("test");
MicoGpioOutputTrigger(MICO_GPIO_5);
}
void key_init(void)
{
button_init_t button_config={
.gpio=Button,
.long_pressed_func=key_long_press,
.pressed_func=key_short_press,
.long_pressed_timeout=800,
};
button_init(IOBUTTON_USER_1,button_config);
}

View File

@@ -1,7 +1,7 @@
#define os_log(format, ...) custom_log("RTC", format, ##__VA_ARGS__) #define os_log(format, ...) custom_log("RTC", format, ##__VA_ARGS__)
#include "main.h" #include "main.h"
//#include "key.h" #include "user_gpio.h"
#include "sntp.h" #include "sntp.h"
#include "user_sntp.h" #include "user_sntp.h"
@@ -16,7 +16,7 @@ OSStatus user_sntp_get_time( )
char ** pptr = NULL; char ** pptr = NULL;
struct in_addr ipp; struct in_addr ipp;
mico_rtc_time_t rtc_time; // mico_rtc_time_t rtc_time;
hostent_content = gethostbyname( "pool.ntp.org" ); hostent_content = gethostbyname( "pool.ntp.org" );
pptr = hostent_content->h_addr_list; pptr = hostent_content->h_addr_list;
@@ -28,10 +28,6 @@ OSStatus user_sntp_get_time( )
ipp.s_addr = 0xd248912c; ipp.s_addr = 0xd248912c;
err = sntp_get_time( &ipp, &current_time ); err = sntp_get_time( &ipp, &current_time );
} }
if ( err != kNoErr )
{
os_log("sntp_get_time2 err = %d.", err);
}
if ( err == kNoErr ) if ( err == kNoErr )
{ {
@@ -51,6 +47,7 @@ OSStatus user_sntp_get_time( )
} }
else else
{ {
os_log("sntp_get_time2 err = %d.", err);
return err; return err;
} }
@@ -96,6 +93,8 @@ void rtc_thread( mico_thread_arg_t arg )
LinkStatusTypeDef LinkStatus; LinkStatusTypeDef LinkStatus;
mico_rtc_time_t rtc_time; mico_rtc_time_t rtc_time;
mico_utc_time_t utc_time;
while ( 1 ) while ( 1 )
{ //<2F>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifi<66>ſ<EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD> { //<2F>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifi<66>ſ<EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
micoWlanGetLinkStatus( &LinkStatus ); micoWlanGetLinkStatus( &LinkStatus );
@@ -114,25 +113,9 @@ void rtc_thread( mico_thread_arg_t arg )
while ( 1 ) while ( 1 )
{ {
if ( rtc_init == 0 || rtc_time.sec == 0 && rtc_time.min == 0 ) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУ׼һ<D7BC><D2BB>
{
micoWlanGetLinkStatus( &LinkStatus );
if ( LinkStatus.is_connected == 1 )
{
err = user_sntp_get_time( );
if ( err == kNoErr ) rtc_init = 1;
}
}
// MicoRtcGetTime( &rtc_time );
// os_log("time:20%02d/%02d/%02d %d %02d:%02d:%02d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec);
os_log("rtc_thread");
mico_utc_time_t utc_time;
mico_rtc_time_t rtc_time;
mico_time_get_utc_time( &utc_time ); mico_time_get_utc_time( &utc_time );
utc_time+= 28800; utc_time += 28800;
struct tm * currentTime = localtime( (const time_t *) &utc_time ); struct tm * currentTime = localtime( (const time_t *) &utc_time );
rtc_time.sec = currentTime->tm_sec; rtc_time.sec = currentTime->tm_sec;
rtc_time.min = currentTime->tm_min; rtc_time.min = currentTime->tm_min;
@@ -146,6 +129,19 @@ void rtc_thread( mico_thread_arg_t arg )
// MicoRtcSetTime( &rtc_time ); //MicoRtc<74><63><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʱ! // MicoRtcSetTime( &rtc_time ); //MicoRtc<74><63><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʱ!
os_log("time:20%02d/%02d/%02d %d %02d:%02d:%02d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec); os_log("time:20%02d/%02d/%02d %d %02d:%02d:%02d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec);
if ( rtc_init != 1 || (rtc_time.sec == 0 && rtc_time.min == 0) ) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУ׼һ<D7BC><D2BB>
{
micoWlanGetLinkStatus( &LinkStatus );
if ( LinkStatus.is_connected == 1 )
{
err = user_sntp_get_time( );
if ( err == kNoErr )
rtc_init = 1;
else
rtc_init = 2;
}
}
mico_rtos_thread_msleep( 900 ); mico_rtos_thread_msleep( 900 );
} }

View File

@@ -2,7 +2,7 @@
#define os_log(format, ...) custom_log("SNTP", format, ##__VA_ARGS__) #define os_log(format, ...) custom_log("SNTP", format, ##__VA_ARGS__)
#include "main.h" #include "main.h"
//#include "key.h" //#include "user_gpio.h"
#include "user_sntp.h" #include "user_sntp.h"
/* Callback function when MiCO UTC time in sync to NTP server */ /* Callback function when MiCO UTC time in sync to NTP server */

View File

@@ -2,8 +2,8 @@
#include "main.h" #include "main.h"
#include "mico_socket.h" #include "mico_socket.h"
#include "user_gpio.h"
#include "user_sntp.h" #include "user_sntp.h"
#include "user_key.h"
#define os_log(format, ...) custom_log("WIFI", format, ##__VA_ARGS__) #define os_log(format, ...) custom_log("WIFI", format, ##__VA_ARGS__)
@@ -32,7 +32,7 @@ void wifi_start_easylink( )
{ {
wifi_status = WIFI_STATE_EASYLINK; wifi_status = WIFI_STATE_EASYLINK;
micoWlanStartEasyLink( 20000 ); micoWlanStartEasyLink( 20000 );
led( 1 ); user_led_set( 1 );
} }
uint32_t ip=0xd248912c; uint32_t ip=0xd248912c;
//easylink <20><><EFBFBD>ɻص<C9BB> //easylink <20><><EFBFBD>ɻص<C9BB>
@@ -92,7 +92,7 @@ static void wifi_led_timer_callback( void* arg )
{ {
case WIFI_STATE_FAIL: case WIFI_STATE_FAIL:
os_log("wifi connect fail"); os_log("wifi connect fail");
led( 0 ); user_led_set( 0 );
mico_rtos_stop_timer( &wifi_led_timer ); mico_rtos_stop_timer( &wifi_led_timer );
break; break;
case WIFI_STATE_NOCONNECT: case WIFI_STATE_NOCONNECT:
@@ -103,17 +103,17 @@ static void wifi_led_timer_callback( void* arg )
//if ( num > 1 ) //if ( num > 1 )
{ {
num = 0; num = 0;
led( -1 ); user_led_set( -1 );
} }
break; break;
case WIFI_STATE_NOEASYLINK: case WIFI_STATE_NOEASYLINK:
wifi_start_easylink( ); wifi_start_easylink( );
break; break;
case WIFI_STATE_EASYLINK: case WIFI_STATE_EASYLINK:
led( 1 ); user_led_set( 1 );
break; break;
case WIFI_STATE_CONNECTED: case WIFI_STATE_CONNECTED:
led( 0 ); user_led_set( 0 );
mico_rtos_stop_timer( &wifi_led_timer ); mico_rtos_stop_timer( &wifi_led_timer );
break; break;
} }