From ef0128717ff336dcf58c883763b4dff60dcac132 Mon Sep 17 00:00:00 2001 From: Taehee Jeong Date: Fri, 10 Jun 2016 17:24:04 +0900 Subject: [PATCH] Improved enum access capabilities Actual values of enums can be accessed by enumsByName of enumsByValue. Usage: ``` var value = enumsByName.ENUM_NAME.PROPERTY_NAME.value; var name = enumsByValue.ENUM_NAME[PROPERTY_VALUE].name; ``` Specifying .value or .name is to preserve descriptions along with the value or name in the same key --- Readme.md | 14 +++++++++++++- src/mavlink.js | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 528814b..e80ebfe 100755 --- a/Readme.md +++ b/Readme.md @@ -145,4 +145,16 @@ The events emitted by node-mavlink are: In addition to these events, each message received emits an event corresponding to the message type. For example on receipt of a GPS_RAW_INT message an event named "GPS_RAW_INT" is emitted. These events have both the message object passed as first argument along with an object containing the decoded fields. An example of this is given in the Parsing Data section above. -These message specific events allow you to tailor certain behaviours to certain message types, whilst ignoring other messages. For example, updating the aim of a tracking antenna whenever new position information is available. \ No newline at end of file +These message specific events allow you to tailor certain behaviours to certain message types, whilst ignoring other messages. For example, updating the aim of a tracking antenna whenever new position information is available. + +Enums +----- +You can acces enums by their names or values, using enumsByName or enumsByValue. + +Make sure to put ".value" for enumsByName and ".name" for enumsByValue at the end of each statements. +``` +var command = myMAV.enumsByName.MAV_CMD.MAV_CMD_NAV_TAKEOFF.value; + +var sensorType = 1 //MAV_DISTANCE_SENSOR_ULTRASOUND +var senorName = myMAV.enumsByValue.MAV_DISTANCE_SENSOR[sensorType].name; +``` diff --git a/src/mavlink.js b/src/mavlink.js index f5274b3..fc73be2 100755 --- a/src/mavlink.js +++ b/src/mavlink.js @@ -61,6 +61,8 @@ var mavlink = function(sysid, compid, version, definitions) { this.messagesByID = new Array(255); this.messagesByName = new Object(); this.enums = new Array(); + this.enumsByName = new Object(); + this.enumsByValue = new Object(); //Add definitions to be loaded for (var i = 0; i