From 9eeb91b17334557955862eaffe7be0e2902675df Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Sun, 20 May 2012 16:06:18 +1000 Subject: [PATCH] EditTime utility to support time and alarm editing --- .../RTC/examples/AlarmClock/AlarmClock.pde | 14 ++ .../RTC/examples/AlarmClock/EditTime.cpp | 219 ++++++++++++++++++ libraries/RTC/examples/AlarmClock/EditTime.h | 53 +++++ 3 files changed, 286 insertions(+) create mode 100644 libraries/RTC/examples/AlarmClock/EditTime.cpp create mode 100644 libraries/RTC/examples/AlarmClock/EditTime.h diff --git a/libraries/RTC/examples/AlarmClock/AlarmClock.pde b/libraries/RTC/examples/AlarmClock/AlarmClock.pde index 1ef089c5..a2fca062 100644 --- a/libraries/RTC/examples/AlarmClock/AlarmClock.pde +++ b/libraries/RTC/examples/AlarmClock/AlarmClock.pde @@ -29,6 +29,7 @@ #include #include #include "FrontScreen.h" +#include "EditTime.h" // I/O pins that are used by this sketch. #define BUZZER 12 @@ -61,6 +62,10 @@ uint8_t prevHour = 24; Form mainForm(lcd); FrontScreenField frontScreen(mainForm); 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() { // Enable the screen saver. @@ -75,6 +80,15 @@ void setup() { // Read the clock settings from the realtime clock's NVRAM. hourMode.setValue(rtc.readByte(SETTING_24HOUR) != 0); frontScreen.set24HourMode(hourMode.value()); + 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(); diff --git a/libraries/RTC/examples/AlarmClock/EditTime.cpp b/libraries/RTC/examples/AlarmClock/EditTime.cpp new file mode 100644 index 00000000..09dd3677 --- /dev/null +++ b/libraries/RTC/examples/AlarmClock/EditTime.cpp @@ -0,0 +1,219 @@ +/* + * 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 "EditTime.h" +#include + +#define EDIT_ENABLED 0 +#define EDIT_HOUR 1 +#define EDIT_MINUTE_TENS 2 +#define EDIT_MINUTE 3 + +EditTime::EditTime(Form &form, const String &label) + : Field(form, label) + , isAlarm(false) + , alarmEnabled(false) + , editField(EDIT_HOUR) +{ + _value.hour = 0; + _value.minute = 0; + _value.second = 0; +} + +int EditTime::dispatch(int event) +{ + RTCTime newValue; + if (event == LCD_BUTTON_UP) { + newValue = _value; + if (editField == EDIT_HOUR) { + newValue.hour = (newValue.hour + 1) % 24; + } else if (editField == EDIT_MINUTE_TENS) { + newValue.minute = (newValue.minute + 10) % 60; + } else if (editField == EDIT_MINUTE) { + newValue.minute = (newValue.minute + 1) % 60; + } else if (editField == EDIT_ENABLED) { + alarmEnabled = !alarmEnabled; + if (alarmEnabled) + editField = EDIT_HOUR; + printTime(); + return FORM_CHANGED; + } + newValue.second = 0; + _value = newValue; + printTime(); + return FORM_CHANGED; + } else if (event == LCD_BUTTON_DOWN) { + newValue = _value; + if (editField == EDIT_HOUR) { + newValue.hour = (newValue.hour + 23) % 24; + } else if (editField == EDIT_MINUTE_TENS) { + newValue.minute = (newValue.minute + 50) % 60; + } else if (editField == EDIT_MINUTE) { + newValue.minute = (newValue.minute + 59) % 60; + } else if (editField == EDIT_ENABLED) { + alarmEnabled = !alarmEnabled; + if (alarmEnabled) + editField = EDIT_HOUR; + printTime(); + return FORM_CHANGED; + } + _value = newValue; + printTime(); + return FORM_CHANGED; + } else if (event == LCD_BUTTON_LEFT) { + if (isAlarm) { + if (editField != EDIT_ENABLED) { + --editField; + printTime(); + return 0; + } + } else { + if (editField != EDIT_HOUR) { + --editField; + printTime(); + return 0; + } + } + } else if (event == LCD_BUTTON_RIGHT) { + if (isAlarm) { + if (alarmEnabled) { + if (editField != EDIT_MINUTE) { + ++editField; + printTime(); + return 0; + } + } + } else { + if (editField != EDIT_MINUTE) { + ++editField; + printTime(); + return 0; + } + } + } + return -1; +} + +void EditTime::enterField(bool reverse) +{ + Field::enterField(reverse); + if (isAlarm) { + if (alarmEnabled) { + if (reverse) + editField = EDIT_MINUTE; + else + editField = EDIT_ENABLED; + } else { + editField = EDIT_ENABLED; + } + } else { + if (reverse) + editField = EDIT_MINUTE; + else + editField = EDIT_HOUR; + } + printTime(); + lcd()->cursor(); +} + +void EditTime::exitField() +{ + lcd()->noCursor(); + Field::exitField(); +} + +void EditTime::setValue(const RTCTime &value) +{ + _value = value; + isAlarm = false; + if (isCurrent()) + printTime(); +} + +RTCAlarm EditTime::alarmValue() const +{ + RTCAlarm alarm; + alarm.hour = _value.hour; + alarm.minute = _value.minute; + alarm.flags = alarmEnabled ? 1 : 0; +} + +void EditTime::setAlarmValue(const RTCAlarm &value) +{ + _value.hour = value.hour; + _value.minute = value.minute; + _value.second = 0; + isAlarm = true; + alarmEnabled = ((value.flags & 0x01) != 0); + if (isCurrent()) + printTime(); +} + +void EditTime::printTime() +{ + lcd()->setCursor(0, 1); + int hour = _value.hour; + int minute = _value.minute; + int timeCol = 0; + bool pm; + if (isAlarm) { + if (alarmEnabled) { + lcd()->print("On "); + timeCol = 3; + } else { + lcd()->print("Off "); + lcd()->setCursor(0, 1); + return; + } + } + if (0) { //_hourMode) { + lcd()->write('0' + hour / 10); + lcd()->write('0' + hour % 10); + pm = false; + } else if (hour == 0 || hour == 12) { + lcd()->write('1'); + lcd()->write('2'); + pm = (hour == 12); + } else if (hour < 12) { + lcd()->write('0' + hour / 10); + lcd()->write('0' + hour % 10); + pm = false; + } else { + hour -= 12; + lcd()->write('0' + hour / 10); + lcd()->write('0' + hour % 10); + pm = true; + } + lcd()->write(':'); + lcd()->write('0' + minute / 10); + lcd()->write('0' + minute % 10); + if (1) // if (!_hourMode) + lcd()->print(pm ? "pm" : "am"); + if (editField == EDIT_ENABLED) + lcd()->setCursor(0, 1); + else if (editField == EDIT_HOUR) + lcd()->setCursor(timeCol + 1, 1); + else if (editField == EDIT_MINUTE_TENS) + lcd()->setCursor(timeCol + 3, 1); + else if (editField == EDIT_MINUTE) + lcd()->setCursor(timeCol + 4, 1); +} diff --git a/libraries/RTC/examples/AlarmClock/EditTime.h b/libraries/RTC/examples/AlarmClock/EditTime.h new file mode 100644 index 00000000..03116284 --- /dev/null +++ b/libraries/RTC/examples/AlarmClock/EditTime.h @@ -0,0 +1,53 @@ +/* + * 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 EditTime_h +#define EditTime_h + +#include "Field.h" +#include + +class EditTime : public Field { +public: + EditTime(Form &form, const String &label); + + int dispatch(int event); + + void enterField(bool reverse); + void exitField(); + + RTCTime value() const { return _value; } + void setValue(const RTCTime &value); + + RTCAlarm alarmValue() const; + void setAlarmValue(const RTCAlarm &value); + +private: + bool isAlarm; + bool alarmEnabled; + RTCTime _value; + uint8_t editField; + + void printTime(); +}; + +#endif