Last active
May 23, 2026 04:36
-
-
Save julianpoma/0dbfbc6d22f694211dad063bb58fd9cc to your computer and use it in GitHub Desktop.
X-Plane 12: progressive single button brake (lua script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- progressive_brakes.lua | |
| -- XP12 + FlyWithLua | |
| -- Progressive both-brakes command for whitelisted aircraft only | |
| -- Includes double-tap brake hold | |
| local SCRIPT_NAME = "progressive_brakes" | |
| ------------------------------------------------------------ | |
| -- User configuration | |
| ------------------------------------------------------------ | |
| local SECONDS_TO_FULL_BRAKE = 3.0 | |
| local SECONDS_TO_RELEASE = 1.5 | |
| local DOUBLE_TAP_WINDOW = 0.5 | |
| local aircraft_config = { | |
| ["A330.acf"] = { | |
| override_toe_brakes = false, | |
| }, | |
| -- ["Some_Other_Aircraft.acf"] = { | |
| -- override_toe_brakes = true, | |
| -- }, | |
| } | |
| ------------------------------------------------------------ | |
| -- Datarefs | |
| ------------------------------------------------------------ | |
| dataref("total_running_time_sec", "sim/time/total_running_time_sec") | |
| dataref("left_brake_ratio_dref", "sim/cockpit2/controls/left_brake_ratio", "writable") | |
| dataref("right_brake_ratio_dref", "sim/cockpit2/controls/right_brake_ratio", "writable") | |
| ------------------------------------------------------------ | |
| -- Internal state | |
| ------------------------------------------------------------ | |
| local enabled = false | |
| local current_aircraft_config = nil | |
| local brake_ratio = 0.0 | |
| local brake_start_time = 0.0 | |
| local brake_release_time = 0.0 | |
| local brake_ratio_at_press = 0.0 | |
| local brake_ratio_at_release = 0.0 | |
| local last_release_time = -999.0 | |
| local brake_hold_active = false | |
| ------------------------------------------------------------ | |
| -- Aircraft whitelist | |
| ------------------------------------------------------------ | |
| local function get_aircraft_config() | |
| local current_acf = tostring(AIRCRAFT_FILENAME or "") | |
| return aircraft_config[current_acf] | |
| end | |
| current_aircraft_config = get_aircraft_config() | |
| local function is_whitelisted_aircraft() | |
| local current_acf = tostring(AIRCRAFT_FILENAME or "") | |
| for _, acf in ipairs(whitelisted_acf) do | |
| if current_acf == acf then | |
| return true | |
| end | |
| end | |
| return false | |
| end | |
| if current_aircraft_config ~= nil then | |
| enabled = true | |
| if current_aircraft_config.override_toe_brakes == true then | |
| set("sim/operation/override/override_toe_brakes", 1) | |
| logMsg(SCRIPT_NAME .. ": override_toe_brakes enabled") | |
| end | |
| logMsg(SCRIPT_NAME .. ": enabled for " .. tostring(AIRCRAFT_FILENAME)) | |
| else | |
| enabled = false | |
| logMsg(SCRIPT_NAME .. ": disabled for " .. tostring(AIRCRAFT_FILENAME)) | |
| end | |
| ------------------------------------------------------------ | |
| -- Helpers | |
| ------------------------------------------------------------ | |
| local function clamp(value, min_value, max_value) | |
| return math.max(min_value, math.min(max_value, value)) | |
| end | |
| local function set_both_brakes(ratio) | |
| brake_ratio = clamp(ratio, 0.0, 1.0) | |
| left_brake_ratio_dref = brake_ratio | |
| right_brake_ratio_dref = brake_ratio | |
| end | |
| ------------------------------------------------------------ | |
| -- Command callbacks | |
| ------------------------------------------------------------ | |
| function progressive_brakes_start() | |
| if not enabled then return end | |
| local now = total_running_time_sec | |
| local is_double_tap = (now - last_release_time) <= DOUBLE_TAP_WINDOW | |
| if is_double_tap then | |
| -- Second press within the double-tap window: | |
| -- hold the current brake pressure while the button remains pressed. | |
| brake_hold_active = true | |
| brake_start_time = 0.0 | |
| brake_release_time = 0.0 | |
| brake_ratio_at_press = brake_ratio | |
| brake_ratio_at_release = 0.0 | |
| set_both_brakes(brake_ratio) | |
| return | |
| end | |
| -- Normal press: | |
| -- start progressive braking from the current brake ratio. | |
| brake_hold_active = false | |
| brake_start_time = now | |
| brake_release_time = 0.0 | |
| brake_ratio_at_press = brake_ratio | |
| end | |
| function progressive_brakes_held() | |
| if not enabled then return end | |
| if brake_hold_active then return end | |
| if brake_start_time == 0.0 then return end | |
| local elapsed = total_running_time_sec - brake_start_time | |
| local ratio = brake_ratio_at_press + elapsed / SECONDS_TO_FULL_BRAKE | |
| set_both_brakes(ratio) | |
| end | |
| function progressive_brakes_released() | |
| if not enabled then return end | |
| local now = total_running_time_sec | |
| last_release_time = now | |
| -- If the second press was holding the brakes, | |
| -- releasing the button releases the brakes normally. | |
| if brake_hold_active then | |
| brake_hold_active = false | |
| end | |
| brake_start_time = 0.0 | |
| brake_release_time = now | |
| brake_ratio_at_release = brake_ratio | |
| end | |
| ------------------------------------------------------------ | |
| -- Per-frame update | |
| ------------------------------------------------------------ | |
| function progressive_brakes_update() | |
| if not enabled then return end | |
| if brake_hold_active then return end | |
| if brake_release_time == 0.0 then return end | |
| local elapsed = total_running_time_sec - brake_release_time | |
| local ratio = brake_ratio_at_release - elapsed / SECONDS_TO_RELEASE | |
| set_both_brakes(ratio) | |
| if ratio <= 0.0 then | |
| brake_release_time = 0.0 | |
| brake_ratio_at_release = 0.0 | |
| end | |
| end | |
| ------------------------------------------------------------ | |
| -- FlyWithLua command | |
| ------------------------------------------------------------ | |
| create_command( | |
| "neucoas/single_button_brake", | |
| "Single button brake", | |
| "progressive_brakes_start()", | |
| "progressive_brakes_held()", | |
| "progressive_brakes_released()" | |
| ) | |
| do_every_frame("progressive_brakes_update()") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment