# INAV JavaScript Programming Documentation Complete documentation for the INAV JavaScript transpiler that allows programming flight controller logic conditions in JavaScript. ## ๐Ÿ“š Table of Contents - [Quick Start](#quick-start) - [User Guides](#user-guides) - [Developer Documentation](#developer-documentation) - [Features](#features) --- ## Quick Start The INAV JavaScript transpiler converts JavaScript code into INAV logic conditions, enabling you to program flight controller behavior using familiar JavaScript syntax instead of raw logic condition commands. ### Features at a Glance **โœจ Modern Development Experience** ![IntelliSense Autocomplete](intellisense.png) *Real-time autocomplete with type information and documentation* ![Helpful Warnings](warnings.png) *Clear error messages with line numbers and suggestions* ### Basic Example ```javascript // Increase VTX power when far from home if (inav.flight.homeDistance > 500) { inav.override.vtx.power = 4; } ``` ![VTX Power Control Example](example_vtx_power.png) *Complete example showing VTX power control based on distance* ### RC Channel Override Example ![RC Override Example](example_override_rc.png) *Using RC channels to control behavior* --- ## User Guides ### ๐Ÿ“– [JavaScript Programming Guide](JAVASCRIPT_PROGRAMMING_GUIDE.md) **Start here if you're new to INAV JavaScript programming** Quick reference covering: - Pattern guide: `if`, `edge()`, `sticky()`, `delay()`, `timer()`, `whenChanged()` - Common patterns (initialization, event counting, debouncing) - Key differences between patterns - Available objects and APIs - Debugging techniques ### ๐Ÿ”ง [Operations Reference](OPERATIONS_REFERENCE.md) **Complete reference for all supported operations** Comprehensive guide to all INAV logic condition operations: - โœ… Arithmetic: `+`, `-`, `*`, `/`, `%` - โœ… Comparisons: `===`, `>`, `<`, `approxEqual()` - โœ… Logical: `&&`, `||`, `!`, `xor()`, `nand()`, `nor()` - โœ… Math: `Math.min()`, `Math.max()`, `Math.sin()`, `Math.cos()`, `Math.tan()`, `Math.abs()` - โœ… Scaling: `mapInput()`, `mapOutput()` - โœ… Flow control: `edge()`, `sticky()`, `delay()`, `timer()`, `whenChanged()` - โœ… Variables: `gvar[0-7]`, `let`, `var` - โœ… Overrides: `override.vtx.*`, `override.throttle`, `override.armSafety`, etc. - โœ… RC channel states: `rc[n].low`, `rc[n].mid`, `rc[n].high`, `rc[n].value` Includes usage examples and notes for each operation. ### โฑ๏ธ [Timer and WhenChanged Examples](TIMER_WHENCHANGED_EXAMPLES.md) **Practical examples for time-based and change-detection patterns** Examples covering: - Timer patterns (blinking, cycling, periodic actions) - Change detection (RSSI monitoring, altitude tracking) - Combined patterns with multiple conditions - Real-world use cases --- ## Developer Documentation ### ๐Ÿ—๏ธ [Technical Implementation Overview](implementation_summary.md) **Architecture and design of the transpiler system** Covers: - System architecture and component interaction - Transpiler pipeline (Parser โ†’ Analyzer โ†’ Optimizer โ†’ Codegen) - Decompiler pattern recognition - Key features and capabilities - Integration with Monaco Editor ### ๐Ÿงช [Testing Guide](TESTING_GUIDE.md) **How to test changes to the transpiler** Essential reading before making changes: - Which components need updates (parser, analyzer, codegen, decompiler) - Round-trip testing template (JavaScript โ†’ CLI โ†’ JavaScript) - Common issues and solutions - Verification checklist - Step-by-step example of adding a new operation **Always use round-trip testing when modifying the transpiler!** ### ๐Ÿ”Œ [API Definitions Summary](api_definitions_summary.md) **Structure of the INAV API definitions** Documents the API definition system: - Available API objects (`flight`, `override`, `rc`, `gvar`, `waypoint`, etc.) - Property types and metadata - INAV operand mappings - How definitions drive IntelliSense ### ๐Ÿ› ๏ธ [API Maintenance Guide](api_maintenance_guide.md) **How to add, modify, or maintain API definitions** Step-by-step instructions for: - Adding new properties to existing APIs - Creating new API objects - Updating operand mappings - Testing API changes - Common pitfalls ### โฒ๏ธ [Timer and WhenChanged Implementation](TIMER_WHENCHANGED_IMPLEMENTATION.md) **Technical details of TIMER and DELTA operations** Implementation guide for: - How TIMER operation works (operations 49) - How DELTA operation works (operation 50) - AST representation - Code generation strategy - Decompiler pattern recognition ### โš™๏ธ [Generate Constants README](GENERATE_CONSTANTS_README.md) **How to regenerate INAV constants from firmware** Instructions for: - Extracting operation codes from firmware - Regenerating `inav_constants.js` - Keeping constants in sync with firmware - What to do when firmware adds new operations --- ## Features ### Bidirectional Translation - **Transpiler**: JavaScript โ†’ INAV Logic Conditions - **Decompiler**: INAV Logic Conditions โ†’ JavaScript - **Round-trip capable**: Code survives transpile/decompile cycles ### Development Experience - **Monaco Editor Integration**: Full-featured code editor - **IntelliSense**: Context-aware autocomplete with documentation - **Real-time Validation**: Immediate feedback on syntax errors - **Syntax Highlighting**: JavaScript syntax with INAV-specific extensions - **Error Messages**: Clear, actionable error messages with line numbers ### Language Support **All INAV Operations Supported:** - Arithmetic operations - Comparison and logical operations - Math functions (trigonometry, min/max) - Flow control (edge detection, sticky conditions, delays, timers) - Variable management (global variables, let/var) - RC channel access and state detection - Flight parameter overrides - Waypoint navigation **JavaScript Features:** - Namespaced API access: `inav.flight.*`, `inav.override.*`, `inav.events.*` - `let`/`const` variables: compile-time constant substitution - `var` variables: allocated to global variables - Ternary operator: `condition ? value1 : value2` - Arrow functions: `() => condition` - Object property access: `inav.flight.altitude`, `inav.rc[0].value` - Binary expressions: `+`, `-`, `*`, `/`, `%` - Comparison operators: `>`, `<`, `===` - Logical operators: `&&`, `||`, `!` - Math methods: `Math.min()`, `Math.max()`, `Math.sin()`, etc. - Flight mode detection: `inav.flight.mode.poshold`, `inav.flight.mode.rth`, etc. - PID controller outputs: `inav.pid[0-3].output` ### Validation - Property access validation against API definitions - Range checking (gvar indices, heading values, etc.) - Type checking for function arguments - Dead code detection - Uninitialized variable detection - Helpful suggestions for common mistakes ### Optimization - Common Subexpression Elimination (CSE) - Efficient operand usage - Optimized GVAR_INC/GVAR_DEC generation - Minimal logic condition count --- ## File Organization ``` js/transpiler/ โ”œโ”€โ”€ docs/ # Documentation (this directory) โ”‚ โ”œโ”€โ”€ index.md # This file - documentation index โ”‚ โ”œโ”€โ”€ JAVASCRIPT_PROGRAMMING_GUIDE.md # User guide โ”‚ โ”œโ”€โ”€ OPERATIONS_REFERENCE.md # All operations โ”‚ โ”œโ”€โ”€ TIMER_WHENCHANGED_EXAMPLES.md # Timer/change examples โ”‚ โ”œโ”€โ”€ implementation_summary.md # Technical overview โ”‚ โ”œโ”€โ”€ TESTING_GUIDE.md # Testing workflow โ”‚ โ”œโ”€โ”€ api_definitions_summary.md # API structure โ”‚ โ”œโ”€โ”€ api_maintenance_guide.md # API maintenance โ”‚ โ”œโ”€โ”€ GENERATE_CONSTANTS_README.md # Constants generation โ”‚ โ”œโ”€โ”€ TIMER_WHENCHANGED_IMPLEMENTATION.md # Implementation details โ”‚ โ”œโ”€โ”€ intellisense.png # Screenshot: autocomplete โ”‚ โ”œโ”€โ”€ warnings.png # Screenshot: errors โ”‚ โ”œโ”€โ”€ example_vtx_power.png # Screenshot: VTX example โ”‚ โ””โ”€โ”€ example_override_rc.png # Screenshot: RC example โ”œโ”€โ”€ api/ # API definitions โ”‚ โ””โ”€โ”€ definitions/ # INAV API object definitions โ”œโ”€โ”€ editor/ # Monaco editor integration โ”‚ โ”œโ”€โ”€ monaco_setup.js # Editor configuration โ”‚ โ”œโ”€โ”€ intellisense.js # Autocomplete provider โ”‚ โ””โ”€โ”€ diagnostics.js # Real-time validation โ”œโ”€โ”€ transpiler/ # Core transpiler โ”‚ โ”œโ”€โ”€ index.js # Main transpiler entry point โ”‚ โ”œโ”€โ”€ parser.js # JavaScript parser (Acorn wrapper) โ”‚ โ”œโ”€โ”€ analyzer.js # Semantic analysis โ”‚ โ”œโ”€โ”€ optimizer.js # Code optimization (CSE) โ”‚ โ”œโ”€โ”€ codegen.js # INAV command generation โ”‚ โ”œโ”€โ”€ decompiler.js # INAV โ†’ JavaScript โ”‚ โ”œโ”€โ”€ inav_constants.js # Operation codes and types โ”‚ โ”œโ”€โ”€ variable_handler.js # let/var variable management โ”‚ โ”œโ”€โ”€ arrow_function_helper.js # Arrow function utilities โ”‚ โ””โ”€โ”€ error_handler.js # Error collection โ””โ”€โ”€ tools/ # Build tools โ””โ”€โ”€ generate-constants.js # Extract constants from firmware ``` --- ## Getting Help ### Common Questions **Q: Which pattern should I use?** - Use `if` for continuous control (checking every cycle) - Use `edge()` for one-time actions when condition becomes true - Use `sticky()` for latching conditions with hysteresis - Use `delay()` for actions that need sustained conditions - Use `timer()` for periodic toggling - Use `whenChanged()` for detecting value changes See the [JavaScript Programming Guide](JAVASCRIPT_PROGRAMMING_GUIDE.md) for detailed examples. **Q: How do I debug my code?** Use global variables to track state and view them in the OSD or configurator. See the debugging section in the [JavaScript Programming Guide](JAVASCRIPT_PROGRAMMING_GUIDE.md). **Q: What operations are supported?** All INAV logic condition operations! See [Operations Reference](OPERATIONS_REFERENCE.md) for the complete list. **Q: How do I contribute?** Read the [Testing Guide](TESTING_GUIDE.md) to understand the testing workflow, then make your changes ensuring all 4 components (parser, analyzer, codegen, decompiler) are updated and round-trip tested. --- ## Version History **2025-11-25**: Complete implementation - All INAV logic condition operations now supported - RC channel state detection (LOW/MID/HIGH) - XOR/NAND/NOR logical operations - APPROX_EQUAL comparison - MAP_INPUT/MAP_OUTPUT scaling - Comprehensive documentation **Last Updated**: 2025-11-25