/
main.c
172 lines (142 loc) · 4.26 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
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include "eeprom.h"
#include "adc.h"
#include "input.h"
#include "remote.h"
#ifdef _UARTCONTROL
#include "uart.h"
#endif
#ifdef _ALARM
#include "alarm.h"
#endif
#include "i2c.h"
#include "display.h"
#include "tuner/tuner.h"
#ifdef _TEMPCONTROL
#include "temp.h"
#endif
#include "actions.h"
#include "pins.h"
#include "rtc.h"
#ifdef _SPISW
#include "spisw.h"
#endif
void hwReset()
{
OUT(DISP_RESET);
IN(I2C_SCL);
SET(I2C_SCL);
IN(I2C_SDA);
SET(I2C_SDA);
CLR(DISP_RESET); // Start display and Si470x reset
CLR(I2C_SCL); // Put to zero in this sequence to avoid fake START
OUT(I2C_SCL);
_delay_ms(1); // Select 2-wire interface:
CLR(I2C_SDA); // SDA = 0
OUT(I2C_SDA);
IN(I2C_SCL); // SCL = 1
SET(I2C_SCL);
_delay_ms(5); // Reset
SET(DISP_RESET); // End of reset
_delay_ms(1);
IN(I2C_SDA); // SDA = 1
SET(I2C_SDA);
_delay_ms(1);
}
// Hardware initialization
static void hwInit(uint8_t extFunc)
{
hwReset();
#ifdef _TEMPCONTROL
loadTempParams();
#endif
if (extFunc == USE_DS18B20) {
#ifdef _TEMPCONTROL
ds18x20SearchDevices();
tempInit(); // Init temperature control
setSensTimer(TEMP_MEASURE_TIME);
#endif
} else {
#ifdef _SPISW
SPIswInitLines(extFunc);
#endif
}
I2CInit(); // I2C bus
displayInit(); // Load params and text labels before fb scan started
rcInit(); // IR Remote control
inputInit(); // Buttons/encoder polling
#ifdef _UARTCONTROL
uartInit(); // USART
#endif
adcInit(); // Analog-to-digital converter
rtcInit();
#ifdef _ALARM
alarmInit();
alarm0.eam = ALARM_NOEDIT;
#endif
sei(); // Gloabl interrupt enable
tunerInit(); // Tuner
OUT(STMU_STBY); // Standby port
OUT(STMU_MUTE); // Mute port
sndInit(); // Load labels/icons/etc
}
int main()
{
int8_t encCnt = 0;
uint8_t action = CMD_RC_STBY;
uint8_t extFunc = eeprom_read_byte((uint8_t *)EEPROM_EXT_FUNC);
uint8_t initMode = eeprom_read_byte((uint8_t *)EEPROM_INIT_MODE);
if (initMode == INIT_WORK_MODE)
action = ACTION_EXIT_STANDBY;
// Init hardware
hwInit(extFunc);
while (1) {
#ifdef _TEMPCONTROL
// Control temperature
if (extFunc == USE_DS18B20) {
if (getSensTimer() == 0) {
ds18x20Process();
setSensTimer(SENSOR_POLL_INTERVAL);
}
tempControlProcess();
}
#endif
// Emulate poweroff if any of timers expired
if (getStbyTimer() == 0 || getSilenceTimer() == 0)
action = CMD_RC_STBY;
// Init hardware if init timer expired
if (getInitTimer() == 0)
action = ACTION_INIT_HARDWARE;
// Check alarm and update time
if (action == ACTION_NOACTION)
action = checkAlarmAndTime();
// Convert input command to action
if (action == ACTION_NOACTION)
action = getAction();
// Handle action
handleAction(action, initMode);
// Handle encoder
encCnt = getEncoder(); // Get value from encoder
if (action == CMD_RC_VOL_UP) // Emulate VOLUME_UP action as encoder action
encCnt++;
if (action == CMD_RC_VOL_DOWN) // Emulate VOLUME_DOWN action as encoder action
encCnt--;
handleEncoder(encCnt);
// Reset silence timer on any action
if (encCnt || (action != ACTION_NOACTION && action != ACTION_EXIT_STANDBY))
enableSilenceTimer();
// Reset handled action
action = ACTION_NOACTION;
// Check if we need exit to default mode
handleExitDefaultMode();
// Switch to timer mode if it expires
handleTimers();
// Clear screen if mode has changed
handleModeChange();
// Show things
showScreen();
}
return 0;
}