增加GYRTC案例

This commit is contained in:
庞建文 2021-10-11 20:52:37 -07:00
parent 2155ddbc42
commit edd008bd24
15 changed files with 1934 additions and 0 deletions

View File

@ -81,6 +81,7 @@
| TW203 | Module | tricolor_light| [人体感应联动三色灯](applications/app/TW203_Module_tricolor_light/README.md) | 核心板 + 人体感应扩展板|
| TW205 | Module | motor | [马达开发板演示](applications/app/TW205_Module_motor/README.md) | 核心板 + 马达板|
| TW207 | Module | gyro | [GYRO驱动](applications/app/TW207_Module_gyro/README.md) | 核心板 + GYRO扩展板|
| TW208 | Module | JYRTC | [JYRTC时钟演示](applications/app/TW208_Module_JYRTC/README.md) | 核心板 + JYRTC扩展板 + OLED扩展板|
| TW210 | Module | battery | [电池电源管理](applications/app/TW210_Module_battery/README.md) | 核心板 + NFC扩展板 + OLED扩展板|
| TW301 | Network | wifista | [WiFi-STA联网演示](applications/app/TW301_Network_wifista/README.md) | 核心板 |
| TW302 | Network | wifiap | [WiFi-AP热点演示](applications/app/TW302_Network_wifiap/README.md) | 核心板 |

View File

@ -29,10 +29,12 @@ lite_component("app") {
# "TW105_I2C_sht30:i2c_sht30",
# "TW106_UART:uart_example",
# "TW201_Module_oled:module_oled_example",
# "TW202_Module_traffic_light:module_traffic_light_example",
# "TW203_Module_tricolor_light:module_tricolor_light_example",
# "TW204_Module_enviroment:module_enviroment_example",
# "TW205_Module_motor:module_motor_example",
# "TW207_Module_gyro:module_gyro_example",
# "TW208_Module_JYRTC:module_JYRTC_example",
# "TW210_Module_battery:module_battery_example",
# "TW301_Network_wifista:network_wifista_example",
# "TW302_Network_wifiap:network_wifiap_example",

View File

@ -31,6 +31,7 @@
| TW203 | Module | tricolor_light| [人体感应联动三色灯](TW203_Module_tricolor_light/README.md) | 核心板 + 人体感应板|
| TW205 | Module | motor | [马达开发板演示](TW205_Module_motor/README.md) | 核心板 + 马达板|
| TW207 | Module | gyro | [GYRO驱动](TW207_Module_gyro/README.md) | 核心板 + GYRO扩展板|
| TW208 | Module | JYRTC | [JYRTC时钟演示](TW208_Module_JYRTC/README.md) | 核心板 + JYRTC扩展板 + OLED扩展板|
| TW210 | Module | battery | [电池电源管理](TW210_Module_battery/README.md) | 核心板 + NFC扩展板 + OLED扩展板|
| TW301 | Network | wifista | [WiFi-STA联网演示](TW301_Network_wifista/README.md) | 核心板 |
| TW302 | Network | wifiap | [WiFi-AP热点演示](TW302_Network_wifiap/README.md) | 核心板 |

View File

@ -0,0 +1,18 @@
static_library("module_JYRTC_example") {
sources = [
"JYRTC_rtc.c",
"ssd1306_oled.c",
"JYRTC_example.c",
"ntpWifi.c",
"ntpUdp.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/kal/cmsis",
"//base/iot_hardware/peripheral/interfaces/kits",
"//device/talkweb/niobe/sdk_liteos/third_party/lwip_sack/include",
"//foundation/communication/wifi_lite/interfaces/wifiservice" ,
"//device/talkweb/niobe/sdk_liteos/include/",
]
}

View File

@ -0,0 +1,135 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "JYRTC_rtc.h"
#include "ntpWifi.h"
#include "ntpUdp.h"
#include "iot_gpio.h"
#define TASK_STACK_SIZE 1024 * 8
#define TASK_PRIO 25
#define SELECT_WIFI_SSID "aaa"
#define SELECT_WIFI_PASSWORD "talkweb1996"
#define THREAD_STACK_SIZE 10240
#define NTP_ALIYUN_COM "ntp.aliyun.com"
#define NTP_SERVER_PORT 123
#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",
"ntp3.aliyun.com",
"ntp4.aliyun.com",
"ntp5.aliyun.com",
"ntp6.aliyun.com",
"ntp7.aliyun.com",
"cn.pool.ntp.org"
};
volatile osSemaphoreId_t sem_udp;
struct tm time_value = {0};
static void wifiConnect(void* argc)
{
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);
}
return;
}
static void DS1307_Task(void* argc)
{
printf("start ds1307 task\n");
rtc_timer(argc);
}
static void DS1307ExampleEntry(void)
{
sem_udp = osSemaphoreNew(32, 0, NULL);
osThreadAttr_t attr;
InitGpio5Isr();
attr.name = "1307_Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = TASK_STACK_SIZE;
attr.priority = TASK_PRIO;
if (osThreadNew((osThreadFunc_t)wifiConnect, (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)
{
printf("Falied to create Example_Task!\n");
}
}
APP_FEATURE_INIT(DS1307ExampleEntry);

View File

@ -0,0 +1,495 @@
#include <hi_early_debug.h>
#include <hi_i2c.h>
#include "JYRTC_rtc.h"
#include <hi_task.h>
#include <hi_time.h>
#include <hi_stdlib.h>
#include <hi_errno.h>
#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};
/*写寄存器*/
hi_u32 ds1307_i2c_write( hi_u8 reg_addr, hi_u8 high_8, hi_u8 low_8, hi_u8 reg_len)
{
hi_u32 status =0;
hi_i2c_idx id = HI_I2C_IDX_0;
hi_i2c_data ds1307_i2c_write_cmd_addr ={0};
hi_u8 temp1 =0;
hi_u8 temp2 =0;
temp1 = (high_8/10*16)+(high_8%10); //16进制转BCD
temp2 = (low_8/10*16)+(low_8%10); //16进制转BCD
// printf("temp1= %x, temp2 = %x \r\n", temp1, temp2);
hi_u8 _send_user_cmd[SEND_BUF_LEN] = {reg_addr, temp1, temp2};
ds1307_i2c_write_cmd_addr.send_buf = _send_user_cmd;
ds1307_i2c_write_cmd_addr.send_len = reg_len;
status = hi_i2c_write(id, (DS1307_ADDRESS<<1)|DS1307_WRITE_ADDRESS, &ds1307_i2c_write_cmd_addr);
if (status != HI_ERR_SUCCESS) {
printf("===== Error: ds1307 sensor I2C write cmd address status = 0x%x! =====\r\n", status);
return status;
}
return HI_ERR_SUCCESS;
}
hi_u8 *ds1307_read(hi_u8 rtc_reg, hi_u32 recv_len, hi_u8 *rct_buf)
{
// ds1307_rtc_type read_rtc;
hi_u32 status = 0;
hi_i2c_idx id =HI_I2C_IDX_0;
hi_u8 recv_data[DS1307_REG_ARRAY_LEN] = { 0 };
hi_i2c_data ds1307_i2c_data = { 0 };
/* Request memory space */
memset(rct_buf, 0x00, sizeof(rct_buf));
memset(recv_data, 0x0, sizeof(recv_data));
memset(&ds1307_i2c_data, 0x0, sizeof(hi_i2c_data));
ds1307_i2c_data.receive_buf = recv_data;
ds1307_i2c_data.receive_len = recv_len;
status = hi_i2c_read(id, (DS1307_ADDRESS<<1)|DS1307_READ_ADDRESS, &ds1307_i2c_data);
if (status != HI_ERR_SUCCESS) {
printf("===== Error: ds1307 sencor I2C read status = 0x%x! =====\r\n", status);
return status;
}
switch (rtc_reg) {
case RCT_SECOND:
rct_buf[0] = recv_data[0];
break;
case RCT_MINUTE:
rct_buf[0] = recv_data[0];
break;
case RCT_HOUR:
rct_buf[0] = recv_data[0];
break;
case RCT_DAY :
rct_buf[0] = recv_data[0];
break;
case RCT_DATE:
rct_buf[0] = recv_data[0];
break;
case RCT_MONTH:
rct_buf[0] = recv_data[0];
break;
case RCT_YEAR:
rct_buf[0] = recv_data[0];
break;
default:
break;
}
return rct_buf;
}
/*rtc timer setting*/
hi_void DS1307_SetInit(const struct tm* time_value)
{
if (time_value == NULL) {
printf("time value is null\n");
return;
}
//ds1307_rtc_type rct_time_set ={0};
printf("DS1307_SetInit:%04d/%02d/%02d %02d:%02d:%02d",time_value->tm_year+1900, time_value->tm_mon+1,time_value->tm_mday,
time_value->tm_hour,time_value->tm_min,time_value->tm_sec);
rct_time_set.rtc_second[0] = (hi_u8)time_value->tm_sec;
rct_time_set.rtc_minue[0] = (hi_u8)time_value->tm_min;
rct_time_set.rtc_hour[0] = (hi_u8)time_value->tm_hour;
rct_time_set.rtc_day[0] = (hi_u8)time_value->tm_wday;
rct_time_set.rtc_date[0] = (hi_u8)time_value->tm_mday;
rct_time_set.rtc_month[0] = (hi_u8)(time_value->tm_mon + 1);
rct_time_set.rtc_year[0] = (hi_u8)((time_value->tm_year + 1900)-2000);
return;
}
hi_void rct_set_init(hi_void)
{
hi_u32 ret;
//ds1307_rtc_type rct_time_set ={0};
//rct_time_set.rtc_second[0] = 30;
//rct_time_set.rtc_minue[0] =59;
//rct_time_set.rtc_hour[0] = 23;
//rct_time_set.rtc_day[0] = 5;
//rct_time_set.rtc_date[0] = 31;
//rct_time_set.rtc_month[0] = 12;
//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) {
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) {
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) {
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) {
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) {
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) {
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) {
printf("Failed to year cmd\r\n");
}
hi_udelay(DELAY_TIME);
}
/* change type*/
hi_u8 *int_to_char(hi_s32 dec, hi_u8 *str)
{
hi_u8 str1[40] = {0};
hi_s32 j = 0;
hi_s32 k = 0;
hi_s32 i = 0;
if (i == dec) {
str[0] = '0';
return str;
}
i = (hi_s32)dec;//浮点数的整数部分
while (i > 0) {
str1[j++] = i % 10 + '0';
i = i / 10;
}
for (k = 0;k < j;k++) {
str[k] = str1[j-1-k];//被提取的整数部分正序存放到另一个数组
}
return str;
}
/*read rtc time*/
hi_void *rtc_timer(hi_void *param)
{
hi_u8 rct_read_data[2] ={0};
ds1307_rtc_type rtc_data ={0};
hi_s32 temp_second =0;
hi_s32 temp_minute =0;
hi_s32 temp_hour =0;
hi_s32 temp_day =0;
hi_s32 temp_date =0;
hi_s32 temp_month =0;
hi_s32 temp_year =0;
hi_u8 ch_year[5] ={0};
hi_u8 ch_month[3] ={0};
hi_u8 ch_day[3] ={0};
hi_u8 ch_date[3] ={0};
hi_u8 ch_hour[3] ={0};
hi_u8 ch_minute[3] ={0};
hi_u8 ch_second[3] ={0};
char yearmd[48]={0};
char hourms[48]={0};
hi_unref_param(param);
/*初始化时屏幕 i2c baudrate setting*/
hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
hi_i2c_init(HI_I2C_IDX_0, 400000); /* baudrate: 400kbps */
hi_i2c_set_baudrate(HI_I2C_IDX_0, 400000);
/*ssd1306 config init*/
//OLED_ShowString(8,16,"hello world",16);
//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();
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]) {
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) {
//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 {
//OLED_ShowString(36, 20, "0", 12);
//OLED_ShowString(42, 20, ch_second, 12);
printf("have_oled rtc_timer ch_second=%s\n",ch_second);
}
//OLED_ShowString(48, 20, ".", 12);
}
/*----------------------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) {
//OLED_ShowString(18, 20, ch_minute, 12);
printf("have_oled rtc_timer ch_minute=%s\n",ch_minute);
//OLED_ShowString(30, 20, ":", 12);
} 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);
//OLED_ShowString(30, 20, ":", 12);
}
//OLED_ShowString(56, 6, "0", 12);
}
/*----------------------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) {
//OLED_ShowString(0, 20, ch_hour, 12);
printf("have_oled rtc_timer ch_hour=%s\n",ch_hour);
//OLED_ShowString(12, 20, ":", 12);
} 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);
//OLED_ShowString(12, 20, ":", 12);
}
//OLED_ShowString(64, 6, " ", 12);
}
/*----------------------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);
//OLED_ShowString(0, 34, "week:", 12);
//OLED_ShowString(36, 34, ch_day, 12);
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) {
//OLED_ShowString(48, 6, ch_date, 12);
printf("have_oled rtc_timer ch_date=%s\n",ch_date);
} 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);
}
//OLED_ShowString(60, 20, ".", 12);
}
/*----------------------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) {
//OLED_ShowString(30, 6, ch_month, 12);
printf("have_oled rtc_timer ch_month=%s\n",ch_month);
//OLED_ShowString(30, 6, "-", 12);
} 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);
// OLED_ShowString(30, 5, "-", 12);
}
}
/*----------------------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);
//OLED_ShowString(0, 6, "20", 12);
//OLED_ShowString(12, 6, ch_year, 12);
printf("have_oled rtc_timer ch_year=%s\n",ch_year);
//OLED_ShowString(32, 6, "-", 12);
}
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) {
printf("sprintf time error\n");
}
printf("have_oled rtc_timer yearmd=%s\n",yearmd);
printf("have_oled rtc_timer hourms=%s\n",hourms);
OLED_ShowString(0, 6, yearmd, 12);
OLED_ShowString(0, 20, hourms, 12);
OLED_Refresh();
hi_sleep(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);
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);
}
}
/*----------------------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;
}
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);
}
}
/*----------------------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);
}
}
}

View File

@ -0,0 +1,53 @@
#ifndef __DS1307_RTC_H__
#define __DS1307_RTC_H__
#include <hi_types_base.h>
#include "hi_time.h"
#include <time.h>
#define DS1307_ADDRESS 0x32
#define DS1307_WRITE_ADDRESS 0x00
#define DS1307_READ_ADDRESS 0x01
#define DS1307_REG_ARRAY_LEN 2
#define RTC_REG_TIME_BUF 2
#define SEND_BUF_LEN 3
#define SEND_SET_REG_LEN 2
#define SEND_READ_DATA_LEN 1
#define DS1307_TASK_STAK_SIZE (1024*2)
#define DS1307_TASK_PRIORITY (25)
#define MONTH_SETTING ((hi_u8)0x05)
#define DELAY_TIME ((hi_u32)10000)
#define RTC_FIRST_SECOND 0
#define RTC_FIRST_SECOND 0
#define have_oled 1
/*ds1307 reg*/
typedef enum {
RCT_SECOND =0,
RCT_MINUTE,
RCT_HOUR,
RCT_DAY,
RCT_DATE,
RCT_MONTH,
RCT_YEAR
}rct_reg;
typedef struct
{
hi_u8 rtc_second[RTC_REG_TIME_BUF];
hi_u8 rtc_minue[RTC_REG_TIME_BUF];
hi_u8 rtc_hour[RTC_REG_TIME_BUF];
hi_u8 rtc_day[RTC_REG_TIME_BUF];
hi_u8 rtc_date[RTC_REG_TIME_BUF];
hi_u8 rtc_month[RTC_REG_TIME_BUF];
hi_u8 rtc_year[RTC_REG_TIME_BUF];
}ds1307_rtc_type;
hi_void DS1307_SetInit(const struct tm* time_value);
hi_u32 ds1307_i2c_write( hi_u8 reg_addr, hi_u8 high_8, hi_u8 low_8, hi_u8 reg_len);
hi_u8 *ds1307_read(hi_u8 rtc_reg, hi_u32 recv_len, hi_u8 *rct_buf);
hi_void *rtc_timer(hi_void *param);
hi_void app_demo_rtc_task(hi_void);
hi_void rct_set_init(hi_void);
#endif

View File

@ -0,0 +1,90 @@
# OpenHarmony模组开发-JYRTC时钟板控制
本案例使用到了2个副板一个时钟板一个OLED板。
本案例代码流程有两种情况
1、接了OLED副板情况JYRTC_rtc.c中宏定义have_oled设置为1
按下OLED副板上的右边按键触发中断释放信号量因为ntpwifi连接前一直在等待信号量所以这步必须。
通过ntpwifi连接阿里云通过UDP服务获取阿里云时间把阿里云时间写入RTC相应寄存器后读取RTC时间并把它显示到OLED上。
2、没有接OLED副板情况JYRTC_rtc.c中宏定义have_oled设置为0
直接获取RTC时间并把时间用串口信息打印出来
# 目录结构:
## include
JYRTC_rtc.h --JYRTC时钟控制头文件
ntpUdp.h --ntpUdp头文件
ntpWifi.h --ntpWifi连接头文件
ssd1306_font.h --oled头文件
ssd1306_oled.h --oled头文件
## src
JYRTC_example.c --JYRTC示例源文件
JYRTC_rtc.c --JYRTC时钟控制源文件
ntpUdp.c --ntpUdp源文件
ntpWifi.c --ntpWifi源文件
ssd1306_oled.c --ssd1306_oled源文件
# 相关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.
initial_count Indicates the initial number of available tokens.
attr Indicates the pointer to the semaphore attributes. This parameter is not used.
IoTGpioInit(unsigned int id) Initializes a GPIO device.
参数:
id Indicates the GPIO pin number.
IotIoSetFunc(IOT_GPIO_IO_NAME id, unsigned char val) 注册GPIO功能
参数:
id Indicates the GPIO pin number.
val 注册GPIO功能
IotIoSetPull(IOT_GPIO_IO_NAME id, IOT_IO_PULL val) 设置上拉或下拉
参数:
id Indicates the GPIO pin number.
val 上拉或下拉
IoTGpioSetDir(unsigned int id, IotGpioDir dir) Sets the direction for a GPIO pin.
参数:
id Indicates the GPIO pin number.
dir Indicates the GPIO input/output direction.
IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity, GpioIsrCallbackFunc func, char *arg) Enables the interrupt feature for a GPIO pin.
参数:
id Indicates the GPIO pin number.
intType Indicates the interrupt type.
intPolarity Indicates the interrupt polarity.
func Indicates the interrupt callback function.
arg Indicates the pointer to the argument used in the interrupt callback function.
osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) Creates an active thread.
参数:
func Indicates the entry of the thread callback function.
argument Indicates the pointer to the argument passed to the thread.
attr Indicates the thread attributes.
osSemaphoreRelease(osSemaphoreId_t semaphore_id) Releases a token of a semaphore object.
参数:
semaphore_id Indicates the semaphore ID, which is obtained using osSemaphoreNew
osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout) Acquires a token of a semaphore object.
参数:
semaphore_id Indicates the semaphore ID, which is obtained using osSemaphoreNew.
timeout Indicates the timeout duration. This parameter is the number of ticks.
RegisterWifiEvent(WifiEvent *event) Registers a callback for a specified Wi-Fi event.
参数:
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
实时的RTC时间在串口信息中打印

View File

@ -0,0 +1,274 @@
#include "ntpUdp.h"
#include "hi_wifi_api.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/inet.h"
#include "lwip/opt.h"
#include "lwip/sntp.h"
#define THREAD_UDP_STACK_SIZE 4096
#define TIME_OFFSET 8*3600
#define SEVENZYYEARS 2208988800UL
#define UDP_PACKET_SIZE 48
#define IP_BUFFER_SIZE 16
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;
_Bool GetThreadRunStatus()
{
return udpThreadActive;
}
void SetThreadRunStatus(_Bool status)
{
udpThreadActive = status;
}
int64_t GetEpochTime()
{
return currentEpochTime;
}
char* GetEpochTimeString(TIME_FORMAT format,const char* hostname, uint16_t port)
{
currentEpochTime = 0;
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) {
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) {
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 {
// do nothing
return NULL;
}
return timeString;
}else {
return NULL;
}
}
_Bool GetTimeTm(const char* hostname, const uint16_t port, struct tm * time_tm)
{
if (hostname == NULL || time_tm == NULL) {
printf("time tm is NULL\n");
return false;
}
if (StartUdpConnect(hostname,port))
{
memset(timeString, 0, 48);
long rawTime = GetEpochTime() + TIME_OFFSET; //getEpochTime();
struct tm* time_temp = NULL;
time_temp = gmtime(&rawTime);
time_tm->tm_sec = time_temp->tm_sec;
time_tm->tm_min = time_temp->tm_min;
time_tm->tm_hour = time_temp->tm_hour;
time_tm->tm_mday = time_temp->tm_mday;
time_tm->tm_mon = time_temp->tm_mon;
time_tm->tm_year = time_temp->tm_year;
time_tm->tm_wday = time_temp->tm_wday;
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;
}
}
void getFormattedTime(char* timeFormat, unsigned long currentEpoc)
{
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) {
printf("sprintf time error\n");
}
return; // hoursStr + ":" + minuteStr + ":" + secondStr;
}
static void setSendBuf(void)
{
memset(sendBuf,0,READ_RECV_BUFFER_SIZE);
//sendBuf[0] = 0b11100011; // LI, Version, Mode
sendBuf[0] = 0xE3;
sendBuf[1] = 0; // Stratum, or type of clock
sendBuf[2] = 6; // Polling Interval
sendBuf[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
sendBuf[12] = 49;
sendBuf[13] = 0x4E;
sendBuf[14] = 49;
sendBuf[15] = 52;
}
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;
}
_Bool StartUdpConnect(const char* hostname, uint16_t port)
{
int ret;
struct sockaddr_in clientaddr;
ntpSocket = socket(PF_INET, SOCK_DGRAM, 0);
if (ntpSocket == -1) {
printf("create socket fd failed\n");
} 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) {
printf("set nonblock failed\n");
if (ntpSocket >= 0) close(ntpSocket);
return false;
}
int nZero = 0;
setsockopt(ntpSocket, SOL_SOCKET,SO_RCVBUF,(char*)&nZero, sizeof(int));
if (GetIpByHostName(hostname,ipAddress) != 0) {
printf("dns failed\n");
if (ntpSocket >= 0) close(ntpSocket);
return false;
}
int reuse = 1;
if (setsockopt(ntpSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
if (ntpSocket >= 0) close(ntpSocket);
return false;
}
//服务器 ip port
bzero(&clientaddr, sizeof(clientaddr));
clientaddr.sin_family = AF_INET;
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 {
printf("send ntp socket failed\n");
if (ntpSocket >= 0) close(ntpSocket);
return false;
}
uint8_t counts = 0;
while(udpThreadActive && counts < 100)
{
struct sockaddr_in serverAddr = {0};
int sizeClientAddr = sizeof(struct sockaddr_in);
memset(recvBuf, 0, sizeof(recvBuf));
FD_ZERO(&read_set);
FD_CLR(ntpSocket, &read_set);
FD_SET(ntpSocket, &read_set);
struct timeval tv;
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)) {
memset(recvBuf, 0, 50);
ret = recvfrom(ntpSocket, recvBuf, READ_RECV_BUFFER_SIZE, 0 , (struct sockaddr*)&serverAddr,(socklen_t*)&sizeClientAddr);
if(ret > 0)
{
//short highword = (recvBuf[40]<<8) + recvBuf[41];
//short lowword = (recvBuf[42]<<8) + recvBuf[43];
//currentEpochTime = (highword << 16) + lowword;
//currentEpochTime -= SEVENZYYEARS;
currentEpochTime = (unsigned long)recvBuf[40] << 24;
currentEpochTime |= (unsigned long)recvBuf[41] << 16;
currentEpochTime |= (unsigned long)recvBuf[42] << 8;
currentEpochTime |= (unsigned long)recvBuf[43];
currentEpochTime -= SEVENZYYEARS;
if (ntpSocket >= 0) close(ntpSocket);
return true;
}
}
} else {
counts++;
}
}
//udpThreadActive = false;
if (ntpSocket >= 0) close(ntpSocket);
return false;
}

View File

@ -0,0 +1,37 @@
/******************************************************************************
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:
******************************************************************************/
#ifndef NTPUDP_H
#define NTPUDP_H
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include <time.h>
typedef enum {
YEAR_MONTH_DAY = 0,
HOUR_MIN_SEC,
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);
int64_t GetEpochTime();
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();
#endif

View File

@ -0,0 +1,271 @@
#include "ntpWifi.h"
#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"
#define DEF_TIMEOUT 15
#define ONE_SECOND 1
#define SELECT_WLAN_PORT "wlan0"
#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;
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);
static void OnWifiScanStateChangedHandler(int state, int size);
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);
static void OnHotspotStaLeaveHandler(StationInfo *info);
static void OnHotspotStaJoinHandler(StationInfo *info)
{
(void)info;
printf("STA join AP\n");
return;
}
static void OnHotspotStaLeaveHandler(StationInfo *info)
{
(void)info;
printf("HotspotStaLeave:info is null.\n");
return;
}
static void OnHotspotStateChangedHandler(int state)
{
printf("HotspotStateChanged:state is %d.\n", state);
return;
}
static void WiFiInit(void)
{
printf("<--Wifi Init-->\r\n");
g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
error = RegisterWifiEvent(&g_wifiEventHandler);
if (error != WIFI_SUCCESS)
{
printf("register wifi event fail!\r\n");
}
else
{
printf("register wifi event succeed!\r\n");
}
}
static void OnWifiScanStateChangedHandler(int state, int size)
{
(void)state;
if (size > 0)
{
ssid_count = size;
g_staScanSuccess = 1;
}
return;
}
int WifiConnect(const char *ssid, const char *psk, void* arg)
{
WifiScanInfo *wifi_info = NULL;
unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
static struct netif *g_lwip_netif = NULL;
WifiDeviceConfig select_ap_config = {0};
osDelay(200);
printf("<--System Init-->\r\n");
//初始化WIFI
WiFiInit();
if (EnableWifi() != WIFI_SUCCESS)
{
printf("EnableWifi failed, error = %d\n", error);
return -1;
}
//判断WIFI是否激活
if (IsWifiActive() == 0)
{
printf("Wifi station is not actived.\n");
return -1;
}
//分配空间保存WiFi信息
wifi_info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
if (wifi_info == NULL)
{
printf("faild to create wifiscanInfo.\n");
return -1;
}
do
{
// 重置标志位
ssid_count = 0;
g_staScanSuccess = 0;
// 开启wifi扫描
Scan();
// 等待扫描结果
WaitSacnResult();
//获取扫描列表
error = GetScanInfoList(wifi_info, &size);
} while (g_staScanSuccess != 1);
//打印WiFi列表
printf("********************\r\n");
for (uint8_t i = 0; i < ssid_count; i++)
{
printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i + 1, wifi_info[i].ssid, wifi_info[i].rssi / 100);
}
printf("********************\r\n");
//插件指定的wifi是否存在
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);
//拷贝要连接的热点信息
strcpy(select_ap_config.ssid, wifi_info[i].ssid);
strcpy(select_ap_config.preSharedKey, psk);
select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE;
if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS)
{
if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1)
{
printf("WiFi connect succeed!\r\n");
g_lwip_netif = netifapi_netif_find(SELECT_WLAN_PORT);
break;
}
}
}
if(i == ssid_count-1)
{
printf("ERROR: No wifi as expected\r\n");
while(1) osDelay(100);
}
}
// 启动DHCP
if (g_lwip_netif)
{
dhcp_start(g_lwip_netif);
printf("begain to dhcp");
}
//等待DHCP
for(;;)
{
if(dhcp_is_bound(g_lwip_netif) == ERR_OK)
{
printf("<-- DHCP state:OK -->\r\n");
// 打印获取到的IP信息
netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL);
osSemaphoreRelease((osSemaphoreId_t)arg);
break;
}
printf("<-- DHCP state:Inprogress -->\r\n");
osDelay(100);
}
osDelay(100);
if (wifi_info != NULL) {
free(wifi_info);
wifi_info = NULL;
}
return 0;
}
static int WaitConnectResult(void)
{
int ConnectTimeout = DEF_TIMEOUT;
while (ConnectTimeout > 0)
{
sleep(1);
ConnectTimeout--;
if (g_ConnectSuccess == 1)
{
printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout));
break;
}
}
if (ConnectTimeout <= 0)
{
printf("WaitConnectResult:timeout!\n");
return 0;
}
return 1;
}
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
{
(void)info;
if (state > 0)
{
g_ConnectSuccess = 1;
printf("callback function for wifi connect\r\n");
}
else
{
printf("connect error,please check password\r\n");
}
return;
}
static void WaitSacnResult(void)
{
int scanTimeout = DEF_TIMEOUT;
while (scanTimeout > 0)
{
sleep(ONE_SECOND);
scanTimeout--;
if (g_staScanSuccess == 1)
{
printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
break;
}
}
if (scanTimeout <= 0)
{
printf("WaitSacnResult:timeout!\n");
}
}

View File

@ -0,0 +1,15 @@
/******************************************************************************
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

@ -0,0 +1,209 @@
#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>************************************/
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*/
{0x00,0x00,0x30,0x00,0x40,0x00,0x30,0x00,0x40,0x00,0x00,0x00},/*""",2*/
{0x09,0x00,0x0B,0xC0,0x3D,0x00,0x0B,0xC0,0x3D,0x00,0x09,0x00},/*"#",3*/
{0x18,0xC0,0x24,0x40,0x7F,0xE0,0x22,0x40,0x31,0x80,0x00,0x00},/*"$",4*/
{0x18,0x00,0x24,0xC0,0x1B,0x00,0x0D,0x80,0x32,0x40,0x01,0x80},/*"%",5*/
{0x03,0x80,0x1C,0x40,0x27,0x40,0x1C,0x80,0x07,0x40,0x00,0x40},/*"&",6*/
{0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x80,0x20,0x40,0x40,0x20},/*"(",8*/
{0x00,0x00,0x40,0x20,0x20,0x40,0x1F,0x80,0x00,0x00,0x00,0x00},/*")",9*/
{0x09,0x00,0x06,0x00,0x1F,0x80,0x06,0x00,0x09,0x00,0x00,0x00},/*"*",10*/
{0x04,0x00,0x04,0x00,0x3F,0x80,0x04,0x00,0x04,0x00,0x00,0x00},/*"+",11*/
{0x00,0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
{0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00},/*"-",13*/
{0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
{0x00,0x20,0x01,0xC0,0x06,0x00,0x38,0x00,0x40,0x00,0x00,0x00},/*"/",15*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"0",16*/
{0x00,0x00,0x10,0x40,0x3F,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"1",17*/
{0x18,0xC0,0x21,0x40,0x22,0x40,0x24,0x40,0x18,0x40,0x00,0x00},/*"2",18*/
{0x10,0x80,0x20,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"3",19*/
{0x02,0x00,0x0D,0x00,0x11,0x00,0x3F,0xC0,0x01,0x40,0x00,0x00},/*"4",20*/
{0x3C,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x23,0x80,0x00,0x00},/*"5",21*/
{0x1F,0x80,0x24,0x40,0x24,0x40,0x34,0x40,0x03,0x80,0x00,0x00},/*"6",22*/
{0x30,0x00,0x20,0x00,0x27,0xC0,0x38,0x00,0x20,0x00,0x00,0x00},/*"7",23*/
{0x1B,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"8",24*/
{0x1C,0x00,0x22,0xC0,0x22,0x40,0x22,0x40,0x1F,0x80,0x00,0x00},/*"9",25*/
{0x00,0x00,0x00,0x00,0x08,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
{0x00,0x00,0x00,0x00,0x04,0x60,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
{0x00,0x00,0x04,0x00,0x0A,0x00,0x11,0x00,0x20,0x80,0x40,0x40},/*"<",28*/
{0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x00,0x00},/*"=",29*/
{0x00,0x00,0x40,0x40,0x20,0x80,0x11,0x00,0x0A,0x00,0x04,0x00},/*">",30*/
{0x18,0x00,0x20,0x00,0x23,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"?",31*/
{0x1F,0x80,0x20,0x40,0x27,0x40,0x29,0x40,0x1F,0x40,0x00,0x00},/*"@",32*/
{0x00,0x40,0x07,0xC0,0x39,0x00,0x0F,0x00,0x01,0xC0,0x00,0x40},/*"A",33*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"B",34*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x30,0x80,0x00,0x00},/*"C",35*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"D",36*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x40,0x30,0xC0,0x00,0x00},/*"E",37*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x00,0x30,0x00,0x00,0x00},/*"F",38*/
{0x0F,0x00,0x10,0x80,0x20,0x40,0x22,0x40,0x33,0x80,0x02,0x00},/*"G",39*/
{0x20,0x40,0x3F,0xC0,0x04,0x00,0x04,0x00,0x3F,0xC0,0x20,0x40},/*"H",40*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x00,0x00},/*"I",41*/
{0x00,0x60,0x20,0x20,0x20,0x20,0x3F,0xC0,0x20,0x00,0x20,0x00},/*"J",42*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x0B,0x00,0x30,0xC0,0x20,0x40},/*"K",43*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x40,0x00,0x40,0x00,0xC0},/*"L",44*/
{0x3F,0xC0,0x3C,0x00,0x03,0xC0,0x3C,0x00,0x3F,0xC0,0x00,0x00},/*"M",45*/
{0x20,0x40,0x3F,0xC0,0x0C,0x40,0x23,0x00,0x3F,0xC0,0x20,0x00},/*"N",46*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"O",47*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"P",48*/
{0x1F,0x80,0x21,0x40,0x21,0x40,0x20,0xE0,0x1F,0xA0,0x00,0x00},/*"Q",49*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x26,0x00,0x19,0xC0,0x00,0x40},/*"R",50*/
{0x18,0xC0,0x24,0x40,0x24,0x40,0x22,0x40,0x31,0x80,0x00,0x00},/*"S",51*/
{0x30,0x00,0x20,0x40,0x3F,0xC0,0x20,0x40,0x30,0x00,0x00,0x00},/*"T",52*/
{0x20,0x00,0x3F,0x80,0x00,0x40,0x00,0x40,0x3F,0x80,0x20,0x00},/*"U",53*/
{0x20,0x00,0x3E,0x00,0x01,0xC0,0x07,0x00,0x38,0x00,0x20,0x00},/*"V",54*/
{0x38,0x00,0x07,0xC0,0x3C,0x00,0x07,0xC0,0x38,0x00,0x00,0x00},/*"W",55*/
{0x20,0x40,0x39,0xC0,0x06,0x00,0x39,0xC0,0x20,0x40,0x00,0x00},/*"X",56*/
{0x20,0x00,0x38,0x40,0x07,0xC0,0x38,0x40,0x20,0x00,0x00,0x00},/*"Y",57*/
{0x30,0x40,0x21,0xC0,0x26,0x40,0x38,0x40,0x20,0xC0,0x00,0x00},/*"Z",58*/
{0x00,0x00,0x00,0x00,0x7F,0xE0,0x40,0x20,0x40,0x20,0x00,0x00},/*"[",59*/
{0x00,0x00,0x70,0x00,0x0C,0x00,0x03,0x80,0x00,0x40,0x00,0x00},/*"\",60*/
{0x00,0x00,0x40,0x20,0x40,0x20,0x7F,0xE0,0x00,0x00,0x00,0x00},/*"]",61*/
{0x00,0x00,0x20,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00},/*"^",62*/
{0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10},/*"_",63*/
{0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
{0x00,0x00,0x02,0x80,0x05,0x40,0x05,0x40,0x03,0xC0,0x00,0x40},/*"a",65*/
{0x20,0x00,0x3F,0xC0,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"b",66*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x06,0x40,0x00,0x00},/*"c",67*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x24,0x40,0x3F,0xC0,0x00,0x40},/*"d",68*/
{0x00,0x00,0x03,0x80,0x05,0x40,0x05,0x40,0x03,0x40,0x00,0x00},/*"e",69*/
{0x00,0x00,0x04,0x40,0x1F,0xC0,0x24,0x40,0x24,0x40,0x20,0x00},/*"f",70*/
{0x00,0x00,0x02,0xE0,0x05,0x50,0x05,0x50,0x06,0x50,0x04,0x20},/*"g",71*/
{0x20,0x40,0x3F,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"h",72*/
{0x00,0x00,0x04,0x40,0x27,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"i",73*/
{0x00,0x10,0x00,0x10,0x04,0x10,0x27,0xE0,0x00,0x00,0x00,0x00},/*"j",74*/
{0x20,0x40,0x3F,0xC0,0x01,0x40,0x07,0x00,0x04,0xC0,0x04,0x40},/*"k",75*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x00,0x40,0x00,0x40,0x00,0x00},/*"l",76*/
{0x07,0xC0,0x04,0x00,0x07,0xC0,0x04,0x00,0x03,0xC0,0x00,0x00},/*"m",77*/
{0x04,0x40,0x07,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"n",78*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"o",79*/
{0x04,0x10,0x07,0xF0,0x04,0x50,0x04,0x40,0x03,0x80,0x00,0x00},/*"p",80*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x50,0x07,0xF0,0x00,0x10},/*"q",81*/
{0x04,0x40,0x07,0xC0,0x02,0x40,0x04,0x00,0x04,0x00,0x00,0x00},/*"r",82*/
{0x00,0x00,0x06,0x40,0x05,0x40,0x05,0x40,0x04,0xC0,0x00,0x00},/*"s",83*/
{0x00,0x00,0x04,0x00,0x1F,0x80,0x04,0x40,0x00,0x40,0x00,0x00},/*"t",84*/
{0x04,0x00,0x07,0x80,0x00,0x40,0x04,0x40,0x07,0xC0,0x00,0x40},/*"u",85*/
{0x04,0x00,0x07,0x00,0x04,0xC0,0x01,0x80,0x06,0x00,0x04,0x00},/*"v",86*/
{0x06,0x00,0x01,0xC0,0x07,0x00,0x01,0xC0,0x06,0x00,0x00,0x00},/*"w",87*/
{0x04,0x40,0x06,0xC0,0x01,0x00,0x06,0xC0,0x04,0x40,0x00,0x00},/*"x",88*/
{0x04,0x10,0x07,0x10,0x04,0xE0,0x01,0x80,0x06,0x00,0x04,0x00},/*"y",89*/
{0x00,0x00,0x04,0x40,0x05,0xC0,0x06,0x40,0x04,0x40,0x00,0x00},/*"z",90*/
{0x00,0x00,0x00,0x00,0x04,0x00,0x7B,0xE0,0x40,0x20,0x00,0x00},/*"{",91*/
{0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0x00,0x00},/*"|",92*/
{0x00,0x00,0x40,0x20,0x7B,0xE0,0x04,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
{0x40,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x20,0x00,0x40,0x00},/*"~",94*/
};
//16*16 ASCII字符集点阵
const unsigned char asc2_1608[95][16]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
{0x00,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x00,0x00},/*""",2*/
{0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x00,0x00},/*"#",3*/
{0x00,0x00,0x0E,0x18,0x11,0x04,0x3F,0xFF,0x10,0x84,0x0C,0x78,0x00,0x00,0x00,0x00},/*"$",4*/
{0x0F,0x00,0x10,0x84,0x0F,0x38,0x00,0xC0,0x07,0x78,0x18,0x84,0x00,0x78,0x00,0x00},/*"%",5*/
{0x00,0x78,0x0F,0x84,0x10,0xC4,0x11,0x24,0x0E,0x98,0x00,0xE4,0x00,0x84,0x00,0x08},/*"&",6*/
{0x08,0x00,0x68,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x18,0x18,0x20,0x04,0x40,0x02,0x00,0x00},/*"(",8*/
{0x00,0x00,0x40,0x02,0x20,0x04,0x18,0x18,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00},/*")",9*/
{0x02,0x40,0x02,0x40,0x01,0x80,0x0F,0xF0,0x01,0x80,0x02,0x40,0x02,0x40,0x00,0x00},/*"*",10*/
{0x00,0x80,0x00,0x80,0x00,0x80,0x0F,0xF8,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x00},/*"+",11*/
{0x00,0x01,0x00,0x0D,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80},/*"-",13*/
{0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
{0x00,0x00,0x00,0x06,0x00,0x18,0x00,0x60,0x01,0x80,0x06,0x00,0x18,0x00,0x20,0x00},/*"/",15*/
{0x00,0x00,0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"0",16*/
{0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"1",17*/
{0x00,0x00,0x0E,0x0C,0x10,0x14,0x10,0x24,0x10,0x44,0x11,0x84,0x0E,0x0C,0x00,0x00},/*"2",18*/
{0x00,0x00,0x0C,0x18,0x10,0x04,0x11,0x04,0x11,0x04,0x12,0x88,0x0C,0x70,0x00,0x00},/*"3",19*/
{0x00,0x00,0x00,0xE0,0x03,0x20,0x04,0x24,0x08,0x24,0x1F,0xFC,0x00,0x24,0x00,0x00},/*"4",20*/
{0x00,0x00,0x1F,0x98,0x10,0x84,0x11,0x04,0x11,0x04,0x10,0x88,0x10,0x70,0x00,0x00},/*"5",21*/
{0x00,0x00,0x07,0xF0,0x08,0x88,0x11,0x04,0x11,0x04,0x18,0x88,0x00,0x70,0x00,0x00},/*"6",22*/
{0x00,0x00,0x1C,0x00,0x10,0x00,0x10,0xFC,0x13,0x00,0x1C,0x00,0x10,0x00,0x00,0x00},/*"7",23*/
{0x00,0x00,0x0E,0x38,0x11,0x44,0x10,0x84,0x10,0x84,0x11,0x44,0x0E,0x38,0x00,0x00},/*"8",24*/
{0x00,0x00,0x07,0x00,0x08,0x8C,0x10,0x44,0x10,0x44,0x08,0x88,0x07,0xF0,0x00,0x00},/*"9",25*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x03,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
{0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
{0x00,0x00,0x00,0x80,0x01,0x40,0x02,0x20,0x04,0x10,0x08,0x08,0x10,0x04,0x00,0x00},/*"<",28*/
{0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x00,0x00},/*"=",29*/
{0x00,0x00,0x10,0x04,0x08,0x08,0x04,0x10,0x02,0x20,0x01,0x40,0x00,0x80,0x00,0x00},/*">",30*/
{0x00,0x00,0x0E,0x00,0x12,0x00,0x10,0x0C,0x10,0x6C,0x10,0x80,0x0F,0x00,0x00,0x00},/*"?",31*/
{0x03,0xE0,0x0C,0x18,0x13,0xE4,0x14,0x24,0x17,0xC4,0x08,0x28,0x07,0xD0,0x00,0x00},/*"@",32*/
{0x00,0x04,0x00,0x3C,0x03,0xC4,0x1C,0x40,0x07,0x40,0x00,0xE4,0x00,0x1C,0x00,0x04},/*"A",33*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x11,0x04,0x0E,0x88,0x00,0x70,0x00,0x00},/*"B",34*/
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x04,0x10,0x08,0x1C,0x10,0x00,0x00},/*"C",35*/
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"D",36*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x17,0xC4,0x10,0x04,0x08,0x18,0x00,0x00},/*"E",37*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x17,0xC0,0x10,0x00,0x08,0x00,0x00,0x00},/*"F",38*/
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x44,0x1C,0x78,0x00,0x40,0x00,0x00},/*"G",39*/
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x10,0x84,0x1F,0xFC,0x10,0x04},/*"H",40*/
{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x00,0x00,0x00,0x00},/*"I",41*/
{0x00,0x03,0x00,0x01,0x10,0x01,0x10,0x01,0x1F,0xFE,0x10,0x00,0x10,0x00,0x00,0x00},/*"J",42*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x03,0x80,0x14,0x64,0x18,0x1C,0x10,0x04,0x00,0x00},/*"K",43*/
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x0C,0x00,0x00},/*"L",44*/
{0x10,0x04,0x1F,0xFC,0x1F,0x00,0x00,0xFC,0x1F,0x00,0x1F,0xFC,0x10,0x04,0x00,0x00},/*"M",45*/
{0x10,0x04,0x1F,0xFC,0x0C,0x04,0x03,0x00,0x00,0xE0,0x10,0x18,0x1F,0xFC,0x10,0x00},/*"N",46*/
{0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"O",47*/
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80,0x0F,0x00,0x00,0x00},/*"P",48*/
{0x07,0xF0,0x08,0x18,0x10,0x24,0x10,0x24,0x10,0x1C,0x08,0x0A,0x07,0xF2,0x00,0x00},/*"Q",49*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x11,0xC0,0x11,0x30,0x0E,0x0C,0x00,0x04},/*"R",50*/
{0x00,0x00,0x0E,0x1C,0x11,0x04,0x10,0x84,0x10,0x84,0x10,0x44,0x1C,0x38,0x00,0x00},/*"S",51*/
{0x18,0x00,0x10,0x00,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x00,0x18,0x00,0x00,0x00},/*"T",52*/
{0x10,0x00,0x1F,0xF8,0x10,0x04,0x00,0x04,0x00,0x04,0x10,0x04,0x1F,0xF8,0x10,0x00},/*"U",53*/
{0x10,0x00,0x1E,0x00,0x11,0xE0,0x00,0x1C,0x00,0x70,0x13,0x80,0x1C,0x00,0x10,0x00},/*"V",54*/
{0x1F,0xC0,0x10,0x3C,0x00,0xE0,0x1F,0x00,0x00,0xE0,0x10,0x3C,0x1F,0xC0,0x00,0x00},/*"W",55*/
{0x10,0x04,0x18,0x0C,0x16,0x34,0x01,0xC0,0x01,0xC0,0x16,0x34,0x18,0x0C,0x10,0x04},/*"X",56*/
{0x10,0x00,0x1C,0x00,0x13,0x04,0x00,0xFC,0x13,0x04,0x1C,0x00,0x10,0x00,0x00,0x00},/*"Y",57*/
{0x08,0x04,0x10,0x1C,0x10,0x64,0x10,0x84,0x13,0x04,0x1C,0x04,0x10,0x18,0x00,0x00},/*"Z",58*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x00,0x00},/*"[",59*/
{0x00,0x00,0x30,0x00,0x0C,0x00,0x03,0x80,0x00,0x60,0x00,0x1C,0x00,0x03,0x00,0x00},/*"\",60*/
{0x00,0x00,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},/*"]",61*/
{0x00,0x00,0x00,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00},/*"^",62*/
{0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01},/*"_",63*/
{0x00,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
{0x00,0x00,0x00,0x98,0x01,0x24,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xFC,0x00,0x04},/*"a",65*/
{0x10,0x00,0x1F,0xFC,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"b",66*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x00},/*"c",67*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x11,0x08,0x1F,0xFC,0x00,0x04},/*"d",68*/
{0x00,0x00,0x00,0xF8,0x01,0x44,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xC8,0x00,0x00},/*"e",69*/
{0x00,0x00,0x01,0x04,0x01,0x04,0x0F,0xFC,0x11,0x04,0x11,0x04,0x11,0x00,0x18,0x00},/*"f",70*/
{0x00,0x00,0x00,0xD6,0x01,0x29,0x01,0x29,0x01,0x29,0x01,0xC9,0x01,0x06,0x00,0x00},/*"g",71*/
{0x10,0x04,0x1F,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"h",72*/
{0x00,0x00,0x01,0x04,0x19,0x04,0x19,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"i",73*/
{0x00,0x00,0x00,0x03,0x00,0x01,0x01,0x01,0x19,0x01,0x19,0xFE,0x00,0x00,0x00,0x00},/*"j",74*/
{0x10,0x04,0x1F,0xFC,0x00,0x24,0x00,0x40,0x01,0xB4,0x01,0x0C,0x01,0x04,0x00,0x00},/*"k",75*/
{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"l",76*/
{0x01,0x04,0x01,0xFC,0x01,0x04,0x01,0x00,0x01,0xFC,0x01,0x04,0x01,0x00,0x00,0xFC},/*"m",77*/
{0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"n",78*/
{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0xF8,0x00,0x00},/*"o",79*/
{0x01,0x01,0x01,0xFF,0x00,0x85,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"p",80*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x05,0x01,0xFF,0x00,0x01},/*"q",81*/
{0x01,0x04,0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x04,0x01,0x00,0x01,0x80,0x00,0x00},/*"r",82*/
{0x00,0x00,0x00,0xCC,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x98,0x00,0x00},/*"s",83*/
{0x00,0x00,0x01,0x00,0x01,0x00,0x07,0xF8,0x01,0x04,0x01,0x04,0x00,0x00,0x00,0x00},/*"t",84*/
{0x01,0x00,0x01,0xF8,0x00,0x04,0x00,0x04,0x00,0x04,0x01,0x08,0x01,0xFC,0x00,0x04},/*"u",85*/
{0x01,0x00,0x01,0x80,0x01,0x70,0x00,0x0C,0x00,0x10,0x01,0x60,0x01,0x80,0x01,0x00},/*"v",86*/
{0x01,0xF0,0x01,0x0C,0x00,0x30,0x01,0xC0,0x00,0x30,0x01,0x0C,0x01,0xF0,0x01,0x00},/*"w",87*/
{0x00,0x00,0x01,0x04,0x01,0x8C,0x00,0x74,0x01,0x70,0x01,0x8C,0x01,0x04,0x00,0x00},/*"x",88*/
{0x01,0x01,0x01,0x81,0x01,0x71,0x00,0x0E,0x00,0x18,0x01,0x60,0x01,0x80,0x01,0x00},/*"y",89*/
{0x00,0x00,0x01,0x84,0x01,0x0C,0x01,0x34,0x01,0x44,0x01,0x84,0x01,0x0C,0x00,0x00},/*"z",90*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x3E,0xFC,0x40,0x02,0x40,0x02},/*"{",91*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},/*"|",92*/
{0x00,0x00,0x40,0x02,0x40,0x02,0x3E,0xFC,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
{0x00,0x00,0x60,0x00,0x80,0x00,0x80,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x20,0x00},/*"~",94*/
};
#endif

View File

@ -0,0 +1,294 @@
#include <unistd.h>
#include "stdio.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include <hi_types_base.h>
#include <hi_i2c.h>
#include <hi_early_debug.h>
#include <hi_stdlib.h>
#include "ssd1306_oled.h"
#include "ssd1306_font.h"
u8 OLED_GRAM[144][8];
hi_u8 g_send_data[2] = { 0 };
hi_u32 my_i2c_write(hi_i2c_idx id, hi_u16 device_addr, hi_u32 send_len)
{
hi_u32 status;
hi_i2c_data es8311_i2c_data = { 0 };
es8311_i2c_data.send_buf = g_send_data;
es8311_i2c_data.send_len = send_len;
status = hi_i2c_write(id, device_addr, &es8311_i2c_data);
if (status != HI_ERR_SUCCESS) {
printf("===== Error: I2C write status = 0x%x! =====\r\n", status);
return status;
}
return HI_ERR_SUCCESS;
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
g_send_data[0] = 0x00;
g_send_data[1] = IIC_Command;
my_i2c_write(HI_I2C_IDX_0, 0x78, 2);
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
g_send_data[0] = 0x40;
g_send_data[1] = IIC_Data;
my_i2c_write(HI_I2C_IDX_0, 0x78, 2);
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd) {
Write_IIC_Data(dat);
}
else {
Write_IIC_Command(dat);
}
}
//反显函数
void OLED_ColorTurn(u8 i)
{
if(i==0) {
OLED_WR_Byte(0xA6,OLED_CMD);//正常显示
}
if(i==1) {
OLED_WR_Byte(0xA7,OLED_CMD);//反色显示
}
}
//屏幕旋转180度
void OLED_DisplayTurn(u8 i)
{
if(i==0) {
OLED_WR_Byte(0xC8,OLED_CMD);//正常显示
OLED_WR_Byte(0xA1,OLED_CMD);
}
if(i==1) {
OLED_WR_Byte(0xC0,OLED_CMD);//反转显示
OLED_WR_Byte(0xA0,OLED_CMD);
}
}
//开启OLED显示
void OLED_DisPlay_On(void)
{
OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
OLED_WR_Byte(0x14,OLED_CMD);//开启电荷泵
OLED_WR_Byte(0xAF,OLED_CMD);//点亮屏幕
}
//关闭OLED显示
void OLED_DisPlay_Off(void)
{
OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
OLED_WR_Byte(0x10,OLED_CMD);//关闭电荷泵
OLED_WR_Byte(0xAF,OLED_CMD);//关闭屏幕
}
//更新显存到OLED
void OLED_Refresh(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte(0xb0+i,OLED_CMD); //设置行起始地址
OLED_WR_Byte(0x00,OLED_CMD); //设置低列起始地址
OLED_WR_Byte(0x10,OLED_CMD); //设置高列起始地址
for(n=0;n<128;n++)
OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
}
}
//清屏函数
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
for(n=0;n<128;n++)
{
OLED_GRAM[n][i]=0;//清除所有数据
}
}
OLED_Refresh();//更新显示
}
//画点
//x:0~127
//y:0~63
void OLED_DrawPoint(u8 x,u8 y)
{
u8 i,m,n;
i=y/8;
m=y%8;
n=1<<m;
OLED_GRAM[x][i]|=n;
}
//清除一个点
//x:0~127
//y:0~63
void OLED_ClearPoint(u8 x,u8 y)
{
u8 i,m,n;
i=y/8;
m=y%8;
n=1<<m;
OLED_GRAM[x][i]=~OLED_GRAM[x][i];
OLED_GRAM[x][i]|=n;
OLED_GRAM[x][i]=~OLED_GRAM[x][i];
}
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//size:选择字体 12/16/24
//取模方式 逐列式
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1)
{
u8 i,m,temp,size2,chr1;
u8 y0=y;
size2=(size1/8+((size1%8)?1:0))*(size1/2); //得到字体一个字符对应点阵集所占的字节数
chr1=chr-' '; //计算偏移后的值
for(i=0;i<size2;i++)
{
//temp=asc2_1206[chr1][i];
if(size1==12)
{temp=asc2_1206[chr1][i];} //调用1206字体
else if(size1==16)
{temp=asc2_1608[chr1][i];} //调用1608字体
else return;
for(m=0;m<8;m++) //写入数据
{
if(temp&0x80)OLED_DrawPoint(x,y);
else OLED_ClearPoint(x,y);
temp<<=1;
y++;
if((y-y0)==size1)
{
y=y0;
x++;
break;
}
}
}
}
//显示字符串
//x,y:起点坐标
//size1:字体大小
//*chr:字符串起始地址
void OLED_ShowString(u8 x,u8 y,unsigned char *chr,u8 size1)
{
while((*chr>=' ')&&(*chr<='~'))//判断是不是非法字符!
{
OLED_ShowChar(x,y,*chr,size1);
x+=size1/2;
if(x>128-size1) //换行
{
x=0;
y+=2;
}
chr++;
}
}
//m^n
u32 OLED_Pow(u8 m,u8 n)
{
u32 result=1;
while(n--)
{
result*=m;
}
return result;
}
////显示2个数字
////x,y :起点坐标
////len :数字的位数
////size:字体大小
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1)
{
u8 t,temp;
for(t=0;t<len;t++)
{
temp=(num/OLED_Pow(10,len-t-1))%10;
if(temp==0)
{
OLED_ShowChar(x+(size1/2)*t,y,'0',size1);
}
else
{
OLED_ShowChar(x+(size1/2)*t,y,temp+'0',size1);
}
}
}
//配置写入数据的起始位置
void OLED_WR_BP(u8 x,u8 y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);//设置行起始地址
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
void oled_init(void)
{
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel #
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address #
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address #
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F) #
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register #
OLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current Brightness #
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常 #
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常 #
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64) #
OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty #
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F) #
OLED_WR_Byte(0x00,OLED_CMD);//-not offset #
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency #
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec #
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period #
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock #
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration #
OLED_WR_Byte(0x12,OLED_CMD); // #
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh #
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level #
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02) #
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable #
OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable #
OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5) #
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7) #
OLED_WR_Byte(0xAF,OLED_CMD);// #
OLED_Clear();
}

View File

@ -0,0 +1,39 @@
#ifndef __SSD1306_OLED_H__
#define __SSD1306_OLED_H__
#define __OLED_H
#define OLED_MODE 0
#define SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#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
void oled_init(void);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1);
void OLED_Clear(void);
void OLED_ShowString(u8 x,u8 y,unsigned char *chr,u8 size1);
void OLED_Refresh(void);
//反显函数
void OLED_ColorTurn(u8 i);
//屏幕旋转180度
void OLED_DisplayTurn(u8 i);
//开启OLED显示
void OLED_DisPlay_On(void);
#endif