mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
Add a 12/24 hour setting to the clock
This commit is contained in:
parent
aee276ed3a
commit
2d031bd2e4
@ -24,6 +24,7 @@
|
||||
#include <FreetronicsLCD.h>
|
||||
#include <Form.h>
|
||||
#include <Field.h>
|
||||
#include <BoolField.h>
|
||||
#include <SoftI2C.h>
|
||||
#include <DS1307RTC.h>
|
||||
#include <Melody.h>
|
||||
@ -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.
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user