!34 更新代码

* update
* add lic
This commit is contained in:
候鹏飞 2021-10-12 10:50:48 +00:00
parent 41be90fbe1
commit 792b5a6df1
19 changed files with 590 additions and 426 deletions

View File

@ -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

View File

@ -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);

View File

@ -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"
]

View File

@ -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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include <los_typedef.h>
#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)
{

View File

@ -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 <hi_early_debug.h>
#include <hi_i2c.h>
#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);
}
}
}
}

View File

@ -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__

View File

@ -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

View File

@ -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;
}

View File

@ -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 <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "los_typedef.h"
#include <time.h>
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

View File

@ -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

View File

@ -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__
//<2F><><EFBFBD><EFBFBD>ASCII<49><49>
//ƫ<><C6AB><EFBFBD><EFBFBD>32
//ASCII<49>ַ<EFBFBD><D6B7><EFBFBD>
//ƫ<><C6AB><EFBFBD><EFBFBD>32
//<2F><>С:12*6
/************************************6*8<>ĵ<EFBFBD><C4B5><EFBFBD>************************************/
#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*/

View File

@ -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 <unistd.h>
#include "stdio.h"
#include "ohos_init.h"

View File

@ -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

View File

@ -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 <stdio.h>
#include <unistd.h>
#include <string.h>
#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");
}
}

View File

@ -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__ */

View File

@ -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

View File

@ -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

View File

@ -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 <stdio.h>
#include <stdlib.h>

View File

@ -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;
}
}