IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> WiFi-IoT 鸿蒙开发套件样例开发 -> 正文阅读

[嵌入式]WiFi-IoT 鸿蒙开发套件样例开发

HiSpark WiFi-IoT鸿蒙开发套件?首发于HDC 2020,是首批支持HarmonyOS?2.0的开发套件,亦是鸿蒙官方推荐套件,由润和软件HiHope量身打造,已在鸿蒙社区和广大鸿蒙开发者中得到广泛应用。

一、Thread API

osThreadNew()
osThreadId_t osThreadNew(osThreadFunc_t? ? ? ? func, void *argument,const osThreadAttr_t *attr )? ? ? ? 复制


注意 :不能在中断服务调用该函数
参数

osThreadTerminate()
osStatus_t osThreadTerminate (osThreadId_t thread_id)复制



二、代码分析
创建线程,创建成功则打印线程名字和线程ID
osThreadId_t newThread(char *name, osThreadFunc_t func, void *arg) {? ? osThreadAttr_t attr = {? ?? ???name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0? ? };? ? osThreadId_t?tid = osThreadNew(func, arg, &attr);? ? if (tid == NULL) {? ?? ???printf("osThreadNew(%s) faiLED.\r\n", name);? ? } else {? ?? ???printf("osThreadNew(%s) success, thread id: %d.\r\n", name, tid);? ? }? ? return tid;}复制


该函数首先会打印自己的参数,然后对全局变量count进行循环+1操作,之后会打印count的值
void threadTest(void *arg) {? ? static int count = 0;? ? printf("%s\r\n",(char *)arg);? ? osThreadId_t tid = osThreadGetId();? ? printf("threadTest osThreadGetId, thread id:%p\r\n", tid);? ? while (1) {? ?? ???count++;? ?? ???printf("threadTest, count: %d.\r\n", count);? ?? ???osDelay(20);? ? }}复制


主程序rtosv2_thread_main创建线程并运行,并使用上述API进行相关操作,最后终止所创建的线程。
void rtosv2_thread_main(void *arg) {? ? (void)arg;? ? osThreadId_t tid=newThread("test_thread", threadTest, "This is a test thread.");? ? const char *t_name = osThreadGetName(tid);? ? printf("[Thread Test]osThreadGetName, thread name: %s.\r\n", t_name);? ? osThreadState_t state = osThreadGetState(tid);? ? printf("[Thread Test]osThreadGetState, state :%d.\r\n", state);? ? osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4);? ? printf("[Thread Test]osThreadSetPriority, status: %d.\r\n", status);? ? osPriority_t pri = osThreadGetPriority (tid);? ?? ? printf("[Thread Test]osThreadGetPriority, priority: %d.\r\n", pri);? ? status = osThreadSuspend(tid);? ? printf("[Thread Test]osThreadSuspend, status: %d.\r\n", status);? ?? ?status = osThreadResume(tid);? ? printf("[Thread Test]osThreadResume, status: %d.\r\n", status);? ? uint32_t stacksize = osThreadGetStackSize(tid);? ? printf("[Thread Test]osThreadGetStackSize, stacksize: %d.\r\n", stacksize);? ? uint32_t stackspace = osThreadGetStackSpace(tid);? ? printf("[Thread Test]osThreadGetStackSpace, stackspace: %d.\r\n", stackspace);? ? uint32_t t_count = osThreadGetCount();? ? printf("[Thread Test]osThreadGetCount, count: %d.\r\n", t_count);? ?? ?osDelay(100);? ? status = osThreadTerminate(tid);? ? printf("[Thread Test]osThreadTerminate, status: %d.\r\n", status);}


三、如何编译
将此目录下的 thread.c 和 BUILD.gn 复制到openharmony源码的applications\sample\wifi-iot\app\iothardware目录下,
修改openharmony源码的applications\sample\wifi-iot\app\BUILD.gn文件,将其中的 features 改为:
? ? features = [? ?? ???"iothardware:thread_demo",? ? ]复制


3.在openharmony源码顶层目录执行:python build.py wifiiot
四、运行结果
[Thread Test] osThreadNew(test_thread) success.[Thread Test] osThreadGetName, thread name: test_thread.[Thread Test] osThreadGetState, state :1.[Thread Test] This is a test thread.??<-testThread log[Thread Test] threadTest osThreadGetId, thread id:0xe8544[Thread Test] threadTest, count: 1.? ?<-testThread log[Thread Test] osThreadSetPriority, status: 0.[Thread Test] osThreadGetPriority, priority: 28.[Thread Test] osThreadSuspend, status: 0.[Thread Test] osThreadResume, status: 0.[Thread Test] osThreadGetStackSize, stacksize: 2048.[Thread Test] osThreadGetStackSpace, stackspace: 1144.[Thread Test] osThreadGetCount, count: 12.[Thread Test] threadTest, count: 2.? ?<-testThread log[Thread Test] threadTest, count: 3.? ?<-testThread log[Thread Test] threadTest, count: 4.? ?<-testThread log[Thread Test] threadTest, count: 5.? ?<-testThread log[Thread Test] threadTest, count: 6.? ?<-testThread log[Thread Test] osThreadTerminate, status: 0.复制


?

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2022-06-29 19:16:14  更:2022-06-29 19:16:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 7:54:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码