[文章]WiFi-IoT 鸿蒙开发套件样例开发

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

一、Thread API

osThreadNew()
  1. osThreadId_t osThreadNew(osThreadFunc_t        func, void *argument,const osThreadAttr_t *attr )        
复制代码

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

  1. osStatus_t osThreadTerminate (osThreadId_t thread_id)
复制代码



二、代码分析
创建线程,创建成功则打印线程名字和线程ID
  1. osThreadId_t newThread(char *name, osThreadFunc_t func, void *arg) {
  2.     osThreadAttr_t attr = {
  3.         name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
  4.     };
  5.     osThreadId_t tid = osThreadNew(func, arg, &attr);
  6.     if (tid == NULL) {
  7.         printf("osThreadNew(%s) failed.
  8. ", name);
  9.     } else {
  10.         printf("osThreadNew(%s) success, thread id: %d.
  11. ", name, tid);
  12.     }
  13.     return tid;
  14. }
复制代码

该函数首先会打印自己的参数,然后对全局变量count进行循环+1操作,之后会打印count的值
  1. void threadTest(void *arg) {
  2.     static int count = 0;
  3.     printf("%s
  4. ",(char *)arg);
  5.     osThreadId_t tid = osThreadGetId();
  6.     printf("threadTest osThreadGetId, thread id:%p
  7. ", tid);
  8.     while (1) {
  9.         count++;
  10.         printf("threadTest, count: %d.
  11. ", count);
  12.         osDelay(20);
  13.     }
  14. }
复制代码

主程序rtosv2_thread_main创建线程并运行,并使用上述API进行相关操作,最后终止所创建的线程。
  1. void rtosv2_thread_main(void *arg) {
  2.     (void)arg;
  3.     osThreadId_t tid=newThread("test_thread", threadTest, "This is a test thread.");

  4.     const char *t_name = osThreadGetName(tid);
  5.     printf("[Thread Test]osThreadGetName, thread name: %s.
  6. ", t_name);

  7.     osThreadState_t state = osThreadGetState(tid);
  8.     printf("[Thread Test]osThreadGetState, state :%d.
  9. ", state);

  10.     osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4);
  11.     printf("[Thread Test]osThreadSetPriority, status: %d.
  12. ", status);

  13.     osPriority_t pri = osThreadGetPriority (tid);   
  14.     printf("[Thread Test]osThreadGetPriority, priority: %d.
  15. ", pri);

  16.     status = osThreadSuspend(tid);
  17.     printf("[Thread Test]osThreadSuspend, status: %d.
  18. ", status);  

  19.     status = osThreadResume(tid);
  20.     printf("[Thread Test]osThreadResume, status: %d.
  21. ", status);

  22.     uint32_t stacksize = osThreadGetStackSize(tid);
  23.     printf("[Thread Test]osThreadGetStackSize, stacksize: %d.
  24. ", stacksize);

  25.     uint32_t stackspace = osThreadGetStackSpace(tid);
  26.     printf("[Thread Test]osThreadGetStackSpace, stackspace: %d.
  27. ", stackspace);

  28.     uint32_t t_count = osThreadGetCount();
  29.     printf("[Thread Test]osThreadGetCount, count: %d.
  30. ", t_count);  

  31.     osDelay(100);
  32.     status = osThreadTerminate(tid);
  33.     printf("[Thread Test]osThreadTerminate, status: %d.
  34. ", status);
  35. }
复制代码

三、如何编译
将此目录下的 thread.c 和 BUILD.gn 复制到openharmony源码的applicationssamplewifi-iotappiothardware目录下,修改openharmony源码的applicationssamplewifi-iotappBUILD.gn文件,将其中的 features 改为:
  1.     features = [
  2.         "iothardware:thread_demo",
  3.     ]
复制代码

3.在openharmony源码顶层目录执行:python build.py wifiiot

四、运行结果
  1. [Thread Test] osThreadNew(test_thread) success.
  2. [Thread Test] osThreadGetName, thread name: test_thread.
  3. [Thread Test] osThreadGetState, state :1.
  4. [Thread Test] This is a test thread.  <-testThread log
  5. [Thread Test] threadTest osThreadGetId, thread id:0xe8544
  6. [Thread Test] threadTest, count: 1.   <-testThread log
  7. [Thread Test] osThreadSetPriority, status: 0.
  8. [Thread Test] osThreadGetPriority, priority: 28.
  9. [Thread Test] osThreadSuspend, status: 0.
  10. [Thread Test] osThreadResume, status: 0.
  11. [Thread Test] osThreadGetStackSize, stacksize: 2048.
  12. [Thread Test] osThreadGetStackSpace, stackspace: 1144.
  13. [Thread Test] osThreadGetCount, count: 12.
  14. [Thread Test] threadTest, count: 2.   <-testThread log
  15. [Thread Test] threadTest, count: 3.   <-testThread log
  16. [Thread Test] threadTest, count: 4.   <-testThread log
  17. [Thread Test] threadTest, count: 5.   <-testThread log
  18. [Thread Test] threadTest, count: 6.   <-testThread log
  19. [Thread Test] osThreadTerminate, status: 0.
复制代码

【套件支持】
HiHope官网-资源中心(SDK包、技术文档下载)www.hihope.org

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友