 #define LOCKED      LOW
 #define UNLOCKED   HIGH
 #define UNDEFINED     2
+#define HEARTBEAT_TIMEOUT       10000
+#define HEARTBEAT_SMALL_TIMEOUT  3333
+
+#define RUN           8
 
 byte button = 0;
 byte pushlock = UNDEFINED;

    fdel = -fdel;
  }
  delay(fade_delay);
}

void show_btn_color( int mode ) {
  switch (mode) {
    case 1:
            fade_btn(10);
            
            break;
    case 2:
            fade_btn(5);
            break;        
    case 3:
            analogWrite(LED_R, 0);
            analogWrite(LED_G, 255);
            analogWrite(LED_B, 0);
            
            break;
    case 4:
            analogWrite(LED_R, 0);
            analogWrite(LED_G, 0);
            analogWrite(LED_B, 255);
            
            break;
    case 5:
            analogWrite(LED_R, 0);
            analogWrite(LED_G, 255);
            analogWrite(LED_B, 255);
           
            break;
    case 6:
            analogWrite(LED_R, 255);
            analogWrite(LED_G, 255);
            analogWrite(LED_B, 0);
           
            break;
    case 7:
            fade_btn(1);
            break;
    case 8:
            c_r = 255;
            c_g = 0;
            fade_btn(10);
            break;
    default:
            cmode = 1;
            analogWrite(LED_R, 0);
            analogWrite(LED_G, 0);
            analogWrite(LED_B, 0);
            delay(50);
            analogWrite(LED_R, 200);
            analogWrite(LED_G, 200);
            analogWrite(LED_B, 200);
            delay(50);
            analogWrite(LED_R, 0);
            analogWrite(LED_G, 0);
            analogWrite(LED_B, 0);
            break;
  }
}

void button_push() {

  pinMode(LED_R, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(LED_B, OUTPUT);

  pinMode(RUN, OUTPUT);
  
  attachInterrupt(digitalPinToInterrupt(BUTTON), button_push, FALLING);
  attachInterrupt(digitalPinToInterrupt(PUSHLOCK_NO), pushlock_change, CHANGE);
  //attachInterrupt(digitalPinToInterrupt(PUSHLOCK_NC), pushlock_open, CHANGE);

  pushlock = digitalRead(PUSHLOCK_NO);
  
//  if (pushlock == LOCKED) {
//    statefunc = server_locked;
//  } else {
    statefunc = server_booting;
//  }
  tracker = millis();
  tracker2 = tracker;

  Serial.begin(115200);
}

void loop() {
  statefunc = (StateFunc)(*statefunc)();
}

void touch_run_pins() {
  digitalWrite(RUN, HIGH);
  delay(50);
  digitalWrite(RUN, LOW);
}

void fade_bootable(byte del) {
  analogWrite(LED_R, 255);
  analogWrite(LED_G, 255-c_g);
  analogWrite(LED_B, 255);

  c_g = c_g + fade_g;
  
  if ( c_g <= 0 || c_g >= 87 ) {
    fade_g = -fade_g;
  }

  if (fade_g > 0) {
   delay(del/2); 
  } else {
   delay(del); 
  }
}

void fade_shutdown(byte del) {
  analogWrite(LED_R, 255-c_r);
  analogWrite(LED_G, 255-c_r);
  analogWrite(LED_B, 255);

  c_r = c_r + fade_b;
  
  if ( c_r <= 0 || c_r >= 140 ) {
    fade_b = -fade_b;
  }

  if (fade_b > 0) {
   delay(del); 
  } else {
   delay(del/3); 
  }
}

void fade_hungup(byte del) {
  analogWrite(LED_R, 255-c_r);
  analogWrite(LED_G, 255);
  analogWrite(LED_B, 255);

  c_r = c_r + fade_r;
  
  if ( c_r <= 0 || c_r >= 240 ) {
    fade_r = -fade_r;
  }
  
  delay(del);
}

void show_red() {
  analogWrite(LED_R, 0);
  analogWrite(LED_G, 255);
  analogWrite(LED_B, 255);
}

void show_color(byte r, byte g, byte b) {
  analogWrite(LED_R, 255-r);
  analogWrite(LED_G, 255-g);
  analogWrite(LED_B, 255-b);
}

void show_none() {
  show_color(0,0,0);
}

void show_heartbeat() {
  show_color(0,255,0);
  delay(10);
  show_color(255,0,0);
  delay(10);
  show_color(0,0,0);
  delay(15);
}

void show_missing_heartbeat() {
  show_color(255,0,0);
  delay(10);
  show_color(255,0,0);
  delay(10);
  show_color(0,0,0);
  delay(15);
}

void fade_boot(byte del) {
  analogWrite(LED_R, 255-c_r);
  analogWrite(LED_G, 255-c_g);
  analogWrite(LED_B, 255-c_b);
