tasks.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. xTaskCreatePinnedToCore(
  41. AnimationController, /* Task function. */
  42. "animator", /* name of task. */
  43. 8192, /* Stack size of task */
  44. NULL, /* parameter of the task */
  45. 2, /* priority of the task */
  46. &animationTask,
  47. 0
  48. );
  49. xTaskCreatePinnedToCore(
  50. PrimaryController, /* Task function. */
  51. "primary", /* name of task. */
  52. 24576, /* Stack size of task */
  53. NULL, /* parameter of the task */
  54. 1, /* priority of the task */
  55. &primaryTask, /* Task handle to keep track of created task */
  56. 1
  57. );
  58. vTaskStartScheduler();
  59. }
  60. //For movement and primary logics
  61. void PrimaryController( void * pvParameters ) {
  62. Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
  63. clearFrame();
  64. bool switchPushed = getSwitchState();
  65. if (switchPushed || ENABLE_WIFI_DEBUG) {
  66. /* Switch was on when device power on. Start WiFi & Web Server */
  67. //Set display to AP icon
  68. setAnimationCode('w');
  69. //Start AP and web server
  70. if (ENABLE_WIFI_DEBUG) {
  71. //Use WiFi client mode
  72. WiFi.mode(WIFI_STA); //Optional
  73. WiFi.begin(DEBUG_SSID, DEBUG_PWD);
  74. Serial.println("\nConnecting");
  75. while (WiFi.status() != WL_CONNECTED) {
  76. Serial.print(".");
  77. delay(100);
  78. }
  79. Serial.println("\nConnected to the WiFi network");
  80. Serial.print("Local IP: ");
  81. Serial.println(WiFi.localIP());
  82. } else {
  83. WiFi.softAP(AP_SSID, NULL);
  84. Serial.print("Manual mode started. SSID=" + String(AP_SSID) + " listening on : ");
  85. Serial.println(WiFi.softAPIP());
  86. //Setup DNS Server
  87. dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  88. dnsServer.start(53, "*", WiFi.softAPIP());
  89. }
  90. // Start the web server
  91. registerAPIEndpoints();
  92. server.begin();
  93. while(1) {
  94. dnsServer.processNextRequest();
  95. delay(1);
  96. }
  97. } else {
  98. /* Switch is off during power on. Use automatic mode */
  99. int seqCounter = 0; //Modify this value to change start state of seq
  100. while(1) {
  101. switchPushed = getSwitchState();
  102. if (switchPushed) {
  103. //Switch pushed
  104. executePushAnimationSequence(seqCounter);
  105. seqCounter++;
  106. }
  107. delay(1);
  108. }
  109. }
  110. }
  111. //For animation rendering
  112. void AnimationController( void * pvParameters ) {
  113. Serial.println("Animation render started on core " + String(xPortGetCoreID()));
  114. while(1) {
  115. char anicode = getAnimationCode();
  116. handleAnimationRendering(anicode);
  117. delay(1);
  118. }
  119. }