tasks.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //Get the current state of the switch
  32. // true = After human pushed
  33. // false = After robot pushed
  34. bool getSwitchState() {
  35. int switchState = digitalRead(TOGGLE_SWITCH);
  36. return (switchState == 1);
  37. }
  38. /* Multi-core process definations */
  39. void startCoreTasks() {
  40. //core 1
  41. xTaskCreatePinnedToCore(
  42. AnimationController, /* Task function. */
  43. "animator", /* name of task. */
  44. 10000, /* Stack size of task */
  45. NULL, /* parameter of the task */
  46. 1, /* priority of the task */
  47. &animationTask, /* Task handle to keep track of created task */
  48. 1
  49. );
  50. delay(500);
  51. //core 0
  52. xTaskCreatePinnedToCore(
  53. PrimaryController, /* Task function. */
  54. "primary", /* name of task. */
  55. 10000, /* Stack size of task */
  56. NULL, /* parameter of the task */
  57. 1, /* priority of the task */
  58. &primaryTask, /* Task handle to keep track of created task */
  59. 0
  60. ); /* pin task to core 0 */
  61. }
  62. //Core 0 code, for movement and primary logics
  63. void PrimaryController( void * pvParameters ) {
  64. Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
  65. clearFrame();
  66. int seqCounter = 0; //Modify this value to change start state of seq
  67. for (;;) {
  68. runDebugSequence();
  69. Serial.println("Sequence ended, restart in 3 sec");
  70. delay(3000);
  71. }
  72. /*
  73. for (;;) {
  74. bool switchPushed = getSwitchState();
  75. if (switchPushed){
  76. //Switch pushed
  77. executePushAnimationSequence(seqCounter);
  78. seqCounter++;
  79. }else{
  80. delay(100);
  81. }
  82. }
  83. */
  84. }
  85. //Core 1 code, for animation rendering
  86. void AnimationController( void * pvParameters ) {
  87. Serial.println("Animation render started on core " + String(xPortGetCoreID()));
  88. for (;;) {
  89. if (SD_exists){
  90. char anicode = getAnimationCode();
  91. handleAnimationRendering(anicode);
  92. }else{
  93. //SD card not exists. Lets this core idle
  94. delay(1000);
  95. }
  96. }
  97. }