• Home
Name Date Size #Lines LOC

..--

BUILD.gnD12-May-2024771 2219

README.mdD12-May-20244.4 KiB12489

timer.cD12-May-20243.2 KiB10559

README.md

1# HiSpark WiFi-IoT 套件样例开发--软定时器(Timer)
2
3![hihope_illustration](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/hihope_illustration.png)
4
5[HiSpark WiFi-IoT开发套件](https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w5003-23341819265.1.bf644a82Da9PZK&id=622343426064&scene=taobao_shop) 首发于HDC 2020,是首批支持OpenHarmony 2.0的开发套件,亦是官方推荐套件,由润和软件HiHope量身打造,已在OpenHarmony社区和广大OpenHarmony开发者中得到广泛应用。
6
7![wifi_iot](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/2.png)
8
9## 一、Timer API
10
11| API名称          | 说明                     |
12| ---------------- | ------------------------ |
13| osTimerNew       | 创建和初始化定时器       |
14| osTimerGetName   | 获取指定的定时器名字     |
15| osTimerStart     | 启动或者重启指定的定时器 |
16| osTimerStop      | 停止指定的定时器         |
17| osTimerIsRunning | 检查一个定时器是否在运行 |
18| osTimerDelete    | 删除定时器               |
19
20### osTimerNew()
21
22```c
23osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
24```
25
26**参数:**
27
28| 名字     | 描述                                                         |
29| :------- | :----------------------------------------------------------- |
30| func     | 定时器回调函数.                                              |
31| type     | 定时器类型,osTimerOnce表示单次定时器,ostimer周期表示周期性定时器. |
32| argument | 定时器回调函数的参数                                         |
33| attr     | 定时器属性                                                   |
34
35## 二、代码分析
36
37定时器的回调函数
38
39```c
40void cb_timeout_periodic(void *arg) {
41    (void)arg;
42    times++;
43}
44```
45
46使用osTimerNew创建一个100个时钟周期调用一次回调函数cb_timeout_periodic定时器,每隔100个时钟周期检查一下全局变量times是否小于3,若不小于3则停止时钟周期
47
48```c
49void timer_periodic(void) {
50    osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
51    if (periodic_tid == NULL) {
52        printf("[Timer Test] osTimerNew(periodic timer) failed.\r\n");
53        return;
54    } else {
55        printf("[Timer Test] osTimerNew(periodic timer) success, tid: %p.\r\n",periodic_tid);
56    }
57    osStatus_t status = osTimerStart(periodic_tid, 100);
58    if (status != osOK) {
59        printf("[Timer Test] osTimerStart(periodic timer) failed.\r\n");
60        return;
61    } else {
62        printf("[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.\r\n");
63    }
64
65    while(times < 3) {
66        printf("[Timer Test] times:%d.\r\n",times);
67        osDelay(100);
68    }
69
70    status = osTimerStop(periodic_tid);
71    printf("[Timer Test] stop periodic timer, status :%d.\r\n", status);
72    status = osTimerDelete(periodic_tid);
73    printf("[Timer Test] kill periodic timer, status :%d.\r\n", status);
74}
75
76```
77
78
79
80## 三、如何编译
81
821. 将此目录下的 `timer.c` 和 `BUILD.gn` 复制到openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下,
832. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
84
85```
86    features = [
87        "iothardware:timer_demo",
88    ]
89```
90
913. 在openharmony源码顶层目录执行:`python build.py wifiiot`
92
93## 四、运行结果
94
95```
96[Timer Test] osTimerNew(periodic timer) success, tid: 0xe9b4c.
97[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.
98[Timer Test] times:0.
99[Timer Test] times:1.
100[Timer Test] times:2.
101[Timer Test] stop periodic timer, status :0.
102[Timer Test] kill periodic timer, status :0.
103```
104
105### 【套件支持】
106
107##### 1. 套件介绍  http://www.hihope.org/pro/pro1.aspx?mtt=8
108
109##### 2. 套件购买  https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop
110
111##### 3. 技术资料
112
113- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg**
114
115- HiHope官网-资源中心(SDK包、技术文档下载)[**www.hihope.org**](http://www.hihope.org/)
116
117##### 4. 互动交流
118
119- 润和HiHope技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方群#,予以邀请入群)
120- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429**
121- 润和HiHope售后服务群(QQ:980599547)
122- 售后服务电话(025-52668590)
123
124