 
   if (b == SYN_FIELD && n > 2 && frame.get_byte(n - 1) == 0) {
     digitalWrite(RX_LED, LOW);
-    
+
     frame.pop_byte();
     handle_swm_frame();
     frame.reset();
-    
+
     digitalWrite(RX_LED, HIGH);
   } else if (n == LinFrame::kMaxBytes) {
     frame.reset();


#include "Mouse.h"
#include "Keyboard.h"

// https://github.com/zapta/linbus/tree/master/analyzer/arduino
#include "lin_frame.h"

#define CS_PIN 15
#define RX_LED 17

#define HEARTBEAT_TIMEOUT 2000
#define CLICK_TIMEOUT 100

#define MOUSE_BASE_SPEED 8
#define MOUSE_SPEEDUP 3

#define SYN_FIELD 0x55
#define SWM_ID 0x20

// Volvo V50 2007 SWM key codes
//

    case JOYSTICK_RIGHT:
      move_mouse(1, 0);
      break;
  }
}

void handle_buttons() {
  switch (frame.get_byte(2)) {
    case BUTTON_BACK:
      if (!lastBackDown)
        Keyboard.press(KEY_ESC);

      lastBackDown = currentMillis;
      break;

    case BUTTON_ENTER:
      if (!lastEnterDown && state == STATE_ON)
        Mouse.press();

      lastEnterDown = currentMillis;
      break;
  }
}

void move_mouse(int dx, int dy) {
  Mouse.move(dx * mouseSpeed, dy * mouseSpeed, 0);
  lastMouseDown = currentMillis;
  mouseSpeed += MOUSE_SPEEDUP;
}

void release_keys() {
  if (lastEnterDown && currentMillis - lastEnterDown > CLICK_TIMEOUT) {
    if (state == STATE_ON)
      Mouse.release();
    else
      turn_on();

    lastEnterDown = 0;
  }

  if (lastBackDown && currentMillis - lastBackDown > CLICK_TIMEOUT) {
    Keyboard.release(KEY_ESC);
    lastBackDown = 0;
  }

  if (lastMouseDown && currentMillis - lastMouseDown > CLICK_TIMEOUT) {
    lastMouseDown = 0;
    mouseSpeed = MOUSE_BASE_SPEED;
  }
}

void check_ignition_key() {
  if (lastHeartbeat && currentMillis - lastHeartbeat > HEARTBEAT_TIMEOUT) {
    lastHeartbeat = 0;
    turn_off();
  }
}

void turn_on() {
  state = STATE_ON;
  turn_on_phone();
}

void turn_on_phone() {
  analogWrite(MOBILE_CHARGER_PIN, 255);
  delay(MOBILE_CHARGER_TRIGGER_TIME);
  analogWrite(MOBILE_CHARGER_PIN, 0);
}

void turn_off() {
  state = STATE_OFF;
}

// send serial data to Volvo RTI screen mechanism
void rti() {
  if (currentMillis - lastRtiWrite < RTI_INTERVAL) return;
  
  switch (rtiStep) {
    case 0: // mode
      if (state == STATE_OFF) 
        rti_print(0x46);
      else
        rti_print(0x40);
              
      rtiStep++;
      break;

    case 1: // brightness
      rti_print(0x20);
      rtiStep++;
      break;

    case 2: // sync
      rti_print(0x83);
      rtiStep = 0;
      break;    
  }

  lastRtiWrite = currentMillis;
}

void rti_print(char byte) {
  rtiSerial.print(byte);
}

// -- debugging
