tasks.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. bool switchPushed = getSwitchState();
  67. if (switchPushed) {
  68. /* Switch was on when device power on. Start WiFi & Web Server */
  69. //Set display to AP icon
  70. setAnimationCode('w');
  71. //Start AP and web server
  72. WiFi.softAP(AP_SSID, NULL);
  73. Serial.print("Manual mode started. SSID=" + String(AP_SSID) + " listening on : ");
  74. Serial.println(WiFi.softAPIP());
  75. registerAPIEndpoints();
  76. //Setup DNS Server
  77. dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  78. dnsServer.start(53, "*", WiFi.softAPIP());
  79. // Start the server
  80. server.begin();
  81. for (;;) {
  82. dnsServer.processNextRequest();
  83. server.handleClient();
  84. }
  85. } else {
  86. /* Switch is off during power on. Use automatic mode */
  87. int seqCounter = 0; //Modify this value to change start state of seq
  88. for (;;) {
  89. switchPushed = getSwitchState();
  90. if (switchPushed) {
  91. //Switch pushed
  92. executePushAnimationSequence(seqCounter);
  93. seqCounter++;
  94. } else {
  95. //Prevent crashing due to infinite loop
  96. delay(100);
  97. }
  98. }
  99. }
  100. }
  101. //Core 1 code, for animation rendering
  102. void AnimationController( void * pvParameters ) {
  103. Serial.println("Animation render started on core " + String(xPortGetCoreID()));
  104. for (;;) {
  105. char anicode = getAnimationCode();
  106. handleAnimationRendering(anicode);
  107. }
  108. }