Chewpacker

Simple Multi-Thread Test

Jan 11th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <FastLED.h>
  3.  
  4. #define NUM_LEDS 300
  5. #define DATA_PIN 5
  6. #define LED_TYPE WS2812B
  7. #define COLOUR_ORDER GRB
  8. #define BRIGHTNESS 64
  9.  
  10. CRGB leds[NUM_LEDS];
  11.  
  12. TaskHandle_t Left;
  13. TaskHandle_t Right;
  14.  
  15. const int dataChannel = 0;
  16. const int freq = 5000;
  17. const int resolution = 8;
  18.  
  19. const int M = NUM_LEDS/2; //midpoint of strip
  20.  
  21. void setup() {
  22.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOUR_ORDER>(leds, NUM_LEDS);
  23.   FastLED.setBrightness(BRIGHTNESS);
  24.  
  25.   Serial.begin(115200);
  26.   ledcSetup(dataChannel, freq, resolution);
  27.   ledcAttachPin(DATA_PIN, dataChannel);
  28.  
  29.   xTaskCreatePinnedToCore(
  30.       LeftCode, /* Function to implement the task */
  31.       "Left", /* Name of the task */
  32.       10000,  /* Stack size in words */
  33.       NULL,  /* Task input parameter */
  34.       2,  /* Priority of the task */
  35.       &Left,  /* Task handle. */
  36.       1); /* Core where the task should run */
  37.  
  38.       delay(50);
  39.  
  40.    xTaskCreatePinnedToCore(
  41.       RightCode, /* Function to implement the task */
  42.       "Right", /* Name of the task */
  43.       10000,  /* Stack size in words */
  44.       NULL,  /* Task input parameter */
  45.       5,  /* Priority of the task */
  46.       &Right,  /* Task handle. */
  47.       0); /* Core where the task should run */
  48. }
  49.  
  50. void LeftCode(void * pvParameters){
  51. for(;;){
  52.   Serial.println("Starting Left...");
  53.   for (int i=0; i<M; i++){
  54.     leds[i] = CRGB::Red;
  55.     FastLED.show();
  56.   }
  57.   delay(500);
  58.   for (int i = M-1; i>=0; i--){
  59.     leds[i] = CRGB::Black;
  60.     FastLED.show();
  61.   }
  62. }
  63. }
  64.  
  65. void RightCode(void * pvParameters){
  66. for(;;){
  67.   Serial.println("Starting Right...");
  68.   for (int i=M; i<NUM_LEDS; i++){
  69.     leds[i] = CRGB::Green;
  70.   }
  71.   FastLED.show();
  72.   delay(500);
  73.   for (int i=M; i<NUM_LEDS; i++){
  74.     leds[i] = CRGB::Blue;
  75.   }
  76.   FastLED.show();
  77.   delay(500);
  78.   for (int i=M; i<NUM_LEDS; i++){
  79.     leds[i] = CRGB::Red;
  80.   }
  81.   FastLED.show();
  82.   delay(500);
  83.   for (int i=M; i<NUM_LEDS; i++){
  84.     leds[i] = CRGB::Black;
  85.   }
  86. }
  87. }
  88.  
  89. void loop(){
  90.   vTaskDelay(50);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment