Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.80 KB | None | 0 0
  1. // Known commands for JVC KD-R531:
  2. //   HEX   DEC  BIN(7b)  FUNCTION
  3. //   0x04    4  0000100  Volume +
  4. //   0x05    5  0000101  Volume -
  5. //   0x08    8  0001000  Source cycle
  6. //   0x0D   13  0001101  Equalizer preset cycle
  7. //   0x0E   14  0001110  Mute toggle / Play/pause toggle
  8. //   0x12   18  0010010  Tuner Search + / Track + (and Manual Tune + / Fast Forward with press & hold)
  9. //   0x13   19  0010011  Tuner Search - / Track - (and Manual Tune - / Fast Rewind with press & hold)
  10. //   0x14   20  0010100  Tuner Preset + / USB Folder +
  11. //   0x15   21  0010101  Tuner Preset - / USB Folder -
  12. //   0x37   55  0110111  UNKNOWN, appears to be a sort of reset as well as a display test
  13. //   0x58   88  1011000  UNKNOWN, displays 'SN WRITING' where WRITING is blinking
  14.  
  15. int PIN = 9;          // Digital IO pin connected to base of transistor
  16. int Length = 537;     // Length in Microseconds
  17. int IncomingByte = 0; // Initialize Serial Buffer
  18. int Reps = 3;         // Number of times to repeat each transmission
  19.  
  20. // Коды имеющихся на руле кнопок
  21. const int VOL_UP=1;
  22. const int VOL_DN=2;
  23. const int PREV_TR=3;
  24. const int NEXT_TR=4;
  25. const int MODE=5;
  26. const int MUTE=6;
  27. const int FOLDER_DOWN=7;
  28. const int FOLDER_UP=8;
  29.  
  30. int wheelPin=A2; // аналоговый пин, на котором мы считываем сопротивление нажатой на руле кнопки
  31. int wheelMod=12; // цифровой пин, на котором считываем состояние кнопки Mode
  32.  
  33. int i=0;
  34. int prevButton=0;
  35.  
  36. unsigned long  pressedTime; // Время нажатия кнопки
  37.  
  38. void setup() {
  39.   pinMode(wheelPin, INPUT);  //set pin A2 as INPUT
  40.   pinMode(wheelMod, INPUT);  //set pin 12 as INPUT
  41.   pinMode(PIN, OUTPUT); // Set pin to output
  42.   digitalWrite(PIN, LOW); // Make PIN low to shut off transistor
  43.  
  44.   //Serial.begin(9600);
  45.  
  46. }
  47.  
  48. int getR() { // Эта функция читает сопротивление с кнопок на руле и возвращает код нажатой кнопки, либо 0
  49.  
  50.   // читаем сопротивление (на самом деле напряжение, конечно) на аналоговом пине
  51.   int r = analogRead(wheelPin);
  52.  
  53. //  Serial.println(r);
  54.  
  55.   // Ищем, какая кнопка соответствует этому сопротивлению.
  56.   // Данные значения сопротивлений подходят для Toyota, для других автомобилей числа будут другие.
  57.   if (r>=235 && r<=245) return(VOL_UP); // 240
  58.   if (r>=496 && r<=506) return(VOL_DN); // 501
  59.   if (r>=88 && r<=98) return(PREV_TR); // 93
  60.   if (r>=0 && r<=5) return(NEXT_TR); // 0
  61.  
  62.   // если ни одна из кнопок на пине wheelPin не нажата, проверяем нажата ли кнопка Mode
  63.      r = digitalRead(wheelMod);
  64.      if (r == LOW) return(MODE);
  65.      
  66.   // если ни одна из кнопок не нажата, возвращаем 0
  67.   return (0);
  68. }
  69.  
  70. void loop() {
  71.  int currButton=getR(); // заносим в переменную currButton код нажатой кнопки
  72.   if (currButton!=prevButton) { // если значение поменялось с прошлого раза
  73.  
  74.     delay(10);
  75.     currButton=getR(); // ждем 10ms и читаем еще раз, чтобы исключить "дребезг" кнопки
  76.  
  77.     if (currButton!=prevButton) { // если код кнопки точно поменялся с прошлого раза
  78.       //Serial.println(currButton);
  79.  
  80.       // если нажата PREV_TR или NEXT_TR или MODE, запоминаем время нажатия, но на магнитолу никакой сигнал пока не отправляем и выходим.
  81.       if (currButton==PREV_TR || currButton==NEXT_TR || currButton == MODE) {
  82.          pressedTime=millis();
  83.          prevButton=currButton;
  84.          return;
  85.       }
  86.        
  87.       // Если отжата PREV_TR, смотрим, сколько времени она удерживалась - больше или меньше 3 секунд.
  88.       // В зависимости от этого времени меняем значение currButton на FOLDER_UP или PREV_TR
  89.       if (currButton==0 && prevButton==PREV_TR  && pressedTime>0) {
  90.         if ((millis()-pressedTime)>3000) currButton=FOLDER_DOWN;
  91.         else currButton=PREV_TR;
  92.         pressedTime=0; // обнуляем таймер, чтобы при следующем выполении цикла loop эти проверки не сработали повторно.
  93.       }      
  94.          
  95.       // то же самое если отжата NEXT_TR
  96.       if (currButton==0 && prevButton==NEXT_TR && pressedTime>0) {
  97.         if ((millis()-pressedTime)>3000) currButton=FOLDER_UP;
  98.         else currButton=NEXT_TR;
  99.         pressedTime=0;
  100.       }  
  101.    
  102.       // то же самое если отжата MODE
  103.       if (currButton==0 && prevButton==MODE && pressedTime>0) {
  104.         if ((millis() - pressedTime) > 3000) currButton = MUTE;
  105.         else currButton = MODE;
  106.         pressedTime = 0;
  107.       }
  108.      
  109.       prevButton=currButton;     // сохраняем новое значение в переменную prevButton
  110.    
  111.       switch(currButton) {
  112.        case VOL_UP: JVCVolUp(); break;  
  113.        case VOL_DN: JVCVolDn(); break;  
  114.        case PREV_TR: JVCSkipBack(); break;
  115.        case NEXT_TR: JVCSkipFwd(); break;  
  116.        case FOLDER_DOWN: JVCSkipBackHold(); break;
  117.        case FOLDER_UP: JVCSkipFwdHold(); break;  
  118.        case MODE: JVCSource(); break;
  119.        case MUTE: JVCMute(); break;
  120.  
  121.        default: break;
  122.      }
  123.    }
  124.   }
  125.   delay(5);
  126.  
  127. }
  128.  
  129. void JVCVolUp() {      // Send 0x04
  130.   for (int i = 1; i <= Reps; i++); {
  131.     Preamble();
  132.    
  133.     bZERO();
  134.     bZERO();
  135.     bONE();    // 4
  136.     bZERO();
  137.    
  138.     bZERO();
  139.     bZERO();   // 0
  140.     bZERO();
  141.  
  142.     Postamble();
  143.   }
  144. }
  145.  
  146. void JVCVolDn() {      // Send 0x05
  147.   for (int i = 1; i <= Reps; i++); {
  148.     Preamble();
  149.    
  150.     bONE();
  151.     bZERO();
  152.     bONE();    // 5
  153.     bZERO();
  154.    
  155.     bZERO();
  156.     bZERO();   // 0
  157.     bZERO();
  158.  
  159.     Postamble();
  160.   }
  161. }
  162.  
  163. void JVCSource() {      // Send 0x08
  164.   for (int i = 1; i <= Reps; i++); {
  165.     Preamble();
  166.    
  167.     bZERO();
  168.     bZERO();
  169.     bZERO();    // 8
  170.     bONE();
  171.    
  172.     bZERO();
  173.     bZERO();   // 0
  174.     bZERO();
  175.  
  176.     Postamble();
  177.   }
  178. }
  179.  
  180. void JVCMute() {      // Send 0x0E
  181.   for (int i = 1; i <= Reps; i++); {
  182.     Preamble();
  183.    
  184.     bZERO();
  185.     bONE();
  186.     bONE();    // E (14)
  187.     bONE();
  188.    
  189.     bZERO();
  190.     bZERO();   // 0
  191.     bZERO();
  192.  
  193.     Postamble();
  194.   }
  195. }
  196.  
  197. void JVCSkipFwd() {      // Send 0x12
  198.   for (int i = 1; i <= Reps; i++); {
  199.     Preamble();
  200.    
  201.     bZERO();
  202.     bONE();
  203.     bZERO();    // 2
  204.     bZERO();
  205.    
  206.     bONE();
  207.     bZERO();   // 1
  208.     bZERO();
  209.  
  210.     Postamble();
  211.   }
  212. }
  213.  
  214. void JVCSkipBack() {      // Send 0x13
  215.   for (int i = 1; i <= Reps; i++); {
  216.     Preamble();
  217.    
  218.     bONE();
  219.     bONE();
  220.     bZERO();    // 3
  221.     bZERO();
  222.    
  223.     bONE();
  224.     bZERO();   // 1
  225.     bZERO();
  226.  
  227.     Postamble();
  228.   }
  229. }
  230.  
  231. void JVCSkipFwdHold() {      // Send 0x14
  232.   for (int i = 1; i <= Reps; i++); {
  233.     Preamble();
  234.    
  235.     bZERO();
  236.     bZERO();
  237.     bONE();    // 4
  238.     bZERO();
  239.    
  240.     bONE();
  241.     bZERO();   // 1
  242.     bZERO();
  243.  
  244.     Postamble();
  245.   }
  246. }
  247.  
  248. void JVCSkipBackHold() {      // Send 0x15
  249.   for (int i = 1; i <= Reps; i++); {
  250.     Preamble();
  251.    
  252.     bONE();
  253.     bZERO();
  254.     bONE();    // 5
  255.     bZERO();
  256.    
  257.     bONE();
  258.     bZERO();   // 1
  259.     bZERO();
  260.  
  261.     Postamble();
  262.   }
  263. }
  264.  
  265. void bONE() {                     // Send a binary ONE over the line
  266.   digitalWrite(PIN, HIGH);        // Pull 3.5mm TIP low
  267.   delayMicroseconds(Length);      // for 537us
  268.   digitalWrite(PIN, LOW);         // Allow 3.5mm TIP to go high
  269.   delayMicroseconds(Length * 3);  // for 537 * 3 = 1611us
  270. }
  271.  
  272.  
  273. void bZERO() {                    // Send a binary ZERO over the line
  274.   digitalWrite(PIN, HIGH);        // Pull 3.5mm TIP low
  275.   delayMicroseconds(Length);      // for 537us
  276.   digitalWrite(PIN, LOW);         // Allow 3.5mm TIP to go high
  277.   delayMicroseconds(Length);      // for 537us
  278. }
  279.  
  280. void Preamble() {
  281.   digitalWrite(PIN, LOW);         // Not sure what this does
  282.   delayMicroseconds(Length * 1);
  283.  
  284.   digitalWrite(PIN, HIGH);        // AGC
  285.   delayMicroseconds(Length * 16);
  286.  
  287.   digitalWrite(PIN, LOW);         // AGC
  288.   delayMicroseconds(Length * 8);
  289.  
  290.   bONE();    // 1 Start Bit
  291.  
  292.   bONE();    //       (7 bit device code)
  293.   bONE();    
  294.   bONE();    // 7
  295.   bZERO();
  296.  
  297.   bZERO();
  298.   bZERO();    //4
  299.   bONE();
  300. }
  301.    
  302.    
  303. void Postamble() {
  304.   bONE();
  305.   bONE();    // 2 stop bits
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement