forked from mastmees/silicon_radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
silicon_radio.cpp
307 lines (285 loc) · 7.58 KB
/
silicon_radio.cpp
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
/*
The MIT License (MIT)
Copyright (c) 2016 Madis Kaal <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include "si4703.hpp"
#include "display.hpp"
#include "meter.hpp"
#include "encoder.hpp"
uint16_t EEMEM ee_frequency = 9780; // Retro FM in Tallinn, Estonia
uint16_t frequency;
VU_Meter meter;
SI4703 radio;
RDSDecoder decoder;
Display display;
Encoder encoder;
// baseradio defines some pure virtuals, so we need to
// create a handler for this
extern "C" void __cxa_pure_virtual()
{
display.clear();
display.puts("Purevirt");
while (1); // we'll suffer horrible death by watchdog in few seconds
}
enum DISPLAY_STATES { SKIP, SHOW, SCROLL };
// there must be at least one function that is always able to
// return SHOW or SCROLL result, otherwise radio_display may
// go to endless loop
uint8_t display_frequency()
{
display.clear();
int32_t f=radio.get_frequency()/10;
if (f<1000)
display.putc(' ');
display.putn(f/10);
display.putc('.');
display.putn(f%10);
display.putc(' ');
if (radio.is_stereo()) {
display.putc('S');
display.putc('T');
}
else {
display.putc('M');
display.putc(' ');
}
display.refresh();
return SHOW;
}
uint8_t display_station()
{
if (*decoder.get_ps()) {
display.puts(decoder.get_ps());
return SHOW;
}
return SKIP;
}
uint8_t display_radiotext()
{
if (*decoder.get_rt()) {
display.puts(decoder.get_rt());
return SCROLL;
}
return SKIP;
}
// these are functions will be called by main display logic.
// when new frequency is set, then the first function is always called
// main logic reacts to display function return codes
uint8_t (*displayfunctions[])() = {
display_frequency,
display_station,
display_radiotext,
NULL
};
// this handles encoder inputs and different display modes
//
void radio_display(uint8_t reset=0)
{
static uint8_t state=0,func=0,r;
static uint16_t count=0;
int8_t i;
if (reset) {
func=0;
state=0;
count=0;
}
else
count++;
i=encoder.read_encoder();
switch (i) {
case 1:
if (frequency<radio.get_max_frequency()) {
frequency=frequency+10;
radio.set_frequency(frequency);
func=0;
state=0;
}
break;
case -1:
if (frequency>radio.get_min_frequency()) {
frequency=frequency-10;
radio.set_frequency(frequency);
func=0;
state=0;
}
break;
}
if (encoder.read_button())
eeprom_write_word(&ee_frequency,frequency);
meter.stop();
while (!state) { // find next function with output
count=0;
if (displayfunctions[func]==NULL)
func=0;
r=displayfunctions[func++]();
if (r==SHOW)
state=1;
if (r==SCROLL)
state=2;
}
switch (state) {
case 1: // show text without scrolling
if (count>=400)
state=0;
break;
case 2: // wait a bit before starting scrolling
if (count>=120)
state=3;
break;
case 3: // scroll the text
if (count>20) {
if (display.scroll())
state=0;
count=0;
}
break;
}
meter.start();
}
ISR(TIMER0_OVF_vect)
{
// reset timer for next interrupt
TCNT0=0xc0;
}
ISR(WDT_vect)
{
}
ISR(PCINT0_vect)
{
}
ISR(PCINT1_vect)
{
}
/*
I/O configuration
-----------------
I/O pin direction DDR PORT
PC0 ON/OFF (low=ON) input 0 1
PC1 vu meter backlight led cathode output 1 1
PC2 button input 0 1
PC3 radio /RST output 1 1
PC4 SDA output 1 1
PC5 SCL output 1 1
PD0 D0 output 1 0
PD1 D1 output 1 0
PD2 D2 output 1 0
PD3 D3 output 1 0
PD4 D4 output 1 0
PD5 D5 output 1 0
PD6 D6 output 1 0
PD7 /WR output 1 1
PB0 A0 output 1 0
PB1 A1 output 1 0
PB2 /CE1 output 1 1
PB3 /CE2 output 1 1
PB4 encoder A input,pullup 0 1
PB5 encoder B input,pullup 0 1
*/
int main(void)
{
MCUSR=0;
MCUCR=0;
// I/O directions
DDRC=0x3a;
DDRD=0xff;
DDRB=0x0f;
// initial state
PORTC=0x3f;
PORTD=0x80;
PORTB=0x3c;
//
PCMSK1=0x04; // PCINT10 enable
PCMSK0=0x30; // PCINT4,5 enable
PCICR=3; // enable PCINT0,PCINT1
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
// configure watchdog to interrupt&reset, 4 sec timeout
WDTCSR|=0x18;
WDTCSR=0xe8;
TCCR0B=5; // timer0 clock prescaler to 256
TIMSK0=1; // enable overflow interrupts
TCNT0=0xc0;
display.clear();
sei();
display.puts("NORADIO");
if (!radio.is_connected())
while (1);
display.clear();
radio.init();
frequency=eeprom_read_word(&ee_frequency);
if (frequency>radio.get_max_frequency())
frequency=radio.get_max_frequency();
if (frequency<radio.get_min_frequency())
frequency=radio.get_min_frequency();
radio.set_decoder(&decoder);
radio.set_mono(0);
radio.set_soft_mute(1);
enum POWERSTATE { POWER_ON,STAY_ON,POWER_OFF,STAY_OFF };
uint8_t tcount=0,powerstate=(PINC&1)?POWER_OFF:POWER_ON;
while (1) {
sleep_cpu(); // timer ot pin change interrupt wakes us up
wdt_reset();
WDTCSR|=0x40;
switch (powerstate)
{
case STAY_ON:
if (PINC&1) {
powerstate=POWER_OFF;
break;
}
if ((tcount&3)==0)
{
radio.run();
meter.set(radio.get_rssi());
}
tcount=(tcount+1)&3;
radio_display();
break;
case STAY_OFF:
if (!(PINC&1))
powerstate=POWER_ON;
break;
default:
case POWER_OFF:
radio.set_volume(0);
meter.stop();
PORTC|=2; // meter backlight off
radio.sleep();
display.clear();
powerstate=STAY_OFF;
break;
case POWER_ON:
radio.wakeup();
radio.set_frequency(frequency);
PORTC&=~2; // meter backlight on
meter.start();
radio_display(1);
radio.set_volume(3);
tcount=0;
powerstate=STAY_ON;
break;
}
}
}