1
|
#include <LiquidCrystal.h>
|
2
|
|
3
|
/* Rotary encoder read example */
|
4
|
#define ENC_A 14
|
5
|
#define ENC_B 15
|
6
|
#define ENC_PORT PINC
|
7
|
#define PLAY 8
|
8
|
|
9
|
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
|
10
|
|
11
|
String channelStr[16] = {
|
12
|
String("channel01"), String("channel02"), String("channel03"), String("channel04"),
|
13
|
String("channel05"), String("channel06"), String("channel07"), String("channel08"),
|
14
|
String("channel09"), String("channel10"), String("channel11"), String("channel12"),
|
15
|
String("channel13"), String("channel14"), String("channel15"), String("channel16")};
|
16
|
|
17
|
String name = String("");
|
18
|
String title = String("");
|
19
|
|
20
|
byte numChan = 16;
|
21
|
|
22
|
char inData[100];
|
23
|
byte index = 0;
|
24
|
byte numChar = 23;
|
25
|
byte numCh;
|
26
|
|
27
|
boolean playState;
|
28
|
boolean playPressed;
|
29
|
|
30
|
unsigned long scroll;
|
31
|
int scnm = 0;
|
32
|
int sctl = 0;
|
33
|
int scn = 0;
|
34
|
int sct = 0;
|
35
|
int ndir = 1;
|
36
|
int sdir = 1;
|
37
|
|
38
|
void setup()
|
39
|
{
|
40
|
/* Setup encoder pins as inputs */
|
41
|
pinMode(ENC_A, INPUT);
|
42
|
digitalWrite(ENC_A, HIGH);
|
43
|
pinMode(ENC_B, INPUT);
|
44
|
digitalWrite(ENC_B, HIGH);
|
45
|
|
46
|
pinMode(PLAY, INPUT);
|
47
|
digitalWrite(PLAY, HIGH);
|
48
|
|
49
|
Serial.begin (115200);
|
50
|
Serial.println("Start");
|
51
|
|
52
|
// set up the LCD's number of columns and rows:
|
53
|
lcd.begin(20, 4);
|
54
|
// Print a message to the LCD.
|
55
|
lcd.setCursor(1, 0);
|
56
|
lcd.print(channelStr[0]);
|
57
|
lcd.setCursor(0, 0);
|
58
|
lcd.print(">");
|
59
|
|
60
|
playState = false;
|
61
|
playPressed = false;
|
62
|
|
63
|
lcd.setCursor(16, 3);
|
64
|
lcd.print("Stop");
|
65
|
|
66
|
scroll = millis();
|
67
|
}
|
68
|
|
69
|
void loop()
|
70
|
{
|
71
|
static uint8_t counter = 0; //this variable will be changed by encoder input
|
72
|
static boolean bis = false;
|
73
|
int8_t tmpdata;
|
74
|
char c;
|
75
|
|
76
|
if (Serial.available() > 0) {
|
77
|
if (index < numChar) {
|
78
|
if (index == 0) {
|
79
|
c = Serial.read();
|
80
|
if (c == 'A' || c == 'N' || c == 'S') {
|
81
|
inData[index++] = c;
|
82
|
}
|
83
|
}
|
84
|
else if (index == 1 && (inData[0] == 'A' || inData[0] == 'N' || inData[0] == 'S')) {
|
85
|
c = Serial.read();
|
86
|
if (c == 'B' || c == 'M' || c == 'T') {
|
87
|
inData[index++] = c;
|
88
|
}
|
89
|
else {
|
90
|
inData[0] = ' ';
|
91
|
index = 0;
|
92
|
}
|
93
|
}
|
94
|
else if (index == 2 && inData[0] == 'N' && inData[1] == 'M') {
|
95
|
c = Serial.read();
|
96
|
inData[index++] = c;
|
97
|
numCh = (c - 48) * 10;
|
98
|
}
|
99
|
else if (index == 3 && inData[0] == 'N' && inData[1] == 'M') {
|
100
|
c = Serial.read();
|
101
|
inData[index++] = c;
|
102
|
numCh = numCh + c - 48;
|
103
|
numChar = numCh;
|
104
|
}
|
105
|
else if (index == 2 && inData[0] == 'S' && inData[1] == 'T') {
|
106
|
c = Serial.read();
|
107
|
inData[index++] = c;
|
108
|
numCh = (c - 48) * 10;
|
109
|
}
|
110
|
else if (index == 3 && inData[0] == 'S' && inData[1] == 'T') {
|
111
|
c = Serial.read();
|
112
|
inData[index++] = c;
|
113
|
numCh = numCh + c - 48;
|
114
|
numChar = numCh;
|
115
|
}
|
116
|
else {
|
117
|
inData[index++] = Serial.read();
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
|
122
|
if (index >= numChar) {
|
123
|
index = 0;
|
124
|
Serial.flush();
|
125
|
}
|
126
|
|
127
|
String tmpStr = String("");
|
128
|
|
129
|
if (index == 0 && inData[0] == 'A' && inData[1] == 'B') {
|
130
|
for(int i = 0; i < (numChar - 2); i++) {
|
131
|
if (i > 1) {
|
132
|
if (inData[i] == '#')
|
133
|
tmpStr += ' ';
|
134
|
else
|
135
|
tmpStr += inData[i];
|
136
|
}
|
137
|
inData[i] = ' ';
|
138
|
}
|
139
|
int chNum = (inData[numChar - 2] - 48) * 10 + (inData[numChar - 1] - 48);
|
140
|
channelStr[chNum] = tmpStr;
|
141
|
if (chNum == (counter % numChan)) {
|
142
|
lcd.setCursor(1, 0);
|
143
|
lcd.print(channelStr[chNum]);
|
144
|
Serial.print(counter, DEC);
|
145
|
Serial.print('\n');
|
146
|
}
|
147
|
lcd.setCursor(8, 3);
|
148
|
lcd.print(" ");
|
149
|
lcd.setCursor(8, 3);
|
150
|
lcd.print(chNum, DEC);
|
151
|
inData[numChar - 2] = ' ';
|
152
|
inData[numChar - 1] = ' ';
|
153
|
}
|
154
|
else if (index == 0 && inData[0] == 'N' && inData[1] == 'M') {
|
155
|
for(int i = 0; i < numChar; i++) {
|
156
|
if (i > 3) {
|
157
|
tmpStr += inData[i];
|
158
|
}
|
159
|
inData[i] = ' ';
|
160
|
}
|
161
|
lcd.setCursor(0, 1);
|
162
|
lcd.print(" ");
|
163
|
lcd.setCursor(0, 1);
|
164
|
tmpStr.trim();
|
165
|
name = tmpStr;
|
166
|
lcd.print(name.substring(0, 20));
|
167
|
numChar = 23;
|
168
|
}
|
169
|
else if (index == 0 && inData[0] == 'S' && inData[1] == 'T') {
|
170
|
for(int i = 0; i < numChar; i++) {
|
171
|
if (i > 3) {
|
172
|
tmpStr += inData[i];
|
173
|
}
|
174
|
inData[i] = ' ';
|
175
|
}
|
176
|
lcd.setCursor(0, 2);
|
177
|
lcd.print(" ");
|
178
|
lcd.setCursor(0, 2);
|
179
|
tmpStr.trim();
|
180
|
title = tmpStr;
|
181
|
lcd.print(title.substring(0, 20));
|
182
|
numChar = 23;
|
183
|
sctl = 0;
|
184
|
sct = 0;
|
185
|
sdir = 1;
|
186
|
}
|
187
|
|
188
|
if (millis() > scroll) {
|
189
|
scroll = scroll + 200;
|
190
|
|
191
|
int nmlen = name.length();
|
192
|
int tllen = title.length();
|
193
|
|
194
|
if (nmlen > 20) {
|
195
|
if (scnm >= 0 && scnm <= nmlen - 20) {
|
196
|
lcd.setCursor(0, 1);
|
197
|
lcd.print(name.substring(scnm, 20 + scnm));
|
198
|
if (scnm == 0 && scn < 10) {
|
199
|
scn++;
|
200
|
ndir = 1;
|
201
|
}
|
202
|
else if (scnm == (nmlen - 20) && scn < 10) {
|
203
|
scn++;
|
204
|
ndir = -1;
|
205
|
}
|
206
|
else
|
207
|
{
|
208
|
scnm += ndir;
|
209
|
scn = 0;
|
210
|
}
|
211
|
}
|
212
|
//else {
|
213
|
//scnm = 0;
|
214
|
//}
|
215
|
}
|
216
|
|
217
|
if (tllen > 20) {
|
218
|
if (sctl >= 0 && sctl <= tllen - 20) {
|
219
|
lcd.setCursor(0, 2);
|
220
|
lcd.print(title.substring(sctl, 20 + sctl));
|
221
|
if (sctl == 0 && sct < 10) {
|
222
|
sct++;
|
223
|
sdir = 1;
|
224
|
}
|
225
|
else if (sctl == (tllen - 20) && sct < 10) {
|
226
|
sct++;
|
227
|
sdir = -1;
|
228
|
}
|
229
|
else
|
230
|
{
|
231
|
sctl += sdir;
|
232
|
sct = 0;
|
233
|
}
|
234
|
}
|
235
|
//else {
|
236
|
//sctl = 0;
|
237
|
//}
|
238
|
}
|
239
|
}
|
240
|
|
241
|
read_button();
|
242
|
|
243
|
tmpdata = read_encoder();
|
244
|
delay(1);
|
245
|
if (tmpdata != 0) {
|
246
|
bis = !bis;
|
247
|
if (bis) {
|
248
|
counter += tmpdata;
|
249
|
|
250
|
//Serial.print("Counter value: ");
|
251
|
Serial.print((counter % 16), DEC);
|
252
|
Serial.print('\n');
|
253
|
|
254
|
lcd.setCursor(0, 0);
|
255
|
lcd.print(" ");
|
256
|
lcd.setCursor(1, 0);
|
257
|
lcd.print(" ");
|
258
|
lcd.setCursor(1, 0);
|
259
|
lcd.print(channelStr[counter % numChan]);
|
260
|
lcd.setCursor(0, 0);
|
261
|
lcd.print(">");
|
262
|
|
263
|
lcd.setCursor(12, 3);
|
264
|
lcd.print(" ");
|
265
|
lcd.setCursor(12, 3);
|
266
|
lcd.print(counter, DEC);
|
267
|
|
268
|
scnm = 0;
|
269
|
sctl = 0;
|
270
|
scn = 0;
|
271
|
sct = 0;
|
272
|
ndir = 1;
|
273
|
sdir = 1;
|
274
|
name = "";
|
275
|
title = "";
|
276
|
}
|
277
|
}
|
278
|
}
|
279
|
|
280
|
void read_button()
|
281
|
{
|
282
|
if (digitalRead(PLAY) == LOW && playPressed == false)
|
283
|
{
|
284
|
playPressed = true;
|
285
|
playState = !playState;
|
286
|
scnm = 0;
|
287
|
sctl = 0;
|
288
|
scn = 0;
|
289
|
sct = 0;
|
290
|
ndir = 1;
|
291
|
sdir = 1;
|
292
|
name = "";
|
293
|
title = "";
|
294
|
lcd.setCursor(16, 3);
|
295
|
if (playState)
|
296
|
{
|
297
|
lcd.print("Play");
|
298
|
Serial.print(100, DEC);
|
299
|
Serial.print('\n');
|
300
|
}
|
301
|
else
|
302
|
{
|
303
|
lcd.print("Stop");
|
304
|
Serial.print(101, DEC);
|
305
|
Serial.print('\n');
|
306
|
}
|
307
|
}
|
308
|
else if (playPressed == true && digitalRead(PLAY) == HIGH)
|
309
|
{
|
310
|
playPressed = false;
|
311
|
}
|
312
|
}
|
313
|
|
314
|
/* returns change in encoder state (-1,0,1) */
|
315
|
int8_t read_encoder()
|
316
|
{
|
317
|
static int8_t enc_states[] = {
|
318
|
0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0 };
|
319
|
static uint8_t old_AB = 0;
|
320
|
|
321
|
old_AB <<= 2; //remember previous state
|
322
|
old_AB |= ( ENC_PORT & 0x03 ); //add current state
|
323
|
return (enc_states[( old_AB & 0x0f )]);
|
324
|
}
|
325
|
|