Skip to content

Commit 2be60b3

Browse files
Merge pull request iNavFlight#11143 from sensei-hacker/docs_javascript_programming
Add JavaScript programming documentation
2 parents 7d5b39f + aa662ec commit 2be60b3

15 files changed

Lines changed: 2438 additions & 0 deletions

docs/Programming Framework.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ IPF can be edited using INAV Configurator user interface, or via CLI. To use COn
2626

2727
**Note:** IPF uses integer math. If your programming line returns a decimal, it will be truncated to an integer. So if your math is `1` / `3` = , IPF will truncate the decimal and return `0`.
2828

29+
## JavaScript-Based Programming (Alternative)
30+
31+
INAV also supports a JavaScript-based programming interface that provides a more
32+
familiar syntax for those comfortable with JavaScript. The JavaScript code is transpiled
33+
(converted) into traditional logic conditions, so both methods ultimately use the same
34+
underlying system.
35+
36+
See the [JavaScript Programming Guide](javascript_programming/JAVASCRIPT_PROGRAMMING_GUIDE.md)
37+
for complete documentation on using JavaScript to program your flight controller.
38+
39+
**Benefits of JavaScript programming:**
40+
- Modern code editor with IntelliSense autocomplete
41+
- Real-time syntax validation and error messages
42+
- Familiar programming constructs (if statements, functions, variables)
43+
- Automatic conversion to logic conditions
44+
2945
## Logic Conditions
3046

3147
### CLI
@@ -356,3 +372,9 @@ choose *value* and enter the channel number. Choosing "get RC value" is a common
356372
which does something other than what you probably want.
357373

358374
![screenshot of override an RC channel with a value](./assets/images/ipf_set_get_rc_channel.png)
375+
376+
## Related Documentation
377+
378+
- [JavaScript Programming Guide](javascript_programming/JAVASCRIPT_PROGRAMMING_GUIDE.md) - Alternative JavaScript-based syntax for programming logic conditions
379+
- [Operations Reference](javascript_programming/OPERATIONS_REFERENCE.md) - Complete reference for all supported operations in JavaScript
380+
- [Timer and Change Detection Examples](javascript_programming/TIMER_WHENCHANGED_EXAMPLES.md) - Practical examples for time-based patterns
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# INAV Constants Generator
2+
3+
## Purpose
4+
5+
Generates `inav_constants.js` from the INAV firmware header file (`logic_condition.h`) to ensure the transpiler/decompiler constants exactly match the actual firmware.
6+
7+
## Single Source of Truth Architecture
8+
9+
**Firmware Header** (`logic_condition.h`)
10+
↓ (parse)
11+
**Constants File** (`inav_constants.js`) - AUTO-GENERATED
12+
↓ (import)
13+
**API Definitions** (`flight.js`, etc.) - Reference constants
14+
↓ (use)
15+
**Transpiler & Decompiler** - Use constants
16+
17+
## Usage
18+
19+
### Generate Constants
20+
21+
```bash
22+
node scripts/generate-constants.js <path-to-logic_condition.h> [output-path]
23+
```
24+
25+
### Example
26+
27+
```bash
28+
# From INAV configurator root
29+
node scripts/generate-constants.js \
30+
../inav/src/main/programming/logic_condition.h \
31+
js/transpiler/transpiler/inav_constants.js
32+
```
33+
34+
### Default Output
35+
36+
If output path is not specified, defaults to:
37+
```
38+
js/transpiler/transpiler/inav_constants.js
39+
```
40+
41+
## What It Parses
42+
43+
The parser extracts these enums from `logic_condition.h`:
44+
45+
1. **logicOperation_e**`OPERATION`
46+
- TRUE, EQUAL, GREATER_THAN, etc.
47+
48+
2. **logicOperandType_s**`OPERAND_TYPE`
49+
- VALUE, RC_CHANNEL, FLIGHT, etc.
50+
51+
3. **logicFlightOperands_e**`FLIGHT_PARAM`
52+
- ARM_TIMER, HOME_DISTANCE, RSSI, YAW, etc.
53+
54+
4. **logicFlightModeOperands_e**`FLIGHT_MODE`
55+
- FAILSAFE, MANUAL, RTH, etc.
56+
57+
5. **logicWaypointOperands_e**`WAYPOINT_PARAM`
58+
- IS_WP, WAYPOINT_INDEX, etc.
59+
60+
## Generated File Format
61+
62+
```javascript
63+
/**
64+
* AUTO-GENERATED from firmware header files
65+
* DO NOT EDIT MANUALLY
66+
*/
67+
68+
const OPERAND_TYPE = {
69+
VALUE: 0,
70+
RC_CHANNEL: 1,
71+
FLIGHT: 2,
72+
// ...
73+
};
74+
75+
const OPERATION = {
76+
TRUE: 0,
77+
EQUAL: 1,
78+
// ...
79+
};
80+
81+
const FLIGHT_PARAM = {
82+
ARM_TIMER: 0,
83+
HOME_DISTANCE: 1,
84+
// ...
85+
YAW: 40, // ← Correct value from firmware!
86+
// ...
87+
};
88+
89+
// ... exports
90+
```
91+
92+
## Build Integration
93+
94+
Add to package.json scripts:
95+
96+
```json
97+
{
98+
"scripts": {
99+
"generate-constants": "node scripts/generate-constants.js ../inav/src/main/programming/logic_condition.h",
100+
"prebuild": "npm run generate-constants"
101+
}
102+
}
103+
```
104+
105+
This ensures constants are regenerated before each build.
106+
107+
## Next Steps: Update API Definitions
108+
109+
Currently, API definitions have hardcoded values:
110+
111+
```javascript
112+
// flight.js - WRONG (hardcoded)
113+
yaw: {
114+
inavOperand: { type: 2, value: 17 } // Wrong value!
115+
}
116+
```
117+
118+
Change to reference constants:
119+
120+
```javascript
121+
// flight.js - CORRECT (references constants)
122+
const { OPERAND_TYPE, FLIGHT_PARAM } = require('../../transpiler/inav_constants.js');
123+
124+
yaw: {
125+
inavOperand: { type: OPERAND_TYPE.FLIGHT, value: FLIGHT_PARAM.ATTITUDE_YAW }
126+
}
127+
```
128+
129+
Benefits:
130+
- ✅ Single source of truth (firmware)
131+
- ✅ Type-safe references
132+
- ✅ Compile-time errors if constants missing
133+
- ✅ Auto-update when firmware changes
134+
135+
## Verification
136+
137+
After generating, verify key values:
138+
139+
```bash
140+
grep "ATTITUDE_YAW" js/transpiler/transpiler/inav_constants.js
141+
# Should show: ATTITUDE_YAW: 40,
142+
143+
grep "IS_ARMED" js/transpiler/transpiler/inav_constants.js
144+
# Should show: IS_ARMED: 17,
145+
```
146+
147+
## Error Handling
148+
149+
The parser handles:
150+
- ✅ Both `typedef enum { } name_e;` and `typedef enum name { }` formats
151+
- ✅ Explicit values (`= 40`)
152+
- ✅ Auto-incrementing values
153+
- ✅ Hex values (`= 0x10`)
154+
- ✅ C-style comments (`//` and `/* */`)
155+
- ✅ Missing enums (warns but continues)
156+
157+
## Maintenance
158+
159+
**When INAV firmware updates:**
160+
1. Get new `logic_condition.h` from firmware repo
161+
2. Run `npm run generate-constants`
162+
3. Review diff in `inav_constants.js`
163+
4. Test transpiler/decompiler
164+
5. Commit updated constants file
165+
166+
**DO NOT manually edit `inav_constants.js`** - all changes will be overwritten!

0 commit comments

Comments
 (0)