forked from deenine/SonyAlpine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSonyAlpine.c
More file actions
198 lines (179 loc) · 4.95 KB
/
Copy pathSonyAlpine.c
File metadata and controls
198 lines (179 loc) · 4.95 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
/*
resistance voltage
src 2200 4.1
att 4400 3.47
off 35 4.98
vol down 23850 1.47
vol up 17000 1.85
seek down 12000 2.25
seek up 8800 2.65
back 6660 3
front 33940 1.13
bottom 48800 0.84
*/
/** General Config **/
const int sonyPin = A0;
const int sonyShiftPin = A2;
const int ledPin = 13;
const int alpPin = 3;
const int arrowDelay = 250; //repeat rate for arrow key presses
const int enterDelay = 350; //repeat rate for enter/esc key presses
boolean headunit = true; //True if stalk is controlling HU, false if emulating kb
/** Alpine Codes **/
char alpVolUp[] = "110101111101101110101011110110111101011011010101";
char alpVolDown[] = "110101111101101110101011011011011111011011010101";
char alpAtt[] = "110101111101101110101011101011011110111011010101";
char alpSource[] = "110101111101101110101011101101111101101101010101";
char alpOff[] = "110101111101101110101011011101111110101101010101";
char alpTrackUp[] = "110101111101101110101011101110111101101011010101";
char alpTrackDown[] = "110101111101101110101011010111011111101011010101";
char alpPstUp[] = "110101111101101110101011101010111110111101010101";
char alpPstDown[] = "110101111101101110101011010101011111111101010101";
/** Sony Voltages **/
const int sonySrc = 41; //4.1v
const int sonyAtt = 34;
const int sonyOff = 49;
const int sonyVolDown = 14;
const int sonyVolUp = 18;
const int sonySeekDown = 26;
const int sonySeekUp = 22;
const int sonyBack = 29;
const int sonyFront = 11;
const int sonyBottom = 8;
/** Setup **/
void setup() {
pinMode(alpPin,OUTPUT);
pinMode(ledPin,OUTPUT);
digitalWrite(2,LOW);
Keyboard.begin();
}
/** Main program loop **/
void loop() {
boolean shift = false;
float sonyPress = analogRead(sonyPin);
float voltage = (sonyPress / 1024) * 5.0;
if (voltage > 0.01) {
delay(10); //filter out the initial flutter in voltage
//take a second reading
sonyPress = analogRead(sonyPin);
voltage = (sonyPress / 1024) * 5.0;
float sonyShiftPress = analogRead(sonyShiftPin);
float shiftVoltage = (sonyShiftPress / 1024) * 5.0;
if (shiftVoltage > 0.01) {
shift = true;
}
checkVoltage(int((voltage) * 10), shift);
}
if(headunit == true)
digitalWrite(ledPin,HIGH);
else
digitalWrite(ledPin,LOW);
}
void checkVoltage(int voltage, boolean shift) {
Serial.println(voltage);
Serial.println(shift);
if (shift) {
switch (voltage) {
case sonySeekUp:
if (headunit)
sendCode(alpTrackUp);
else
sendCode(alpTrackUp);
return;
case sonySeekDown:
if (headunit)
sendCode(alpTrackDown);
else
sendCode(alpTrackDown);
return;
}
}
switch (voltage) {
case sonySrc:
sendCode(alpSource);
return;
case sonyVolDown:
sendCode(alpVolDown);
return;
case sonyVolUp:
sendCode(alpVolUp);
return;
case sonyBottom:
if (headunit == true)
headunit = false;
else
headunit = true;
delay(500);
return;
case sonyAtt:
if (headunit)
sendCode(alpAtt);
else
sendKey(KEY_RETURN);
return;
case sonyOff:
if (headunit)
sendCode(alpOff);
else
sendKey(KEY_ESC);
return;
case sonySeekUp:
if (headunit)
sendCode(alpTrackUp);
else
sendKey(KEY_RIGHT_ARROW);
return;
case sonySeekDown:
if (headunit)
sendCode(alpTrackDown);
else
sendKey(KEY_LEFT_ARROW);
return;
case sonyBack:
if (headunit)
sendCode(alpPstDown);
else
sendKey(KEY_DOWN_ARROW);
return;
case sonyFront:
if (headunit)
sendCode(alpPstUp);
else
sendKey(KEY_UP_ARROW);
return;
default:
break;
}
}
void sendKey(int key){
Keyboard.write(key);
//longer delay between enter/return than arrow keys.
if (key == KEY_RETURN || key == KEY_ESC)
delay(enterDelay);
else
delay(arrowDelay);
return;
}
void sendCode(char *code) {
const int alpCodeLength = 47;
const int alpRate = 500; //microseconds
const int alpInterval = 41; //milliseconds
//Innit
digitalWrite(alpPin,HIGH);
delay(8); // 8 ms high init
digitalWrite(alpPin,LOW);
delayMicroseconds(4500); // 4.5 ms low init
//Send command
for (int i = 0; i <= alpCodeLength; i++) {
if (code[i] == '1') {
digitalWrite(alpPin,HIGH);
} else {
digitalWrite(alpPin,LOW);
}
delayMicroseconds(alpRate);
digitalWrite(alpPin,LOW);
delayMicroseconds(alpRate);
}
digitalWrite(alpPin,LOW);
delay(41); //41ms interval between commands
}