-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.cpp
127 lines (123 loc) · 3.14 KB
/
clock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//-----------------------------------------------------------------------------
#include "avr-misc/avr-misc.h"
#include "avr-debug/debug.h"
#include "DS3231/ds3231.h"
#include "data.h"
#include "clock.h"
#include "body.h"
#include "global.h"
#include "tripComp.h"
#include "options.h"
//-----------------------------------------------------------------------------
static BYTE_DATA hours;
static BYTE_DATA minutes;
static BYTE_DATA alarmRings;
static bool clockSet = false;
static bool clockVisible = false;
static char str[3] = { 0, 0, 0 };
CImprovedAreaSet disp_clockH;
CImprovedAreaSet disp_clockM;
CFontAreaSet disp_clockColon;
CFontAreaSet disp_alarm;
extern BYTE FPS;
//-----------------------------------------------------------------------------
void time_changed(__unused BYTE result, BYTE datetime[7])
{
hours = datetime[HOUR];
minutes = datetime[MIN];
tripDay = datetime[DAY];
}
//-----------------------------------------------------------------------------
__inline void clockHide()
{
disp_clockH.hide();
disp_clockM.hide();
clockVisible = false;
}
//-----------------------------------------------------------------------------
__inline void clockEnable()
{
RTC.enable_SQW_output(RTC_SQW_FREQ_1HZ);
RTC.alarm2.set_every_minute();
RTC.get_datetime_bcd(time_changed);
// enabe pin-change interrupt
set_bit(PCMSK0, PCINT0);
set_bit(PCIFR, PCIF0);
set_bit(PCICR, PCIE0);
}
//-----------------------------------------------------------------------------
void RTC_init()
{
clockHide();
if(RTC.connected())
{
RTC.alarm1.unset();
RTC.alarm2.unset();
RTC.enable_run_on_battery();
RTC.disable_32kHz_output();
RTC.disable_SQW_output();
if(!RTC.time_lost())
{
clockSet = true;
clockEnable();
}
}
}
//-----------------------------------------------------------------------------
void alarms_receiever(BYTE alarms)
{
timer++;
if(alarms & 0x02)
{
RTC.get_time_bcd(time_changed);
}
if(alarms & 0x01)
{
alarmRings = 1;
}
if(ignition)
{
tripTimer++;
}
if(selectorAT & 0x1F)
{
totalDriveTime++;
}
if(options.showFPS)
{
DVAR(FPS);
FPS = 0;
}
}
//-----------------------------------------------------------------------------
void displayClock()
{
if(clockSet && !clockVisible)
{
disp_clockH.show();
disp_clockM.show();
disp_clockColon.blink();
clockVisible = true;
}
if(hours.updated)
{
str[0] = (hours.value >> 4) + '0';
str[1] = (hours.value & 0x0F) + '0';
disp_clockH.print(str);
}
if(minutes.updated)
{
str[0] = (minutes.value >> 4) + '0';
str[1] = (minutes.value & 0x0F) + '0';
disp_clockM.print(str);
}
}
//-----------------------------------------------------------------------------
ISR(PCINT0_vect)
{
if(!test_bit(PINA, 0))
{
RTC.clear_alarm_events(&alarms_receiever);
}
}
//-----------------------------------------------------------------------------