 #include "FiniteStateMachine.h"
 #include "AlarmToneLanguage.h"
 #include "AlarmManager.h"
+#include "ClickButton.h"
 #include "Runtime.h"
 
+SYSTEM_MODE(SEMI_AUTOMATIC);
 SYSTEM_THREAD(ENABLED);
-SYSTEM_MODE(SEMI_AUTOMATIC);
-
-State Start = State(NULL, updateStart, exitStart);
-State Clock = State(updateClock);
-State Alarm = State(enterAlarm, updateAlarm, exitAlarm);
-// State Snuze = State(enterSnuze, updateSnuze, exitSnuze); // Snooze is too many letters, broke my trend
-
-FSM stateMachine = FSM(Start);
 
 // use hardware SPI
 #define OLED_DC     D3
 #define OLED_CS     D4
 #define OLED_RESET  D5
+
+#define SPEAKER     D0
+#define BUTTON      D1
 
 #if defined COLOR_SCREEN
 // Color definitions

}

void enterAlarm() {
  alarmRuntime.start();
  RGB.color(0, 0, 255);

  Particle.publish("alarm:start", Time.format(Time.now(), TIME_FORMAT_ISO8601_FULL));

  /*parser = new AlarmToneLanguage(song);*/
  /*player.reset();*/
}

void updateAlarm() {
  RGB.brightness(map(abs((millis() % 2000) - 1000), 0, 1000, 0, 255));

  // TODO: Remove when snooze/terminate functionality is implemented
  if(alarmRuntime.check(ALARM_LENGTH)) {
    stateMachine.transitionTo(Clock);
  }
}

void exitAlarm() {
  /*player.stop();*/
  noTone(speakerPin);

  RGB.color(0, 0, 0);
  RGB.brightness(0);

  Particle.publish("alarm:end", Time.format(Time.now(), TIME_FORMAT_ISO8601_FULL));

  /*delete parser;*/
}

void render() {

  time_t time = Time.now();

  #if !defined COLOR_SCREEN
  Display.clearDisplay();
  #endif

  #if defined COLOR_SCREEN
  Display.setTextColor(YELLOW, BLACK);
  #else
  Display.setTextColor(WHITE);
  #endif
  Display.setTextSize(3);
  Display.setCursor(12, 22);

  Display.print(Time.format(time, Time.second() % 2 == 0 ? "%l:%M" : "%l %M"));

  Display.setTextSize(1);

  if(Time.isAM()) {
    #if defined COLOR_SCREEN
    Display.print("  ");
    #endif
    Display.setCursor(102, 37);
  }
  Display.print(Time.format(time, "%p"));

  Display.setTextSize(1);
  Display.setCursor(68, 1);
  Display.print(Time.format(time, "%F"));

  #if !defined COLOR_SCREEN
  Display.display();
  #endif
}

void sync() {

  if(Time.hour() == 0 && Time.minute() == 0) Particle.syncTime();
}

void play() {
  if(playerRuntime.check(parser->getDuration())) {
    if(parser->next()) {
      parser->isRest() ? noTone(SPEAKER) : tone(SPEAKER, parser->getNote(), parser->getDuration());
    } else {
      parser->rewind();
    }
  }
}

int handleAlarm(String value) {
  bool result = false;
