1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Setting the current time and date

This commit is contained in:
Rhys Weatherley 2012-05-22 10:32:59 +10:00
parent f90571c4d7
commit ad47e33947
7 changed files with 319 additions and 7 deletions

View File

@ -32,6 +32,8 @@
#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.
@ -60,10 +62,13 @@ 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");
@ -97,8 +102,9 @@ void setup() {
alarmMelody.stop(); // Force Timer2 to be disabled.
// Read the clock settings from the realtime clock's NVRAM.
hourMode.setValue(rtc.readByte(SETTING_24HOUR) != 0);
frontScreen.set24HourMode(hourMode.value());
is24HourClock = rtc.readByte(SETTING_24HOUR) != 0;
hourMode.setValue(is24HourClock);
frontScreen.set24HourMode(is24HourClock);
RTCAlarm alarm;
rtc.readAlarm(0, &alarm);
alarm1.setAlarmValue(alarm);
@ -124,8 +130,10 @@ void loop() {
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);
@ -140,8 +148,9 @@ void loop() {
int event = lcd.getButton();
if (mainForm.dispatch(event) == FORM_CHANGED) {
if (hourMode.isCurrent()) {
frontScreen.set24HourMode(hourMode.value());
rtc.writeByte(SETTING_24HOUR, (byte)hourMode.value());
is24HourClock = hourMode.value();
frontScreen.set24HourMode(is24HourClock);
rtc.writeByte(SETTING_24HOUR, (byte)is24HourClock);
}
prevHour = 24; // Force an update of the main screen.
}

View File

@ -28,6 +28,8 @@
#define EDIT_MINUTE_TENS 2
#define EDIT_MINUTE 3
extern bool is24HourClock;
EditTime::EditTime(Form &form, const String &label)
: Field(form, label)
, isAlarm(false)
@ -76,6 +78,7 @@ int EditTime::dispatch(int event)
printTime();
return FORM_CHANGED;
}
newValue.second = 0;
_value = newValue;
printTime();
return FORM_CHANGED;
@ -185,7 +188,7 @@ void EditTime::printTime()
return;
}
}
if (0) { //_hourMode) {
if (is24HourClock) {
lcd()->write('0' + hour / 10);
lcd()->write('0' + hour % 10);
pm = false;
@ -206,7 +209,7 @@ void EditTime::printTime()
lcd()->write(':');
lcd()->write('0' + minute / 10);
lcd()->write('0' + minute % 10);
if (1) // if (!_hourMode)
if (!is24HourClock)
lcd()->print(pm ? "pm" : "am");
if (editField == EDIT_ENABLED)
lcd()->setCursor(0, 1);

View File

@ -41,7 +41,7 @@ public:
RTCAlarm alarmValue() const;
void setAlarmValue(const RTCAlarm &value);
private:
protected:
bool isAlarm;
bool alarmEnabled;
RTCTime _value;

View File

@ -0,0 +1,150 @@
/*
* 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 "SetDate.h"
#include <DS1307RTC.h>
#define EDIT_DAY 0
#define EDIT_MONTH 1
#define EDIT_YEAR 2
extern DS1307RTC rtc;
SetDate::SetDate(Form &form, const String &label)
: Field(form, label)
, editField(EDIT_DAY)
{
_value.day = 1;
_value.month = 1;
_value.year = 2012;
}
int SetDate::dispatch(int event)
{
RTCDate newValue;
if (event == LCD_BUTTON_UP) {
newValue = _value;
if (editField == EDIT_DAY)
RTC::adjustDays(&newValue, RTC::INCREMENT | RTC::WRAP);
else if (editField == EDIT_MONTH)
RTC::adjustMonths(&newValue, RTC::INCREMENT | RTC::WRAP);
else if (editField == EDIT_YEAR)
RTC::adjustYears(&newValue, RTC::INCREMENT | RTC::WRAP);
_value = newValue;
printDate();
rtc.writeDate(&_value);
return FORM_CHANGED;
} else if (event == LCD_BUTTON_DOWN) {
newValue = _value;
if (editField == EDIT_DAY)
RTC::adjustDays(&newValue, RTC::DECREMENT | RTC::WRAP);
else if (editField == EDIT_MONTH)
RTC::adjustMonths(&newValue, RTC::DECREMENT | RTC::WRAP);
else if (editField == EDIT_YEAR)
RTC::adjustYears(&newValue, RTC::DECREMENT | RTC::WRAP);
_value = newValue;
printDate();
rtc.writeDate(&_value);
return FORM_CHANGED;
} else if (event == LCD_BUTTON_LEFT) {
if (editField != EDIT_DAY) {
--editField;
printDate();
return 0;
}
} else if (event == LCD_BUTTON_RIGHT) {
if (editField != EDIT_YEAR) {
++editField;
printDate();
return 0;
}
}
return -1;
}
void SetDate::enterField(bool reverse)
{
rtc.readDate(&_value);
Field::enterField(reverse);
if (reverse)
editField = EDIT_YEAR;
else
editField = EDIT_DAY;
printDate();
lcd()->cursor();
}
void SetDate::exitField()
{
lcd()->noCursor();
Field::exitField();
}
void SetDate::setValue(const RTCDate &value)
{
_value = value;
if (isCurrent())
printDate();
}
void SetDate::updateCurrentDate()
{
if (isCurrent()) {
rtc.readDate(&_value);
printDate();
}
}
extern const char *months[]; // Table of month names; e.g. " Jan ".
void SetDate::printDate()
{
lcd()->setCursor(0, 1);
int dayCol;
int monthCol;
int yearCol;
int col = 0;
dayCol = col;
if (_value.day < 10) {
lcd()->write('0' + _value.day);
++col;
} else {
lcd()->write('0' + _value.day / 10);
lcd()->write('0' + _value.day % 10);
col += 2;
}
monthCol = col + 1;
lcd()->print(months[_value.month - 1]);
col += 5;
yearCol = col;
lcd()->write('0' + _value.year / 1000);
lcd()->write('0' + (_value.year / 100) % 10);
lcd()->write('0' + (_value.year / 10) % 10);
lcd()->write('0' + _value.year % 10);
lcd()->write(' ');
if (editField == EDIT_DAY)
lcd()->setCursor(dayCol, 1);
else if (editField == EDIT_MONTH)
lcd()->setCursor(monthCol, 1);
else
lcd()->setCursor(yearCol, 1);
}

View File

@ -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 SetDate_h
#define SetDate_h
#include "Field.h"
#include <RTC.h>
class SetDate : public Field {
public:
SetDate(Form &form, const String &label);
int dispatch(int event);
void enterField(bool reverse);
void exitField();
RTCDate value() const { return _value; }
void setValue(const RTCDate &value);
void updateCurrentDate();
private:
RTCDate _value;
uint8_t editField;
void printDate();
};
#endif

View File

@ -0,0 +1,61 @@
/*
* 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 "SetTime.h"
#include <DS1307RTC.h>
extern DS1307RTC rtc;
SetTime::SetTime(Form &form, const String &label)
: EditTime(form, label)
{
}
int SetTime::dispatch(int event)
{
int result = EditTime::dispatch(event);
if (result == FORM_CHANGED) {
// Update the realtime clock with the new value.
RTCTime time = value();
rtc.writeTime(&time);
}
return result;
}
void SetTime::enterField(bool reverse)
{
// Read the current time when the field is entered.
rtc.readTime(&_value);
EditTime::enterField(reverse);
}
void SetTime::updateCurrentTime()
{
if (isCurrent()) {
RTCTime time;
rtc.readTime(&time);
if (time.hour != _value.hour || time.minute != _value.minute) {
_value = time;
printTime();
}
}
}

View File

@ -0,0 +1,39 @@
/*
* 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 SetTime_h
#define SetTime_h
#include "EditTime.h"
class SetTime : public EditTime {
public:
SetTime(Form &form, const String &label);
int dispatch(int event);
void enterField(bool reverse);
void updateCurrentTime();
};
#endif