diff --git a/doc/Doxyfile b/doc/Doxyfile index 6ab7595c..fb06feee 100644 --- a/doc/Doxyfile +++ b/doc/Doxyfile @@ -610,7 +610,7 @@ WARN_LOGFILE = # directories like "/usr/src/myproject". Separate the files or directories # with spaces. -INPUT = ../libraries/FreetronicsLCD ../libraries/BlinkLED ../libraries/I2C ../libraries/RTC ../libraries/Melody . +INPUT = ../libraries/FreetronicsLCD ../libraries/BlinkLED ../libraries/I2C ../libraries/RTC ../libraries/Melody ../libraries/PowerSave . # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is diff --git a/doc/mainpage.dox b/doc/mainpage.dox index 605cf4c1..ee8677f0 100644 --- a/doc/mainpage.dox +++ b/doc/mainpage.dox @@ -70,4 +70,8 @@ The default implementation simulates the time and date based on the value of realtime clock and the FreetronicsLCD library to implement an alarm clock where the internal battery is charged by hand-cranking a dynamo. +\section main_other Other + +\li \ref power_save "Power saving utility functions" + */ diff --git a/libraries/PowerSave/PowerSave.cpp b/libraries/PowerSave/PowerSave.cpp new file mode 100644 index 00000000..b8da0077 --- /dev/null +++ b/libraries/PowerSave/PowerSave.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2012 Southern Storm Software, Pty Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "PowerSave.h" +#include +#include +#include +#include + +/** + * \defgroup power_save Power saving utility functions + * + * The functions in this module assist with reducing power consumption on + * Arduino boards by disabling features that are not used or putting the + * device to sleep when it is inactive. + */ +/*\@{*/ + +/** + * \fn void unusedPin(uint8_t pin) + * \brief Marks an I/O \a pin as unused. + * \ingroup power_save + * + * This function sets \a pin to be an input with pullups enabled, which will + * reduce power consumption compared to pins that are left floating. + */ + +/** @cond */ +ISR(WDT_vect) +{ + wdt_disable(); +} +/** @endcond */ + +/** + * \enum SleepDuration + * \brief Duration to put the CPU to sleep with sleepFor(). + * \ingroup power_save + * + * \sa sleepFor() + */ + +/** + * \var SLEEP_15_MS + * \brief Sleep for 15 milliseconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_30_MS + * \brief Sleep for 30 milliseconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_60_MS + * \brief Sleep for 60 milliseconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_120_MS + * \brief Sleep for 120 milliseconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_250_MS + * \brief Sleep for 250 milliseconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_500_MS + * \brief Sleep for 500 milliseconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_1_SEC + * \brief Sleep for 1 second. + * \ingroup power_save + */ + +/** + * \var SLEEP_2_SEC + * \brief Sleep for 2 seconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_4_SEC + * \brief Sleep for 4 seconds. + * \ingroup power_save + */ + +/** + * \var SLEEP_8_SEC + * \brief Sleep for 8 seconds. + * \ingroup power_save + */ + +/** + * \brief Puts the CPU to sleep for a specific \a duration. + * \ingroup power_save + * + * The analog to digital converter and the brown out detector will + * be disabled during sleep mode. + */ +void sleepFor(SleepDuration duration) +{ + // Turn off the analog to digital converter. + ADCSRA &= ~(1 << ADEN); + power_adc_disable(); + + // Turn on the watchdog timer for the desired duration. + wdt_enable(duration); + WDTCSR |= (1 << WDIE); + + // Put the device to sleep, including turning off the Brown Out Detector. + set_sleep_mode(SLEEP_MODE_IDLE); + cli(); + sleep_enable(); +#if defined(sleep_bod_disable) + sleep_bod_disable(); +#endif + sei(); + sleep_cpu(); + sleep_disable(); + sei(); + + // Turn the analog to digital converter back on. + power_adc_enable(); + ADCSRA |= (1 << ADEN); +} + +/*\@}*/ diff --git a/libraries/PowerSave/PowerSave.h b/libraries/PowerSave/PowerSave.h new file mode 100644 index 00000000..cba81e2d --- /dev/null +++ b/libraries/PowerSave/PowerSave.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2012 Southern Storm Software, Pty Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef PowerSave_h +#define PowerSave_h + +#include + +inline void unusedPin(uint8_t pin) +{ + pinMode(pin, INPUT); + digitalWrite(pin, HIGH); +} + +enum SleepDuration +{ + SLEEP_15_MS, + SLEEP_30_MS, + SLEEP_60_MS, + SLEEP_120_MS, + SLEEP_250_MS, + SLEEP_500_MS, + SLEEP_1_SEC, + SLEEP_2_SEC, + SLEEP_4_SEC, + SLEEP_8_SEC +}; + +void sleepFor(SleepDuration duration); + +#endif diff --git a/libraries/PowerSave/keywords.txt b/libraries/PowerSave/keywords.txt new file mode 100644 index 00000000..6a6b6bc1 --- /dev/null +++ b/libraries/PowerSave/keywords.txt @@ -0,0 +1,2 @@ +unusedPin KEYWORD2 +sleepFor KEYWORD2 diff --git a/libraries/RTC/examples/AlarmClock/AlarmClock.pde b/libraries/RTC/examples/AlarmClock/AlarmClock.pde index a2fca062..a4b18207 100644 --- a/libraries/RTC/examples/AlarmClock/AlarmClock.pde +++ b/libraries/RTC/examples/AlarmClock/AlarmClock.pde @@ -28,6 +28,7 @@ #include #include #include +#include #include "FrontScreen.h" #include "EditTime.h" @@ -68,6 +69,15 @@ EditTime alarm3(mainForm, "Alarm 3"); EditTime alarm4(mainForm, "Alarm 4"); void setup() { + // Reduce power consumption on I/O pins we don't need. + unusedPin(A2); + unusedPin(0); + unusedPin(1); + unusedPin(2); + unusedPin(10); + unusedPin(11); + unusedPin(13); + // Enable the screen saver. lcd.setScreenSaverMode(FreetronicsLCD::BacklightOnSelect); lcd.enableScreenSaver(3); @@ -128,7 +138,12 @@ void loop() { } // If the alarm is playing and a button was pressed, then turn it off. - if (event != LCD_BUTTON_NONE) - alarmMelody.stop(); - alarmMelody.run(); + if (alarmMelody.isPlaying()) { + if (event != LCD_BUTTON_NONE) + alarmMelody.stop(); + alarmMelody.run(); + } else { + // No alarm playing, so put the device to sleep to save power. + sleepFor(SLEEP_15_MS); + } }