/// \file PinballGame/PopBumper.cpp
/// \copyright No copyright, 2016, Garth Zeglin.  This file is explicitly placed in the public domain.

/****************************************************************/

#include "Arduino.h"
#include "PopBumper.h"

/****************************************************************/

PopBumper::PopBumper(int pin)
{
  output_pin = pin;
  output_timer = 0;
  active = false;
  event_count = 0;
  pulse_width = 100000;  // 100 msec = 0.1 seconds
}

void PopBumper::update(unsigned long interval)
{
  // for now, this only needs to check when to turn off the solenoid
  if (active) {
    output_timer -= interval;
    if (output_timer < 0) {
      active = false;
      digitalWrite(output_pin, LOW);
    }
  }
}

void PopBumper::trigger(void)
{
  // only accept a trigger if not already active; if the solenoid is currently firing, the new trigger is ignored
  if (!active) {
    output_timer = pulse_width;
    active = true;
    digitalWrite(output_pin, HIGH);
  }
}
  
void PopBumper::send_debug(void)
{
  Serial.print("bumper pin:");
  Serial.print(output_pin);

  Serial.print("  active: ");
  Serial.print(active);

  Serial.print("  events: ");
  Serial.print(event_count);

  Serial.print("  pulse width: ");
  Serial.print(pulse_width);
  
  Serial.println();
}

/****************************************************************/
