#include "Buzzer.h"
#include "States.h"

Buzzer::Buzzer() {
  active = false;
  startTime = 0;
  beepDuration = 0;
}

void Buzzer::begin() {
  pinMode(BUZZER_PIN, OUTPUT);

  digitalWrite(BUZZER_PIN, LOW);
}

void Buzzer::update() {
  if (active) {
    if (millis() - startTime >= beepDuration) {
      off();
    }
  }
}

void Buzzer::on() {
  digitalWrite(BUZZER_PIN, HIGH);

  active = true;

  startTime = millis();
}

void Buzzer::off() {
  digitalWrite(BUZZER_PIN, LOW);

  active = false;
}

void Buzzer::beep(uint16_t duration) {
  beepDuration = duration;

  on();
}

/////////////////////////////////////////////////////////

void Buzzer::startup() {
  beep(80);

  delay(120);

  beep(120);

  delay(160);
}

/////////////////////////////////////////////////////////

void Buzzer::focusStart() {
  beep(100);

  delay(120);

  beep(100);
}

/////////////////////////////////////////////////////////

void Buzzer::pickup() {
  beep(40);
}

/////////////////////////////////////////////////////////

void Buzzer::warning() {
  for (int i = 0; i < 3; i++) {
    beep(60);

    delay(100);
  }
}

/////////////////////////////////////////////////////////

void Buzzer::complete() {
  beep(120);

  delay(150);

  beep(120);

  delay(150);

  beep(220);
}