-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
227 lines (180 loc) · 5.16 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
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
/* these two lines take care of the IO definitions for the device, and the interrupts available for them */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#define SETH(reg,id) reg |= (1 << id)
#define SETL(reg,id) reg &= ~(1 << id)
#define F_CPU 8E6 // 8MHz
#include <util/delay_basic.h>
#include <util/delay.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "lcd.h"
#include "buttons.h"
#include "fm.h"
#include "spi2.h"
#include "bitmaps.h"
#define LCD_X 84
#define LCD_Y 48
/* Intended for ATmega644P */
/* Clock rate is 8.0 MHz (8MHz internal oscillator (default), divide by 8 (default), to give CLKIO of 1MHz */
// Fuse high byte is default (0x99) See data sheet page 297
/* Fuse low byte is 0xE2: ((NB fuses active low) See data sheet page 297
* CLKDIV8 1 (div8 inactive)
* CLKOUT 1 (clk out inactive)
* SUT1 1 (fast rising power)
* SUT0 0
* CKSEL3 0 8MHz internal osc
* CKSEL2 0
* CKSEL1 1
* CKSEL0 0
*/
/* Interrupt vectors */
/* Port direction setting */
inline void port_direction_init() {
/* DDR is Data Direction Register
PORT is used when referring to outputs
PIN is used when referring to inputs
See ATMEGA644P datasheet section 13 */
DDRB = 0xBF ;
PORTB = 0x1F ; // SS, CS, reset inactive
// the led
SETH(DDRB, PB0); // for backlight on the LCD
// PORTC all inputs, no pullups
DDRC = 0 ;
PORTC = 0xFF ;
// PORTD all inputs, no pullups
DDRD = 0;
PORTD = 0xFF;
// power reduction
PRR = 0x7B;
}
char rdsname[9] = "HELLO :)";
char rdsrt[65] = "Welcome to radio";
char strbuff[9];
#define MAXSCALEFREQ (1097)
#define MINSCALEFREQ (864)
#define LCD_BACKLIGHT_TIMEOUT (2000)
uint16_t lcd_timeout = LCD_BACKLIGHT_TIMEOUT;
uint16_t freq;
uint8_t vol = 1;
uint8_t rds = 0;
uint8_t rdschartoshow = 0;
void drawScreen(void) {
uint8_t rssi, stereo;
freq = fm_getChannel(&rssi, &stereo);
const uint8_t xpointerpx = (bmp_scale.width * (freq - MINSCALEFREQ)) / (MAXSCALEFREQ - MINSCALEFREQ);
const uint8_t freqsize = str_putrawfreq(strbuff, freq, 0)*5+1;
const uint8_t startx = (xpointerpx < 2 + (freqsize >> 1)) ? 0 : ( (xpointerpx + (freqsize >> 1) + 2 > LCD_X) ? (LCD_X - freqsize - 4) : (xpointerpx - (freqsize >> 1) - 2) );
const uint8_t sizeoftext = 8;
const uint8_t pos_of_vfo = LCD_Y-16+sizeoftext;
const uint8_t size_of_vfo = 16-sizeoftext;
lcd_bitmap(&bmp_scale, 0, 0, 4);
lcd_clearrect(startx, LCD_Y-18, freqsize+3, sizeoftext+3);
lcd_fillrect(startx+1, LCD_Y-17, freqsize+1, sizeoftext+1);
lcd_clearrect(xpointerpx - 4, pos_of_vfo, 1, size_of_vfo);
lcd_fillrect(xpointerpx - 3, pos_of_vfo, 3, size_of_vfo);
lcd_clearrect(xpointerpx, pos_of_vfo, 1, size_of_vfo);
lcd_fillrect(xpointerpx + 1, pos_of_vfo, 3, size_of_vfo);
lcd_clearrect(xpointerpx + 4, pos_of_vfo, 1, size_of_vfo);
lcd_stringwhite(strbuff, startx+2, 4);
lcd_stringlarge(rdsname, 0, 1);
if (rdschartoshow<64)
lcd_string(&rdsrt[rdschartoshow], 0, 3);
if (rdschartoshow>64-18)
lcd_string(rdsrt, 5*(65-rdschartoshow), 3);
lcd_bitmap(&bmp_sound, (vol == 0) ? (0) : (1+5 * (vol-1) / 14), 0, 0);
if (rds == RDS_AVAILABLE) lcd_bitmap(&bmp_rds, 0, 20, 0);
if (stereo) lcd_bitmap(&bmp_stereo, 0, 43, 0);
lcd_bitmap(&bmp_rssi, (rssi < 20) ? (0) : ( (rssi > 55) ? (6) : (1+5 * (rssi-20) / (55-20))), 64, 0);
}
int main(void) {
// Initialise ports and SPI
port_direction_init();
buttons_init();
init_spi(3);
lcd_init();
uint8_t fmstat = fm_turn_on();
// Let everything settle
_delay_ms(1000);
SREG |= 0x80 ;
lcd_clear();
drawScreen();
lcd_repaint();
lcd_timeout = LCD_BACKLIGHT_TIMEOUT;
lcd_backlight(1);
uint8_t letter;
uint8_t state;
fm_setVolume(vol);
while (1)
{
if (lcd_timeout == 1) {
lcd_timeout = 0;
lcd_backlight(0);
lcd_clear();
drawScreen();
lcd_repaint();
lcd_timeout = LCD_BACKLIGHT_TIMEOUT;
} else if (lcd_timeout != 0)
lcd_timeout--;
if (buttons_poll(&letter, &state) && state == BUTTONS_PRESSED) {
lcd_timeout = LCD_BACKLIGHT_TIMEOUT;
lcd_backlight(1);
switch(letter) {
case '1':
fm_seek(0);
rdschartoshow = 0;
break;
case '3':
fm_seek(1);
rdschartoshow = 0;
break;
case '#':
if (vol < 15) vol++;
fm_setVolume(vol);
break;
case '*':
if (vol > 0) vol--;
fm_setVolume(vol);
break;
case '6':
if (freq == 1080)
freq = 875;
else
freq++;
fm_setChannel(freq);
rdschartoshow = 0;
break;
case '4':
if (freq == 875)
freq = 1080;
else
freq--;
fm_setChannel(freq);
rdschartoshow = 0;
break;
case '9':
if (rdschartoshow == 65)
rdschartoshow = 0;
else
rdschartoshow++;
break;
case '7':
if (rdschartoshow == 0)
rdschartoshow = 65;
else
rdschartoshow--;
break;
}
lcd_clear();
drawScreen();
lcd_repaint();
} else if (rds = fm_readRDS(rdsname, rdsrt)) {
lcd_clear();
drawScreen();
lcd_repaint();
}
}
}