Rattknappar-Arduino till JVC sterero. Mjukvaran.

Har byggt vidare på rattknappsstyrningen till att styra en JVC stereo.
Det blev en blandning mellan Alpine mjukvaran och JVC mjukvara jag hittat på nätet.
https://www.avforums.com/threads/jvc-stalk-adapter-diy.248455/
http://saab9000vector.blogspot.se/2012/05/rattknappar-till-en-alpine-stereo.html

Och här kommer mjukvaran...........

//Inklusive Tutan med egen utgång pin8 ,pull up moständ 470 ohm.
// Connect Arduino PIN to Base of NPN transistor
/* Emitter is tied to ground
Collector is tied to 3.5mm TIP
Ground 3.5mm Ring/Sleeve
__________ JVC remote input
---- | /C
| 3.3K |/
PIC|----/\/\/\---| NPN (PN2222 or 2N3904)
| B|\
---- | \E
|_________________|__________ Ground
*/
// this works with a simgle button and has the correct timings
// adding the send of signals to stereo.. simulating by sending to serial
#define CHECK_MS 5
#define DOUBLE_MS 400
#define HOLD_MS 1200

#define NO_B 870 //nothing pressed 4,2 volt
#define MODE_B 765 // NXT(mode)776 3,6
#define NXTD_B 643 // NEXT DOWN<< 3,0 #define NXTU_B 508 // NEXT UP>> 2,4
#define MUTE_B 385 // SRC (mute) 1,8
#define VOLU_B 264 // VOLUME UP+ 1,2
#define VOLD_B 151 // VOLUME DOWN- 0,7
#define HORN 020 // TUTAN 0,0 volt

#define outPIN 1 //utgång 7 JVC stereo
#define hornPin 3 //utgång 8 för tutan 480ms high
int Reps = 4; // 3 Number of times to repeat each transmission
int Length = 537; // Length in Microseconds

boolean spFlag = 0;
boolean dpFlag = 0;
boolean hpFlag = 0;

int button = 0;
int spbutton = 0;

void setup() {
Serial.begin(9600);
pinMode(outPIN, OUTPUT); // stereo utgång
pinMode(hornPin, OUTPUT); //tut utgången
}
void loop() {
//----------------- tut funktionen
int anaPin = 0; // rattknappsignal och tutan
anaPin = analogRead(anaPin);
if (anaPin > HORN - 20 && anaPin < HORN + 20 ) {digitalWrite(hornPin, HIGH); //hornPin delay(48); digitalWrite(hornPin, LOW); //hornPin } //-------------avläsning av rattknappar static unsigned long oldTime = 0; int switchStatus = 0; if (millis() >= oldTime + CHECK_MS) {
oldTime = millis();
switchStatus = CheckSwitch();

if (switchStatus == 0) { //no button press

} else if (switchStatus == 1) {
// single press
switch (spbutton) {
case 0: // no butto, why are wh here
break;
case 1: // NXT- PLAY ON/OFF
entPlaySend(); // JVC Equlizer
break;
case 2: // next up
trkUpSend();
break;
case 3: // next down
trkDnSend();
break;
case 4: // SRC
sourceSend();
break;
case 5: // vol up
volUpSend();
break;
case 6: // vol down
volDnSend();
break;
}
} else if (switchStatus == 2) {
//double press
switch (spbutton) {
case 0: // no butto, why are wh here
break;
case 1: // NXT mode
soundSend(); // JVC sound
break;
case 2: // next up (folder up)
pstUpSend();
break;
case 3: // next down (folder down)
pstDnSend();
break;
case 4: // SRC mute
muteSend();
break;
case 5: // vol up
volUpSend();
break;
case 6: // vol down
volDnSend();
break;
}
} else if (switchStatus == 3) {
//hold
switch (spbutton) {
case 0: // no butto, why are wh here
break;
case 1: // NXT mode mute
muteSend();
break;
case 2: // next up (track fwd)
trkUpSend();
break;
case 3: // next down (track rev)
trkDnSend();
break;
case 4: // SRC
powerSend();
break;
case 5: // vol up
volUpSend();
break;
case 6: // vol down
volDnSend();
break;
}
}
}
}

// Service routine called by a timer interrupt
int CheckSwitch() {
static uint16_t State = 0; // Current debounce status
static int curTime = 0;

State=(State<<1) | !RawKeyPressed() | 0xe000; if (!spFlag) { if (hpFlag) { if (State == 0xe000) { return 3; } else { hpFlag = 0; } } if (State==0xf000) { spFlag = 1; hpFlag = 1; dpFlag = 1; curTime = 0; spbutton = button; return 0; } } if (hpFlag && State != 0xe000) { hpFlag = 0; } if (spFlag && hpFlag && curTime > HOLD_MS/CHECK_MS) {
spFlag = 0;
dpFlag = 0;
return 3;
} else if (spFlag && dpFlag && State == 0xf000 && curTime < DOUBLE_MS) { spFlag = 0; hpFlag = 0; dpFlag = 0; return 2; } else if (spFlag && !hpFlag && curTime > DOUBLE_MS) {
spFlag = 0;
hpFlag = 0;
dpFlag = 0;
return 1;
} else {
curTime = curTime + CHECK_MS;
return 0;
}
}
//----------resistansnätet Spänningsnivån på knapparna
boolean RawKeyPressed() {
int anaPin = 0;
anaPin = analogRead(0);
if (anaPin > NO_B - 20) {
button = 0;
return 0;
} else if (anaPin > MODE_B - 20 && anaPin < MODE_B + 20 ) { button = 1; return 1; } else if (anaPin > NXTU_B - 40 && anaPin < NXTU_B + 40 ) { button = 2; return 1; } else if (anaPin > NXTD_B - 40 && anaPin < NXTD_B + 40 ) { button = 3; return 1; } else if (anaPin > MUTE_B - 40 && anaPin < MUTE_B + 40 ) { button = 4; return 1; } else if (anaPin > VOLU_B - 40 && anaPin < VOLU_B + 40 ) { button = 5; return 1; } else if (anaPin > VOLD_B - 40 && anaPin < VOLD_B + 40 ) {
button = 6;
return 1;
} else {
button = 0;
return 0;
}
}
//-----------SOURCE------------------
void sourceSend() { // Send 0x08
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bZERO(); bZERO(); bONE(); // 8
bZERO(); bZERO(); bZERO(); // 0
Postamble();
}
}
//-----------POWER ON/OFF------------------
void powerSend() { // Send 0x00
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bZERO(); bZERO(); bZERO(); // 0
bZERO(); bZERO(); bZERO(); // 0
Postamble();
}
}
//---------Track NEXT UP-----------------------------------------------
void trkUpSend() { // Send 0x12
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bONE(); bZERO(); bZERO(); // 2
bONE(); bZERO(); bZERO(); // 1
Postamble();
}
}
//---------Track NEXT DOWN----------------------------------------------
void trkDnSend() { // Send 0x13
for (int i = 1; i <= Reps; i++) {
Preamble();
bONE(); bONE(); bZERO(); bZERO(); // 3
bONE(); bZERO(); bZERO(); // 1
Postamble();
}
}
//---------Folder PST? UP-----------------------------------------------
void pstUpSend() { // Send 0x14
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bZERO(); bONE(); bZERO(); //4
bONE(); bZERO(); bZERO(); // 1
Postamble();
}
}
//---------Folder PST? DOWN----------------------------------------------
void pstDnSend() { // Send 0x15
for (int i = 1; i <= Reps; i++) {
Preamble();
bONE(); bZERO(); bONE(); bZERO(); // 5
bONE(); bZERO(); bZERO(); // 1
Postamble();
}
}
//---------MUTE-onoff?--------------------------------------------------
void onoffSend() { // Send 0x0E
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bONE(); bONE(); bONE(); // E (14)
bZERO(); bZERO(); bZERO(); // 0
Postamble();
}
}
//----------Mute--------------------------------------------------
void muteSend() { // Send 0x06 ATT mute
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bONE(); bONE(); bZERO(); // 6
bZERO(); bZERO(); bZERO(); // 0
Postamble();
}
}
//---------JVC Equilazer---------------------------------------------------
void entPlaySend() { // Send 0x16
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bONE(); bONE(); bZERO(); // 6
bONE(); bZERO(); bZERO(); // 1
Postamble();
}
}
//---------VOL UP-----------------------------------------------
void volUpSend() { // Send 0x04
for (int i = 1; i <= Reps; i++) {
Preamble();
bZERO(); bZERO(); bONE(); bZERO(); //4
bZERO(); bZERO(); bZERO(); // 0
Postamble(); Serial.println("Vup");
}
}
//---------VOL DOWN-----------------------------------------------
void volDnSend() { // Send 0x05
for (int i = 1; i <= Reps; i++) {
Preamble();
bONE(); bZERO(); bONE(); bZERO(); // 5
bZERO(); bZERO(); bZERO(); // 0
Postamble();
}
}
//----------JVC sound----------------------------------------------
void soundSend() { // Send 0x0D
for (int i = 1; i <= Reps; i++) {
Preamble();
bONE(); bZERO(); bONE(); bONE(); // D (13)
bZERO(); bZERO(); bZERO(); // 0
Postamble();
}
}

//---------------JVC koder
void bONE() { // Send a binary ONE over the line
digitalWrite(outPIN, HIGH); // Pull 3.5mm TIP low
delayMicroseconds(Length); // for 537us
digitalWrite(outPIN, LOW); // Allow 3.5mm TIP to go high
delayMicroseconds(Length * 3); // for 537 * 3 = 1611us
Serial.print(" 1");
}
void bZERO() { // Send a binary ZERO over the line
digitalWrite(outPIN, HIGH); // Pull 3.5mm TIP low
delayMicroseconds(Length); // for 537us
digitalWrite(outPIN, LOW); // Allow 3.5mm TIP to go high
delayMicroseconds(Length); // for 537us
Serial.print(" 0");
}
void Preamble() {
digitalWrite(outPIN, LOW); // Not sure what this does
delayMicroseconds(Length * 1);
digitalWrite(outPIN, HIGH); // AGC
delayMicroseconds(Length * 16);
digitalWrite(outPIN, LOW); // AGC
delayMicroseconds(Length * 8);
bONE(); // 1 Start Bit
// (7 bit device code)
bONE(); bONE(); bONE(); bZERO(); // 7
bZERO(); bZERO(); bONE(); //4
}
void Postamble() {
bONE(); bONE(); // 2 stop bits
}

Inga kommentarer:

Skicka en kommentar