summaryrefslogtreecommitdiffstats
path: root/StripClock.c
blob: be723be86a65a19d9284eca7ecc0f9dc5e6cf360 (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
#include <avr/interrupt.h>
#include <avr/io.h>
#include <string.h>

#include "i2cmaster.h"
#include "ds1307.h"
#include "light_ws2812.h"


#define BTN_MODE DDD3
#define BTN_MODE_PORT PORTD
#define BTN_MODE_PIN PIND
#define BTN_MODE_DDR DDRD

#define BTN_INC DDB0
#define BTN_INC_PORT PORTB
#define BTN_INC_PIN PINB
#define BTN_INC_DDR DDRB

enum display_mode {
	DM_NONE,
	DM_HOUR,
	DM_MINUTE,
	DM_SECOND,
	DM_END
};

volatile uint8_t updated = 0;
volatile uint16_t ticks = 4095;

volatile uint8_t btn_debounce = 0;

volatile enum display_mode mode = DM_NONE;

volatile struct datetime dt;

#define LEDS 60
struct cRGB leds[LEDS];

uint8_t updateStrip(void);

int main(void)
{
	cli();
	//enable INT0 on falling edges
	MCUCR |= (1 << ISC01);
	//enable INT0 on falling edges
	MCUCR |= (1 << ISC11);
	GIMSK |= ((1 << INT1) | (1 << INT0));

	//set BTN_* as input and pull-ups high
	BTN_INC_DDR &= ~(1 << BTN_INC);
	BTN_INC_PORT |= (1 << BTN_INC);
	BTN_MODE_DDR &= ~(1 << BTN_MODE);
	BTN_MODE_PORT |= (1 << BTN_MODE);

	//set INT0 from sqw as input
	PORTD &= ~(1 << DDD2);

	i2c_init();

	ds1307_start();
	ds1307_SQWRate(SQW_4kHz);
	ds1307_SQW(1);

	ds1307_getDateTime(&dt);

	sei();

	for(;;) {
		if (mode == DM_NONE && updated) {
			ds1307_getDateTime(&dt);
			updateStrip();
			updated = 0;
		} else {
			switch (mode) {
				memset(leds, 0, sizeof(struct cRGB) * LEDS);
				case DM_HOUR:
					for (uint8_t i = 0; i < dt.hour; i++) {
						leds[i].r = 255;
						if (i % 5 == 0) {
							leds[i].g = 50;
							leds[i].b = 50;
						}
					}
					ws2812_setleds(leds, LEDS);
					break;
				case DM_MINUTE:
					for (uint8_t i = 0; i < dt.minute; i++) {
						leds[i].g = 255;
						if (i % 5 == 0) {
							leds[i].r = 50;
							leds[i].b = 50;
						}
					}
					ws2812_setleds(leds, LEDS);
					break;
				case DM_SECOND:
					for (uint8_t i = 0; i < dt.hour; i++) {
						leds[i].b = 255;
						if (i % 5 == 0) {
							leds[i].r = 50;
							leds[i].g = 50;
						}
					}
					ws2812_setleds(leds, LEDS);
					break;
				case DM_END:
					  ds1307_setDateTime(&dt);
					  mode = DM_NONE;
					  updateStrip();
					  break;
				default:
					  break;
			}
		}

	}
}

ISR(INT0_vect)
{
	//after 4096 ticks 1 sec is elapsed
	if (--ticks == 0) {
		updated = 1;
		ticks = 4095;
	}
}

ISR(INT1_vect)
{
	//debounce button, by temp disabling this interrupt
	GIMSK &= ~(1 << INT1);

	if (mode == DM_NONE) {
		//enable TIMER0 in normal mode, prescale 1024
		TCCR0A = 0;
		TCCR0B = ((1 << CS02) | (1 << CS00));
		//enable overflow intr
		TIMSK |= (1 << TOIE0);
	}

	mode++;


	//ctc mode
	TCCR1A = 0;
	//ctc && precale 1024
	TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10);
	//should be about 50ms
	OCR1A = 1024;

	//enable ctc intrrupt
	TIMSK |= (1 << OCIE1A);
}

ISR(TIMER1_COMPA_vect)
{
	//debounce of BTN_MODE

	//disable timer
	TCCR1B = 0;
	//disable this interrupt
	TIMSK &= ~(1 << OCIE1A);

	//enable INT1
	GIMSK |= (1 << INT1);
}

ISR(TIMER0_OVF_vect)
{
	//after 30 overflows, ~ 360ms elapsed
	if (++btn_debounce == 30) {
		btn_debounce = 0;

		if (BTN_INC_PIN & (1 << BTN_INC)) {
			switch (mode) {
				case DM_HOUR:
					dt.hour = (++dt.hour) % 24;
					break;
				case DM_MINUTE:
					dt.minute = (++dt.second) % 60;
					break;
				case DM_SECOND:
					dt.second = (++dt.second) % 60;
					break;
				default:
					break;
			}
		}

	}
}



uint8_t updateStrip(void)
{
	uint8_t hour_led =  ((dt.hour % 12) * (LEDS/12)) - 1;
	if (hour_led == -1)
		hour_led = 59;

	for (uint8_t i = 0; i < LEDS; i++) {
		int16_t r, g, b = 0;

		const int16_t cur_min_r = 4;
		const int16_t cur_min_g = 16;
		const int16_t cur_min_b = 1;
		const int16_t past_min_r = 2;
		const int16_t past_min_g = 2;
		const int16_t past_min_b = 1;

		if (i < dt.minute - 1) {
			r = g = 2;
			b = 1;
		} else if (i == dt.minute - 1 || (i == 59 && dt.minute == 0)) {
			r = cur_min_r + (past_min_r - cur_min_r) * (int16_t)dt.second / 60;
			g = cur_min_g + (past_min_g - cur_min_g) * (int16_t)dt.second / 60;
			b = cur_min_b + (past_min_b - cur_min_b) * (int16_t)dt.second / 60;


			//r = scalePing((uint8_t)r);
			//g = scalePing((uint8_t)g);
			//b = scalePing((uint8_t)b);
		} else if (i == dt.minute) {
			r = past_min_r + (cur_min_r - past_min_r) * (int16_t)dt.second / 60;
			g = past_min_g + (cur_min_g - past_min_g) * (int16_t)dt.second / 60;
			b = past_min_b + (cur_min_b - past_min_b) * (int16_t)dt.second / 60;

			//r = scalePing((uint8_t)r);
			//g = scalePing((uint8_t)g);
			//b = scalePing((uint8_t)b);
		} else if (i % 5 == 4) {
			r = past_min_r;
			g = past_min_g;
			b = past_min_b;
		} else {
			r = g = b = 0;
		}
		if (i % 15 == 14) {
			r *= 5;
			g *= 10;
			b *= 5;
		} else if (i % 5 == 4) {
			r *= 7;
			g *= 7;
			b *= 6;
		}

		if (i == hour_led) {
			g += 50;
			r += 40;
		}

		leds[i].r = r;
		leds[i].g = g;
		leds[i].b = b;
	}

	ws2812_setleds(leds, LEDS);

	return 0;
}