-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAmp_Control.ino
More file actions
378 lines (321 loc) · 6.69 KB
/
Amp_Control.ino
File metadata and controls
378 lines (321 loc) · 6.69 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
Jeelabs Library <www.jeelabs.org>
AudioPlug Library <info@theater4all.nl> http://opensource.org/licenses/mit-license.php
Wiring Connections
- Pwr Relay - D4 Orange
- CS - D7 Blue Pin 2
- Clk - A2 Grey Pin 6
- Data - D6 Orange Pin 3
- Mute - A3 White Pin 8
- IR - A1 Port 2
Basic Functions
- Control Power Relay - Done
- Control Mute, Volume Increment and Decrement
- Via IR - Done
- Via RF - Note sure yet if I will implement this.
- Learn IR Codes and Associate - Done
- Store in Eprom - Done
EEPROM Memory Mapping
- 1 to 4 Volume Up IR
- 5 to 8 Volume Down IR
- 9 to 12 Mute IR
- 13 to 16 Power IR
- 17 Start Up Volume IR. Limited on recall. If Greater >150 then zero is returned. For sanity!
*/
//Jeelib Setup
#include <JeeLib.h>
#define IRPORT 2 //IR Data Input Port
InfraredPlug ir (IRPORT);
//AudioPlug Setup
#include <AudioPlug.h>
#define INCR 3 //Volume Increments
AudioPlug Plug(PORTHIGH);
//IR Code Setup
#include <EEPROM.h>
byte VolIncr[4];
byte VolDecr[4];
byte MuteCode[4];
byte PwrCode[4];
byte count;
const byte* data;
byte lastValue[4];
byte volValue;
bool ValidCode;
bool muteActive;
//Other Setup
#define POWERRELAY 4 //Power Relay Digital PIN
bool pwrActive;
byte value;
//End of Config
//Interupt Vector - Called when IR data causes interupt
ISR(PCINT1_vect) {
ir.poll();
}
void setup ()
{
delay(500);
Serial.begin(57600);
Serial.println("\n[PGA2311 Amp]");
//Enable pin change interrupts on PC1
PCMSK1 = bit(1);
PCICR |= bit(PCIE1);
//init state variables
ValidCode = false;
muteActive = false;
pwrActive = false;
//Setup Relay Pin as Output and ensure the relay is off.
pinMode(POWERRELAY, OUTPUT);
digitalWrite(POWERRELAY, pwrActive);
// init audioPlug
Plug.addBoard(TWOCHANNELS);
setVolume(recallStartVolume());
//recall IR codes in EEprom, these could be invalid if never learnt.
recallIR();
}
bool getIRValue(void)
{
while (1)
{
count = ir.done();
if (count > 0)
{
data = ir.buffer();
if (ir.decoder(count) == InfraredPlug::NEC)
{
return true;
}
return false;
}
}
}
void updateVolume(byte val)
{
Serial.print(F("Volume: "));
int f = 31 - ((255 - val) >> 1);
if (f >= 0)
{
Serial.print (" ");
if (f < 10) Serial.print (" ");
Serial.print(f,DEC);
}
else if (f > -10) {
Serial.print("- ");
Serial.print(-f,DEC);
}
else {
Serial.print(f,DEC);
}
Serial.println("dB");
}
void setVolume(byte val)
{
Plug.setVolume(0, val);
Plug.setVolume(1, val);
updateVolume(val);
Plug.sendVolumeData();
}
void printHelp()
{
Serial.println(F("Any Key - Help"));
Serial.println(F("L - Learn IR"));
Serial.println(F("nS - Store start volume, 1 = store, 0 = reset"));
Serial.println(F("U - Volume Up"));
Serial.println(F("D - Volume Down"));
Serial.println(F("nV - Set Volume, 0-255"));
Serial.println(F("nM - Mute, 0 or 1"));
Serial.println(F("nP - Power, 0 or 1"));
}
void incrementVolume(bool d = 0)
{
int val = Plug.getVolume(0);
if (d == 1)
{
val += INCR;
if (val > 255) val = 255;
}
else
{
val -= INCR;
if (val < 0) val = 0;
}
setVolume(val);
}
//Handle Data on the Serial Port
void handleSerial (char chr)
{
// Should Respond to the following commands
// L - Learn IR Commands, follow on screen instructions
// U - Volume Up
// D - Volume Down
// V - Set Volume, 0-255
// G - Set Gain
// M - Mute, 0 or 1 State
// P - Power, 0 or 1 State
// Unknown Key - Help Text
if (chr >= '0' & chr <= '9')
{
value = value * 10 + chr - '0';
}
else
{
switch (chr)
{
case 'L':
learnIR();
return;
case 'U':
incrementVolume(1);
return;
case 'D':
incrementVolume();
return;
case 'V':
setVolume(value);
value = 0;
return;
case 'M':
muteActive = value;
setMute(muteActive);
value = 0;
return;
case 'P':
setPowerRelay (value);
value = 0;
return;
case 'S':
setStartVolume (value);
value = 0;
return;
default :
printHelp();
}
value = 0;
}
}
void loop ()
{
if(Serial.available()) handleSerial(Serial.read());
count = ir.done();
if (count > 0)
{
data = ir.buffer();
// check code
if (ir.decoder(count) == InfraredPlug::NEC)
{
lastValue[2] = data[2];
lastValue[3] = data[3];
ValidCode = true;
}
else if (ir.decoder(count) == InfraredPlug::NEC_REP)
{
// do not repeat Mute-toggle
if (((lastValue[2] == MuteCode[2]) and (lastValue[3] == MuteCode[3])) or ((lastValue[2] == PwrCode[2]) and (lastValue[3] == PwrCode[3])))
{
ValidCode = !true;
}
else
{
ValidCode = true;
delay(200);
}
}
// got something ?
if (ValidCode)
{
ValidCode = false;
if ((lastValue[2] == MuteCode[2]) and (lastValue[3] == MuteCode[3]))
{
muteActive = !muteActive;
setMute(muteActive);
}
else if ((lastValue[2] == VolIncr[2]) and (lastValue[3] == VolIncr[3]))
{
incrementVolume(1);
}
else if ((lastValue[2] == VolDecr[2]) and (lastValue[3] == VolDecr[3]))
{
incrementVolume();
}
else if ((lastValue[2] == PwrCode[2]) and (lastValue[3] == PwrCode[3]))
{
pwrActive = !pwrActive;
setPowerRelay(pwrActive);
}
}
}
}
void setMute (bool state)
{
Serial.println(state ? "Mute On" : "Mute Off");
Plug.setMute(state ? MUTEON : MUTEOFF);
}
void setPowerRelay (bool state)
{
Serial.println(state ? "Power On" : "Power Off");
digitalWrite(POWERRELAY, state);
}
//Routine to learn IR commands
void learnIR ()
{
Serial.println(F("IR Learning Mode"));
Serial.println(F("Press:"));
Serial.println(F("Volume Up"));
if (getIRValue()) writeIR (1);
delay(300);
Serial.println(F("Volume Down"));
if (getIRValue()) writeIR (5);
delay(300);
Serial.println(F("Press Mute"));
if (getIRValue()) writeIR (9);
delay(300);
Serial.println(F("Press Pwr"));
if (getIRValue()) writeIR (13);
delay(300);
Serial.println(F("Done"));
delay(100);
recallIR();
}
//Set the amplifier start up volume
void setStartVolume(byte value)
{
if (value == 1)
{
EEPROM.write(17, Plug.getVolume(0));
}
else
{
EEPROM.write(17, 0);
}
Serial.print("Start ");
updateVolume(recallStartVolume());
}
//Recall IR Commands in EEprom
void recallIR()
{
readIR(1, VolIncr);
readIR(5, VolDecr);
readIR(9, MuteCode);
readIR(13, PwrCode);
}
//Write current IR data buffer to EEProm at address specified
void writeIR (int Addr)
{
for (int i = Addr; i < Addr + 4; i++)
{
EEPROM.write(i, data[i-Addr]);
}
}
//Read an IR command from a location and store in array pointed to
void readIR (int Addr, byte* Arr)
{
for (int i = Addr; i < Addr + 4; i++)
{
Arr[i-Addr] = EEPROM.read(i), HEX;
}
}
//Recall start volume
byte recallStartVolume()
{
byte v = EEPROM.read(17);
return v > 150 ? 0 : v;
}