////////////////////////////////////////////////////////////////////////////////////////
// OVERVIEW
////////////////////////////////////////////////////////////////////////////////////////
/*
DESCRIPTION: Basic sleep demo. Simply allows us to play with various sleep modes and measure
current. The program starts up, prints text for a moment, then sleeps the processor forever
with no wake.

AUTHOR: Andre' LaMothe

COMMENTS: 

VT100 commands can be found here:

http://www.braun-home.net/michael/info/misc/VT100_commands.htm

HISTORY: 

*/


////////////////////////////////////////////////////////////////////////////////////////
// INCLUDES 
////////////////////////////////////////////////////////////////////////////////////////

#include <Arduino.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <avr/sleep.h>
#include <avr/power.h>

using namespace std;

////////////////////////////////////////////////////////////////////////////////////////
// PROTOTYPES
////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////
// DEFINES AND MACROS
////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
////////////////////////////////////////////////////////////////////////////////////////

// string buffer for printing
char gStringBuffer[80]; 

////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////

void setup() 
{
// the setup function runs once when you press reset or power the board  

// initialize serial port
Serial.begin(115200);
Serial.write( "\n\rSleep demo.\n\r" );

} // end setup

////////////////////////////////////////////////////////////////////////////////////////

void loop() 
{
static int loopCounter = 0;
// print something...
sprintf(gStringBuffer, "\n\r(%d)Running Main Loop...", loopCounter++ );
Serial.write( gStringBuffer );


//SLEEP_MODE_IDLE 
//SLEEP_MODE_ADC
//SLEEP_MODE_PWR_SAVE
//SLEEP_MODE_EXT_STANDBY
//SLEEP_MODE_STANDBY
//SLEEP_MODE_PWR_DOWN

// time to sleep?
if ( loopCounter > 31 )
  {
  Serial.write( "\n\rGood night...");
  Serial.flush();

  // disable ADC
  ADCSRA = 0;  

  // turn off various modules via the PRR power reduction register
  // note: according to the datasheet the PRR only applies in active (non-sleep) and idle modes. 
  // In other sleep modes, those modules are already turned off by definition, so not helpful if you
  // use more aggressive sleep modes, the modules are already disabled.
  power_all_disable ();

  // put processor to sleep..
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
  sleep_cpu ();  
  } // end if

// once processor is asleep it will never get here until woke up

// pause a moment
delay( 100 );

} // end loop

////////////////////////////////////////////////////////////////////////////////////////