|
| 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