tasks.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Multi-core Task Handler*/
  2. TaskHandle_t primaryTask;
  3. TaskHandle_t animationTask;
  4. SemaphoreHandle_t animationMutex;
  5. /* Semaphore for handling mutex */
  6. void createSemaphore() {
  7. animationMutex = xSemaphoreCreateMutex();
  8. xSemaphoreGive(animationMutex);
  9. }
  10. void mutexLock() {
  11. xSemaphoreTake(animationMutex, portMAX_DELAY);
  12. }
  13. void mutexUnlock() {
  14. xSemaphoreGive(animationMutex);
  15. }
  16. //Set the animation code, suppose to be used in core 0 only
  17. void setAnimationCode(char filename) {
  18. Serial.println("Updating animation frame to " + String(filename));
  19. mutexLock();
  20. animation = filename;
  21. mutexUnlock();
  22. }
  23. //Get the animation code, suppose to be used in core 1 only
  24. char getAnimationCode() {
  25. char anicode = 'a';
  26. mutexLock();
  27. anicode = animation;
  28. mutexUnlock();
  29. return anicode;
  30. }
  31. /* Multi-core process definations */
  32. void startCoreTasks() {
  33. //core 1
  34. xTaskCreatePinnedToCore(
  35. AnimationController, /* Task function. */
  36. "animator", /* name of task. */
  37. 10000, /* Stack size of task */
  38. NULL, /* parameter of the task */
  39. 1, /* priority of the task */
  40. &animationTask, /* Task handle to keep track of created task */
  41. 1
  42. );
  43. delay(500);
  44. //core 0
  45. xTaskCreatePinnedToCore(
  46. PrimaryController, /* Task function. */
  47. "primary", /* name of task. */
  48. 10000, /* Stack size of task */
  49. NULL, /* parameter of the task */
  50. 1, /* priority of the task */
  51. &primaryTask, /* Task handle to keep track of created task */
  52. 0
  53. ); /* pin task to core 0 */
  54. }
  55. //Core 0 code, for movement and primary logics
  56. void PrimaryController( void * pvParameters ) {
  57. Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
  58. for (;;) {
  59. //Loop of primary logics
  60. setAnimationCode('a');
  61. delay(30000);
  62. //setAnimationCode('c');
  63. //delay(5000);
  64. setAnimationCode('j');
  65. delay(30000);
  66. //Stepper test
  67. /*
  68. forward(50);
  69. delay(3000);
  70. backward(50);
  71. delay(3000);
  72. rotateAntiClockwise(100);
  73. delay(1000);
  74. //blink
  75. loadAndRender("/b.bin");
  76. delay(300);
  77. loadAndRender("/a.bin");
  78. delay(2000);
  79. //rotate backward
  80. rotateClockwise(100);
  81. delay(3000);
  82. //Servo test
  83. servoCoverPusher.write(90);
  84. delay(1000);
  85. servoSwitchPusher.write(180);
  86. delay(1000);
  87. servoCoverPusher.write(0);
  88. servoSwitchPusher.write(0);
  89. delay(3000);
  90. */
  91. }
  92. }
  93. //Core 1 code, for animation rendering
  94. void AnimationController( void * pvParameters ) {
  95. Serial.println("Animation render started on core " + String(xPortGetCoreID()));
  96. for (;;) {
  97. char anicode = getAnimationCode();
  98. handleAnimationRendering(anicode);
  99. }
  100. }