Pass Array in xTaskCreate()

CamGenius
Posts: 2
Joined: Thu Dec 31, 2020 9:21 am

Pass Array in xTaskCreate()

Postby CamGenius » Thu Dec 31, 2020 9:27 am

I am trying to pass in the array of data that is created to the second core for sending over LoRa but am unable to get the syntax right. I commented all the important lines where the states[] array is used. Any help would be much appreciated.

Code: Select all

#include <Arduino.h>
#include "heltec.h"

#define BAND    915E6

const uint8_t receiverInput = 12;
const uint8_t escPin = 13;

int timer;
bool ticked = false;

int currentState = 0;
int states[11]; //Initial Creation

void sendPacket(void * parameter){
      LoRa.beginPacket();
      LoRa.setTxPower(14, RF_PACONFIG_PASELECT_PABOOST);
      for(int i = 1; i < 12; i++)
      {
        LoRa.println(states[i]); //To Print Here
        Serial.print("Sent Packet: ");
        Serial.println(i);
        Serial.println(*((int*)parameter[i])); //And To Print Here
      }
      LoRa.endPacket();
      vTaskDelete(NULL);
}

void IRAM_ATTR escTick(){
  states[currentState] = micros() - timer;
  ticked = true;
  if(currentState == 11)
  {
    Serial.println(states[currentState]);
    xTaskCreatePinnedToCore(sendPacket, "SendPacket", 1000, (void*)&states, 1, NULL, 1); //Passing in Variable Here
  }
}

void IRAM_ATTR pwmChange(){
  if(digitalRead(receiverInput))
  {
    detachInterrupt(escPin);
    pinMode(escPin, OUTPUT);
    digitalWrite(escPin, LOW);
    if(!ticked)
    {
      currentState = 0;
    }
  }
  else
  {
    digitalWrite(escPin, HIGH);
    pinMode(escPin, INPUT);
    attachInterrupt(escPin, escTick, FALLING);
    timer = micros();
    currentState += 1;
    ticked = false;
  }
}

void setup() {
  Heltec.begin(false, true, true, true, BAND);
  Serial.begin(115200);
  pinMode(receiverInput, INPUT);
  pinMode(escPin, OUTPUT);
  digitalWrite(escPin, HIGH);
  attachInterrupt(receiverInput, pwmChange, CHANGE);
  Serial.println("Initialized!");
}

void loop() {
}

lbernstone
Posts: 671
Joined: Mon Jul 22, 2019 3:20 pm

Re: Pass Array in xTaskCreate()

Postby lbernstone » Thu Dec 31, 2020 4:38 pm

Code: Select all

uint8_t myarray[5];
void mytask(void* param) {
  uint8_t* localarray = (uint8_t*)param;
  Serial.println(localarray[3]);
  vTaskDelete(NULL);
}
void setup() {
  Serial.begin(115200);
  for (int x=0; x<5; x++) myarray[x]=random(255);
  xTaskCreate(mytask, "mytask", 1024, &myarray, 5, NULL);
}

CamGenius
Posts: 2
Joined: Thu Dec 31, 2020 9:21 am

Re: Pass Array in xTaskCreate()

Postby CamGenius » Fri Jan 01, 2021 12:48 am

Thank you that worked great!

Who is online

Users browsing this forum: No registered users and 142 guests