diff --git a/libraries/RTC/examples/AlarmClock/AlarmClock.pde b/libraries/RTC/examples/AlarmClock/AlarmClock.pde index a4b18207..09e7bc0f 100644 --- a/libraries/RTC/examples/AlarmClock/AlarmClock.pde +++ b/libraries/RTC/examples/AlarmClock/AlarmClock.pde @@ -29,8 +29,10 @@ #include #include #include +#include #include "FrontScreen.h" #include "EditTime.h" +#include "LowPowerMelody.h" // I/O pins that are used by this sketch. #define BUZZER 12 @@ -55,7 +57,7 @@ DS1307RTC rtc(bus, RTC_ONE_HZ); // Melody to play when the alarm sounds. int alarmNotes[] = {NOTE_C6, NOTE_C6, NOTE_C6, NOTE_C6, NOTE_REST}; byte alarmLengths[] = {8, 8, 8, 8, 2}; -Melody alarmMelody(BUZZER); +LowPowerMelody alarmMelody(BUZZER); uint8_t prevHour = 24; @@ -78,6 +80,12 @@ void setup() { unusedPin(11); unusedPin(13); + // Turn off peripherals we don't need. + power_spi_disable(); + power_usart0_disable(); + power_twi_disable(); + power_timer1_disable(); + // Enable the screen saver. lcd.setScreenSaverMode(FreetronicsLCD::BacklightOnSelect); lcd.enableScreenSaver(3); @@ -86,6 +94,7 @@ void setup() { alarmMelody.setMelody(alarmNotes, alarmLengths, sizeof(alarmLengths)); alarmMelody.setLoopDuration(120000UL); //alarmMelody.play(); + alarmMelody.stop(); // Force Timer2 to be disabled. // Read the clock settings from the realtime clock's NVRAM. hourMode.setValue(rtc.readByte(SETTING_24HOUR) != 0); @@ -139,7 +148,7 @@ void loop() { // If the alarm is playing and a button was pressed, then turn it off. if (alarmMelody.isPlaying()) { - if (event != LCD_BUTTON_NONE) + if (event > 0) alarmMelody.stop(); alarmMelody.run(); } else { diff --git a/libraries/RTC/examples/AlarmClock/LowPowerMelody.cpp b/libraries/RTC/examples/AlarmClock/LowPowerMelody.cpp new file mode 100644 index 00000000..0548af97 --- /dev/null +++ b/libraries/RTC/examples/AlarmClock/LowPowerMelody.cpp @@ -0,0 +1,42 @@ +/* + * 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 "LowPowerMelody.h" +#include + +void LowPowerMelody::play() +{ + // Turn on Timer2. + power_timer2_enable(); + + // Start the melody playing. + Melody::play(); +} + +void LowPowerMelody::stop() +{ + // Stop the melody playing. + Melody::stop(); + + // Turn off Timer2. + power_timer2_disable(); +} diff --git a/libraries/RTC/examples/AlarmClock/LowPowerMelody.h b/libraries/RTC/examples/AlarmClock/LowPowerMelody.h new file mode 100644 index 00000000..4e2be183 --- /dev/null +++ b/libraries/RTC/examples/AlarmClock/LowPowerMelody.h @@ -0,0 +1,36 @@ +/* + * 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 LowPowerMelody_h +#define LowPowerMelody_h + +#include + +class LowPowerMelody : public Melody { +public: + LowPowerMelody(uint8_t pin) : Melody(pin) {} + + void play(); + void stop(); +}; + +#endif