// src/hardware/button.cpp
#include "button.h"

ButtonHandler::ButtonHandler(PinName pin, PinName led_pin) : button(pin), led(led_pin, 0) {
  // ISR for button press (falling edge)
  button.fall(callback(this, &ButtonHandler::on_pressed));
}

void ButtonHandler::on_pressed() {
  pressed_flag = true;
}

ButtonEvent ButtonHandler::poll() {
  static Timer blink_timer;
  static bool blinking = false;

  if (pressed_flag) {
    pressed_flag = false;
    led = 1;
    blink_timer.reset();
    blink_timer.start();
    blinking = true;
    return ButtonEvent::Pressed;
  }

  if (blinking && blink_timer.elapsed_time() >= 500ms) {
    led = 0;
    blink_timer.stop();
    blinking = false;
  }

  return ButtonEvent::None;
}
