forked from WiseLord/oledfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
201 lines (177 loc) · 4.3 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
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "ssd1306.h"
#include "glcd.h"
#include "fonts.h"
#include "screen.h"
#include "input.h"
#include "i2c.h"
#include "tuner/tuner.h"
#include <util/delay.h>
#include "pins.h"
#include "eeprom.h"
#define VOL_MIN 0
#define VOL_MAX 15
void setVolume(int8_t value)
{
if (value > VOL_MAX)
value = VOL_MAX;
if (value < VOL_MIN)
value = VOL_MIN;
tunerSetVolume(value);
tunerSetMute(!tuner.volume);
return;
}
void hwInit()
{
glcdInit();
inputInit();
screenInit();
#ifdef _SI470X
si470xReset();
#endif
I2CInit();
// Setup sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Interrupts
TIMSK0 |= (1 << OCIE0A); // Input timer compare
sei();
tunerInit();
tunerSetVolume(tuner.volume);
OUT(LED_RED);
}
void sleep(void)
{
// Prepare sleep
TIMSK0 &= ~(1 << OCIE0A); // Input timer compare disable
PCMSK2 = BUTTON_0_LINE | BUTTON_1_LINE | BUTTON_2_LINE | BUTTON_3_LINE;
if (Screen == SCREEN_STANDBY)
PCMSK2 = BUTTON_0_LINE;
PCICR |= (1<<PCIE2); // Buttons interrupt enable
SET(LED_RED);
glcdSleep();
// Sleep
sleep_mode();
glcdWakeup();
CLR(LED_RED);
// Wakeup
PCICR &= ~(1<<PCIE2); // Buttons interrupt disable
TIMSK0 |= (1 << OCIE0A); // Input timer compare enable
}
int main(void)
{
hwInit();
screenSet(SCREEN_STANDBY);
screenUpdate();
timerSleepSet(SLEEP_TIMER_STANDBY);
// Temporary power on the device
_delay_ms(400);
tunerPowerOn();
setVolume(tuner.volume);
tunerSetMono(0);
tunerSetFreq();
screenSet(SCREEN_MAIN);
timerSleepSet(SLEEP_TIMER_WORK);
while (1) {
uint8_t btnCmd = getBtnCmd();
if (timerSleepGet() == 0) {
sleep();
}
if (btnCmd) {
if (Screen == SCREEN_STANDBY)
timerSleepSet(SLEEP_TIMER_STANDBY);
else
timerSleepSet(SLEEP_TIMER_WORK);
}
switch (btnCmd) {
case BTN_0:
switch (Screen) {
default:
break;
}
break;
case BTN_1:
switch (Screen) {
case SCREEN_MAIN:
tunerSeek(-1);
// tunerNextStation(-1);
break;
default:
break;
}
break;
case BTN_2:
switch (Screen) {
case SCREEN_MAIN:
tunerSeek(+1);
// tunerNextStation(+1);
break;
default:
break;
}
break;
case BTN_3:
switch (Screen) {
default:
// tunerSetMono(!tuner.mono);
// tunerSetRDS(!tuner.rds);
tunerSetBass(!tuner.bass);
break;
}
break;
case BTN_0_LONG:
switch (Screen) {
case SCREEN_STANDBY:
tunerPowerOn();
setVolume(tuner.volume);
tunerSetFreq();
screenSet(SCREEN_MAIN);
timerSleepSet(SLEEP_TIMER_WORK);
break;
case SCREEN_MAIN:
tunerPowerOff();
screenSet(SCREEN_STANDBY);
timerSleepSet(SLEEP_TIMER_STANDBY);
break;
default:
break;
}
break;
case BTN_1_LONG:
switch (Screen) {
case SCREEN_MAIN:
setVolume(tuner.volume - 1);
break;
default:
break;
}
break;
case BTN_2_LONG:
switch (Screen) {
case SCREEN_MAIN:
setVolume(tuner.volume + 1);
break;
default:
break;
}
break;
case BTN_3_LONG:
switch (Screen) {
default:
break;
}
break;
case BTN_1_LONG | BTN_2_LONG:
switch (Screen) {
default:
break;
}
break;
default:
break;
}
screenUpdate();
}
return 0;
}