# Toggle

Przetwarzanie

Konwertuje monostabilny impuls wejściowy na bistabilne wyjście przełączające. Każde zbocze narastające na wejściu przełącza stan wyjścia.

Toggle
TG
ON
OFF
O
ON
OFF

# Wejścia

IDSkrótNazwaTypDomyślnieOpis
inputTGPrzełączBOOLEANfalseSygnał wejściowy impulsu. Przy każdym zboczu narastającym wyjście zmienia swój stan.
onONWłączBOOLEANfalseWłącza wyjście (O). Reaguje tylko na zbocze narastające.
offOFFWyłączBOOLEANfalseImpuls: Resetuje wyjście do stanu wyłączonego. Przy ciągłym stanie wysokim: Blok jest zablokowany. Wejście dominujące.

# Wyjścia

IDSkrótNazwaTypDomyślnieOpis
outputOWyjścieBOOLEANfalseBistabilne wyjście, które przełącza swój stan przy każdym impulsie wejściowym.
onONWłączonyBOOLEANfalseImpuls gdy wyjście jest włączane. Stała szerokość impulsu 100ms.
offOFFWyłączonyBOOLEANfalseImpuls gdy wyjście jest wyłączane. Stała szerokość impulsu 100ms.

# Stan

IDNazwaTypDomyślnieJednostkaOpis
last_input_stateOstatni stan wejściaBOOLEANfalsePrzechowuje poprzedni stan wejścia przełączającego
last_on_stateOstatni stan włączeniaBOOLEANfalsePrzechowuje poprzedni stan wejścia włączenia
last_off_stateOstatni stan wyłączeniaBOOLEANfalsePrzechowuje poprzedni stan wejścia wyłączenia

# Kod źródłowy

Pokaż kod Volang
pulse_ms = 100
channel = input::channel()
value = input::value()

// Single callback handler - resets both pulse outputs
// Only one callback can be active at a time per block
extern fn onCallback(unused) {
    output::set("on", false)
    output::set("off", false)
}

// Handle "off" input first (dominant input)
if (channel == "off") {
    last_off_state = state::get("last_off_state")

    if (value and !last_off_state) {
        // Rising edge on off input - turn off output
        prev = output::get("output")
        if (prev) {
            output::set("output", false)
            callback::clear()
            output::set("on", false)
            output::set("off", true)
            callback::set(pulse_ms, "onCallback", 0)
        }
    }

    state::set("last_off_state", value)
    return
}

// If off input is held high, block is locked - ignore other inputs
// Still track input states to avoid false edges after unlock
if (state::get("last_off_state")) {
    if (channel == "input") {
        state::set("last_input_state", value)
    } else if (channel == "on") {
        state::set("last_on_state", value)
    }
    return
}

// Handle "on" input
if (channel == "on") {
    last_on_state = state::get("last_on_state")

    if (value and !last_on_state) {
        // Rising edge on "on" input - turn on output
        prev = output::get("output")
        if (!prev) {
            output::set("output", true)
            callback::clear()
            output::set("off", false)
            output::set("on", true)
            callback::set(pulse_ms, "onCallback", 0)
        }
    }

    state::set("last_on_state", value)
    return
}

// Handle toggle input
if (channel == "input") {
    last_input_state = state::get("last_input_state")

    // Only react to rising edge (false -> true)
    if (value and !last_input_state) {
        prev = output::get("output")
        output::toggle("output")
        callback::clear()
        output::set("on", false)
        output::set("off", false)
        if (prev) {
            // Was on, now off - emit off pulse
            output::set("off", true)
        } else {
            // Was off, now on - emit on pulse
            output::set("on", true)
        }
        callback::set(pulse_ms, "onCallback", 0)
    }

    state::set("last_input_state", value)
}
Konwertuje monostabilny impuls wejściowy na bistabilne wyjście przełączające. Każde zbocze narastające na wejściu przełącza stan wyjścia.