/// \file PinballGame/PopBumper.h

/// \brief Control a single pinball solenoid actuator.

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

/// \details This file contains support code for implementing impulsive actuator
/// control for the Arduino pinball machine demo.  This is very simple for now,
/// but could be expanded to support PWM control for reducing holding currents.

/****************************************************************/
class PopBumper {

private:
  /// Number of the digital pin to use for output.  The hardware is assumed to be
  /// active-high, i.e., idling at a low voltage, and driven high when firing the solenoid.
  int output_pin;

  /// Countdown for the actuator ON period, in microseconds.
  long output_timer;

  /// Duration for the actuator ON period, in microseconds.
  long pulse_width;

  /// The current Boolean state of the output.
  bool active;

  /// Count of the total number of output events.
  long event_count;
  
public:

  /// Constructor to initialize an instance of the class.  Note that this only
  /// initializes object state, it does not configure the hardware, which should
  /// be performed directly by the user.
  PopBumper(int pin);    

  /// Update function to be called as frequently as possible to operate the
  /// output state machine. It requires the number of microseconds elapsed since
  /// the last update.
  void update(unsigned long interval);

  /// Trigger function to start an actuation cycle.
  void trigger(void);
  
  /// Debugging function to print a representation of the current state to the serial port.
  void send_debug(void);

  /// Access function to return the current state.
  bool isActive(void) { return active; }
};

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