-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
188 lines (177 loc) · 5.93 KB
/
main.c
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
#include "common.h"
#include "clock.h"
#include "disp.h"
#include "autogenerated/baloons.h"
#include "autogenerated/hearts.h"
#include "autogenerated/kuce.h"
#include "autogenerated/ring.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <stdint.h>
#define RIGHT_BUTTON (!(PIND & 0x20))
#define CENTER_BUTTON (!(PIND & 0x40))
#define LEFT_BUTTON (!(PIND & 0x80))
bool left_button_int = false;
bool center_button_int = false;
bool right_button_int = false;
void init(void) {
/* start rtc */
clk_start();
/* set button pins for reading */
DDRD &= 0x1F;
/* init epaper display */
disp_init();
/* disable ADC */
ADCSRA = 0;
/* configure sleep mode */
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
/* enable INT0 so RTC can wake up CPU from sleep */
// set DDD2 as input
DDRD &= ~(1 << DDD2);
// set INT0 to occur on low level
EICRA = 0;
// enable INT0
EIMSK |= (1 << INT0);
// and enable interrupts
sei();
}
void sleep(void) {
sei();
sleep_enable();
sleep_bod_disable();
sleep_cpu();
sleep_disable();
//clk_clear_alarm();
cli();
}
uint8_t bcd_to_uint(uint8_t bcd) {
return (bcd % 16) + ((bcd / 16) * 10);
}
uint8_t uint_to_bcd(uint8_t uint) {
return (uint % 10) + ((uint / 10) * 16);
}
ISR(INT0_vect) {
bool underscored[] = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };
left_button_int = LEFT_BUTTON;
center_button_int = CENTER_BUTTON;
right_button_int = RIGHT_BUTTON;
TIME time = {};
}
bool display_holiday(TIME *time) {
if ((time->months == 0x08 && time->days == 0x31) ||
(time->months == 0x09 && time->days == 0x21) ||
(time->months == 0x10 && time->days == 0x11) ||
(time->months == 0x11 && time->days == 0x05))
return true;
else
return false;
}
bool display_Malgosias_day(TIME *time) {
if ((time->months == 0x01 && time->days == 0x01) ||
(time->months == 0x02 && time->days == 0x05) ||
(time->months == 0x03 && time->days == 0x10) ||
(time->months == 0x04 && time->days == 0x15) ||
(time->months == 0x05 && time->days == 0x20) ||
(time->months == 0x06 && time->days == 0x25) ||
(time->months == 0x07 && time->days == 0x30))
return true;
else
return false;
}
int main(void) {
init();
TIME time = {};
uint8_t underscored_index = 0;
uint8_t prev_min;
bool refresh = true;
bool underscored_tab[8][15] = {
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{true, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, true, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, true, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, true, true, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, true, true, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, true, true, true, true},
};
while (true) {
/* get current time */
prev_min = time.minutes;
if (clk_get_time(&time) && time.minutes != prev_min) refresh = true;
/* handle buttons */
if (center_button_int) {
center_button_int = 0;
underscored_index += 1;
underscored_index %= 8;
if (underscored_index == 0) clk_start();
else clk_stop();
refresh = true;
}
else if (left_button_int || right_button_int)
{
refresh = true;
time.seconds = 0x00;
switch (underscored_index)
{
case 1:
if (left_button_int && (time.hours & 0xF0) != 0x00) time.hours -= 0x10;
else if (right_button_int && (time.hours & 0xF0) != 0x20) time.hours += 0x10;
if (time.hours >= 0x24) time.hours = 0x23;
break;
case 2:
if (left_button_int && (time.hours & 0x0F) != 0x00) time.hours -= 0x01;
else if (right_button_int && (time.hours & 0x0F) != 0x09 && time.hours != 0x24) time.hours += 0x01;
break;
case 3:
if (left_button_int && (time.minutes & 0xF0) != 0x00) time.minutes -= 0x10;
else if (right_button_int && (time.minutes & 0xF0) != 0x50) time.minutes += 0x10;
break;
case 4:
if (left_button_int && (time.minutes & 0x0F) != 0x00) time.minutes -= 0x01;
else if (right_button_int && (time.minutes & 0x0F) != 0x09) time.minutes += 0x01;
break;
case 5:
if (left_button_int && time.days != 0x01) time.days = uint_to_bcd(bcd_to_uint(time.days) - 1);
else if(right_button_int && (time.days != 0x31)) time.days = uint_to_bcd(bcd_to_uint(time.days) + 1);
break;
case 6:
if (left_button_int && time.months != 0x01) time.months = uint_to_bcd(bcd_to_uint(time.months) - 1);
else if (right_button_int && (time.months != 0x12)) time.months = uint_to_bcd(bcd_to_uint(time.months) + 1);
break;
case 7:
if (left_button_int && time.years != 0x00) time.years = uint_to_bcd(bcd_to_uint(time.years) - 1);
else if (right_button_int && (time.years != 0x99)) time.years = uint_to_bcd(bcd_to_uint(time.years) + 1);
break;
default:
break;
}
clk_set_time(&time);
}
/* write time to screen memory */
if (refresh) {
if (underscored_index == 0 && display_holiday(&time)) {
disp_writeSmallTime(&time, baloons);
}
else if (underscored_index == 0 && display_Malgosias_day(&time)) {
disp_writeSmallTime(&time, hearts);
}
else {
disp_writeBigTime(&time, underscored_tab[underscored_index]);
if (left_button_int || center_button_int || right_button_int) {
/* repeat to clear buffers */
/* inconsistency happens only if button was pressed to early */
disp_writeBigTime(&time, underscored_tab[underscored_index]);
}
}
/* and print it */
disp_displayFrame();
refresh = false;
left_button_int = false;
center_button_int = false;
right_button_int = false;
}
}
return 0;
}