3.6 KiB
INAV Constants Generator
Purpose
Generates inav_constants.js from the INAV firmware header file (logic_condition.h) to ensure the transpiler/decompiler constants exactly match the actual firmware.
Single Source of Truth Architecture
Firmware Header (logic_condition.h)
↓ (parse)
Constants File (inav_constants.js) - AUTO-GENERATED
↓ (import)
API Definitions (flight.js, etc.) - Reference constants
↓ (use)
Transpiler & Decompiler - Use constants
Usage
Generate Constants
node scripts/generate-constants.js <path-to-logic_condition.h> [output-path]
Example
# From INAV configurator root
node scripts/generate-constants.js \
../inav/src/main/programming/logic_condition.h \
js/transpiler/transpiler/inav_constants.js
Default Output
If output path is not specified, defaults to:
js/transpiler/transpiler/inav_constants.js
What It Parses
The parser extracts these enums from logic_condition.h:
-
logicOperation_e →
OPERATION- TRUE, EQUAL, GREATER_THAN, etc.
-
logicOperandType_s →
OPERAND_TYPE- VALUE, RC_CHANNEL, FLIGHT, etc.
-
logicFlightOperands_e →
FLIGHT_PARAM- ARM_TIMER, HOME_DISTANCE, RSSI, YAW, etc.
-
logicFlightModeOperands_e →
FLIGHT_MODE- FAILSAFE, MANUAL, RTH, etc.
-
logicWaypointOperands_e →
WAYPOINT_PARAM- IS_WP, WAYPOINT_INDEX, etc.
Generated File Format
/**
* AUTO-GENERATED from firmware header files
* DO NOT EDIT MANUALLY
*/
const OPERAND_TYPE = {
VALUE: 0,
RC_CHANNEL: 1,
FLIGHT: 2,
// ...
};
const OPERATION = {
TRUE: 0,
EQUAL: 1,
// ...
};
const FLIGHT_PARAM = {
ARM_TIMER: 0,
HOME_DISTANCE: 1,
// ...
YAW: 40, // ← Correct value from firmware!
// ...
};
// ... exports
Build Integration
Add to package.json scripts:
{
"scripts": {
"generate-constants": "node scripts/generate-constants.js ../inav/src/main/programming/logic_condition.h",
"prebuild": "npm run generate-constants"
}
}
This ensures constants are regenerated before each build.
Next Steps: Update API Definitions
Currently, API definitions have hardcoded values:
// inav.flight.js - WRONG (hardcoded)
yaw: {
inavOperand: { type: 2, value: 17 } // Wrong value!
}
Change to reference constants:
// inav.flight.js - CORRECT (references constants)
const { OPERAND_TYPE, FLIGHT_PARAM } = require('../../transpiler/inav_constants.js');
yaw: {
inavOperand: { type: OPERAND_TYPE.FLIGHT, value: FLIGHT_PARAM.ATTITUDE_YAW }
}
Benefits:
- ✅ Single source of truth (firmware)
- ✅ Type-safe references
- ✅ Compile-time errors if constants missing
- ✅ Auto-update when firmware changes
Verification
After generating, verify key values:
grep "ATTITUDE_YAW" js/transpiler/transpiler/inav_constants.js
# Should show: ATTITUDE_YAW: 40,
grep "IS_ARMED" js/transpiler/transpiler/inav_constants.js
# Should show: IS_ARMED: 17,
Error Handling
The parser handles:
- ✅ Both
typedef enum { } name_e;andtypedef enum name { }formats - ✅ Explicit values (
= 40) - ✅ Auto-incrementing values
- ✅ Hex values (
= 0x10) - ✅ C-style comments (
//and/* */) - ✅ Missing enums (warns but continues)
Maintenance
When INAV firmware updates:
- Get new
logic_condition.hfrom firmware repo - Run
npm run generate-constants - Review diff in
inav_constants.js - Test transpiler/decompiler
- Commit updated constants file
DO NOT manually edit inav_constants.js - all changes will be overwritten!