I have some code running on ESP32 microcontroller with arduino core, In the setup()
function I initialize and detach a thread as follows:
std::unique_ptr<std::thread> sensorCalib;
void setup(){
sensorCalib.reset(new std::thread(threadPressureCalib));
std::thread* pc = sensorCalib.get();
pc->detach();
}
Then, I define threadPressureCalib()
as follows:
void threadPressureCalib()
{
float pressure=0;
int count;
for(timestarted = millis();(millis()-timestarted) < botSettings[initial_valve_delay].value();)
{ // THIS ONE BLOCKS SETUP() CODE EXECUTION
if(!VALVE_STATE)
{
release_valve(true);
Serial.println("Waiting for pressure to release");
}
}
Serial.println("CALIBRATING PRESSURE SENSOR");
for (count=1; count<= 5;count++)
{ //THIS ONE DOES NOT BLOCK SETUP()
float temp;
while(!timer2.Delay(2000)); //Not sure if this is blocking anything
do{
temp = adc_pressure();
}while(temp>104.0 || temp<70.0); //Catch errors
pressure += temp;
}
changeSetting(pressure/5.0);
return;
}
During the first for loop, the setup()
function's execution is stopped
During the second for loop, nothing is stopped and the rest of the code runs in parallel
Why is it that the first half of this code blocks, and then the second half does not?
Sorry if the question is vague or improperly asked, my first q here.
Aucun commentaire:
Enregistrer un commentaire