PHP код:
/*
Steering Wheels Sequences (from NewSoftSerial arduino library)
--------------------------------------------------------------
DESCRIPTION DATA KeyID
--------------------------------------------------------------
+ button press 50 23 3C ED 32 11 FB 1
- button press 50 23 3C ED 32 10 1E 2
> BUTTON PRESS 50 23 3C ED 3B 01 06 11
> BUTTON HOLD 50 23 3C ED 3B 11 B3 12
> BUTTON RELEASE 50 23 3C ED 3B 21 33 13
< BUTTON PRESS 50 23 3C ED 3B 08 0F 21
< BUTTON HOLD 50 23 3C ED 3B 18 FB 22
< BUTTON RELEASE 50 23 3C ED 3B 28 7B 23
VOICE BUTTON PRESS 50 23 3C F9 3B 80 27 31
VOICE BUTTON HOLD 50 23 3C F9 3B 90 BB 32
VOICE BUTTON RELEASE 50 23 3C F9 3B A0 3B 33
R/T BUTTON PRESS 50 1B 38 F9 01 9A FF 40
*/
#include <NewSoftSerial.h> // use NewSoftSerial library for serial connection to BMW I-BUS
// I-BUS
NewSoftSerial IBUS(7, 8); //set pin for BMW I_BUS serial connection
int IBUSreceiveByte = 0; // byte reading from I-BUS
byte IBUSbyte[8] = {0,0,0,0,0,0,0,0}; // key byte sequence
int Delay = 10; //delay in millisec
int ButtonPressed = 0; //boolean value set 1 if key press detected
int KeyID = 0; // key press number (for easy select case routine)
//hold keys flags
int N_HOLD_F = 0; //next track hold flag
int P_HOLD_F = 0; //prev track hold flag
int V_HOLD_F = 0; // r/t key hold flag
byte CHECK_DOWN[8] = {0, 0x50, 0x08, 0x43, 0xDB, 0x86, 0xEC, 0xF0}; // check press button
byte CHECK1_DOWN[8] = {0, 0x50, 0x08, 0x43, 0xDB, 0x16, 0xCC, 0xE0}; // check press button
byte PLUS_DOWN[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x32, 0x11, 0xFB}; // + press button
byte MINUS_DOWN[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x32, 0x10, 0x1E}; // - press button
byte NEXT_DOWN[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x3B, 0x01, 0x06}; // > press button
byte NEXT_HOLD[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x3B, 0x11, 0xB3}; // > hold button
byte NEXT_REL[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x3B, 0x21, 0x33}; // > release button
byte PREV_DOWN[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x3B, 0x08, 0x0F}; // < press button
byte PREV_HOLD[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x3B, 0x18, 0xFB}; // < hold button
byte PREV_REL[8] = {0, 0x50, 0x23, 0x3C, 0xED, 0x3B, 0x28, 0x7B}; // < release button
byte VOICE_DOWN[8] = {0, 0x50, 0x23, 0x3C, 0xF9, 0x3B, 0x80, 0x27}; // voice press button
byte VOICE_HOLD[8] = {0, 0x50, 0x23, 0x3C, 0xF9, 0x3B, 0x90, 0xBB}; // voice hold button
byte VOICE_REL[8] = {0, 0x50, 0x23, 0x3C, 0xF9, 0x3B, 0xA0, 0x3B}; // voice release button
byte RT_DOWN[8] = {0, 0x50, 0x1B, 0x38, 0xF9, 0x01, 0x9A, 0xFF}; // voice release button
int incomingByte = 0; // byte reading from serial port
void setup()
{
Serial.begin(9600); // открываем порт на скорости 9600 для передачи в ПК
// set the data rate for the NewSoftSerial port (I-BUS)
IBUS.begin(9600);
}
void loop()
{
if (IBUS.available() && ButtonPressed==0){
TryReadIBUSmsg();
}
if (ButtonPressed==1){
Serial.print(millis());
Serial.println(" ----------------------------------------------- ");
switch (KeyID){
case 100: //Если клавиша не опознана или кокоието ошибки в шине при чтении
Serial.print("UNKNOWN KEY: ");
SerialPrintKey();
break;
case 40:
Serial.print("R/T KEY: ");
SerialPrintKey();
break;
case 1:
Serial.print("VOL + key: ");
SerialPrintKey();
break;
case 2:
Serial.print("VOL - key: ");
SerialPrintKey();
break;
case 11:
//Serial.print("NEXT FLAG HOLD=0: ");
//SerialPrintKey();
N_HOLD_F=0;
break;
case 12:
//Serial.print("NEXT FLAG HOLD=1: ");
//SerialPrintKey();
N_HOLD_F=1;
break;
case 13:
if (N_HOLD_F==0){
Serial.print("NEXT TRACK key: ");
SerialPrintKey();
}
else {
Serial.print("NEXT FOLDER key: ");
SerialPrintKey();
}
break;
case 21:
Serial.print("PREV FLAG HOLD=0: ");
SerialPrintKey();
P_HOLD_F=0;
break;
case 22:
Serial.print("PREV FLAG HOLD=1: ");
SerialPrintKey();
P_HOLD_F=1;
break;
case 23:
if (P_HOLD_F==0){
Serial.print("PREV TRACK key: ");
SerialPrintKey();
}
else {
Serial.print("PREV FOLDER key: ");
SerialPrintKey();
}
break;
case 31:
//Serial.print("VOICE FLAG HOLD=0: ");
//SerialPrintKey();
V_HOLD_F=0;
break;
case 32:
//Serial.print("VOICE FLAG HOLD=1: ");
//SerialPrintKey();
V_HOLD_F=1;
break;
case 33:
if (V_HOLD_F==0){
Serial.print("VOICE key press: ");
SerialPrintKey();
}
else {
Serial.print("Voice key hold: ");
SerialPrintKey();
}
break;
}
//SerialPrintKey();
ButtonPressed = 0;
IBUS.flush();
}
}
void TryReadIBUSmsg()
{
IBUSreceiveByte = IBUS.read(); //read 1-st byte Если 50 то начала посылки от рулевых кнопок
if (IBUSreceiveByte==0x50)
{
IBUSbyte[1]=0x50;
for (int i=2; i <= 7; i++){
//IBUSreceiveByte = IBUS.read();
IBUSbyte[i] = IBUS.read(); //read 2,3,4,5,6,7 bytes of data - читаем следующие байты
delay(Delay);
}
KeyID=100; //Устанавливаем по умолчанию что клавиша не опознана
// и меняем если опознана на соответствующий код
if(memcmp(IBUSbyte, CHECK_DOWN, 8) == 0 ){KeyID=50;}
if(memcmp(IBUSbyte, CHECK1_DOWN, 8) == 0 ){KeyID=51;}
if(memcmp(IBUSbyte, PLUS_DOWN, 8) == 0 ){KeyID=1;}
if(memcmp(IBUSbyte, MINUS_DOWN, 8) == 0 ){KeyID=2;}
if(memcmp(IBUSbyte, NEXT_DOWN, 8) == 0 ){KeyID=11;}
if(memcmp(IBUSbyte, NEXT_HOLD, 8) == 0 ){KeyID=12;}
if(memcmp(IBUSbyte, NEXT_REL, 8) == 0 ){KeyID=13;}
if(memcmp(IBUSbyte, PREV_DOWN, 8) == 0 ){KeyID=21;}
if(memcmp(IBUSbyte, PREV_HOLD, 8) == 0 ){KeyID=22;}
if(memcmp(IBUSbyte, PREV_REL, 8) == 0 ){KeyID=23;}
if(memcmp(IBUSbyte, VOICE_DOWN, 8) == 0 ){KeyID=31;}
if(memcmp(IBUSbyte, VOICE_HOLD, 8) == 0 ){KeyID=32;}
if(memcmp(IBUSbyte, VOICE_REL, 8) == 0 ){KeyID=33;}
if(memcmp(IBUSbyte, RT_DOWN, 8) == 0 ){KeyID=40;}
ButtonPressed=1;
}
else
{
ButtonPressed=0;
}
IBUS.flush();
}
void SerialPrintKey() //красивая печать в ком порт посылки из I-BUS
{
for (int x=1; x <= 7; x++){
Serial.print(IBUSbyte[x], HEX);
Serial.print(" ");
}
Serial.println();
}