/
fm_receiver.ino
132 lines (125 loc) · 3.06 KB
/
fm_receiver.ino
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
/*
FM Receiver Project based on Arduino
Copyright (c) 2017-2018 Michael A. Matveev. All right reserved.
This firmware is licensed under a GNU GPL v.3 License.
For more information see: https://www.gnu.org/licenses/gpl-3.0.en.html
*/
#include "FM.h"
#include "Graphics.h"
#include "Tools.h"
Graphics graphics;
FM fm;
void setup() {
// Set backlight
pinMode(PIN_BL, OUTPUT);
digitalWrite(PIN_BL, DEFAULT_BL);
// Init FM Radio module
fm.init();
// Init Graphics module
graphics.init();
// Show splash
graphics.showSplash();
// Draw menu
graphics.drawMenu();
}
void loop() {
// Read analog signal from buttons
int inV = analogRead(A0);
// Menu Button presseded
if (inV > 500 && inV < 524 && checkMillis(300)) {
graphics.menu++;
if (graphics.menu > MENU_ITEMS_COUNT) {
graphics.menu = 1;
}
graphics.drawMenu();
}
// Left Button presseded
if (inV < 50 && checkMillis(150)) {
switch (graphics.menu) {
case MENU_VOLUME:
fm.lowerVolume();
break;
case MENU_MANUAL:
fm.lowerFrequency();
break;
case MENU_AUTO:
graphics.showTuningBox();
fm.autoTune(1);
delay(500);
graphics.hideTuningBox();
break;
case MENU_BL:
if (checkMillis2(450)) {
graphics.toggleBL();
}
break;
case MENU_BASS:
if (checkMillis2(450)) {
graphics.switchBass();
if (graphics.bass) {
fm.moreBass();
} else {
fm.lessBass();
}
}
break;
case MENU_BAND:
if (checkMillis2(250)) {
graphics.band--;
if (graphics.band < 0) {
graphics.band = 3;
}
graphics.drawBandSelect(graphics.band);
fm.setBandByIndex(graphics.band);
}
break;
}
}
// Right Button pressed
if (inV < 700 && inV > 660 && checkMillis(150)) {
switch (graphics.menu) {
case MENU_VOLUME:
fm.higherVolume();
break;
case MENU_MANUAL:
fm.higherFrequency();
break;
case MENU_AUTO:
graphics.showTuningBox();
fm.autoTune(0);
delay(500);
graphics.hideTuningBox();
break;
case MENU_BL:
if (checkMillis2(450)) {
graphics.toggleBL();
}
break;
case MENU_BASS:
if (checkMillis2(450)) {
graphics.switchBass();
if (graphics.bass) {
fm.moreBass();
} else {
fm.lessBass();
}
}
break;
case MENU_BAND:
if (checkMillis2(250)) {
graphics.band++;
if (graphics.band > 3) {
graphics.band = 0;
}
graphics.drawBandSelect(graphics.band);
fm.setBandByIndex(graphics.band);
}
break;
}
}
// Read current state from FM Receiver
fm.readState();
// Update graphics accoring to the current data got from FM receiver
graphics.updateState(fm.strength, fm.stereo, fm.volume, fm.freqText,
fm.station);
}