Created
December 8, 2021 09:47
-
-
Save greggjaskiewicz/f6e68bafc3e1d0bb7ec6a4318fe691ab to your computer and use it in GitHub Desktop.
ESp32 FreeRTOS blinker, v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const byte ledPins[] = { 15, 2, 0, 4, 5, 18, 19, 21, 22, 23}; // define led pins | |
const byte chns[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // define the pwm channels | |
const int dutys[] = { | |
1023, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
}; //define the pwm dutys | |
const int delayTimes = 40; //flowing speed ,the smaller, the faster | |
struct LEDTaskDescription { | |
int channel1; | |
int channel2; | |
int dutyIndex; | |
int direction; | |
}; | |
void ledTask(void *ledTaskDescription) { | |
LEDTaskDescription *description = (LEDTaskDescription *)ledTaskDescription; | |
const int maxSize = (sizeof(dutys)/sizeof(typeof(dutys[0]))); | |
for(;;) { | |
int channel = 0; | |
if (description->direction == 1) { | |
channel = description->channel1; | |
} else { | |
channel = description->channel2; | |
} | |
int duty = dutys[description->dutyIndex]; | |
ledcWrite(channel, duty); | |
// description->dutyIndex += description->direction; | |
description->dutyIndex++; | |
if (description->dutyIndex >= maxSize) { | |
// description->dutyIndex = maxSize-1; | |
description->dutyIndex = 0; | |
description->direction *= -1; | |
// } else if (description->dutyIndex <= 0) { | |
// description->dutyIndex = 0; | |
// description->direction = 1; | |
} | |
vTaskDelay(delayTimes/portTICK_PERIOD_MS); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
int ledCounts = sizeof(ledPins); //get the led counts | |
for (int i = 0; i < ledCounts; i++) { //setup the pwm channels | |
ledcSetup(chns[i], 1000, 10); | |
ledcAttachPin(ledPins[i], chns[i]); | |
BaseType_t xReturned; | |
LEDTaskDescription *desc = new LEDTaskDescription; | |
desc->channel1 = chns[i]; | |
desc->channel2 = chns[ledCounts-i-1]; | |
desc->dutyIndex = i; | |
desc->direction = 1; | |
char name[1024] = ""; | |
sprintf(name, "blink %d", i); | |
xReturned = xTaskCreate( | |
ledTask, | |
name, | |
768, | |
( void * )desc, | |
2, | |
NULL ); | |
if( xReturned != pdPASS ) | |
{ | |
Serial.print("task create fail, error: "); | |
Serial.println(xReturned); | |
while(1){}; | |
} | |
Serial.print("task created: "); | |
Serial.println(name); | |
} | |
Serial.print("setup() is running on core "); | |
Serial.println(xPortGetCoreID()); | |
} | |
void loop() { | |
yield(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment