-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_functions.c
424 lines (365 loc) · 11.1 KB
/
misc_functions.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "misc_functions.h"
extern time_t _current_raw_time;
extern struct tm* _current_time;
extern bool _time_set;
void remote_function(u8 command) {
// If poweroff, respond only on wakeup button, ignore empty commands
if ((settings.poweroff && command != 175)
|| command == 0)
return;
// printf("%d\r\n", command);
if (!settings.poweroff && function_timeout > 20) {
hd44780_backlight(true);
print_station_name(stations->current);
}
function_timeout = 0;
switch(command) {
case 47: // Up arrow
stations->current = stations->current->next;
if (!stations->current)
stations->current = stations->head;
change_station(stations->current);
break;
case 143: // Down arrow
if (stations->current == stations->head) {
stations->current = stations->tail;
} else {
stations->current = stations->current->prev;
}
change_station(stations->current);
break;
case 87: // TV / Audio
settings.mute = rda5807_toggle_mute();
print_settings();
break;
case 199: // Search
settings.boost = !settings.boost;
rda5807_set_bass_boost(settings.boost);
print_settings();
break;
case 7: // Next song
if (++settings.volume > 15)
settings.volume = 15;
rda5807_set_volume(settings.volume);
print_settings();
break;
case 135: // Previous song
if (--settings.volume > 15)
settings.volume = 0;
rda5807_set_volume(settings.volume);
print_settings();
break;
case 175: // Poweroff
poweroff();
break;
case 125: // Exit
break;
case 127: // 1
change_station(shortcuts_list[0].station);
break;
case 191:
change_station(shortcuts_list[1].station);
break;
case 63:
change_station(shortcuts_list[2].station);
break;
case 223:
change_station(shortcuts_list[3].station);
break;
case 95:
change_station(shortcuts_list[4].station);
break;
case 159:
change_station(shortcuts_list[5].station);
break;
case 31:
change_station(shortcuts_list[6].station);
break;
case 239:
change_station(shortcuts_list[7].station);
break;
case 111:
change_station(shortcuts_list[8].station);
break;
case 255: // 0
break;
default:
break;
}
save_settings();
}
// Add radio station to list of stations. Station inserted according to its
// frequency to keep list sorted
radio* add_radio(char* name, u16 frequency) {
radio *new_station = malloc(sizeof(radio));
if (!new_station) while(1); // Out of memory
new_station->frequency = frequency;
new_station->name = strndup(name, 16);
// printf("Adding: %s\n\r", name);
if (stations->head == 0) {
new_station->next = 0;
new_station->prev = 0;
stations->head = new_station;
stations->tail = new_station;
stations->current = new_station;
// printf("Done!\r\n");
return new_station;
}
// Iterate over list to find correct position
radio *current_position = stations->head;
while (current_position) {
// New item will be placed before current position
if (current_position->frequency > new_station->frequency) {
radio *previous = current_position->prev;
current_position->prev = new_station;
new_station->next = current_position;
new_station->prev = previous;
if (previous) {
previous->next = new_station;
} else {
stations->head = new_station;
}
break;
}
current_position = current_position->next;
}
if (current_position == 0) {
radio *prev_tail = stations->tail;
stations->tail = new_station;
new_station->next = 0;
if (prev_tail) {
new_station->prev = prev_tail;
prev_tail->next = new_station;
}
}
// printf("Done!\r\n");
return new_station;
}
// Print station name on LCD display
void print_station_name(radio *station) {
hd44780_cmd(0x01); // clear display, go to 0x0
hd44780_print(station->name);
static char frequency[16];
static u8 main_, rest;
main_ = station->frequency / 10;
rest = station->frequency - main_ * 10;
sprintf(frequency, "%3d.%d0 MHz", main_, rest);
hd44780_go_to_line(1);
hd44780_print(frequency);
print_settings();
}
// Print current settings on LCD display
void print_settings() {
static char volume[2];
hd44780_go_to(1, 11);
hd44780_char(0);
if (settings.mute) {
hd44780_print(" ");
} else {
hd44780_char(1);
}
if (settings.boost) {
hd44780_char(2);
} else {
hd44780_print("B");
}
sprintf(volume, "%2d", settings.volume);
hd44780_print(volume);
}
// Change current active station
void change_station(radio *station) {
if (!settings.poweroff)
print_station_name(station);
rda5807_set_frequency(station->frequency);
}
// Print list of stations (for debugging purposes)
void print_list(radio_list *list) {
radio *ptr = list->head;
while (ptr) {
// printf("%p %s %d %p %p\r\n", ptr, ptr->name, ptr->frequency, ptr->prev, ptr->next);
ptr = ptr->next;
}
}
// "Power off" device
void poweroff() {
settings.poweroff = !settings.poweroff;
if (settings.poweroff) {
rda5807_set_mute(true);
hd44780_cmd(0x01);
// hd44780_cmd(0x08);
hd44780_backlight(false);
} else {
rda5807_set_mute(false);
// hd44780_cmd(0x0C);
print_station_name(stations->current);
hd44780_backlight(true);
}
update_time();
}
// Add custom characters to LCD display memory in order to draw them
void add_custom_characters(void) {
// Volume font, pt. 1
u8 volume[] = {
0b00001,
0b00011,
0b01111,
0b01111,
0b01111,
0b00011,
0b00001,
0b00000
};
// Volume font, pt. 2
u8 off[8] = {
0b01000,
0b10000,
0b00000,
0b11000,
0b00000,
0b10000,
0b01000,
0b00000
};
// Boost icon
u8 boost[8] = {
0b11110,
0b11011,
0b11011,
0b11110,
0b11011,
0b11011,
0b11110
};
hd44780_cgram_write(0, volume);
hd44780_cgram_write(1, off);
hd44780_cgram_write(2, boost);
}
// Create list of stations and assign shortcuts to some of them
void populate_stations(void) {
stations = malloc(sizeof(radio_list));
stations->current = 0;
stations->tail = 0;
stations->head = 0;
radio *s1 = add_radio("Trojka", 994);
radio *s2 = add_radio("Jedynka", 894);
radio *s3 = add_radio("RMF FM", 960);
radio *s4 = add_radio("Radio Krakow", 1016);
radio *s5 = add_radio("Radio ZET", 1041);
radio *s6 = add_radio("Radio Plus Krakow", 1061);
radio *s7 = add_radio("Antyradio", 1013);
radio *s8 = add_radio("Radio Wawa", 888);
radio *s9 = add_radio("Rock Radio", 1038);
add_radio("Dwojka", 1020);
add_radio("Czworka", 972);
add_radio("RMF Classic", 878);
add_radio("RMF Maxxx", 967);
add_radio("Radiofonia", 1005);
add_radio("Radio ZET Chilli", 1010);
add_radio("Radio ZET Gold", 937);
add_radio("Eska Krakow", 977);
add_radio("Radio VOX FM", 1070);
add_radio("Radio TOK FM", 1029);
add_radio("Zlote Przeboje Wanda", 925);
add_radio("Radio Muzo FM", 1049);
add_radio("Radio Bajka", 952);
add_radio("Radio Pogoda", 1024);
add_radio("Radio Maryja", 906);
// print_list(stations);
// Shortcuts are available from remote
shortcuts_list[0].hotkey = 1;
shortcuts_list[0].station = s1;
shortcuts_list[1].hotkey = 2;
shortcuts_list[1].station = s2;
shortcuts_list[2].hotkey = 3;
shortcuts_list[2].station = s3;
shortcuts_list[3].hotkey = 4;
shortcuts_list[3].station = s4;
shortcuts_list[4].hotkey = 5;
shortcuts_list[4].station = s5;
shortcuts_list[5].hotkey = 6;
shortcuts_list[5].station = s6;
shortcuts_list[6].hotkey = 7;
shortcuts_list[6].station = s7;
shortcuts_list[7].hotkey = 8;
shortcuts_list[7].station = s8;
shortcuts_list[8].hotkey = 9;
shortcuts_list[8].station = s9;
}
// Store settings and current FM station on EEPROM
void save_settings(void) {
uint8_t p_settings = settings.boost << 7 |
settings.mute << 6 |
settings.poweroff << 5 |
settings.volume;
uint8_t p_current_station_h = stations->current->frequency >> 8;
uint8_t p_current_station_l = stations->current->frequency;
uint8_t tx[3];
tx[0] = p_settings;
tx[1] = p_current_station_h;
tx[2] = p_current_station_l;
at24c64_write_bytes(0x0000, tx, 3);
}
// Load settings from EEPROM and set previous station
void load_settings(void) {
uint8_t rx[3];
at24c64_read_bytes(0x0000, rx, 3);
settings.boost = rx[0] >> 7 & 0x01;
settings.mute = rx[0] >> 6 & 0x01;
settings.poweroff = !(rx[0] >> 5 & 0x01);
settings.volume = rx[0] & 0x0F;
uint16_t frequency = rx[1] << 8 | rx[2];
rda5807_set_bass_boost(settings.boost);
rda5807_set_mute(settings.mute);
rda5807_set_volume(settings.volume);
// If radio was toggled off, shut it down, enable otherwise
poweroff();
radio *new_station = stations->head;
while (new_station) {
if (new_station->frequency == frequency) {
break;
}
new_station = new_station->next;
}
if (new_station) {
stations->current = new_station;
change_station(new_station);
} else {
change_station(shortcuts_list[0].station);
}
}
// Prints date and time when device is idle (power off)
void _print_idle_time(void) {
if (!_time_set)
return;
hd44780_cmd(0x01);
static char time_buffer[16];
hd44780_go_to_line(0);
strftime(time_buffer, 16, " %d.%m.%Y", _current_time);
hd44780_print(time_buffer);
hd44780_go_to_line(1);
strftime(time_buffer, 16, " %R", _current_time);
hd44780_print(time_buffer);
}
// Prints time only when device is inactive (power on, but there have been some
// time since last action (determined by `function_timeout` variable
void _print_time(void) {
if (!_time_set)
return;
static char time_buffer[11];
hd44780_go_to_line(1);
strftime(time_buffer, 11, " %R ", _current_time);
hd44780_print(time_buffer);
}
void update_time(void) {
if (!_time_set)
return;
if (settings.poweroff)
_print_idle_time();
else if (function_timeout >= 20)
_print_time();
}
void force_update_time(void) {
_current_raw_time = RTC_GetCounter();
_current_time = gmtime(&_current_raw_time);
update_time();
}