Skip to content

Commit

Permalink
generator: add javascript "lite"
Browse files Browse the repository at this point in the history
  • Loading branch information
IamPete1 committed Aug 8, 2024
1 parent fda71ea commit f33fcc8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion generator/mavgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

# List the supported languages. This is done globally because it's used by the GUI wrapper too
# Right now, 'JavaScript' ~= 'JavaScript_Stable', in the future it may be made equivalent to 'JavaScript_NextGen'
supportedLanguages = ["Ada", "C", "CS", "JavaScript", "JavaScript_Stable","JavaScript_NextGen", "TypeScript", "Python2", "Python3", "Python", "Lua", "WLua", "ObjC", "Swift", "Java", "C++11"]
supportedLanguages = ["Ada", "C", "CS", "JavaScript", "JavaScript_Stable","JavaScript_NextGen", "JavaScript_lite", "TypeScript", "Python2", "Python3", "Python", "Lua", "WLua", "ObjC", "Swift", "Java", "C++11"]


def mavgen(opts, args):
Expand Down Expand Up @@ -303,6 +303,9 @@ def mavgen_validate(xmlfile):
else:
from . import mavgen_ada
mavgen_ada.generate(opts.output, xml)
elif opts.language == 'javascript_lite':
from . import mavgen_javascript_lite
mavgen_javascript_lite.generate(opts.output, xml)
else:
print("Unsupported language %s" % opts.language)

Expand Down
44 changes: 44 additions & 0 deletions generator/mavgen_javascript_lite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
'''
parse a MAVLink protocol XML file and generate a javascript file to get CRC extra and name from message ID and MAV_COMPONENT enum. No payload fields are considered.
'''
from __future__ import print_function

from builtins import range

import os

def generate(basename, xml):
msgs = []
enums = []
filelist = []
for x in xml:
msgs.extend(x.message)
enums.extend(x.enum)
filelist.append(os.path.basename(x.filename))

print("Generating %s/mavlink_msgs.js" % basename)
# create the output directory if needed
if not os.path.exists(basename):
os.makedirs(basename)
outf = open("%s/mavlink_msgs.js" % basename, "w")

outf.write("// Auto generated MAVLink parsing script\n\nmavlink_msgs = []\n")

for msg in msgs:
outf.write("mavlink_msgs[%i] = { name: \"%s\", CRC: %i }\n" % (msg.id, msg.name, msg.crc_extra))
outf.write("\n")

for enum in enums:
if enum.name != "MAV_COMPONENT":
# Only need component ID
continue
outf.write("%s = []\n" % enum.name)
for entry in enum.entry:
outf.write("%s[%i] = \"%s\"\n" % (enum.name, entry.value, entry.name))
outf.write("\n")


outf.close()
print("Generated %s OK" % basename)

0 comments on commit f33fcc8

Please sign in to comment.