summaryrefslogtreecommitdiff
path: root/main.c
blob: eec2401e459d9e4d5f8da3488b7791383b1a5a49 (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "main.h"
#include "clock.h"
#include "timebase.h"
#include "dcf77.h"
#include "lcd.h"

#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <util/delay.h>

#ifdef WHOPR
#include "clock.c"
#include "timebase.c"
#include "dcf77.c"
#include "lcd.c"
#endif

#define B_SW_BACK	PINB5
#define B_SW_FRWD	PINB6

#define A_OCPL_A	PA5
#define A_OCPL_B	PA6

#define EE_HOUR		0
#define EE_MINUTE	1
#define EE_CKSUM	2
#define EE_BOOTC	4

static uint8_t ext_hour, ext_minute;
static uint16_t bootcount;

#define WAIT 64
uint8_t waitctr = 0;
enum stepstate {
	STEP_INACTIVE = 0,
	STEP_ACTIVE,
} stepstate = STEP_INACTIVE;

const char PROGMEM disp[2] = {'_', '+'};
const char PROGMEM status[8] = "oksya+a-";
const char PROGMEM zone[4] = "?12?";
const char PROGMEM cksumfail[] = "cksum fail";

static void puttwo(uint8_t arg)
{
	uint8_t b = '0', a = '0' + arg;
	while (a > '9')
		b++, a -= 10;
	lcd_put(b);
	lcd_put(a);
}

static void puttime(void)
{
	const char * PROGMEM msg;

	lcd_line1();
	puttwo(time.year);
	puttwo(time.month);
	puttwo(time.day);
	lcd_put(' ');
	puttwo(time.hour);
	puttwo(time.minute);
	puttwo(time.second);

	lcd_line2();
	lcd_put(time.flags & FLAG_SUMMERTIME_PENDING ? '!' : ' ');
	lcd_put(zone[is_cet() + (is_cest() << 1)]);
	lcd_put(time.flags & FLAG_LEAP_SECOND ? '!' : ' ');

	msg = &status[0];
	if (time.year <= 10)
		msg = &status[2];
	if (~PINB & (1 << B_SW_FRWD))
		msg = &status[4];
	if (~PINB & (1 << B_SW_BACK))
		msg = &status[6];
	lcd_puts(msg, 2);
	lcd_put(__LPM(&disp[stepstate]));
	puttwo(ext_hour);
	puttwo(ext_minute);
	lcd_put(' ');
	puttwo(bootcount / 100);
	puttwo(bootcount % 100);
}

static uint8_t setaccel;

static void countfwd(void)
{
	cli();
	ext_minute++;
	if (ext_minute == 60) {
		ext_minute = 0;
		ext_hour++;
		if (ext_hour == 12)
			ext_hour = 0;
	}
	sei();
}

static void step_set(void)
{
	if (ext_minute & 1) {
		PORTA &= ~(1 << A_OCPL_A);
	} else {
		PORTA &= ~(1 << A_OCPL_B);
	}
}

static void step_end(void)
{
	PORTA |= (1 << A_OCPL_A);
	PORTA |= (1 << A_OCPL_B);
}

static void perform(void)
{
	if (waitctr)
		return;

	step_end();

	if (~PINB & (1 << B_SW_BACK)) {
		waitctr = setaccel;
		if (setaccel > 3)
			setaccel--;
		cli();
		if (ext_minute)
			ext_minute--;
		else {
			ext_minute = 59;
			if (ext_hour)
				ext_hour--;
			else
				ext_hour = 11;
		}
		sei();
		return;
	}
	if (~PINB & (1 << B_SW_FRWD)) {
		waitctr = setaccel;
		if (setaccel > 3)
			setaccel--;
		countfwd();
		return;
	}
	setaccel = WAIT;

	if (time.year <= 10)
		return;

	PORTA &= ~(1 << PA2);

	uint8_t hbuf, mbuf, hdelta;

	cli();
	hbuf = time.hour;
	mbuf = time.minute;
	sei();

	if (hbuf >= 12)
		hbuf -= 12;

	switch (stepstate) {
	case STEP_INACTIVE:
		if (hbuf == ext_hour && mbuf <= ext_minute)
			break;
		hdelta = 11 + ext_hour - hbuf;
		while (hdelta >= 12)
			hdelta -=12;
		if (hdelta < 3)
			break;
		countfwd();

		step_set();
		stepstate = STEP_ACTIVE;
		waitctr = WAIT;
		break;
	case STEP_ACTIVE:
		stepstate = STEP_INACTIVE;
		waitctr = WAIT;
		break;
	}
}

extern void __vectors(void) __attribute__((noreturn));
__attribute__ ((noreturn))
ISR (ANA_COMP_vect)
{
	PORTA = 0xff;

	eeprom_write_byte((void *)EE_HOUR, ext_hour);
	eeprom_write_byte((void *)EE_MINUTE, ext_minute);
	eeprom_write_byte((void *)EE_CKSUM, ext_hour ^ ext_minute ^ 0xff);

	do
		_delay_ms(100);
	while (ACSR & (1 << ACO));
	__vectors();
}

int __attribute__((OS_main))
main( void )
{

  PORTA = 0xFF;		// enable pull ups
  PORTB = 0xFF;
  DDRA |= (1 << PA4) | (1 << PA3) | (1 << PA2) | (1 << A_OCPL_A) | (1 << A_OCPL_B);

	MCUCR = 0;
	ACSR = (1 << ACBG) | (1 << ACIS1) | (1 << ACIS0) | (1 << ACIE);
	_delay_ms(1);
	ACSR |= (1 << ACI);

  timebase_init();
  lcd_init();

	ext_hour   = eeprom_read_byte((void *)EE_HOUR);
	ext_minute = eeprom_read_byte((void *)EE_MINUTE);
	uint8_t ec = eeprom_read_byte((void *)EE_CKSUM);

	if ((ec ^ ext_hour ^ ext_minute) != 0xff) {
		lcd_puts(cksumfail, sizeof(cksumfail) - 1);
		while (1)
			;
	}

	bootcount = eeprom_read_word((void *)EE_BOOTC);
	eeprom_write_word((void *)EE_BOOTC, ++bootcount);

	/* last pulse may not have been long enough */
	step_set();
	_delay_ms(1000);
	step_end();

  sei();

  for(;;){
	bool do_puttime = false;

    scan_dcf77();
#if 1
    if( DCF77_PIN & 1<<DCF77 )
      PORTA |= 1<< PA3;
    else
      PORTA &= ~(1<<PA3);
#endif
	if( timeflags & 1<<ONE_SECOND ){
		timeflags = 0;
		clock();
		do_puttime = true;
	}

	if (!waitctr) {
		perform();
		do_puttime = true;
	}

	if (do_puttime)
		puttime();
/*    sleep_enable();
    sleep_cpu(); */
  }
}