-
Notifications
You must be signed in to change notification settings - Fork 0
/
JensenSWControl.ino
301 lines (240 loc) · 7.05 KB
/
JensenSWControl.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
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
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <driver/dac.h>
#include <WiFiUdp.h>
#include <WiFiManager.h>
#include "teleplot.h"
// ADC2 is used by wifi - so use ADC1 for all ADC channels.
// DAC is on 17 and 18, but an internal pulldown on 18 causes problems so use 17
#define GPIO_LED 15
#define GPIO_KEY1_IN 9
#define GPIO_KEY2_IN 7
#define GPIO_TIP_OUT 17
#define GPIO_RING_OUT 33
#define GPIO_OUTPUT_ADC_SENSOR 5
enum KeyPressed
{
None = 0,
PhoneOn,
PhoneOff,
Pause,
Stop,
TrackNext,
TrackPrev,
VolUp,
VolDown,
Gps,
Mute
};
int key1Min;
int key1Max;
int key2Min;
int key2Max;
long mutePressedCount = 0;
KeyPressed priorKeyPress = KeyPressed::None;
int outValues [11] [2] =
{
HIGH, 255, // none
LOW, 41, // phone on (600 mv)
LOW, 83, // phone off (1010 mv)
LOW, 221, // voice mapped to pause (2628 mv)
HIGH, 221, // band mapped to stop (2628 mv)
HIGH, 132, // track next (600 mv)
HIGH, 155, // track prev (1810 mv)
HIGH, 165, // vol up (2070 mv)
HIGH, 190, // vol down (2320 mv)
HIGH, 40, // menu mapped to GPS (594 mv)
HIGH, 75 // mute (1000 mv)
};
// UDP for teleplot
WiFiUDP Udp;
Teleplot* teleplot = 0;
// WiFiManager
WiFiManager wm;
void setup()
{
Serial.begin(115200);
delay(3000);
Serial.println("JensenSWControl has started");
ResetMinMax();
pinMode(GPIO_LED, GPIO_MODE_OUTPUT);
pinMode(GPIO_RING_OUT, GPIO_MODE_OUTPUT);
digitalWrite(GPIO_RING_OUT, HIGH);
dac_output_enable(DAC_CHANNEL_1);
dacWrite(GPIO_TIP_OUT, 0); // now set it to min
}
void loop()
{
if (WiFi.isConnected())
{
ArduinoOTA.handle();
}
//Serial.println("checking the input");
KeyPressed keyPress = CheckInput();
if (keyPress != KeyPressed::None)
{
priorKeyPress = keyPress;
}
else // no key pressed - if a prior key was valid then process it (we process key input on key UP not down)
{
if (priorKeyPress != KeyPressed::None)
{
Serial.printf("Key pressed: %d \r\n", priorKeyPress);
if (teleplot)
{
teleplot->log("Key was pressed");
teleplot->update("KeyPress", (int)priorKeyPress, 10);
}
if (priorKeyPress == KeyPressed::Mute)
{
mutePressedCount++;
if (mutePressedCount > 5)
{
// blink the led with a long pause
digitalWrite(GPIO_LED, HIGH);
delay(1500);
digitalWrite(GPIO_LED, LOW);
Serial.println("Mute was pressed more than 5 times");
// try to start a wifi session using the wifi manager. If a password is stored and valid
// the wifi will start. If not, a Wifi AP will run for the configured time to allow user to
// put in the wifi credentials
//wm.resetSettings(); // enable this to delete old settings
wm.setConnectTimeout(30); // try to connect for 30 seconds then switch to the AP
wm.setConfigPortalTimeout(180); // user has up to 3 minutes to put in credentials if needed
bool res = wm.autoConnect("JensenSWControl");
if (res == false)
{
Serial.println("Failed to connect to Wifi and/or establish an access point in time");
}
if (WiFi.isConnected())
{
Serial.println("Wifi is connected");
teleplot = new Teleplot("LENOIR-LENOVO:42769");
teleplot->log("JensenSWControl has enabled wifi");
ArduinoOTA.begin();
}
else
{
Serial.println("failed to start wifi");
}
}
}
else
{
mutePressedCount = 0;
}
SendOutput(priorKeyPress);
priorKeyPress = KeyPressed::None;
}
}
delay(100);
}
KeyPressed CheckInput()
{
int key1 = analogReadMilliVolts(GPIO_KEY1_IN);
int key2 = analogReadMilliVolts(GPIO_KEY2_IN);
//*** key is "high" when nothing pressed
if (key1 > 2000 and key2 > 2000)
{
ResetMinMax();
return KeyPressed::None;
}
if (teleplot)
{
teleplot->update("Key1ADC", key1, 10);
teleplot->update("Key2ADC", key2, 10);
}
if (key1 < key1Min)
{
key1Min = key1;
}
if (key1 > key1Max)
{
key1Max = key1;
}
if (key2 < key2Min)
{
key2Min = key2;
}
if (key2 > key2Max)
{
key2Max = key2;
}
//Serial.printf("Key1 Min: %4d Max: %4d Key2 Min: %4d Max: %4d \r\n", key1Min, key1Max, key2Min, key2Max);
if (key1 < 140)
{
return KeyPressed::PhoneOff;
}
if ( key1 < 205)
{
return KeyPressed::Pause;
}
if (key1 < 350)
{
return KeyPressed::VolUp;
}
if (key1 < 500)
{
return KeyPressed::Stop;
}
if (key1 < 650)
{
return KeyPressed::TrackPrev;
}
// check key2 values (all may be above max - error on key1 value)
if (key2 < 75)
{
return KeyPressed::PhoneOn;
}
if (key2 < 875)
{
return KeyPressed::VolDown;
}
if (key2 < 1200)
{
return KeyPressed::Mute;
}
if (key2 < 1675)
{
return KeyPressed::TrackNext;
}
if (key2 < 1900)
{
return KeyPressed::Gps;
}
Serial.printf("Didn't match any ranges. Key1 Min: %4d Max: %4d Key2 Min: %4d Max: %4d \r\n", key1Min, key1Max, key2Min, key2Max);
// Serial.print("Taking default action for key press. Key1: ");
// Serial.print(key1);
// Serial.print(" key2: ");
// Serial.println(key2);
return KeyPressed::None;
}
void ResetMinMax()
{
key1Min = 9999;
key1Max = 0;
key2Min = 9999;
key2Max = 0;
}
void SendOutput(int keyPress)
{
int hiLo = outValues[keyPress][0]; // value is 0 or 1
int value = outValues[keyPress][1]; // value is in units (0 to 255)
digitalWrite(GPIO_RING_OUT, hiLo);
dacWrite(GPIO_TIP_OUT, value);
int adcOut = analogReadMilliVolts(GPIO_OUTPUT_ADC_SENSOR);
Serial.printf("Sending output. Keypress: %d Ring Units: %d Requested: %d adc Measured: %d\r\n", keyPress, hiLo, value, adcOut);
digitalWrite(GPIO_LED, HIGH);
if (teleplot)
{
teleplot->log("sending output for key press");
teleplot->update("keyPressOut", keyPress, 10);
teleplot->update("RequestValue", value, 10);
teleplot->update("adcOutput", adcOut, 10);
}
delay(200);
digitalWrite(GPIO_RING_OUT, HIGH);
dacWrite(GPIO_TIP_OUT, 0);
digitalWrite(GPIO_LED, LOW);
Serial.println("Output complete.");
}