-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMW-2015.ino
262 lines (216 loc) · 8.92 KB
/
BMW-2015.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
#include <WString.h>
#include <Metro.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <EEPROM.h>
#define selfPowerPin A0
#define batteryPin A2
#define playerPin 8
#define cameraPin 4
#define lcdPin 7
#define minVoltage 500
IRsend irsend;
Metro loopTimer = Metro(3000);
unsigned char cSender;
int cLength = 0;
int voltage = 0;
int sc = 0;
unsigned char cReceiver;
unsigned char cBytes[30];
int lcdPinState = 0;
#define eepromLcdPinState 0
const uint8_t message3[] = {0x68, 0x04, 0xFF, 0x36, 0xA0, 05}; // CD input
void setup() {
lcdPinState = EEPROM.read(eepromLcdPinState);
pinMode(13, OUTPUT);
pinMode(selfPowerPin, OUTPUT);
pinMode(playerPin, OUTPUT);
pinMode(lcdPin, OUTPUT);
pinMode(cameraPin, OUTPUT);
digitalWrite(playerPin, LOW);
digitalWrite(lcdPin, lcdPinState);
digitalWrite(selfPowerPin, HIGH);
digitalWrite(cameraPin, LOW);
UCSR0C = UCSR0C | B00100000;
Serial.begin (9600);
//summer.setTempo(300);
// set input to CD
// TODO save/recall to/from eeprom
const uint8_t message2[] = {0x68, 0x04, 0xFF, 0x36, 0xA0, 0x05};
Serial.write(message2, message2[1]+2);
}
void loop(){
if (Serial.available() > 0) {
cSender = Serial.read();
//Serial.println("gavau kazka per seriala");
if ( (cSender == 0x50) || (cSender == 0xF0) || (cSender == 0x68) || (cSender == 0x80) ) {
//Serial.println("gavau senderio bita");
delay(10);
if (Serial.available() > 0) {
cLength = Serial.read();
if (Serial.available() > 0) cReceiver = Serial.read();
unsigned char tmp = cSender;
for(int i=0; i<cLength-1 && i<= 30; i++) {
if (Serial.available() > 0) {
tmp = Serial.read();
cBytes[i] = tmp;
}
}
//************************************************************
// CD CHANGER
//************************************************************
// 68 03 18 01 72 RAD --> CDC : Device status request
if (cSender == 0x68 && cLength == 3 && cBytes[0] == 0x01 && cBytes[1] == 0x72) {
const uint8_t message[] = {0x18, 0x04, 0xFF, 0x02, 0x00, 0xE1};
Serial.write(message, message[1]+2);
uint8_t message2[] = {0x68, 0x10, 0x3B, 0x23, 0x62, 0x30, 0x42, 0x4D, 0x57, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6A};
sendMsg(message2);
uint8_t message3[] = {0x68, 0x1A, 0x3B, 0xA5, 0x62, 0x01, 0x06, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6F, 0x77, 0x6E, 0x33, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x20, 0x20, 0x20, 0x20, 0xD6};
sendMsg(message3);
uint8_t message4[] = {0x68, 0x1A, 0x3B, 0xA5, 0x62, 0x01, 0x07, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x75, 0x6E, 0x74, 0x69, 0x74, 0x6C, 0x65, 0x64, 0x20, 0x20, 0x20, 0x20, 0x20, 0x97};
sendMsg(message4);
// REVERSE ON
} else if (cSender == 0x80 && cLength == 10 && cBytes[0] == 0x13 && (cBytes[1] == 0x03 || cBytes[1] == 0x00 || cBytes[1] == 0x02) && (cBytes[2] == 0x10 || cBytes[2] == 0x11) ) {
digitalWrite(cameraPin, HIGH);
digitalWrite(lcdPin, HIGH);
//const uint8_t vol_down_msg[] = {0xF0, 0x04, 0x68, 0x32, 0x30, 0x9E};
//Serial.write(vol_down_msg, vol_down_msg[1]+2);
// P or D ON
} else if (cSender == 0x80 && cLength == 10 && cBytes[0] == 0x13 && (cBytes[1] == 0x03 || cBytes[1] == 0x02 || cBytes[1] == 0x00) && (cBytes[2] == 0xB0 || cBytes[2] == 0xB1 || cBytes[2] == 0x80 || cBytes[2] == 0x81) ) {
digitalWrite(cameraPin, LOW);
digitalWrite(lcdPin, lcdPinState);
//************************************************************
// 68 05 18 38 00 00 4D RAD --> CDC : CD status request
// 68 05 18 38 03 00 4E
} else if (cSender == 0x68 && cLength == 5 && cBytes[0] == 0x38 && (cBytes[3] == 0x4D || cBytes[3] == 0x4E)) {
delay(2);
uint8_t message2[] = {0x68, 0x10, 0x3B, 0x23, 0x62, 0x30, 0x42, 0x4D, 0x57, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6A};
//Serial.write(message2, message2[1]+2);
sendMsg(message2);
const uint8_t message3[] = {0x68, 0x1A, 0x3B, 0xA5, 0x62, 0x01, 0x06, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6F, 0x77, 0x6E, 0x33, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x20, 0x20, 0x20, 0x20, 0xD6};
Serial.write(message3, message3[1]+2);
const uint8_t message4[] = {0x68, 0x1A, 0x3B, 0xA5, 0x62, 0x01, 0x07, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x75, 0x6E, 0x74, 0x69, 0x74, 0x6C, 0x65, 0x64, 0x20, 0x20, 0x20, 0x20, 0x20, 0x97};
Serial.write(message4, message4[1]+2);
//************************************************************
// STEARING WHEEL
//************************************************************
} else if (cLength == 4 && cBytes[0] == 0x3B) { // VAIRAS
//unsigned char tmp3 = cBytes[1];
//Serial.println(tmp3, HEX);
switch(cBytes[1]) {
case 0xA0: // kalbanti boba
//rrok(1);
if ( digitalRead(cameraPin) == LOW ) { // cammera is off
digitalWrite(cameraPin, HIGH);
digitalWrite(lcdPin, HIGH);
} else { // cammera is on
digitalWrite(cameraPin, LOW);
digitalWrite(lcdPin, lcdPinState);
}
break;
case 0x00: // R/T
case 0x40: // R/T
//rrmusic(1);
lcdPinState = (lcdPinState == 1) ? 0 : 1;
digitalWrite(lcdPin, lcdPinState);
digitalWrite(cameraPin, LOW);
break;
case 0x01: // next
rrnext(1);
break;
case 0x28: // prev
rrprev(1);
break;
}
}
//************************************************************
if (cLength == 4 && cBytes[0] == 0x48) { // TELIKO KNOPKES
switch (cBytes[1]) {
case 0x14: // < >
lcdPinState = (lcdPinState == 1) ? 0 : 1;
digitalWrite(lcdPin, lcdPinState);
break;
case 0x08: // tel
rrreturn(1);
break;
case 0x85: // knob release
if (lcdPinState) rrok(1);
break;
case 0: // >>>
if (lcdPinState) rrright(1);
break;
case 0x10: // <<<
if (lcdPinState) rrleft(1);
break;
case 0x7: // clock
if (lcdPinState) rr16x9(1);
break;
}
}
}
//************************************************************
if (cLength == 4 && cBytes[0] == 0x49) { // ROTARY
switch (cBytes[1]) {
case 0x81: // right 1
if (lcdPinState) rrdown(1);
break;
case 0x82: // right 2
if (lcdPinState) rrdown(2);
break;
case 0x83: // right 3
if (lcdPinState) rrdown(3);
break;
case 0x84:
if (lcdPinState) rrdown(4);
break;
case 0x85:
if (lcdPinState) rrdown(5);
break;
case 0x01: // left 1
if (lcdPinState) rrup(1);
break;
case 0x02: // left 2
if (lcdPinState) rrup(2);
break;
case 0x03: // left 3
if (lcdPinState) rrup(3);
break;
case 0x04:
if (lcdPinState) rrup(4);
break;
case 0x05:
if (lcdPinState) rrup(5);
break;
}
}
}
}
voltage = analogRead(batteryPin) * 2; //1.9965;
if (loopTimer.check()) { // atejo laikas tikrint poweri
loopTimer.reset(); // resetinam timeri
//Serial.print("voltage: ");
//Serial.println(voltage);
if (voltage < minVoltage) { // <------ NERA +12 v
//Serial.println("nera +12v");
if (digitalRead(playerPin) == HIGH) { // <------ NEATJUNGTA RELE
rrpower(1);
delay(1000);
digitalWrite(playerPin, LOW);
digitalWrite(selfPowerPin, LOW);
digitalWrite(lcdPin, LOW);
digitalWrite(13, LOW);
} else { // <------ ATJUNGTA RELE (viskas OK)
digitalWrite(selfPowerPin, LOW);
}
EEPROM.write(eepromLcdPinState, lcdPinState);
} else { // <------- YRA +12 v
//Serial.println("yra +12v");
if (digitalRead(playerPin) == LOW) { // <------- JEI RELE ATJUNGTA
//Serial.println("playeris isjungtas. ijungiam.");
digitalWrite(playerPin, HIGH);
digitalWrite(lcdPin, lcdPinState);
digitalWrite(13, HIGH);
}
}
}
}