/
clockblock.cpp
244 lines (211 loc) · 5.35 KB
/
clockblock.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// clockblock
// Copyright 2013 by Wiley Cousins, LLC.
// shared under the terms of the MIT License
//
// file: clockblock.cpp
// description: application file for the clockblock
// ************************************
// AVR includes necessary for this file
// ************************************
//#include <util/atomic.h>
#include <util/delay.h>
// ********************
// application includes
// ********************
#include "clockblock.h"
// **************************
// INTERRUPT SERVICE ROUTINES
// **************************
// frame counter ISR (timer 0 is clocked by the RTC)
ISR(TIMER0_OVF_vect) {
tick = true;
}
// switch poller / debouncer / timer
ISR(INPUT_TIMER_vect, ISR_NOBLOCK) {
// disable interrupt
buttons.disableTimer();
// handle the polling
buttons.handleTimer();
// re-enable the interrupt
buttons.enableTimer();
}
// ***********
// application
// ***********
// main
int main(void) {
// debugging
#ifdef DEBUG
// set up the heartbeat led
DDRB |= (1<<3);
PORTB |= (1<<3);
// examine the last reset
// watchdog reset - blink LED 4 times
if (MCUSR & (1<<WDRF)) {
for (uint8_t i=0; i<4; i++) {
beatHeart();
_delay_ms(250);
beatHeart();
_delay_ms(250);
}
}
else if (MCUSR & (1<<BORF)) {
for (uint8_t i=0; i<3; i++) {
beatHeart();
_delay_ms(250);
beatHeart();
_delay_ms(250);
}
}
else if (MCUSR & (1<<EXTRF)) {
for (uint8_t i=0; i<2; i++) {
beatHeart();
_delay_ms(250);
beatHeart();
_delay_ms(250);
}
}
else if (MCUSR & (1<<PORF)) {
for (uint8_t i=0; i<1; i++) {
beatHeart();
_delay_ms(250);
beatHeart();
_delay_ms(250);
}
}
// clear the flags
MCUSR = 0;
#endif
// delay for a few ms to allow the RTC to take its initial temp measurement
_delay_ms(1000);
// begin setup - disable interrupts
cli();
// take care of unused pins
initUnusedPins();
// give those ISR volatile vairables some values
tick = false;
// application variables
// time vector - { seconds, minutes, hours}
uint8_t tm[3] = {0, 0, 12};
// animation frame
uint8_t fr = 0;
// arms leds
uint16_t dots[DISPLAY_NUM_DOTS];
// initialize the LED driver
// DO THIS BEFORE INITIALIZING RTC (to ensure proper SPI functionality for the RTC)
tlc.init();
// set the TLC to autorepeat the pattern and to reset the GS counter whenever new data is latched in
tlc.setFC(TLC5971_DSPRPT);
// set brightness to about 50% (to account for bad wall warts)
tlc.setBC(60);
// initialize the RTC
rtc.init();
// enable a 8192 Hz squarewave output on clock input pin
rtc.setSquareWave(PCF2129AT_CLKOUT_8_kHz);
// check the oscillator stop flag on the RTC and give it a new time if necessary
if (rtc.hasLostTime()) {
rtc.setTime(tm, PCF2129AT_AM);
}
// else get the good time from the RTC
else {
rtc.getTime(tm);
}
// initialize the ticker
initTicker();
// set the display mode
leds.setMode(DISPLAY_MODE_BLEND);
// enable inputs
buttons.init();
// end setup - enable interrupts
sei();
// get lost
for (;;) {
// take care of any switch presses
uint8_t buttonState = 0;
if (buttons.getPress(&buttonState)) {
handleButtonPress(buttonState, tm);
}
// update the arms on a tick
if (tick) {
// clear the flag
tick = false;
// get the time
if (++fr >= 32) {
// beat the heart every second
beatHeart();
fr = 0;
rtc.getTime(tm);
}
// update the clock arms
updateArms(tm, fr, dots);
}
}
// one day we might get the answer
return 42;
}
// update the clock arms
// dots array structure: { hr0, mn0, sc0, hr1, mn1, sc1, ... , hr11, mn11, sc11 }
void updateArms(uint8_t *tm, uint8_t frame, uint16_t *dots) {
// get the display
leds.getDisplay(tm, frame, dots);
// send to the LED driver
tlc.setGS(dots);
}
// initialize unused pins as inputs with pullups enabled
void initUnusedPins(void) {
// PORTB
DDRB &= ~UNUSED_PORTB_MASK;
PORTB |= UNUSED_PORTB_MASK;
// PORTA
DDRA &= ~UNUSED_PORTA_MASK;
PORTA |= UNUSED_PORTA_MASK;
}
// button handling logic
void handleButtonPress(uint8_t state, uint8_t *tm) {
// if a time set switch was pressed
if ( state & (INPUT_HOUR | INPUT_MIN)) {
// temporary time register to avoid messing up the seconds LEDs
uint8_t set[3];
// get the real time
rtc.getTime(set);
// if hour switch, increment the hours by 1
if (state & INPUT_HOUR) {
if (++set[2] > 12) {
set[2] -= 12;
}
}
// if minute switch, increment minutes
if (state & INPUT_MIN) {
if (++set[1] > 59) {
set[1] -= 60;
}
}
// tell the clock and then the app
rtc.setTime(set, PCF2129AT_AM);
tm[2] = set[2];
tm[1] = set[1];
}
// if mode switch, increment mode
if (state & INPUT_MODE) {
uint8_t m = leds.getMode();
m = (m < DISPLAY_NUM_MODES-1) ? m+1 : 0;
leds.setMode(m);
}
}
void beatHeart(void) {
#ifdef DEBUG
PINB |= (1<<3);
#endif
}
void initTicker(void) {
// set timer 0 to run normally with no prescaler
TCCR0A = 0;
TCCR0B = (1<<CS00);
// set timer 0 to clock from the RTC squarewave
ASSR = (1<<EXCLK);
// set timer 0 to clock asynchronously and reload the counter
ASSR |= (1<<AS0);
TCNT0 = 0;
// enable timer 0 overflow interrupt
TIMSK0 = (1<<TOIE0);
}