1
|
#include <Bridge.h>
|
2
|
#include <YunServer.h>
|
3
|
#include <YunClient.h>
|
4
|
#include <Process.h>
|
5
|
|
6
|
#include <TFT.h> // Arduino LCD library
|
7
|
#include <SPI.h>
|
8
|
|
9
|
#define cs 10
|
10
|
#define dc 9
|
11
|
#define rst 8
|
12
|
|
13
|
/* Rotary encoder read */
|
14
|
#define ENC_A 3
|
15
|
#define ENC_B 2
|
16
|
#define ENC_PORT PIND
|
17
|
/* Play Stop Button */
|
18
|
#define PLAY 7
|
19
|
|
20
|
#define NCH 16
|
21
|
|
22
|
TFT tft = TFT(cs, dc, rst);
|
23
|
char chPrint[3] = {'0', '1', '\0'};
|
24
|
char chanPrint[20];
|
25
|
char namePrint[80];
|
26
|
char genrePrint[40];
|
27
|
char titlePrint[80];
|
28
|
String nameString, genreString, titleString;
|
29
|
unsigned long lastChange;
|
30
|
unsigned long now, nextEnc, nextCli, nextScr;
|
31
|
unsigned char chn = 1;
|
32
|
|
33
|
boolean playState;
|
34
|
boolean playPressed;
|
35
|
|
36
|
String chnStr;
|
37
|
String channelStr[16] = {
|
38
|
String("01"), String("02"), String("03"), String("04"),
|
39
|
String("05"), String("06"), String("07"), String("08"),
|
40
|
String("09"), String("10"), String("11"), String("12"),
|
41
|
String("13"), String("14"), String("15"), String("16")};
|
42
|
String chStr[16] = {
|
43
|
String("01"), String("02"), String("03"), String("04"),
|
44
|
String("05"), String("06"), String("07"), String("08"),
|
45
|
String("09"), String("10"), String("11"), String("12"),
|
46
|
String("13"), String("14"), String("15"), String("16")};
|
47
|
|
48
|
// Listen on default port 5555, the webserver on the Yun
|
49
|
// will forward there all the HTTP requests for us.
|
50
|
YunServer server;
|
51
|
|
52
|
void setup() {
|
53
|
/* Setup encoder pins as inputs */
|
54
|
pinMode(ENC_A, INPUT_PULLUP);
|
55
|
//digitalWrite(ENC_A, HIGH);
|
56
|
pinMode(ENC_B, INPUT_PULLUP);
|
57
|
//digitalWrite(ENC_B, HIGH);
|
58
|
pinMode(PLAY, INPUT_PULLUP);
|
59
|
|
60
|
// Initialize Bridge
|
61
|
Bridge.begin();
|
62
|
|
63
|
server.listenOnLocalhost();
|
64
|
server.begin();
|
65
|
|
66
|
tft.begin();
|
67
|
tft.background(0, 0, 0);
|
68
|
tft.stroke(0, 0, 255);
|
69
|
tft.setTextSize(2);
|
70
|
tft.setTextWrap(true);
|
71
|
tft.text("FabLab Radio", 0, 0);
|
72
|
|
73
|
chnStr = channelStr[0];
|
74
|
|
75
|
chnStr.toCharArray(chanPrint, chnStr.length() + 1);
|
76
|
tft.stroke(255, 255, 255);
|
77
|
tft.text(chanPrint, 0, 20);
|
78
|
|
79
|
tft.stroke(0, 0, 255);
|
80
|
tft.text("Stop", 0, 110);
|
81
|
|
82
|
tft.stroke(0, 255, 0);
|
83
|
tft.text(chPrint, 130, 110);
|
84
|
|
85
|
playState = false;
|
86
|
playPressed = false;
|
87
|
|
88
|
lastChange = millis();
|
89
|
nextEnc = millis();
|
90
|
nextCli = millis();
|
91
|
nextScr = millis();
|
92
|
}
|
93
|
|
94
|
void loop() {
|
95
|
static uint8_t counter = 0; //this variable will be changed by encoder input
|
96
|
int8_t tmpdata;
|
97
|
static boolean bis = false;
|
98
|
|
99
|
now = millis();
|
100
|
if (now >= nextEnc) {
|
101
|
nextEnc = now + 5;
|
102
|
|
103
|
read_button();
|
104
|
if (playPressed) {
|
105
|
runRadio(chn);
|
106
|
}
|
107
|
|
108
|
tmpdata = read_encoder();
|
109
|
if(tmpdata != 0) {
|
110
|
bis = !bis;
|
111
|
if (bis) {
|
112
|
if ((millis() - lastChange) > 500) {
|
113
|
chStr[chn - 1].toCharArray(chPrint, 3);
|
114
|
tft.setTextSize(2);
|
115
|
tft.stroke(0, 0, 0);
|
116
|
tft.text(chPrint, 130, 110);
|
117
|
|
118
|
counter += tmpdata;
|
119
|
chn = counter % NCH + 1;
|
120
|
|
121
|
chStr[chn - 1].toCharArray(chPrint, 3);
|
122
|
tft.stroke(0, 255, 0);
|
123
|
tft.text(chPrint, 130, 110);
|
124
|
|
125
|
runRadio(chn);
|
126
|
lastChange = millis();
|
127
|
}
|
128
|
//delay(10);
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
|
133
|
now = millis();
|
134
|
if (now >= nextCli) {
|
135
|
nextCli = now + 50;
|
136
|
|
137
|
YunClient client = server.accept();
|
138
|
|
139
|
if (client) {
|
140
|
process(client);
|
141
|
client.stop();
|
142
|
}
|
143
|
}
|
144
|
}
|
145
|
|
146
|
void runRadio(int n) {
|
147
|
String ch = String(n);
|
148
|
String pl = playState ? "1" : "0";
|
149
|
|
150
|
chnStr = channelStr[n - 1];
|
151
|
|
152
|
tft.setTextSize(2);
|
153
|
tft.stroke(0, 0, 0);
|
154
|
tft.text(chanPrint, 0, 20);
|
155
|
chnStr.toCharArray(chanPrint, chnStr.length() + 1);
|
156
|
//tft.background(0, 0, 0);
|
157
|
tft.stroke(255, 255, 255);
|
158
|
tft.text(chanPrint, 0, 20);
|
159
|
|
160
|
if (playState)
|
161
|
{
|
162
|
tft.stroke(0, 0, 0);
|
163
|
tft.text("Stop", 0, 110);
|
164
|
tft.stroke(0, 255, 0);
|
165
|
tft.text("Play", 0, 110);
|
166
|
}
|
167
|
else
|
168
|
{
|
169
|
tft.stroke(0, 0, 0);
|
170
|
tft.text("Play", 0, 110);
|
171
|
tft.stroke(0, 0, 255);
|
172
|
tft.text("Stop", 0, 110);
|
173
|
|
174
|
tft.setTextSize(1);
|
175
|
tft.stroke(0, 0, 0);
|
176
|
tft.textWrap(namePrint, 0, 40);
|
177
|
tft.textWrap(genrePrint, 0, 70);
|
178
|
tft.textWrap(titlePrint, 0, 80);
|
179
|
}
|
180
|
|
181
|
Process p;
|
182
|
|
183
|
p.begin("/root/radioyun1.sh");
|
184
|
p.addParameter(ch);
|
185
|
p.addParameter(pl);
|
186
|
p.run();
|
187
|
}
|
188
|
|
189
|
void read_button()
|
190
|
{
|
191
|
if (digitalRead(PLAY) == LOW && playPressed == false)
|
192
|
{
|
193
|
playPressed = true;
|
194
|
playState = !playState;
|
195
|
}
|
196
|
else if (playPressed == true && digitalRead(PLAY) == HIGH)
|
197
|
{
|
198
|
playPressed = false;
|
199
|
}
|
200
|
}
|
201
|
|
202
|
/* returns change in encoder state (-1,0,1) */
|
203
|
int8_t read_encoder()
|
204
|
{
|
205
|
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
|
206
|
static uint8_t old_AB = 0;
|
207
|
|
208
|
old_AB <<= 2; //remember previous state
|
209
|
old_AB |= ( ENC_PORT & 0x03 ); //add current state
|
210
|
return ( enc_states[( old_AB & 0x0f )]);
|
211
|
}
|
212
|
|
213
|
void process(YunClient client) {
|
214
|
// read the command
|
215
|
String command = client.readStringUntil('/');
|
216
|
|
217
|
if (command == "name") {
|
218
|
nameCommand(client);
|
219
|
} else if (command == "genre") {
|
220
|
genreCommand(client);
|
221
|
} else if (command == "title") {
|
222
|
titleCommand(client);
|
223
|
} else if (command == "chname") {
|
224
|
chnameCommand(client);
|
225
|
}
|
226
|
}
|
227
|
|
228
|
void nameCommand(YunClient client) {
|
229
|
|
230
|
nameString = client.readStringUntil('/');
|
231
|
|
232
|
// Send to the Screen
|
233
|
if (playState)
|
234
|
{
|
235
|
tft.setTextSize(1);
|
236
|
tft.stroke(0, 0, 0);
|
237
|
tft.textWrap(namePrint, 0, 40);
|
238
|
int msgLen = nameString.length();
|
239
|
if (msgLen < 78)
|
240
|
nameString.toCharArray(namePrint, msgLen);
|
241
|
else
|
242
|
nameString.toCharArray(namePrint, 78);
|
243
|
//tft.background(0, 0, 0);
|
244
|
tft.stroke(255, 127, 63);
|
245
|
tft.textWrap(namePrint, 0, 40);
|
246
|
}
|
247
|
}
|
248
|
|
249
|
void genreCommand(YunClient client) {
|
250
|
|
251
|
genreString = client.readStringUntil('/');
|
252
|
|
253
|
// Send to the Screen
|
254
|
if (playState)
|
255
|
{
|
256
|
tft.setTextSize(1);
|
257
|
tft.stroke(0, 0, 0);
|
258
|
tft.textWrap(genrePrint, 0, 70);
|
259
|
int msgLen = genreString.length();
|
260
|
if (msgLen < 40)
|
261
|
genreString.toCharArray(genrePrint, genreString.length());
|
262
|
else
|
263
|
genreString.toCharArray(genrePrint, 40);
|
264
|
//tft.background(0, 0, 0);
|
265
|
tft.stroke(127, 255, 63);
|
266
|
tft.textWrap(genrePrint, 0, 70);
|
267
|
}
|
268
|
}
|
269
|
|
270
|
void titleCommand(YunClient client) {
|
271
|
|
272
|
titleString = client.readStringUntil('/');
|
273
|
|
274
|
// Send to the Screen
|
275
|
if (playState)
|
276
|
{
|
277
|
tft.setTextSize(1);
|
278
|
tft.stroke(0, 0, 0);
|
279
|
tft.textWrap(titlePrint, 0, 80);
|
280
|
int msgLen = titleString.length();
|
281
|
if (msgLen < 78)
|
282
|
titleString.toCharArray(titlePrint, titleString.length());
|
283
|
else
|
284
|
titleString.toCharArray(titlePrint, 78);
|
285
|
//tft.background(0, 0, 0);
|
286
|
tft.stroke(63, 127, 255);
|
287
|
tft.textWrap(titlePrint, 0, 80);
|
288
|
}
|
289
|
}
|
290
|
|
291
|
void chnameCommand(YunClient client) {
|
292
|
|
293
|
String message = client.readStringUntil('/');
|
294
|
int numch = client.parseInt();
|
295
|
|
296
|
channelStr[numch - 1] = message;
|
297
|
}
|
298
|
|