Skip to content

Commit

Permalink
Removed power_all/enable/disable, useless. Added a multiples of the w…
Browse files Browse the repository at this point in the history
…akeup.
  • Loading branch information
maniacbug committed May 11, 2011
1 parent ee4568e commit 4ef1f4b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions examples/pingpair_sleepy/pingpair_sleepy.pde
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ typedef enum { wdt_16ms = 0, wdt_32ms, wdt_64ms, wdt_128ms, wdt_250ms, wdt_500ms
void setup_watchdog(uint8_t prescalar);
void do_sleep(void);

const short sleep_cycles_per_transmission = 4;
volatile short sleep_cycles_remaining = sleep_cycles_per_transmission;

//
// Normal operation
//
Expand Down Expand Up @@ -106,7 +109,7 @@ void setup(void)

// Only the ping out role sleeps. Wake up every 4s to send a ping
if ( role == role_ping_out )
setup_watchdog(wdt_4s);
setup_watchdog(wdt_1s);

//
// Setup and configure rf radio
Expand Down Expand Up @@ -192,13 +195,19 @@ void loop(void)
// Shut down the system
//

// Experiment with some delay here to see if it has an effect
delay(500);

// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
radio.powerDown();

// Sleep the MCU. The watchdog timer will awaken in a short while, and
// continue execution here.
do_sleep();
while( sleep_cycles_remaining )
do_sleep();

sleep_cycles_remaining = sleep_cycles_per_transmission;
}

//
Expand Down Expand Up @@ -242,10 +251,9 @@ void loop(void)
// Sleep helpers
//

// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 0=16ms, 1=32ms,2=64ms,3=125ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec


void setup_watchdog(uint8_t prescalar)
{
prescalar = min(9,prescalar);
Expand All @@ -258,21 +266,18 @@ void setup_watchdog(uint8_t prescalar)
WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE);
}

// Even though it does nothing, it's necessary to have a WDT ISR
ISR(WDT_vect) {
--sleep_cycles_remaining;
}

void do_sleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();

power_all_disable();
sleep_mode(); // System sleeps here

sleep_disable(); // System continues execution here when watchdog timed out
power_all_enable();
}


// vim:ai:cin:sts=2 sw=2 ft=cpp

0 comments on commit 4ef1f4b

Please sign in to comment.