-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio-input-switch.ino
199 lines (163 loc) · 3.76 KB
/
audio-input-switch.ino
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
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#define BTN_IN_1 2
#define BTN_IN_2 3
#define BTN_IN_3 4
#define BTN_IN_4 5
#define CHANNEL_RESET -9
#define CHANNEL_SIZE 4
#define BTN_PIN_ZERO_OFFSET 2
#define INPUT_DELAY_MSEC 500
#define ANIMATION_DELAY_MSEC 250
#define LCD_CLK 9
#define LCD_DIN 10
#define LCD_DC 11
#define LCD_CE 12
#define LCD_RST 13
#define LCD_TEXT_CLEAR_LINE " "
#define LCD_CHAR_WIDTH 6
#define MUX_OUT_A 6
#define MUX_OUT_B 7
#define MUX_INHIBIT 8
const int buttonPins[CHANNEL_SIZE] = {BTN_IN_1, BTN_IN_2, BTN_IN_3, BTN_IN_4};
const int muxSelector[CHANNEL_SIZE][2] = {{LOW, LOW}, {HIGH, LOW}, {LOW, HIGH}, {HIGH, HIGH}};
int channelStates[CHANNEL_SIZE];
int muxInhibitState = HIGH;
int activeChannel = -1;
unsigned long lastInteractionTime = -1;
unsigned long lastAnimationTime = -1;
char txtChannelStatusBuffer[4];
int animatedTitleFrame = 0;
Adafruit_PCD8544 lcd = Adafruit_PCD8544(LCD_CLK, LCD_DIN, LCD_DC, LCD_CE, LCD_RST);
void updateDisplay()
{
lcd.fillRect(0, 10, 84, 38, WHITE);
lcd.setTextSize(1);
lcd.setCursor(72, 10);
lcd.setTextColor(BLACK);
lcd.print(muxInhibitState == HIGH ? "((" : "(X");
lcd.setTextSize(3);
for (int c = 0; c < CHANNEL_SIZE; c++)
{
if (channelStates[c] == LOW)
{
lcd.fillRect((c * 20) + 1, 21, 21, 27, BLACK);
lcd.setTextColor(WHITE);
}
else
{
lcd.setTextColor(BLACK);
}
snprintf(txtChannelStatusBuffer, 4, "%d", c + 1);
lcd.setCursor((c * 20) + 4, 24);
lcd.print(txtChannelStatusBuffer);
}
lcd.display();
}
void setActiveChannel(int channel)
{
if (channel == activeChannel)
{
channelStates[channel] = channelStates[channel] == LOW ? HIGH : LOW;
muxInhibitState = muxInhibitState == LOW ? HIGH : LOW;
}
else
{
muxInhibitState = HIGH;
for (int c = 0; c < CHANNEL_SIZE; c++)
{
channelStates[c] = HIGH;
}
}
if (channel == CHANNEL_RESET)
{
digitalWrite(MUX_INHIBIT, HIGH);
return;
}
activeChannel = channel;
channelStates[channel] = LOW;
digitalWrite(MUX_INHIBIT, muxInhibitState);
digitalWrite(MUX_OUT_A, muxSelector[channel][0]);
digitalWrite(MUX_OUT_B, muxSelector[channel][1]);
}
void checkButton(int btnPin)
{
int state = digitalRead(btnPin);
int channel = 0;
if (state == LOW)
{
lastInteractionTime = millis();
setActiveChannel(btnPin - BTN_PIN_ZERO_OFFSET);
}
}
void checkInputs()
{
for (int b = 0; b < CHANNEL_SIZE; b++)
{
checkButton(buttonPins[b]);
}
}
void animateTitle()
{
if (millis() - lastAnimationTime > ANIMATION_DELAY_MSEC)
{
lcd.setTextSize(1);
lcd.setCursor(0, 0);
lcd.setTextColor(WHITE, BLACK);
switch (animatedTitleFrame)
{
case 0:
lcd.println(" AXi >> ");
break;
case 1:
lcd.println(" AXi >> ");
break;
case 2:
lcd.println(" AXi >>");
break;
}
lcd.setTextColor(BLACK, WHITE);
lcd.display();
animatedTitleFrame++;
if (animatedTitleFrame == 3)
{
animatedTitleFrame = 0;
}
lastAnimationTime = millis();
}
}
void setup()
{
for (int i = 0; i < CHANNEL_SIZE; i++)
{
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(MUX_OUT_A, OUTPUT);
pinMode(MUX_OUT_B, OUTPUT);
pinMode(MUX_INHIBIT, OUTPUT);
setActiveChannel(CHANNEL_RESET);
digitalWrite(MUX_INHIBIT, LOW);
lcd.begin();
lcd.clearDisplay();
lcd.setContrast(11);
lcd.fillRect(0, 0, 84, 48, BLACK);
lcd.display();
lcd.setTextColor(WHITE);
lcd.setCursor(0, 10);
lcd.setTextSize(3);
lcd.println(" AXi ");
lcd.display();
delay(1000);
lcd.clearDisplay();
animateTitle();
}
void loop()
{
while (millis() - lastInteractionTime > INPUT_DELAY_MSEC)
{
checkInputs();
updateDisplay();
animateTitle();
}
}