/
main.cpp
624 lines (580 loc) · 12.9 KB
/
main.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* Pre-amp Controller
*********************
Author Geoff Webster
Current Ver 3.0 Date 14 July 2023
- Changed mas6116::mas6116 construct in mas6116.cpp so that MUTE pin is initialized LOW
Ensures MUTE remains LOW for two seconds after power startup
Ver 2.0 Date 27 April 2022
- Changed mute pin to match new Controller board v2.0 (mutePin = 9). Mute pin used previously was A2 (on Ver 1.0 board)
- Added code to setup() routine which displays the SW version for two seconds at startup
*/
#include <Arduino.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <RC5.h>
#include <rotary.h>
#include <mas6116.h>
#include "custom.h"
#define VERSION_NUM "3.0" // Current software version number
/******* MACHINE STATES *******/
#define STATE_RUN 0 // normal run state
#define STATE_IO 1 // when user selects input/output
#define STATE_OFF 4 // when power down
#define ON LOW
#define OFF HIGH
#define STANDBY 0 // Standby
#define ACTIVE 1 // Active
#define EEPROM_FIRST_USE 0 // EEPROM location: First use
#define EEPROM_VOLUME 1 // EEPROM location: volume
#define EEPROM_SOURCE 2 // EEPROM location: source
#define EEPROM_BALANCE 3 // EEPROM location: balance
#define TIME_EXITSELECT 5 //** Time in seconds to exit I/O select mode when no activity
#define printByte(args) write(args);
/******* TIMING *******/
unsigned long milOnButton; // Stores last time for switch press
unsigned long milOnAction; // Stores last time of user input
unsigned long milOnFadeIn; // LCD fade timing
unsigned long milOnFadeOut; // LCD fade timing
/********* Global Variables *******************/
unsigned char volume; // current volume, between 0 and VOL_STEPS
unsigned char leftVol; // current left volume
unsigned char rightVol; // current right volume
unsigned char balance; // current balance (0<>20)
unsigned char backlight; // current backlight state
int counter = 0;
unsigned char source = 1; // current input channel
unsigned char oldsource = 1; // previous input channel
unsigned char oldtoggle;
unsigned char isMuted; // current mute status
unsigned char state = 0; // current machine state
unsigned char buttonState;
bool btnstate = 0;
unsigned char oldbtnstate = 0;
unsigned char rotarystate; // current rotary encoder status
unsigned char result = 0; // current rotary status
int analogPin = A1;
const char *inputName[] = {
"Phono ",
"Media ",
"CD ",
"Tuner "}; // Elektor i/p board
char buffer1[20] = "";
char balLeft[11] = {21, 20, 19, 18, 16, 15, 13, 11, 8, 5, 0};
char balRight[11] = {0, 5, 8, 11, 13, 15, 16, 18, 19, 20, 21};
// LCD construct
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
// define encoder pins
const char encoderPinA = 6;
const char encoderPinB = 5;
const char encoderbtn = 7;
// Rotary construct
Rotary rotary = Rotary(encoderPinA, encoderPinB, encoderbtn);
// define IR input
unsigned int IR_PIN = 8;
// unsigned long t0;
// RC5 construct
RC5 rc5(IR_PIN);
// define preAmp control pins
const int mutePin = 9;
const int csPin = 10;
// preAmp construct
mas6116 preamp(mutePin, csPin);
// Function prototypes
void RC5Update(void);
void setIO();
void volumeUpdate();
void buttonPressed();
void setVolume();
void sourceUpdate();
void mute();
void unMute();
void toggleMute();
void balanceUpdate();
void defineCustomChars();
void lcdPrintThreeNumber(unsigned char column, unsigned char number);
void saveIOValues();
void lcdPrintBal();
void lcdPrintSpaces();
// Powerdown Interrupt service routine
ISR(ANALOG_COMP_vect)
{
saveIOValues();
backlight = STANDBY;
lcd.noDisplay();
lcd.noBacklight(); // Turn off backlight
mute(); // mute output
state = STATE_OFF;
}
void saveIOValues()
{
EEPROM.update(EEPROM_VOLUME, volume);
EEPROM.update(EEPROM_SOURCE, source);
EEPROM.update(EEPROM_BALANCE, balance);
}
void lcdPrintBal()
{
sprintf(buffer1, " ");
lcd.setCursor(0, 3);
lcd.printstr(buffer1);
sprintf(buffer1, "Bal %d", balance - 5);
lcd.setCursor(0, 3);
lcd.printstr(buffer1);
}
void defineCustomChars()
{
lcd.createChar(0, cc0);
lcd.createChar(1, cc1);
lcd.createChar(2, cc2);
lcd.createChar(3, cc3);
lcd.createChar(4, cc4);
lcd.createChar(5, cc5);
lcd.createChar(6, cc6);
lcd.createChar(7, cc7);
}
void lcdPrintSpaces()
{
lcd.printByte(A); // Blank
lcd.printByte(A); // Blank
lcd.printByte(A); // Blank
lcd.printByte(A); // Blank
}
void lcdPrintThreeNumber(unsigned char column, unsigned char number)
{
unsigned char highnumber;
unsigned char midnumber;
unsigned char lownumber;
highnumber = number / 100;
midnumber = (number - (highnumber * 100)) / 10;
lownumber = (number - (highnumber * 100) - (midnumber * 10));
lcd.setCursor(column, 0);
if (highnumber == 0)
{
lcdPrintSpaces();
}
else
{
lcd.printByte(bn1[highnumber * 4]);
lcd.printByte(bn1[highnumber * 4 + 1]);
lcd.printByte(bn1[highnumber * 4 + 2]);
lcd.printByte(bn1[highnumber * 4 + 3]);
}
lcd.printByte(bn1[midnumber * 4]);
lcd.printByte(bn1[midnumber * 4 + 1]);
lcd.printByte(bn1[midnumber * 4 + 2]);
lcd.printByte(bn1[midnumber * 4 + 3]);
lcd.printByte(bn1[lownumber * 4]);
lcd.printByte(bn1[lownumber * 4 + 1]);
lcd.printByte(bn1[lownumber * 4 + 2]);
lcd.printByte(bn1[lownumber * 4 + 3]);
lcd.setCursor(column, 1);
if (highnumber == 0)
{
lcdPrintSpaces();
}
else
{
lcd.printByte(bn2[highnumber * 4]);
lcd.printByte(bn2[highnumber * 4 + 1]);
lcd.printByte(bn2[highnumber * 4 + 2]);
lcd.printByte(bn2[highnumber * 4 + 3]);
}
lcd.printByte(bn2[midnumber * 4]);
lcd.printByte(bn2[midnumber * 4 + 1]);
lcd.printByte(bn2[midnumber * 4 + 2]);
lcd.printByte(bn2[midnumber * 4 + 3]);
lcd.printByte(bn2[lownumber * 4]);
lcd.printByte(bn2[lownumber * 4 + 1]);
lcd.printByte(bn2[lownumber * 4 + 2]);
lcd.printByte(bn2[lownumber * 4 + 3]);
lcd.setCursor(column, 2);
if (highnumber == 0)
{
lcdPrintSpaces();
}
else
{
lcd.printByte(bn3[highnumber * 4]);
lcd.printByte(bn3[highnumber * 4 + 1]);
lcd.printByte(bn3[highnumber * 4 + 2]);
lcd.printByte(bn3[highnumber * 4 + 3]);
}
lcd.printByte(bn3[midnumber * 4]);
lcd.printByte(bn3[midnumber * 4 + 1]);
lcd.printByte(bn3[midnumber * 4 + 2]);
lcd.printByte(bn3[midnumber * 4 + 3]);
lcd.printByte(bn3[lownumber * 4]);
lcd.printByte(bn3[lownumber * 4 + 1]);
lcd.printByte(bn3[lownumber * 4 + 2]);
lcd.printByte(bn3[lownumber * 4 + 3]);
lcd.setCursor(column, 3);
if (highnumber == 0)
{
lcdPrintSpaces();
}
else
{
lcd.printByte(bn4[highnumber * 4]);
lcd.printByte(bn4[highnumber * 4 + 1]);
lcd.printByte(bn4[highnumber * 4 + 2]);
lcd.printByte(bn4[highnumber * 4 + 3]);
}
lcd.printByte(bn4[midnumber * 4]);
lcd.printByte(bn4[midnumber * 4 + 1]);
lcd.printByte(bn4[midnumber * 4 + 2]);
lcd.printByte(bn4[midnumber * 4 + 3]);
lcd.printByte(bn4[lownumber * 4]);
lcd.printByte(bn4[lownumber * 4 + 1]);
lcd.printByte(bn4[lownumber * 4 + 2]);
lcd.printByte(bn4[lownumber * 4 + 3]);
};
void setIO()
{
digitalWrite(oldsource, LOW);
digitalWrite(source, HIGH);
lcd.setCursor(0, 0);
lcd.print(inputName[source - 1]);
}
void RotaryUpdate()
{
switch (state)
{
case STATE_RUN:
volumeUpdate();
break;
case STATE_IO:
sourceUpdate();
if ((millis() - milOnButton) > TIME_EXITSELECT * 1000)
{
state = STATE_RUN;
}
break;
default:
break;
}
}
void volumeUpdate()
{
// 0 = no rotation, 10 = clockwise, 20 = counter clockwise
// result = rotary.process();
switch (rotary.process())
{
case 0:
// check button
buttonPressed();
break;
case DIR_CW:
if (volume < 234)
{
if (isMuted)
{
unMute();
}
volume = volume + 1;
setVolume();
}
break;
case DIR_CCW:
if (volume != 0)
{
if (isMuted)
{
unMute();
}
volume = volume - 1;
setVolume();
}
default:
break;
}
}
void setVolume()
{
balanceUpdate();
preamp.mas6116Write(mas6116RegLeft, leftVol);
preamp.mas6116Write(mas6116RegRight, rightVol);
// display volume setting
lcdPrintThreeNumber(6, volume);
}
void balanceUpdate(void)
{
leftVol = volume + balLeft[balance];
rightVol = volume + balRight[balance];
}
// button pressed routine
void buttonPressed()
{
if (rotary.buttonPressedReleased(20))
{
switch (state)
{
case STATE_RUN:
state = STATE_IO;
milOnButton = millis();
break;
default:
break;
}
}
}
void sourceUpdate()
{
// 0 = do nothing, 10 = clockwise, 20 = counter clockwise
// result = rotary.process();
switch (rotary.process())
{
case DIR_CW:
oldsource = source;
milOnButton = millis();
if (oldsource < 4)
{
source++;
}
else
{
source = 1;
}
setIO();
break;
case DIR_CCW:
oldsource = source;
milOnButton = millis();
if (source > 1)
{
source--;
}
else
{
source = 4;
}
setIO();
break;
default:
break;
}
}
void RC5Update()
{
/*
System addresses and codes used here match RC-5 infra-red codes for amplifiers (and CDs)
*/
unsigned char toggle;
unsigned char address;
unsigned char command;
// Poll for new RC5 command
if (rc5.read(&toggle, &address, &command))
{
if (address == 0x10) // standard system address for preamplifier
{
switch (command)
{
case 1:
// Phono
if ((oldtoggle != toggle))
{
oldsource = source;
source = 1;
setIO();
}
break;
case 3:
// Tuner
if ((oldtoggle != toggle))
{
oldsource = source;
source = 4;
setIO();
}
break;
case 7:
// CD
if ((oldtoggle != toggle))
{
oldsource = source;
source = 3;
setIO();
}
break;
case 8:
// Media
if ((oldtoggle != toggle))
{
oldsource = source;
source = 2;
setIO();
}
break;
case 13:
// Mute
if ((oldtoggle != toggle))
{
toggleMute();
}
break;
case 16:
// Increase Vol
if (volume < 234)
{
if (isMuted)
{
unMute();
}
volume = volume + 1;
setVolume();
}
break;
case 17:
// Reduce Vol
if (volume != 0)
{
if (isMuted)
{
unMute();
}
volume = volume - 1;
setVolume();
}
break;
case 32:
// up button pressed - being used instead for balance adjust right
if ((oldtoggle != toggle))
{
if (balance < 10) // not yet fully right
{
balance = balance + 1;
lcdPrintBal();
setVolume();
}
}
break;
case 33:
// down button pressed - being used instead for balance adjust left
if ((oldtoggle != toggle))
{
if (balance != 0) // not yet fully left
{
balance = balance - 1;
lcdPrintBal();
setVolume();
}
}
break;
case 59:
// Display Toggle
if ((oldtoggle != toggle))
{
lcd.setCursor(0, 2);
if (backlight)
{
backlight = STANDBY;
lcd.noBacklight(); // Turn off backlight
mute(); // mute output
}
else
{
backlight = ACTIVE;
lcd.backlight(); // Turn on backlight
unMute(); // unmute output
}
}
break;
}
}
else if (address == 0x14) // system address for CD
{
if ((oldtoggle != toggle))
{
if (command == 53) // Play
{
oldsource = source;
source = 3;
setIO();
}
}
}
oldtoggle = toggle;
}
}
void unMute()
{
if (!backlight)
{
backlight = ACTIVE;
lcd.backlight(); // Turn on backlight
}
isMuted = 0;
preamp.mas6116Mute(HIGH);
lcd.setCursor(0, 1);
lcd.print(" ");
}
void mute()
{
isMuted = 1;
preamp.mas6116Mute(LOW);
lcd.setCursor(0, 1);
lcd.print("Muted ");
}
void toggleMute()
{
if (isMuted)
{
unMute();
}
else
{
mute();
}
}
void setup()
{
for (size_t pinOut = 1; pinOut < 5; pinOut++)
{
pinMode(pinOut, OUTPUT);
digitalWrite(pinOut, LOW);
}
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on LCD backlight
backlight = 1;
defineCustomChars();
lcd.home(); // LCD cursor to home position
// show software version briefly in display
lcd.setCursor(0, 3);
sprintf(buffer1, "SW ver " VERSION_NUM);
lcd.printstr(buffer1);
delay(2000);
sprintf(buffer1, " ");
lcd.home();
// test for first use settings completed. If not, carry out
if (EEPROM.read(EEPROM_FIRST_USE))
{
// Set vol, source, balance for first time
EEPROM.write(EEPROM_SOURCE, 1);
EEPROM.write(EEPROM_VOLUME, 0);
EEPROM.write(EEPROM_BALANCE, 5);
EEPROM.write(EEPROM_FIRST_USE, 0x00);
}
// Load source, volume, balance values
volume = EEPROM.read(EEPROM_VOLUME);
source = EEPROM.read(EEPROM_SOURCE);
balance = EEPROM.read(EEPROM_BALANCE);
// AVR native C code for power-down interrupt setup
// Setup Analog Compare Interrupt
ADCSRB = 0x40; // Analog Comparator Multiplexer Enable
ADCSRA = 0x00; // ADC Disabled
ADMUX = 0x01; // Arduino pin A1
ACSR |= (1 << ACBG) | (1 << ACIS1) | (1 << ACIS0); // Analog Comparator Bandgap Select, Interrupt on rising edge
ACSR |= (1 << ACIE); // Analog Comparator Interrupt enable
// set startup volume
setVolume();
// set source
setIO();
// display balance setting
lcdPrintBal();
// unmute
isMuted = 0;
preamp.mas6116Mute(HIGH);
}
void loop()
{
RC5Update();
RotaryUpdate();
}