From 2d031bd2e4ddf149a0f8fe8d10dce0740b1c0731 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Sun, 20 May 2012 10:59:47 +1000 Subject: [PATCH] Add a 12/24 hour setting to the clock --- .../RTC/examples/AlarmClock/AlarmClock.pde | 13 +++++++++++++ .../RTC/examples/AlarmClock/FrontScreen.cpp | 19 +++++++++++++++++-- .../RTC/examples/AlarmClock/FrontScreen.h | 4 ++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/libraries/RTC/examples/AlarmClock/AlarmClock.pde b/libraries/RTC/examples/AlarmClock/AlarmClock.pde index 940c8474..1ef089c5 100644 --- a/libraries/RTC/examples/AlarmClock/AlarmClock.pde +++ b/libraries/RTC/examples/AlarmClock/AlarmClock.pde @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +40,9 @@ // 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; @@ -56,6 +60,7 @@ uint8_t prevHour = 24; // Create the main form and its fields. Form mainForm(lcd); FrontScreenField frontScreen(mainForm); +BoolField hourMode(mainForm, "Hour display", "24 hour clock", "12 hour clock", false); void setup() { // Enable the screen saver. @@ -67,6 +72,10 @@ void setup() { alarmMelody.setLoopDuration(120000UL); //alarmMelody.play(); + // Read the clock settings from the realtime clock's NVRAM. + hourMode.setValue(rtc.readByte(SETTING_24HOUR) != 0); + frontScreen.set24HourMode(hourMode.value()); + // Show the main form for the first time. mainForm.show(); } @@ -97,6 +106,10 @@ void loop() { // Dispatch button events to the main form. int event = lcd.getButton(); if (mainForm.dispatch(event) == FORM_CHANGED) { + if (hourMode.isCurrent()) { + frontScreen.set24HourMode(hourMode.value()); + rtc.writeByte(SETTING_24HOUR, (byte)hourMode.value()); + } prevHour = 24; // Force an update of the main screen. } diff --git a/libraries/RTC/examples/AlarmClock/FrontScreen.cpp b/libraries/RTC/examples/AlarmClock/FrontScreen.cpp index 3ce16222..3ff154e3 100644 --- a/libraries/RTC/examples/AlarmClock/FrontScreen.cpp +++ b/libraries/RTC/examples/AlarmClock/FrontScreen.cpp @@ -39,6 +39,7 @@ FrontScreenField::FrontScreenField(Form &form) , _voltageTrunc(36) , _batteryBars(IND_BATTERY_FULL) , _alarmActive(false) + , _hourMode(false) { _date.day = 1; _date.month = 1; @@ -123,6 +124,15 @@ void FrontScreenField::setAlarmActive(bool active) } } +void FrontScreenField::set24HourMode(bool value) +{ + if (_hourMode != value) { + _hourMode = value; + if (isCurrent()) + updateTime(); + } +} + void FrontScreenField::updateDate() { lcd()->setCursor(0, 0); @@ -141,7 +151,11 @@ void FrontScreenField::updateTime() { lcd()->setCursor(0, 1); bool pm; - if (_time.hour == 0 || _time.hour == 12) { + if (_hourMode) { + lcd()->write('0' + _time.hour / 10); + lcd()->write('0' + _time.hour % 10); + pm = false; + } else if (_time.hour == 0 || _time.hour == 12) { lcd()->write('1'); lcd()->write('2'); pm = (_time.hour == 12); @@ -161,7 +175,8 @@ void FrontScreenField::updateTime() lcd()->write(':'); lcd()->write('0' + _time.second / 10); lcd()->write('0' + _time.second % 10); - lcd()->print(pm ? "pm" : "am"); + if (!_hourMode) + lcd()->print(pm ? "pm" : "am"); } void FrontScreenField::updateVoltage() diff --git a/libraries/RTC/examples/AlarmClock/FrontScreen.h b/libraries/RTC/examples/AlarmClock/FrontScreen.h index 7b20cec0..85e92bad 100644 --- a/libraries/RTC/examples/AlarmClock/FrontScreen.h +++ b/libraries/RTC/examples/AlarmClock/FrontScreen.h @@ -46,6 +46,9 @@ public: bool isAlarmActive() const { return _alarmActive; } void setAlarmActive(bool active); + bool is24HourMode() const { return _hourMode; } + void set24HourMode(bool value); + private: RTCDate _date; RTCTime _time; @@ -53,6 +56,7 @@ private: int _voltageTrunc; int _batteryBars; bool _alarmActive; + bool _hourMode; void updateDate(); void updateTime();