123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /* Multi-core Task Handler*/
- TaskHandle_t primaryTask;
- TaskHandle_t animationTask;
- SemaphoreHandle_t animationMutex;
- /* Semaphore for handling mutex */
- void createSemaphore() {
- animationMutex = xSemaphoreCreateMutex();
- xSemaphoreGive(animationMutex);
- }
- void mutexLock() {
- xSemaphoreTake(animationMutex, portMAX_DELAY);
- }
- void mutexUnlock() {
- xSemaphoreGive(animationMutex);
- }
- //Set the animation code, suppose to be used in core 0 only
- void setAnimationCode(char filename) {
- Serial.println("Updating animation frame to " + String(filename));
- mutexLock();
- animation = filename;
- mutexUnlock();
- }
- //Get the animation code, suppose to be used in core 1 only
- char getAnimationCode() {
- char anicode = 'a';
- mutexLock();
- anicode = animation;
- mutexUnlock();
- return anicode;
- }
- //Get the current state of the switch
- // true = After human pushed
- // false = After robot pushed
- bool getSwitchState() {
- int switchState = digitalRead(TOGGLE_SWITCH);
- return (switchState == 1);
- }
- /* Multi-core process definations */
- void startCoreTasks() {
- //core 1
- xTaskCreatePinnedToCore(
- AnimationController, /* Task function. */
- "animator", /* name of task. */
- 10000, /* Stack size of task */
- NULL, /* parameter of the task */
- 1, /* priority of the task */
- &animationTask, /* Task handle to keep track of created task */
- 1
- );
- delay(500);
- //core 0
- xTaskCreatePinnedToCore(
- PrimaryController, /* Task function. */
- "primary", /* name of task. */
- 10000, /* Stack size of task */
- NULL, /* parameter of the task */
- 1, /* priority of the task */
- &primaryTask, /* Task handle to keep track of created task */
- 0
- ); /* pin task to core 0 */
- }
- //Core 0 code, for movement and primary logics
- void PrimaryController( void * pvParameters ) {
- Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
- clearFrame();
- int seqCounter = 0; //Modify this value to change start state of seq
-
- for (;;) {
- runDebugSequence();
- Serial.println("Sequence ended, restart in 3 sec");
- delay(3000);
- }
- /*
- for (;;) {
- bool switchPushed = getSwitchState();
- if (switchPushed){
- //Switch pushed
- executePushAnimationSequence(seqCounter);
- seqCounter++;
- }else{
- delay(100);
- }
- }
- */
- }
- //Core 1 code, for animation rendering
- void AnimationController( void * pvParameters ) {
- Serial.println("Animation render started on core " + String(xPortGetCoreID()));
- for (;;) {
- if (SD_exists){
- char anicode = getAnimationCode();
- handleAnimationRendering(anicode);
- }else{
- //SD card not exists. Lets this core idle
- delay(1000);
- }
-
- }
- }
|