Author Topic: Small library change to support arbitrary length sleep/suspend  (Read 1236 times)

kobuki

  • Sr. Member
  • ****
  • Posts: 289
Small library change to support arbitrary length sleep/suspend
« on: February 25, 2017, 08:13:45 AM »
I've added the following little convenience function to one of my sketches. I think it might be useful enough to include in Felix's LowPower branch (adapted to his lib for all related functions) instead of keeping it in my local repo. I can create a pull request if it seems a reasonable addition. Comments are welcome in any case.

Calling is similar to the original function, but the first argument is an arbitrary time im ms to sleep.

Code: [Select]
static int16_t periods[10] = { 15, 30, 60, 120, 250, 500, 1000, 2000, 4000, 8000 };

void longPowerDown(int32_t period, adc_t adc, bod_t bod) {
  for (byte i = 9; i >= 0 && period > 0; i--) {
    while (period - periods[i] >= 0) {
      period -= periods[i];
      LowPower.powerDown(i, adc, bod);
    }
  }
}

EDIT: a quick permutation to use PROGMEM:

Code: [Select]
const PROGMEM int16_t wdtPeriods[10] = { 15, 30, 60, 120, 250, 500, 1000, 2000, 4000, 8000 };

void longPowerDown(int32_t period, adc_t adc, bod_t bod) {
  for (byte i = 9; i >= 0 && period > 0; i--) {
    int16_t wdtPeriod = pgm_read_word_near(wdtPeriods + i);
    while (period - wdtPeriod >= 0) {
      period -= wdtPeriod;
      LowPower.powerDown(i, adc, bod);
    }
  }
}