From 792b5a6df15f4782d582f4e57c26500374001dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=80=99=E9=B9=8F=E9=A3=9E?= Date: Tue, 12 Oct 2021 10:50:48 +0000 Subject: [PATCH] =?UTF-8?q?!34=20=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81=20*?= =?UTF-8?q?=20update=20*=20add=20lic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/TW207_Module_gyro/include/gyro.h | 2 +- applications/app/TW207_Module_gyro/src/gyro.c | 2 - applications/app/TW208_Module_JYRTC/BUILD.gn | 15 +- .../app/TW208_Module_JYRTC/JYRTC_example.c | 102 ++--- .../app/TW208_Module_JYRTC/JYRTC_rtc.c | 368 +++++++++++------- .../app/TW208_Module_JYRTC/JYRTC_rtc.h | 15 + applications/app/TW208_Module_JYRTC/README.md | 14 +- applications/app/TW208_Module_JYRTC/ntpUdp.c | 224 ++++++----- applications/app/TW208_Module_JYRTC/ntpUdp.h | 39 +- applications/app/TW208_Module_JYRTC/ntpWifi.h | 15 - .../app/TW208_Module_JYRTC/ssd1306_font.h | 25 +- .../app/TW208_Module_JYRTC/ssd1306_oled.c | 15 + .../app/TW208_Module_JYRTC/ssd1306_oled.h | 19 +- .../{ntpWifi.c => wifi_connect.c} | 80 ++-- .../app/TW208_Module_JYRTC/wifi_connect.h | 24 ++ .../app/TW210_Module_battery/BUILD.gn | 2 +- .../TW210_Module_battery/battery_example.c | 2 +- .../TW303_Network_mqttclient/src/mqtt_main.c | 30 +- .../src/wifi_connect.c | 23 +- 19 files changed, 590 insertions(+), 426 deletions(-) delete mode 100755 applications/app/TW208_Module_JYRTC/ntpWifi.h rename applications/app/TW208_Module_JYRTC/{ntpWifi.c => wifi_connect.c} (78%) mode change 100755 => 100644 create mode 100644 applications/app/TW208_Module_JYRTC/wifi_connect.h diff --git a/applications/app/TW207_Module_gyro/include/gyro.h b/applications/app/TW207_Module_gyro/include/gyro.h index 1c26d2e0..ef26fa9d 100755 --- a/applications/app/TW207_Module_gyro/include/gyro.h +++ b/applications/app/TW207_Module_gyro/include/gyro.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Talkweb Co., Ltd. + * Copyright (c) 2021 Talkweb Co. Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/applications/app/TW207_Module_gyro/src/gyro.c b/applications/app/TW207_Module_gyro/src/gyro.c index c88972a4..0d198b9f 100755 --- a/applications/app/TW207_Module_gyro/src/gyro.c +++ b/applications/app/TW207_Module_gyro/src/gyro.c @@ -24,10 +24,8 @@ #include "iot_i2c.h" #include "hi_io.h" - #define I2C0_INDEX 0 - static void MPUIsrFunc(char* arg) { printf("usr key pressed %s\n",arg); diff --git a/applications/app/TW208_Module_JYRTC/BUILD.gn b/applications/app/TW208_Module_JYRTC/BUILD.gn index 3c0d1b24..ed411873 100755 --- a/applications/app/TW208_Module_JYRTC/BUILD.gn +++ b/applications/app/TW208_Module_JYRTC/BUILD.gn @@ -1,9 +1,22 @@ +# Copyright (c) 2021 Talkweb Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + static_library("module_JYRTC_example") { sources = [ "JYRTC_rtc.c", "ssd1306_oled.c", "JYRTC_example.c", - "ntpWifi.c", + "wifi_connect.c", "ntpUdp.c" ] diff --git a/applications/app/TW208_Module_JYRTC/JYRTC_example.c b/applications/app/TW208_Module_JYRTC/JYRTC_example.c index ab47704e..c00fd506 100755 --- a/applications/app/TW208_Module_JYRTC/JYRTC_example.c +++ b/applications/app/TW208_Module_JYRTC/JYRTC_example.c @@ -1,11 +1,27 @@ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include +#include #include "ohos_init.h" #include "cmsis_os2.h" #include "JYRTC_rtc.h" -#include "ntpWifi.h" +#include "wifi_connect.h" #include "ntpUdp.h" #include "iot_gpio.h" @@ -19,8 +35,6 @@ #define RET_OK 0 #define RET_FAIL 1 -volatile bool wifi_connected = false; - const char* NTP_SERVERS[10] = { "ntp.aliyun.com", "ntp1.aliyun.com", "ntp2.aliyun.com", @@ -32,72 +46,25 @@ const char* NTP_SERVERS[10] = { "ntp.aliyun.com", "cn.pool.ntp.org" }; -volatile osSemaphoreId_t sem_udp; struct tm time_value = {0}; -static void wifiConnect(void* argc) +static void WifiConnectTask() { - while(1) { - osSemaphoreAcquire(sem_udp,osWaitForever); - - if (!wifi_connected) { - if (WifiConnect(SELECT_WIFI_SSID,SELECT_WIFI_PASSWORD,argc) == 0 ) { - printf("wifi connected\n"); - wifi_connected = true; - } - } - - - SetThreadRunStatus(true); - - for (int i = 0; i < 10; i++) { - if (GetTimeTm(NTP_SERVERS[i], NTP_SERVER_PORT, &time_value)) { - DS1307_SetInit(&time_value); - rct_set_init(); - break; - } - } - SetThreadRunStatus(false); - } - -} - - -void Gpio5IsrFunc(char* arg) -{ - //if (osThreadNew((osThreadFunc_t)wifiConnect, (void*)sem_udp, &attr) == NULL) { - // printf("[ntp_wifi_Demo] Falied to create wifi ntp task!\n"); - //} - //void(arg); - if (!GetThreadRunStatus()) { - osSemaphoreRelease(sem_udp); - } else { - printf("thread was running\n"); - } - - return; -} - -void InitGpio5Isr() -{ - unsigned int ret; - - IoTGpioInit(IOT_GPIO_IO_GPIO_5); - IotIoSetFunc(IOT_GPIO_IO_GPIO_5,HI_IO_FUNC_GPIO_5_GPIO); - IotIoSetPull(IOT_GPIO_IO_GPIO_5,IOT_IO_PULL_UP); - - ret = IoTGpioSetDir(IOT_GPIO_IO_GPIO_5, IOT_GPIO_DIR_IN); - if (ret != RET_OK) { - printf("===== ERROR ======gpio -> hi_gpio_set_dir1 ret:%d\r\n", ret); - return; - } - ret = IoTGpioRegisterIsrFunc(IOT_GPIO_IO_GPIO_5, IOT_INT_TYPE_EDGE, - IOT_GPIO_EDGE_RISE_LEVEL_HIGH, Gpio5IsrFunc, NULL); - if (ret != RET_OK) { - printf("===== ERROR ======gpio -> hi_gpio_register_isr_function ret:%d\r\n", ret); + if (WifiConnect(SELECT_WIFI_SSID, SELECT_WIFI_PASSWORD) == 0 ) + { + printf("wifi connected\n"); + } + + for (int i = 0; i < 10; i++) + { + if (GetTimeTm(NTP_SERVERS[i], NTP_SERVER_PORT, &time_value)) + { + DS1307_SetInit(&time_value); + rct_set_init(); + break; + } } - return; } static void DS1307_Task(void* argc) @@ -107,8 +74,7 @@ static void DS1307_Task(void* argc) } static void DS1307ExampleEntry(void) -{ - sem_udp = osSemaphoreNew(32, 0, NULL); +{ osThreadAttr_t attr; InitGpio5Isr(); attr.name = "1307_Task"; @@ -118,10 +84,10 @@ static void DS1307ExampleEntry(void) attr.stack_mem = NULL; attr.stack_size = TASK_STACK_SIZE; attr.priority = TASK_PRIO; - if (osThreadNew((osThreadFunc_t)wifiConnect, (void*)sem_udp, &attr) == NULL) { + if (osThreadNew((osThreadFunc_t)WifiConnectTask, (void*)sem_udp, &attr) == NULL) + { printf("[ntp_wifi_Demo] Falied to create wifi ntp task!\n"); } - //osSemaphoreAcquire(sem_udp,osWaitForever); if (osThreadNew((osThreadFunc_t)DS1307_Task, NULL, &attr) == NULL) { diff --git a/applications/app/TW208_Module_JYRTC/JYRTC_rtc.c b/applications/app/TW208_Module_JYRTC/JYRTC_rtc.c index ff87e97f..87f867a1 100755 --- a/applications/app/TW208_Module_JYRTC/JYRTC_rtc.c +++ b/applications/app/TW208_Module_JYRTC/JYRTC_rtc.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include "JYRTC_rtc.h" @@ -8,6 +23,8 @@ #include "ssd1306_oled.h" #include "iot_gpio.h" #define have_oled 1 + + hi_u32 g_rtc_demo_task_id =0; ds1307_rtc_type rct_time_set ={0}; /*写寄存器*/ @@ -115,47 +132,55 @@ hi_void rct_set_init(hi_void) //rct_time_set.rtc_year[0] = 21; //set second ret = ds1307_i2c_write(RCT_SECOND, rct_time_set.rtc_second[0], NULL, SEND_SET_REG_LEN); - if (ret != HI_ERR_SUCCESS) { + if (ret != HI_ERR_SUCCESS) + { printf("Failed to second cmd\r\n"); } hi_udelay(DELAY_TIME); //set minute ret = ds1307_i2c_write(RCT_MINUTE, rct_time_set.rtc_minue[0], NULL, SEND_SET_REG_LEN); - if (ret != HI_ERR_SUCCESS) { + if (ret != HI_ERR_SUCCESS) + { printf("Failed to minute cmd\r\n"); } hi_udelay(DELAY_TIME); //set hour ret = ds1307_i2c_write(RCT_HOUR, rct_time_set.rtc_hour[0], NULL, SEND_SET_REG_LEN); - if (ret != HI_ERR_SUCCESS) { + if (ret != HI_ERR_SUCCESS) + { printf("Failed to hour cmd\r\n"); } hi_udelay(DELAY_TIME); //set day ret = ds1307_i2c_write(RCT_DAY, rct_time_set.rtc_day[0], NULL, SEND_SET_REG_LEN); - if (ret != HI_ERR_SUCCESS) { + if (ret != HI_ERR_SUCCESS) + { printf("Failed to day cmd\r\n"); } hi_udelay(DELAY_TIME); //set date ret = ds1307_i2c_write(RCT_DATE, rct_time_set.rtc_date[0], NULL, SEND_SET_REG_LEN); - if (ret != HI_ERR_SUCCESS) { + if (ret != HI_ERR_SUCCESS) + { printf("Failed to date cmd\r\n"); } hi_udelay(DELAY_TIME); //set month ret = ds1307_i2c_write(RCT_MONTH, rct_time_set.rtc_month[0], NULL, SEND_SET_REG_LEN); - if (ret != HI_ERR_SUCCESS) { + if (ret != HI_ERR_SUCCESS) + { printf("Failed to month cmd\r\n"); } hi_udelay(DELAY_TIME); //set year ret = ds1307_i2c_write(RCT_YEAR, rct_time_set.rtc_year[0], NULL, SEND_SET_REG_LEN); - if (ret != HI_ERR_SUCCESS) { + if (ret != HI_ERR_SUCCESS) + { printf("Failed to year cmd\r\n"); } hi_udelay(DELAY_TIME); } + /* change type*/ hi_u8 *int_to_char(hi_s32 dec, hi_u8 *str) { @@ -164,18 +189,21 @@ hi_u8 *int_to_char(hi_s32 dec, hi_u8 *str) hi_s32 k = 0; hi_s32 i = 0; - if (i == dec) { + if (i == dec) + { str[0] = '0'; return str; } i = (hi_s32)dec;//浮点数的整数部分 - while (i > 0) { + while (i > 0) + { str1[j++] = i % 10 + '0'; i = i / 10; } - for (k = 0;k < j;k++) { + for (k = 0;k < j;k++) + { str[k] = str1[j-1-k];//被提取的整数部分正序存放到另一个数组 } @@ -219,28 +247,33 @@ hi_void *rtc_timer(hi_void *param) //OLED_Refresh(); //rct_set_init(); //设置RTC时间,已经设置好了,如有需要再打开 //oled_fill_screen(OLED_CLEAN_SCREEN);//clean screen -if(have_oled){ - oled_init(); - OLED_ColorTurn(0);//0正常显示,1 反色显示 - OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示 - OLED_Refresh(); + if(have_oled){ + oled_init(); + OLED_ColorTurn(0);//0正常显示,1 反色显示 + OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示 + OLED_Refresh(); - while (1) { + while (1) + { /*----------------------second--------------*/ ds1307_i2c_write(0x00, NULL, NULL, SEND_READ_DATA_LEN); ds1307_read(RCT_SECOND, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_second[0] != rct_read_data[0]) { + if (rtc_data.rtc_second[0] != rct_read_data[0]) + { rtc_data.rtc_second[0] = rct_read_data[0]; //temp_second = rct_read_data[0]/16*10 + rct_read_data[0]%16; temp_second = ((rct_read_data[0]>>4)&0x0f)*10 + (rct_read_data[0]&0x0f); memset(ch_second, 0, 3); int_to_char(temp_second, ch_second); - if (temp_second>=10) { + if (temp_second>=10) + { //OLED_ShowString(36, 20, ch_second, 12);//oled_show_str(48, 6, ch_second, 1); printf("have_oled rtc_timer ch_second=%s\n",ch_second); - } else { + } + else + { //OLED_ShowString(36, 20, "0", 12); //OLED_ShowString(42, 20, ch_second, 12); printf("have_oled rtc_timer ch_second=%s\n",ch_second); @@ -250,16 +283,20 @@ if(have_oled){ /*----------------------minute--------------*/ ds1307_i2c_write(0x01, NULL, NULL, SEND_READ_DATA_LEN); ds1307_read(RCT_MINUTE, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_minue[0] != rct_read_data[0]) { + if (rtc_data.rtc_minue[0] != rct_read_data[0]) + { rtc_data.rtc_minue[0] = rct_read_data[0]; temp_minute = rct_read_data[0]/16*10 + rct_read_data[0]%16; memset(ch_minute, 0, 3); int_to_char(temp_minute, ch_minute); - if (temp_minute >= 10) { + if (temp_minute >= 10) + { //OLED_ShowString(18, 20, ch_minute, 12); printf("have_oled rtc_timer ch_minute=%s\n",ch_minute); //OLED_ShowString(30, 20, ":", 12); - } else { + } + else + { //OLED_ShowString(18, 20, "0", 12); //OLED_ShowString(24, 20, ch_minute, 12); printf("have_oled rtc_timer ch_minute=%s\n",ch_minute); @@ -272,16 +309,20 @@ if(have_oled){ ds1307_i2c_write(0x02, NULL, NULL, SEND_READ_DATA_LEN); ds1307_read(RCT_HOUR, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_hour[0] != rct_read_data[0]) { + if (rtc_data.rtc_hour[0] != rct_read_data[0]) + { rtc_data.rtc_hour[0] = rct_read_data[0]; temp_hour = rct_read_data[0]/16*10 + rct_read_data[0]%16; memset(ch_hour, 0, 3); int_to_char(temp_hour, ch_hour); - if (temp_hour >= 10) { + if (temp_hour >= 10) + { //OLED_ShowString(0, 20, ch_hour, 12); printf("have_oled rtc_timer ch_hour=%s\n",ch_hour); //OLED_ShowString(12, 20, ":", 12); - } else { + } + else + { //OLED_ShowString(0, 20, "0", 12); //OLED_ShowString(6, 20, ch_hour, 12); printf("have_oled rtc_timer ch_hour=%s\n",ch_hour); @@ -294,33 +335,41 @@ if(have_oled){ ds1307_i2c_write(0x03, NULL, NULL, SEND_READ_DATA_LEN); ds1307_read(RCT_DAY, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_day[0] != rct_read_data[0]) { + if (rtc_data.rtc_day[0] != rct_read_data[0]) + { rtc_data.rtc_day[0] = rct_read_data[0]; temp_day = rct_read_data[0]/16*10 + rct_read_data[0]%16; - if(temp_day>7){ - temp_day=temp_day-5; - }else if(temp_day<=0){ - temp_day=1; + if(temp_day>7) + { + temp_day = temp_day-5; + } + else if(temp_day<=0) + { + temp_day = 1; } memset(ch_day, 0, 3); int_to_char(temp_day, ch_day); //OLED_ShowString(0, 34, "week:", 12); //OLED_ShowString(36, 34, ch_day, 12); - printf("have_oled rtc_timer ch_day=%s\n",ch_day); + printf("have_oled rtc_timer ch_day=%s\n", ch_day); } /*----------------------date--------------*/ ds1307_i2c_write(0x04, NULL, NULL, SEND_READ_DATA_LEN); ds1307_read(RCT_DATE, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_date[0] != rct_read_data[0]) { + if (rtc_data.rtc_date[0] != rct_read_data[0]) + { rtc_data.rtc_date[0] = rct_read_data[0]; temp_date = rct_read_data[0]/16*10 + rct_read_data[0]%16; memset(ch_date, 0, 3); int_to_char(temp_date, ch_date); - if (temp_date >= 10) { + if (temp_date >= 10) + { //OLED_ShowString(48, 6, ch_date, 12); printf("have_oled rtc_timer ch_date=%s\n",ch_date); - } else { + } + else + { //OLED_ShowString(48, 6, "0", 12); //OLED_ShowString(54, 6, ch_date, 12); printf("have_oled rtc_timer ch_date=%s\n",ch_date); @@ -332,16 +381,20 @@ if(have_oled){ ds1307_i2c_write(0x05, NULL, NULL, SEND_READ_DATA_LEN); ds1307_read(RCT_MONTH, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_month[0] != rct_read_data[0]) { + if (rtc_data.rtc_month[0] != rct_read_data[0]) + { rtc_data.rtc_month[0] = rct_read_data[0]; temp_month = rct_read_data[0]/16*10 + rct_read_data[0]%16; memset(ch_month, 0, 3); int_to_char(temp_month, ch_month); - if (temp_month >= 10) { + if (temp_month >= 10) + { //OLED_ShowString(30, 6, ch_month, 12); printf("have_oled rtc_timer ch_month=%s\n",ch_month); //OLED_ShowString(30, 6, "-", 12); - } else { + } + else + { //OLED_ShowString(30, 6, "0", 12); //OLED_ShowString(36, 6, ch_month, 12); printf("have_oled rtc_timer ch_month=%s\n",ch_month); @@ -353,7 +406,8 @@ if(have_oled){ ds1307_i2c_write(0x06, NULL, NULL, SEND_READ_DATA_LEN); ds1307_read(RCT_YEAR, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_year[0] != rct_read_data[0]) { + if (rtc_data.rtc_year[0] != rct_read_data[0]) + { rtc_data.rtc_year[0] = rct_read_data[0]; temp_year = rct_read_data[0]/16*10 + rct_read_data[0]%16; temp_year=temp_year+2000; @@ -365,10 +419,13 @@ if(have_oled){ //OLED_ShowString(32, 6, "-", 12); } - if (sprintf_s(yearmd,48,"%04d:%02d:%02d",temp_year, temp_month, temp_date) == -1) { + if (sprintf_s(yearmd,48,"%04d:%02d:%02d",temp_year, temp_month, temp_date) == -1) + { printf("sprintf time error\n"); } - if (sprintf_s(hourms,48,"%02d:%02d:%02d",temp_hour, temp_minute, temp_second) == -1) { + + if (sprintf_s(hourms,48,"%02d:%02d:%02d",temp_hour, temp_minute, temp_second) == -1) + { printf("sprintf time error\n"); } printf("have_oled rtc_timer yearmd=%s\n",yearmd); @@ -378,118 +435,147 @@ if(have_oled){ OLED_Refresh(); hi_sleep(1); } -}else{ - while (1) { + } + else + { + while (1) + { - /*----------------------second--------------*/ - ds1307_i2c_write(0x00, NULL, NULL, SEND_READ_DATA_LEN); - ds1307_read(RCT_SECOND, SEND_READ_DATA_LEN, rct_read_data); + /*----------------------second--------------*/ + ds1307_i2c_write(0x00, NULL, NULL, SEND_READ_DATA_LEN); + ds1307_read(RCT_SECOND, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_second[0] != rct_read_data[0]) { - rtc_data.rtc_second[0] = rct_read_data[0]; - //temp_second = rct_read_data[0]/16*10 + rct_read_data[0]%16; - temp_second = ((rct_read_data[0]>>4)&0x0f)*10 + (rct_read_data[0]&0x0f); - memset(ch_second, 0, 3); - int_to_char(temp_second, ch_second); - if (temp_second>=10) { - printf("have_oled rtc_timer ch_second=%s\n",ch_second); - } else { - printf("have_oled rtc_timer ch_second=%s\n",ch_second); + if (rtc_data.rtc_second[0] != rct_read_data[0]) + { + rtc_data.rtc_second[0] = rct_read_data[0]; + //temp_second = rct_read_data[0]/16*10 + rct_read_data[0]%16; + temp_second = ((rct_read_data[0]>>4)&0x0f)*10 + (rct_read_data[0]&0x0f); + memset(ch_second, 0, 3); + int_to_char(temp_second, ch_second); + if (temp_second>=10) + { + printf("have_oled rtc_timer ch_second=%s\n", ch_second); + } + else + { + printf("have_oled rtc_timer ch_second=%s\n", ch_second); + } + } + /*----------------------minute--------------*/ + ds1307_i2c_write(0x01, NULL, NULL, SEND_READ_DATA_LEN); + ds1307_read(RCT_MINUTE, SEND_READ_DATA_LEN, rct_read_data); + if (rtc_data.rtc_minue[0] != rct_read_data[0]) + { + rtc_data.rtc_minue[0] = rct_read_data[0]; + temp_minute = rct_read_data[0]/16*10 + rct_read_data[0]%16; + memset(ch_minute, 0, 3); + int_to_char(temp_minute, ch_minute); + if (temp_minute >= 10) + { + printf("have_oled rtc_timer ch_minute=%s\n",ch_minute); + } + else + { + printf("have_oled rtc_timer ch_minute=%s\n",ch_minute); + } } - } - /*----------------------minute--------------*/ - ds1307_i2c_write(0x01, NULL, NULL, SEND_READ_DATA_LEN); - ds1307_read(RCT_MINUTE, SEND_READ_DATA_LEN, rct_read_data); - if (rtc_data.rtc_minue[0] != rct_read_data[0]) { - rtc_data.rtc_minue[0] = rct_read_data[0]; - temp_minute = rct_read_data[0]/16*10 + rct_read_data[0]%16; - memset(ch_minute, 0, 3); - int_to_char(temp_minute, ch_minute); - if (temp_minute >= 10) { - printf("have_oled rtc_timer ch_minute=%s\n",ch_minute); - } else { - printf("have_oled rtc_timer ch_minute=%s\n",ch_minute); - } - } - /*----------------------hour--------------*/ - ds1307_i2c_write(0x02, NULL, NULL, SEND_READ_DATA_LEN); - ds1307_read(RCT_HOUR, SEND_READ_DATA_LEN, rct_read_data); - - if (rtc_data.rtc_hour[0] != rct_read_data[0]) { - rtc_data.rtc_hour[0] = rct_read_data[0]; - temp_hour = rct_read_data[0]/16*10 + rct_read_data[0]%16; - memset(ch_hour, 0, 3); - int_to_char(temp_hour, ch_hour); - if (temp_hour >= 10) { - printf("have_oled rtc_timer ch_hour=%s\n",ch_hour); - } else { - printf("have_oled rtc_timer ch_hour=%s\n",ch_hour); + /*----------------------hour--------------*/ + ds1307_i2c_write(0x02, NULL, NULL, SEND_READ_DATA_LEN); + ds1307_read(RCT_HOUR, SEND_READ_DATA_LEN, rct_read_data); + + if (rtc_data.rtc_hour[0] != rct_read_data[0]) + { + rtc_data.rtc_hour[0] = rct_read_data[0]; + temp_hour = rct_read_data[0]/16*10 + rct_read_data[0]%16; + memset(ch_hour, 0, 3); + int_to_char(temp_hour, ch_hour); + if (temp_hour >= 10) + { + printf("have_oled rtc_timer ch_hour=%s\n",ch_hour); + } + else + { + printf("have_oled rtc_timer ch_hour=%s\n",ch_hour); + } } - } - /*----------------------day-------------*/ - ds1307_i2c_write(0x03, NULL, NULL, SEND_READ_DATA_LEN); - ds1307_read(RCT_DAY, SEND_READ_DATA_LEN, rct_read_data); - - if (rtc_data.rtc_day[0] != rct_read_data[0]) { - rtc_data.rtc_day[0] = rct_read_data[0]; - temp_day = rct_read_data[0]/16*10 + rct_read_data[0]%16; - if(temp_day>7){ - temp_day=temp_day-5; - }else if(temp_day<=0){ - temp_day=1; + /*----------------------day-------------*/ + ds1307_i2c_write(0x03, NULL, NULL, SEND_READ_DATA_LEN); + ds1307_read(RCT_DAY, SEND_READ_DATA_LEN, rct_read_data); + + if (rtc_data.rtc_day[0] != rct_read_data[0]) + { + rtc_data.rtc_day[0] = rct_read_data[0]; + temp_day = rct_read_data[0]/16*10 + rct_read_data[0]%16; + if(temp_day > 7) + { + temp_day=temp_day-5; + } + else if + (temp_day <= 0) + { + temp_day=1; + } + memset(ch_day, 0, 3); + int_to_char(temp_day, ch_day); + printf("have_oled rtc_timer ch_day=%s\n", ch_day); } - memset(ch_day, 0, 3); - int_to_char(temp_day, ch_day); - printf("have_oled rtc_timer ch_day=%s\n",ch_day); - } - /*----------------------date--------------*/ - ds1307_i2c_write(0x04, NULL, NULL, SEND_READ_DATA_LEN); - ds1307_read(RCT_DATE, SEND_READ_DATA_LEN, rct_read_data); - - if (rtc_data.rtc_date[0] != rct_read_data[0]) { - rtc_data.rtc_date[0] = rct_read_data[0]; - temp_date = rct_read_data[0]/16*10 + rct_read_data[0]%16; - memset(ch_date, 0, 3); - int_to_char(temp_date, ch_date); - if (temp_date >= 10) { - printf("have_oled rtc_timer ch_date=%s\n",ch_date); - } else { - printf("have_oled rtc_timer ch_date=%s\n",ch_date); + /*----------------------date--------------*/ + ds1307_i2c_write(0x04, NULL, NULL, SEND_READ_DATA_LEN); + ds1307_read(RCT_DATE, SEND_READ_DATA_LEN, rct_read_data); + + if (rtc_data.rtc_date[0] != rct_read_data[0]) + { + rtc_data.rtc_date[0] = rct_read_data[0]; + temp_date = rct_read_data[0]/16*10 + rct_read_data[0]%16; + memset(ch_date, 0, 3); + int_to_char(temp_date, ch_date); + if (temp_date >= 10) + { + printf("have_oled rtc_timer ch_date=%s\n",ch_date); + } + else + { + printf("have_oled rtc_timer ch_date=%s\n",ch_date); + } } - } - /*----------------------month--------------*/ - ds1307_i2c_write(0x05, NULL, NULL, SEND_READ_DATA_LEN); - ds1307_read(RCT_MONTH, SEND_READ_DATA_LEN, rct_read_data); - - if (rtc_data.rtc_month[0] != rct_read_data[0]) { - rtc_data.rtc_month[0] = rct_read_data[0]; - temp_month = rct_read_data[0]/16*10 + rct_read_data[0]%16; - memset(ch_month, 0, 3); - int_to_char(temp_month, ch_month); - if (temp_month >= 10) { - printf("have_oled rtc_timer ch_month=%s\n",ch_month); - } else { - printf("have_oled rtc_timer ch_month=%s\n",ch_month); + /*----------------------month--------------*/ + ds1307_i2c_write(0x05, NULL, NULL, SEND_READ_DATA_LEN); + ds1307_read(RCT_MONTH, SEND_READ_DATA_LEN, rct_read_data); + + if (rtc_data.rtc_month[0] != rct_read_data[0]) + { + rtc_data.rtc_month[0] = rct_read_data[0]; + temp_month = rct_read_data[0]/16*10 + rct_read_data[0]%16; + memset(ch_month, 0, 3); + int_to_char(temp_month, ch_month); + if (temp_month >= 10) + { + printf("have_oled rtc_timer ch_month=%s\n", ch_month); + } + else + { + printf("have_oled rtc_timer ch_month=%s\n", ch_month); + } } - } - /*----------------------year--------------*/ - ds1307_i2c_write(0x06, NULL, NULL, SEND_READ_DATA_LEN); - ds1307_read(RCT_YEAR, SEND_READ_DATA_LEN, rct_read_data); - - if (rtc_data.rtc_year[0] != rct_read_data[0]) { - rtc_data.rtc_year[0] = rct_read_data[0]; - temp_year = rct_read_data[0]/16*10 + rct_read_data[0]%16; - temp_year=temp_year+2000; - memset(ch_year, 0, 5); - int_to_char(temp_year, ch_year); - printf("have_oled rtc_timer ch_year=%s\n",ch_year); - } - hi_sleep(1); + /*----------------------year--------------*/ + ds1307_i2c_write(0x06, NULL, NULL, SEND_READ_DATA_LEN); + ds1307_read(RCT_YEAR, SEND_READ_DATA_LEN, rct_read_data); + + if (rtc_data.rtc_year[0] != rct_read_data[0]) + { + rtc_data.rtc_year[0] = rct_read_data[0]; + temp_year = rct_read_data[0]/16*10 + rct_read_data[0]%16; + temp_year = temp_year+2000; + memset(ch_year, 0, 5); + int_to_char(temp_year, ch_year); + printf("have_oled rtc_timer ch_year=%s\n", ch_year); + } + hi_sleep(1); + } } } -} diff --git a/applications/app/TW208_Module_JYRTC/JYRTC_rtc.h b/applications/app/TW208_Module_JYRTC/JYRTC_rtc.h index f6795493..d5e2b780 100755 --- a/applications/app/TW208_Module_JYRTC/JYRTC_rtc.h +++ b/applications/app/TW208_Module_JYRTC/JYRTC_rtc.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef __DS1307_RTC_H__ #define __DS1307_RTC_H__ diff --git a/applications/app/TW208_Module_JYRTC/README.md b/applications/app/TW208_Module_JYRTC/README.md index d049c6f5..d360c4f8 100755 --- a/applications/app/TW208_Module_JYRTC/README.md +++ b/applications/app/TW208_Module_JYRTC/README.md @@ -8,22 +8,22 @@ 2、没有接OLED副板情况(JYRTC_rtc.c中宏定义have_oled设置为0) 直接获取RTC时间并把时间用串口信息打印出来 -# 目录结构: -## include +## 目录结构: +### include JYRTC_rtc.h --JYRTC时钟控制头文件 ntpUdp.h --ntpUdp头文件 - ntpWifi.h --ntpWifi连接头文件 + wifi_connect.h --Wifi连接头文件 ssd1306_font.h --oled头文件 ssd1306_oled.h --oled头文件 -## src +### src JYRTC_example.c --JYRTC示例源文件 JYRTC_rtc.c --JYRTC时钟控制源文件 ntpUdp.c --ntpUdp源文件 - ntpWifi.c --ntpWifi源文件 + wifi_connect.c --Wifi源文件 ssd1306_oled.c --ssd1306_oled源文件 -# 相关API介绍 +## 相关API介绍 osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr): Creates and initializes a semaphore object. 参数: max_count – Indicates the maximum number of available tokens that can be applied for. @@ -81,7 +81,7 @@ 参数: event – Indicates the event for which the callback is to be registered. ...... -# 运行结果 +## 运行结果 1、接了OLED副板情况(JYRTC_rtc.c中宏定义have_oled设置为1) 实时的年月日与实时的时分秒分别显示在OLED上。 2、没有接OLED副板情况(JYRTC_rtc.c中宏定义have_oled设置为0) diff --git a/applications/app/TW208_Module_JYRTC/ntpUdp.c b/applications/app/TW208_Module_JYRTC/ntpUdp.c index c9d19f51..71dce0a2 100755 --- a/applications/app/TW208_Module_JYRTC/ntpUdp.c +++ b/applications/app/TW208_Module_JYRTC/ntpUdp.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "ntpUdp.h" #include "hi_wifi_api.h" #include "lwip/ip_addr.h" @@ -8,8 +23,6 @@ #include "lwip/opt.h" #include "lwip/sntp.h" - - #define THREAD_UDP_STACK_SIZE 4096 #define TIME_OFFSET 8*3600 #define SEVENZYYEARS 2208988800UL @@ -21,18 +34,18 @@ volatile int64_t currentEpochTime = 0; uint32_t ntpSocket = -1; fd_set read_set; - uint8_t sendBuf[READ_RECV_BUFFER_SIZE] = {0}; uint8_t recvBuf[READ_RECV_BUFFER_SIZE] = {0}; uint8_t ipAddress[16] = {0}; uint8_t timeString[48] = {0}; -volatile _Bool udpThreadActive = false; +volatile BOOL udpThreadActive = FALSE; -_Bool GetThreadRunStatus() +BOOL GetThreadRunStatus() { return udpThreadActive; } -void SetThreadRunStatus(_Bool status) + +void SetThreadRunStatus(BOOL status) { udpThreadActive = status; } @@ -46,54 +59,66 @@ int64_t GetEpochTime() char* GetEpochTimeString(TIME_FORMAT format,const char* hostname, uint16_t port) { currentEpochTime = 0; - if (StartUdpConnect(hostname,port)) { - if (format == HOUR_MIN_SEC) { + if (StartUdpConnect(hostname,port)) + { + if (format == HOUR_MIN_SEC) + { memset(timeString, 0, 48); long rawTime = GetEpochTime() + TIME_OFFSET;//getEpochTime(); int hours = (rawTime % 86400L) / 3600; int minutes = (rawTime % 3600) / 60; int seconds = rawTime % 60; - if (sprintf_s(timeString,48,"%02d:%02d:%02d",hours, minutes, seconds) == -1) { + if (sprintf_s(timeString,48,"%02d:%02d:%02d",hours, minutes, seconds) == -1) + { printf("sprintf time error\n"); return NULL; } - //hi_set_real_time((unsigned int)rawTime - TIME_OFFSET - SEVENZYYEARS); - } else if (format == YEAR_MONTH_DAY) { - memset(timeString, 0, 48); - long rawTime = GetEpochTime() + TIME_OFFSET; //getEpochTime(); - struct tm* time_tm = NULL; - time_tm = gmtime(&rawTime); - if (sprintf_s(timeString,48,"%04d/%02d/%02d",time_tm->tm_year+1900, time_tm->tm_mon+1, time_tm->tm_mday) == -1) { + } + else if (format == YEAR_MONTH_DAY) + { + memset(timeString, 0, 48); + long rawTime = GetEpochTime() + TIME_OFFSET; //getEpochTime(); + struct tm* time_tm = NULL; + time_tm = gmtime(&rawTime); + if (sprintf_s(timeString,48,"%04d/%02d/%02d",time_tm->tm_year+1900, time_tm->tm_mon+1, time_tm->tm_mday) == -1) + { + printf("sprintf time error\n"); + return NULL; + } + } + else if (format == YEAR_MONTH_DAY_HOUR_MIN_SEC) + { + memset(timeString, 0, 48); + long rawTime = GetEpochTime() + TIME_OFFSET; //getEpochTime(); + struct tm* time_tm = NULL; + time_tm = gmtime(&rawTime); + if (sprintf_s(timeString,48,"%04d/%02d/%02d %02d:%02d:%02d",time_tm->tm_year+1900, time_tm->tm_mon+1,time_tm->tm_mday, + time_tm->tm_hour,time_tm->tm_min,time_tm->tm_sec) == -1) + { printf("sprintf time error\n"); return NULL; - } - } else if (format == YEAR_MONTH_DAY_HOUR_MIN_SEC) { - memset(timeString, 0, 48); - long rawTime = GetEpochTime() + TIME_OFFSET; //getEpochTime(); - struct tm* time_tm = NULL; - time_tm = gmtime(&rawTime); - if (sprintf_s(timeString,48,"%04d/%02d/%02d %02d:%02d:%02d",time_tm->tm_year+1900, time_tm->tm_mon+1,time_tm->tm_mday, - time_tm->tm_hour,time_tm->tm_min,time_tm->tm_sec) == -1) { - printf("sprintf time error\n"); - return NULL; - } - } else { + } + } + else + { // do nothing return NULL; } return timeString; - }else { - return NULL; } - + + return NULL; } -_Bool GetTimeTm(const char* hostname, const uint16_t port, struct tm * time_tm) + +BOOL GetTimeTm(const char* hostname, const uint16_t port, struct tm * time_tm) { - if (hostname == NULL || time_tm == NULL) { + if (hostname == NULL || time_tm == NULL) + { printf("time tm is NULL\n"); - return false; + return FALSE; } + if (StartUdpConnect(hostname,port)) { memset(timeString, 0, 48); @@ -110,31 +135,31 @@ _Bool GetTimeTm(const char* hostname, const uint16_t port, struct tm * time_tm) time_tm->tm_yday = time_temp->tm_yday; time_tm->tm_isdst = time_temp->tm_isdst; printf("GetTimeTm:%04d/%02d/%02d %02d:%02d:%02d\n",time_tm->tm_year+1900, time_tm->tm_mon+1,time_tm->tm_mday,time_tm->tm_hour,time_tm->tm_min,time_tm->tm_sec); - return true; - } else { - return false; - } - -} + return TRUE; + } + return FALSE; +} void getFormattedTime(char* timeFormat, unsigned long currentEpoc) { - if (timeFormat == NULL) { + if (timeFormat == NULL) + { printf("container buffer is null\n"); return; } - memset(timeFormat, 0, 48); - long rawTime = currentEpoc + TIME_OFFSET;//getEpochTime(); - int hours = (rawTime % 86400L) / 3600; - int minutes = (rawTime % 3600) / 60; - int seconds = rawTime % 60; - if (sprintf_s(timeFormat,48,"%02d:%02d:%02d",hours,minutes,seconds) == -1) { + memset(timeFormat, 0, 48); + long rawTime = currentEpoc + TIME_OFFSET;//getEpochTime(); + int hours = (rawTime % 86400L) / 3600; + int minutes = (rawTime % 3600) / 60; + int seconds = rawTime % 60; + + if (sprintf_s(timeFormat,48,"%02d:%02d:%02d",hours,minutes,seconds) == -1) + { printf("sprintf time error\n"); } - return; // hoursStr + ":" + minuteStr + ":" + secondStr; } static void setSendBuf(void) @@ -158,61 +183,69 @@ uint32_t GetIpByHostName(const char* hostname, uint8_t* ipAddr) (void)ipAddr; if (!hostname || !ipAddr) return -1; - struct hostent* host=gethostbyname(hostname); - if (!host) - { - return -1; - } - printf("HostName :%s\n",host->h_name); - // 获取所有的地址 -#if 0 - for (int i = 0; host->h_addr_list[i]; i++) - { - //inet_ntoa(*(ip4_addr_t*)(host->h_addr_list[i]) - // struct in_addr* ia = (*(struct in_addr*)(host->h_addr_list[i])); - printf("%d ip: %s\n", i, inet_ntoa((*(struct in_addr*)(host->h_addr_list[i])))); - } -#endif - if(strncpy_s(ipAddr,IP_BUFFER_SIZE,inet_ntoa((*(struct in_addr*)(host->h_addr_list[0]))),IP_BUFFER_SIZE) != 0) { - printf("copy str error\n"); - return -1; - } else { - printf("get ip address is %s\n",ipAddr); - } - return 0; + struct hostent* host=gethostbyname(hostname); + if (!host) + { + return -1; + } + printf("HostName :%s\n",host->h_name); + // 获取所有的地址 + + if(strncpy_s(ipAddr, IP_BUFFER_SIZE, + inet_ntoa((*(struct in_addr*)(host->h_addr_list[0]))), + IP_BUFFER_SIZE) != 0) + { + printf("copy str error\n"); + return -1; + } + else + { + printf("get ip address is %s\n",ipAddr); + } + + return 0; } -_Bool StartUdpConnect(const char* hostname, uint16_t port) +BOOL StartUdpConnect(const char* hostname, uint16_t port) { int ret; + int nZero = 0; struct sockaddr_in clientaddr; ntpSocket = socket(PF_INET, SOCK_DGRAM, 0); - - if (ntpSocket == -1) { + + if (ntpSocket == -1) + { printf("create socket fd failed\n"); - } else { + } + else + { printf("create socket fd success\n"); } + uint32_t flag = fcntl(ntpSocket, F_GETFL, 0); ret = fcntl(ntpSocket, F_SETFL, flag | O_NONBLOCK); - if (ret == -1) { + if (ret == -1) + { printf("set nonblock failed\n"); if (ntpSocket >= 0) close(ntpSocket); - return false; + return FALSE; } - int nZero = 0; + + setsockopt(ntpSocket, SOL_SOCKET,SO_RCVBUF,(char*)&nZero, sizeof(int)); - if (GetIpByHostName(hostname,ipAddress) != 0) { + if (GetIpByHostName(hostname,ipAddress) != 0) + { printf("dns failed\n"); if (ntpSocket >= 0) close(ntpSocket); - return false; + return FALSE; } int reuse = 1; - if (setsockopt(ntpSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) { + if (setsockopt(ntpSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) + { if (ntpSocket >= 0) close(ntpSocket); - return false; + return FALSE; } //服务器 ip port @@ -221,12 +254,15 @@ _Bool StartUdpConnect(const char* hostname, uint16_t port) clientaddr.sin_addr.s_addr = inet_addr(ipAddress); clientaddr.sin_port = htons(port); setSendBuf(); - if (sendto(ntpSocket, sendBuf,UDP_PACKET_SIZE,0,(struct sockaddr *)&clientaddr,sizeof(clientaddr)) == UDP_PACKET_SIZE) { - ; - } else { + if (sendto(ntpSocket, sendBuf,UDP_PACKET_SIZE,0,(struct sockaddr *)&clientaddr,sizeof(clientaddr)) == UDP_PACKET_SIZE) + { + + } + else + { printf("send ntp socket failed\n"); if (ntpSocket >= 0) close(ntpSocket); - return false; + return FALSE; } uint8_t counts = 0; @@ -242,8 +278,10 @@ _Bool StartUdpConnect(const char* hostname, uint16_t port) tv.tv_sec = 0; tv.tv_usec = 10000; // 10ms ret = select(ntpSocket + 1, &read_set, NULL, NULL, &tv); - if (ret > 0) { - if (FD_ISSET(ntpSocket, &read_set)) { + if (ret > 0) + { + if (FD_ISSET(ntpSocket, &read_set)) + { memset(recvBuf, 0, 50); ret = recvfrom(ntpSocket, recvBuf, READ_RECV_BUFFER_SIZE, 0 , (struct sockaddr*)&serverAddr,(socklen_t*)&sizeClientAddr); if(ret > 0) @@ -258,17 +296,17 @@ _Bool StartUdpConnect(const char* hostname, uint16_t port) currentEpochTime |= (unsigned long)recvBuf[43]; currentEpochTime -= SEVENZYYEARS; if (ntpSocket >= 0) close(ntpSocket); - return true; + return TRUE; } } - } else { + } + else + { counts++; } - - } - //udpThreadActive = false; + if (ntpSocket >= 0) close(ntpSocket); - return false; + return FALSE; } diff --git a/applications/app/TW208_Module_JYRTC/ntpUdp.h b/applications/app/TW208_Module_JYRTC/ntpUdp.h index 1f1a620a..36cc67f7 100755 --- a/applications/app/TW208_Module_JYRTC/ntpUdp.h +++ b/applications/app/TW208_Module_JYRTC/ntpUdp.h @@ -1,12 +1,17 @@ -/****************************************************************************** - Copyright (C), 2021-2022, Talkweb Information System Co.,Ltd. - File: ntpUdp.c - Author: longxingkai - Version: 1.0 - Date: 2021.07 - Description: hi3861 udp communication header file powered by openharmony 2.0 canary - History: -******************************************************************************/ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef NTPUDP_H #define NTPUDP_H @@ -14,6 +19,7 @@ #include #include "ohos_init.h" #include "cmsis_os2.h" +#include "los_typedef.h" #include typedef enum { @@ -22,16 +28,17 @@ typedef enum { YEAR_MONTH_DAY_HOUR_MIN_SEC, TIME_MAX } TIME_FORMAT; + #define READ_RECV_BUFFER_SIZE 50 -_Bool GetThreadRunStatus(); -void SetThreadRunStatus(_Bool status); -_Bool GetTimeTm(const char* hostname, const uint16_t port, struct tm * time_tm); +BOOL GetThreadRunStatus(); +void SetThreadRunStatus(BOOL status); +BOOL GetTimeTm(const char* hostname, const uint16_t port, struct tm* time_tm); int64_t GetEpochTime(); -char* GetEpochTimeString(TIME_FORMAT format,const char* hostname, uint16_t port); -uint32_t ReadPacket(char* buf,size_t len); +char* GetEpochTimeString(TIME_FORMAT format, const char* hostname, uint16_t port); +uint32_t ReadPacket(char* buf, size_t len); uint32_t GetIpByHostName(const char* hostname, uint8_t* ipAddr); -_Bool StartUdpConnect(const char* hostname, uint16_t port); -_Bool StopUdpConnect(); +BOOL StartUdpConnect(const char* hostname, uint16_t port); +BOOL StopUdpConnect(); #endif diff --git a/applications/app/TW208_Module_JYRTC/ntpWifi.h b/applications/app/TW208_Module_JYRTC/ntpWifi.h deleted file mode 100755 index 57e7bd91..00000000 --- a/applications/app/TW208_Module_JYRTC/ntpWifi.h +++ /dev/null @@ -1,15 +0,0 @@ -/****************************************************************************** - Copyright (C), 2021-2022, Talkweb Information System Co.,Ltd. - File: ntpWifi.c - Author: longxingkai - Version: 1.0 - Date: 2021.07 - Description: hi3861 wifi header file powered by openharmony 2.0 canary - History: -******************************************************************************/ -#ifndef NTPWIFI_H -#define NTPWIFI_H - -int WifiConnect(const char* ssid,const char* psk,void* arg); - -#endif diff --git a/applications/app/TW208_Module_JYRTC/ssd1306_font.h b/applications/app/TW208_Module_JYRTC/ssd1306_font.h index 0e6aaa52..699f79c4 100755 --- a/applications/app/TW208_Module_JYRTC/ssd1306_font.h +++ b/applications/app/TW208_Module_JYRTC/ssd1306_font.h @@ -1,11 +1,22 @@ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef __SSD1306_FONT_H__ -#define __SSD1306_FONT_H__ -//����ASCII�� -//ƫ����32 -//ASCII�ַ��� -//ƫ����32 -//��С:12*6 -/************************************6*8�ĵ���************************************/ +#define __SSD1306_FONT_H__ + +//6*8 ASCII字符集点阵 const unsigned char asc2_1206[95][12]={ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/ {0x00,0x00,0x00,0x00,0x3F,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/ diff --git a/applications/app/TW208_Module_JYRTC/ssd1306_oled.c b/applications/app/TW208_Module_JYRTC/ssd1306_oled.c index 7b04f34b..e592e991 100755 --- a/applications/app/TW208_Module_JYRTC/ssd1306_oled.c +++ b/applications/app/TW208_Module_JYRTC/ssd1306_oled.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include "stdio.h" #include "ohos_init.h" diff --git a/applications/app/TW208_Module_JYRTC/ssd1306_oled.h b/applications/app/TW208_Module_JYRTC/ssd1306_oled.h index 5e104de2..73e70c2c 100755 --- a/applications/app/TW208_Module_JYRTC/ssd1306_oled.h +++ b/applications/app/TW208_Module_JYRTC/ssd1306_oled.h @@ -1,8 +1,22 @@ +/* + * Copyright (c) 2021 Talkweb Co. Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef __SSD1306_OLED_H__ #define __SSD1306_OLED_H__ #define __OLED_H - #define OLED_MODE 0 #define SIZE 8 #define XLevelL 0x00 @@ -13,11 +27,9 @@ #define X_WIDTH 128 #define Y_WIDTH 64 - #define OLED_CMD 0 //写命令 #define OLED_DATA 1 //写数据 - #define u8 unsigned char #define u16 unsigned short #define u32 unsigned int @@ -34,6 +46,5 @@ void OLED_DisplayTurn(u8 i); //开启OLED显示 void OLED_DisPlay_On(void); - #endif diff --git a/applications/app/TW208_Module_JYRTC/ntpWifi.c b/applications/app/TW208_Module_JYRTC/wifi_connect.c old mode 100755 new mode 100644 similarity index 78% rename from applications/app/TW208_Module_JYRTC/ntpWifi.c rename to applications/app/TW208_Module_JYRTC/wifi_connect.c index f63f8564..1acd81a4 --- a/applications/app/TW208_Module_JYRTC/ntpWifi.c +++ b/applications/app/TW208_Module_JYRTC/wifi_connect.c @@ -1,15 +1,31 @@ -#include "ntpWifi.h" +/* +* Copyright (c) 2021 Talkweb Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + #include #include #include + #include "ohos_init.h" #include "cmsis_os2.h" + #include "lwip/netif.h" #include "lwip/netifapi.h" #include "lwip/ip4_addr.h" #include "lwip/api_shell.h" -#include "wifi_device.h" +#include "wifi_device.h" #define DEF_TIMEOUT 15 #define ONE_SECOND 1 @@ -21,16 +37,9 @@ static int g_staScanSuccess = 0; static int g_ConnectSuccess = 0; static int ssid_count = 0; -WifiErrorCode error; +WifiErrorCode g_error; WifiEvent g_wifiEventHandler = {0}; -int sock_fd; -int addr_length; - -#define STD_TIMEZONE_OFFSET +8 /* 设置中国 */ -const int timeZone = 8; - - static void WiFiInit(void); static void WaitSacnResult(void); static int WaitConnectResult(void); @@ -68,8 +77,8 @@ static void WiFiInit(void) g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler; g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler; g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; - error = RegisterWifiEvent(&g_wifiEventHandler); - if (error != WIFI_SUCCESS) + g_error = RegisterWifiEvent(&g_wifiEventHandler); + if (g_error != WIFI_SUCCESS) { printf("register wifi event fail!\r\n"); } @@ -90,9 +99,8 @@ static void OnWifiScanStateChangedHandler(int state, int size) return; } -int WifiConnect(const char *ssid, const char *psk, void* arg) +int WifiConnect(const char *ssid, const char *psk) { - WifiScanInfo *wifi_info = NULL; unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT; static struct netif *g_lwip_netif = NULL; @@ -101,12 +109,12 @@ int WifiConnect(const char *ssid, const char *psk, void* arg) osDelay(200); printf("<--System Init-->\r\n"); - //初始化WIFI + // 初始化WIFI WiFiInit(); if (EnableWifi() != WIFI_SUCCESS) { - printf("EnableWifi failed, error = %d\n", error); + printf("EnableWifi failed, error = %d\n", g_error); return -1; } @@ -127,17 +135,17 @@ int WifiConnect(const char *ssid, const char *psk, void* arg) do { - // 重置标志位 + //重置标志位 ssid_count = 0; g_staScanSuccess = 0; - // 开启wifi扫描 + //开启wifi扫描 Scan(); - // 等待扫描结果 + //等待扫描结果 WaitSacnResult(); //获取扫描列表 - error = GetScanInfoList(wifi_info, &size); + g_error = GetScanInfoList(wifi_info, &size); } while (g_staScanSuccess != 1); //打印WiFi列表 @@ -148,14 +156,13 @@ int WifiConnect(const char *ssid, const char *psk, void* arg) } printf("********************\r\n"); - //插件指定的wifi是否存在 - for(uint8_t i = 0; i < ssid_count; i++) + for (uint8_t i = 0; i < ssid_count; i++) { if (strcmp(ssid, wifi_info[i].ssid) == 0) { int result; - printf("Select:%3d wireless, Waiting...\r\n", i+1); + printf("Select:%3d wireless, Waiting...\r\n", i + 1); //拷贝要连接的热点信息 strcpy(select_ap_config.ssid, wifi_info[i].ssid); strcpy(select_ap_config.preSharedKey, psk); @@ -172,31 +179,30 @@ int WifiConnect(const char *ssid, const char *psk, void* arg) } } - if(i == ssid_count-1) + if (i == ssid_count - 1) { printf("ERROR: No wifi as expected\r\n"); - while(1) osDelay(100); + while (1) + osDelay(100); } } - // 启动DHCP + //启动DHCP if (g_lwip_netif) { dhcp_start(g_lwip_netif); printf("begain to dhcp"); } - //等待DHCP - for(;;) + for (;;) { - if(dhcp_is_bound(g_lwip_netif) == ERR_OK) + if (dhcp_is_bound(g_lwip_netif) == ERR_OK) { printf("<-- DHCP state:OK -->\r\n"); - // 打印获取到的IP信息 + //打印获取到的IP信息 netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL); - osSemaphoreRelease((osSemaphoreId_t)arg); break; } @@ -205,10 +211,11 @@ int WifiConnect(const char *ssid, const char *psk, void* arg) } osDelay(100); - if (wifi_info != NULL) { + if (wifi_info != NULL) + { free(wifi_info); - wifi_info = NULL; - } + wifi_info = NULL; + } return 0; } @@ -245,7 +252,7 @@ static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info) } else { - printf("connect error,please check password\r\n"); + printf("connect error,please check ssid and password\r\n"); } return; } @@ -259,7 +266,7 @@ static void WaitSacnResult(void) scanTimeout--; if (g_staScanSuccess == 1) { - printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout)); + printf("WaitSacnResult: wait success [%d] s \n", (DEF_TIMEOUT - scanTimeout)); break; } } @@ -268,4 +275,3 @@ static void WaitSacnResult(void) printf("WaitSacnResult:timeout!\n"); } } - diff --git a/applications/app/TW208_Module_JYRTC/wifi_connect.h b/applications/app/TW208_Module_JYRTC/wifi_connect.h new file mode 100644 index 00000000..c6bb2b6d --- /dev/null +++ b/applications/app/TW208_Module_JYRTC/wifi_connect.h @@ -0,0 +1,24 @@ +/* +* Copyright (c) 2021 Talkweb Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __WIFI_CONNECT_H__ +#define __WIFI_CONNECT_H__ + +int WifiConnect(const char *ssid,const char *psk); + +#endif /* __WIFI_CONNECT_H__ */ + + + diff --git a/applications/app/TW210_Module_battery/BUILD.gn b/applications/app/TW210_Module_battery/BUILD.gn index 64444a77..bdc2c72d 100644 --- a/applications/app/TW210_Module_battery/BUILD.gn +++ b/applications/app/TW210_Module_battery/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Talkweb Co.,ltd. +# Copyright (c) 2021 Talkweb Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/applications/app/TW210_Module_battery/battery_example.c b/applications/app/TW210_Module_battery/battery_example.c index b6f01872..a63e25e4 100644 --- a/applications/app/TW210_Module_battery/battery_example.c +++ b/applications/app/TW210_Module_battery/battery_example.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Talkweb Information System Co.,ltd. + * Copyright (c) 2021 Talkweb Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/applications/app/TW303_Network_mqttclient/src/mqtt_main.c b/applications/app/TW303_Network_mqttclient/src/mqtt_main.c index 534adf30..cb575108 100644 --- a/applications/app/TW303_Network_mqttclient/src/mqtt_main.c +++ b/applications/app/TW303_Network_mqttclient/src/mqtt_main.c @@ -1,19 +1,17 @@ -/******************************************************************************* - * Copyright (c) 2014 IBM Corp. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v10.html - * and the Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * Contributors: - * Ian Craggs - initial API and implementation and/or initial documentation - * Sergio R. Caprile - clarifications and/or documentation extension - *******************************************************************************/ +/* +* Copyright (c) 2021 Talkweb Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include diff --git a/applications/app/TW303_Network_mqttclient/src/wifi_connect.c b/applications/app/TW303_Network_mqttclient/src/wifi_connect.c index 59cb9519..1acd81a4 100644 --- a/applications/app/TW303_Network_mqttclient/src/wifi_connect.c +++ b/applications/app/TW303_Network_mqttclient/src/wifi_connect.c @@ -31,23 +31,15 @@ #define ONE_SECOND 1 #define SELECT_WLAN_PORT "wlan0" -// #define SELECT_WIFI_SSID "aaa" -// #define SELECT_WIFI_PASSWORD "talkweb1996" #define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK static int g_staScanSuccess = 0; static int g_ConnectSuccess = 0; static int ssid_count = 0; -WifiErrorCode error; +WifiErrorCode g_error; WifiEvent g_wifiEventHandler = {0}; -int sock_fd; -int addr_length; - -#define STD_TIMEZONE_OFFSET +8 /* 设置中国 */ -const int timeZone = 8; - static void WiFiInit(void); static void WaitSacnResult(void); static int WaitConnectResult(void); @@ -85,8 +77,8 @@ static void WiFiInit(void) g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler; g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler; g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; - error = RegisterWifiEvent(&g_wifiEventHandler); - if (error != WIFI_SUCCESS) + g_error = RegisterWifiEvent(&g_wifiEventHandler); + if (g_error != WIFI_SUCCESS) { printf("register wifi event fail!\r\n"); } @@ -109,7 +101,6 @@ static void OnWifiScanStateChangedHandler(int state, int size) int WifiConnect(const char *ssid, const char *psk) { - WifiScanInfo *wifi_info = NULL; unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT; static struct netif *g_lwip_netif = NULL; @@ -123,7 +114,7 @@ int WifiConnect(const char *ssid, const char *psk) if (EnableWifi() != WIFI_SUCCESS) { - printf("EnableWifi failed, error = %d\n", error); + printf("EnableWifi failed, error = %d\n", g_error); return -1; } @@ -154,7 +145,7 @@ int WifiConnect(const char *ssid, const char *psk) WaitSacnResult(); //获取扫描列表 - error = GetScanInfoList(wifi_info, &size); + g_error = GetScanInfoList(wifi_info, &size); } while (g_staScanSuccess != 1); //打印WiFi列表 @@ -261,7 +252,7 @@ static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info) } else { - printf("connect error,please check password\r\n"); + printf("connect error,please check ssid and password\r\n"); } return; } @@ -275,7 +266,7 @@ static void WaitSacnResult(void) scanTimeout--; if (g_staScanSuccess == 1) { - printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout)); + printf("WaitSacnResult: wait success [%d] s \n", (DEF_TIMEOUT - scanTimeout)); break; } }