//! Bootloader helpers shared between power-on checks and runtime button chords.

use crate::status::{StatusLed, StatusMode};
use cortex_m::asm;
use rp2040_hal::gpio::AnyPin;
use rp2040_hal::pio::{PIOExt, StateMachineIndex};

/// Returns `true` when the power-on matrix snapshot requests bootloader entry.
///
/// The original firmware required the front-left-lower button to be held during
/// power-up to jump straight into ROM boot.
pub fn startup_requested(buttons: &[bool; crate::hardware::NUMBER_OF_BUTTONS]) -> bool {
    buttons[crate::mapping::BUTTON_FRONT_LEFT_LOWER]
}

/// Puts the RP2040 into the ROM bootloader after updating the status LED.
pub fn enter<P, SM, I>(status_led: &mut StatusLed<P, SM, I>) -> !
where
    P: PIOExt,
    SM: StateMachineIndex,
    I: AnyPin<Function = P::PinFunction>,
{
    status_led.update(StatusMode::Bootloader);
    let gpio_activity_pin_mask: u32 = 0;
    let disable_interface_mask: u32 = 0;
    rp2040_hal::rom_data::reset_to_usb_boot(gpio_activity_pin_mask, disable_interface_mask);
    loop {
        asm::nop();
    }
}
