1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
2012-05-22 10:57:18 +10:00

168 lines
5.5 KiB
Plaintext

/*
* 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 the library code:
#include <FreetronicsLCD.h>
#include <Form.h>
#include <Field.h>
#include <BoolField.h>
#include <SoftI2C.h>
#include <DS1307RTC.h>
#include <Melody.h>
#include <PowerSave.h>
#include <avr/power.h>
#include "FrontScreen.h"
#include "EditTime.h"
#include "SetTime.h"
#include "SetDate.h"
#include "LowPowerMelody.h"
// I/O pins that are used by this sketch.
#define BUZZER 12
#define SENSE_BATTERY A1
#define RTC_DATA A4
#define RTC_CLOCK A5
#define RTC_ONE_HZ A3
// Value to adjust for the voltage drop on D2.
#define VOLTAGE_DROP_ADJUST 70 // 0.7 volts
// Offsets of settings in the realtime clock's NVRAM.
#define SETTING_24HOUR 0 // 0: 12 hour, 1: 24 hour
// Initialize the LCD
FreetronicsLCD lcd;
// Activate the realtime clock chip.
SoftI2C bus(RTC_DATA, RTC_CLOCK);
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};
LowPowerMelody alarmMelody(BUZZER);
uint8_t prevHour = 24;
bool is24HourClock = false;
// Create the main form and its fields.
Form mainForm(lcd);
FrontScreenField frontScreen(mainForm);
SetTime setTime(mainForm, "Set current time");
SetDate setDate(mainForm, "Set current date");
BoolField hourMode(mainForm, "Hour display", "24 hour clock", "12 hour clock", false);
EditTime alarm1(mainForm, "Alarm 1");
EditTime alarm2(mainForm, "Alarm 2");
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);
// 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);
// Initialize the alarm melody.
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.
is24HourClock = rtc.readByte(SETTING_24HOUR) != 0;
hourMode.setValue(is24HourClock);
frontScreen.set24HourMode(is24HourClock);
RTCAlarm alarm;
rtc.readAlarm(0, &alarm);
alarm1.setAlarmValue(alarm);
rtc.readAlarm(1, &alarm);
alarm2.setAlarmValue(alarm);
rtc.readAlarm(2, &alarm);
alarm3.setAlarmValue(alarm);
rtc.readAlarm(3, &alarm);
alarm4.setAlarmValue(alarm);
// Show the main form for the first time.
mainForm.show();
}
void loop() {
// Update the time and date every second based on the 1 Hz RTC output.
if (rtc.hasUpdates() || prevHour >= 24) {
RTCTime time;
rtc.readTime(&time);
frontScreen.setTime(time);
if (time.hour < prevHour) {
// Time has wrapped around, or date update has been forced.
RTCDate date;
rtc.readDate(&date);
frontScreen.setDate(date);
setDate.updateCurrentDate();
}
prevHour = time.hour;
setTime.updateCurrentTime();
// Update the battery status once a second also.
int status = analogRead(SENSE_BATTERY);
int voltage = (int)((status * 500L) / 1024L); // e.g. 2.81V = 281
voltage += VOLTAGE_DROP_ADJUST;
if (voltage > 500)
voltage = 500;
frontScreen.setVoltage(voltage);
}
// Dispatch button events to the main form.
int event = lcd.getButton();
if (mainForm.dispatch(event) == FORM_CHANGED) {
if (hourMode.isCurrent()) {
is24HourClock = hourMode.value();
frontScreen.set24HourMode(is24HourClock);
rtc.writeByte(SETTING_24HOUR, (byte)is24HourClock);
}
prevHour = 24; // Force an update of the main screen.
}
// If the alarm is playing and a button was pressed, then turn it off.
if (alarmMelody.isPlaying()) {
if (event > 0)
alarmMelody.stop();
alarmMelody.run();
} else {
// No alarm playing, so put the device to sleep to save power.
sleepFor(SLEEP_15_MS);
}
}