MultiTask.ino 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: MultiTask. 多线程
  10. * Date: 2021/7/26
  11. *******************************************************************************
  12. */
  13. #include <M5Stack.h>
  14. void task1(void* pvParameters) { // Define the tasks to be executed in
  15. // thread 1. 定义线程1内要执行的任务
  16. while (1) { // Keep the thread running. 使线程一直运行
  17. Serial.print("task1 Uptime (ms): ");
  18. Serial.println(millis()); // The running time of the serial port
  19. // printing program. 串口打印程序运行的时间
  20. delay(
  21. 100); // With a delay of 100ms, it can be seen in the serial
  22. // monitor that every 100ms, thread 1 will be executed once.
  23. // 延迟100ms,在串口监视器内可看到每隔100ms,线程1就会被执行一次
  24. }
  25. }
  26. void task2(void* pvParameters) {
  27. while (1) {
  28. Serial.print("task2 Uptime (ms): ");
  29. Serial.println(millis());
  30. delay(200);
  31. }
  32. }
  33. void task3(void* pvParameters) {
  34. while (1) {
  35. Serial.print("task3 Uptime (ms): ");
  36. Serial.println(millis());
  37. delay(1000);
  38. }
  39. }
  40. void setup() {
  41. M5.begin(); // Init M5Stack.
  42. // 初始化M5Stack(串口初始化也包含在内,波特率为115200)
  43. M5.Power.begin(); // Init power 初始化电源模块
  44. // Creat Task1. 创建线程1
  45. xTaskCreatePinnedToCore(
  46. task1, // Function to implement the task.
  47. // 线程对应函数名称(不能有返回值)
  48. "task1", //线程名称
  49. 4096, // The size of the task stack specified as the number of *
  50. // bytes.任务堆栈的大小(字节)
  51. NULL, // Pointer that will be used as the parameter for the task *
  52. // being created. 创建作为任务输入参数的指针
  53. 1, // Priority of the task. 任务的优先级
  54. NULL, // Task handler. 任务句柄
  55. 0); // Core where the task should run. 将任务挂载到指定内核
  56. // Task 2
  57. xTaskCreatePinnedToCore(task2, "task2", 4096, NULL, 2, NULL, 0);
  58. // Task 3
  59. xTaskCreatePinnedToCore(task3, "task3", 4096, NULL, 3, NULL, 0);
  60. }
  61. void loop() {}