Skip to content

Helper for async delay? #218

@Andersama

Description

@Andersama

Without messing about with the library itself it seems like the existing blockTillConversionComplete function could be rewritten to be it's own utility function to smooth out the async feature. The current function is setup to be called immediately after the temperature request is made, but a small change can be made so both synchronous and asynchronous users can make use of it.

Here's the edit what I ended up with.

void wait_for_temps(DallasTemperature &sensor, unsigned long start) {
  if (checkForConversion && !parasite) {
    while (!isConversionComplete() && (millis() - start < MAX_CONVERSION_TIMEOUT ))
      yield();
  } else {
    // not clear on parasite power usage, is supposed to function timing wise, I'm assuming this is ok, the existing code appears to wait the whole maximum duration
    // this seems to suggest that parasite power users don't really benefit from making an async call
    unsigned long now = millis();
    unsigned long spent = now - start;
    unsigned long wait_for = millisToWaitForConversion(bitResolution);
    sensor.activateExternalPullup();
    unsigned long diff = (wait_for - spent);
    unsigned long delay_for = diff < MAX_CONVERSION_TIMEOUT ?  diff : MAX_CONVERSION_TIMEOUT;
    delay(delay_for);
    sensor.deactivateExternalPullup();
  }
}

The usage if blockTillConversionComplete were rewritten would be something like:

  sensors.setWaitForConversion(false); // could be in setup or loop
  sensors.requestTemperatures();
  unsigned long request_start = millis();
  // do other tasks while we're waiting
  
  // wait out the remaining time needed
  sensors.blockTillConversionComplete(request_start);
  // process temperature reading
  float temp = sensors.getTempCByIndex(0);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions