2017-03-02 20:15:02 -04:00
#!/usr/bin/env python
"""
Emit parameter documentation in markdown format
"""
from param import known_param_fields
from emit import Emit
2022-10-24 05:19:37 -03:00
import time
2017-03-02 20:15:02 -04:00
import re
import os
2022-10-24 05:19:37 -03:00
# ArduSub documentation uses the page title as the top level heading.
# OFFSET drops headings one level when compiling for those docs.
OFFSET = ' # ' if os . getenv ( ' BRDOC ' ) else ' '
2017-03-02 20:15:02 -04:00
# Parameter groups disabled at compile time (Vehicle-specific)
2022-12-16 07:05:20 -04:00
sub_blacklist = [ ' RCMAP_ ' , ' TERRAIN_ ' ]
2017-03-02 20:15:02 -04:00
# Parameter groups with redundant information (ie RCn_, SERVOn_)
# We can keep the documentation concise by only documenting these once
2022-10-24 05:20:26 -03:00
nparams = [ ' RCn_ ' , ' SERVOn_ ' , ' SRn_ ' , ' BTNn_ ' , ' INS_TCALn_ ' , ' BATTn_ ' , ' BAROn_GND_ ' , ' BAROn_WCF_ ' , ' CAN_Dn_ ' , ' CAN_Dn_KDE_ ' , ' CAN_Dn_PC_ ' , ' CAN_Dn_UC_ ' , ' CAN_Dn_TST_ ' , ' CAN_Pn_ ' , ' RNGFNDn_ ' ]
2017-03-02 20:15:02 -04:00
class MDEmit ( Emit ) :
2021-04-07 00:12:41 -03:00
def __init__ ( self , * args , * * kwargs ) :
Emit . __init__ ( self , * args , * * kwargs )
2017-03-02 20:15:02 -04:00
fname = ' Parameters.md '
self . nparams = [ ]
self . f = open ( fname , mode = ' w ' )
self . blacklist = None
# Flag to generate navigation header for BlueRobotics' ArduSub docs
if os . getenv ( ' BRDOC ' ) is not None :
2022-10-24 05:19:37 -03:00
now = time . strftime ( ' % Y- % m- %d T % H: % M: % S % z ' )
now = now [ : - 2 ] + ' : ' + now [ - 2 : ]
self . header = """ +++
title = " Parameters "
description = " Firmware parameter details. "
date = % s
template = " docs/page.html "
sort_by = " weight "
weight = 10
draft = false
[ extra ]
toc = true
top = false """ % now
2017-03-02 20:15:02 -04:00
2022-12-16 07:05:20 -04:00
self . preamble = """ \n This is a complete list of the parameters which can be set via the MAVLink protocol in the EEPROM of your autopilot to control vehicle behaviour. Some parameters may only be available for developers, and are enabled at compile-time. """
2017-03-02 20:15:02 -04:00
self . t = ' '
def close ( self ) :
# Write navigation header for BlueRobotics' ArduSub docs
if os . getenv ( ' BRDOC ' ) is not None :
self . f . write ( self . header )
2022-10-24 05:19:37 -03:00
self . f . write ( ' \n +++ \n ' )
2017-03-02 20:15:02 -04:00
self . f . write ( self . preamble )
self . f . write ( self . t )
self . f . close ( )
def start_libraries ( self ) :
pass
2018-12-06 05:07:51 -04:00
def emit ( self , g ) :
2017-03-02 20:15:02 -04:00
nparam = False # Flag indicating this is a parameter group with redundant information (ie RCn_, SERVOn_)
2021-05-12 01:45:27 -03:00
if g . reference == ' ArduSub ' :
2017-03-02 20:15:02 -04:00
self . blacklist = sub_blacklist
2021-05-12 01:45:27 -03:00
if self . blacklist is not None and g . reference in self . blacklist :
2017-03-02 20:15:02 -04:00
return
2021-05-12 01:45:27 -03:00
pname = g . reference
2017-03-02 20:15:02 -04:00
# Check to see this is a parameter group with redundant information
2021-05-12 01:45:27 -03:00
rename = re . sub ( ' \ d+ ' , ' n ' , g . reference )
2017-03-02 20:15:02 -04:00
if rename in nparams :
if rename in self . nparams :
return
else :
self . nparams . append ( rename )
pname = rename
nparam = True
# Markdown!
tag = ' %s Parameters ' % pname
tag = tag . replace ( ' _ ' , ' ' )
link = tag . replace ( ' ' , ' - ' )
2022-10-24 05:19:37 -03:00
t = ' \n \n %s # %s ' % ( OFFSET , tag )
2017-03-02 20:15:02 -04:00
for param in g . params :
if not hasattr ( param , ' DisplayName ' ) or not hasattr ( param , ' Description ' ) :
continue
d = param . __dict__
name = param . name . split ( ' : ' ) [ - 1 ]
if nparam :
name = re . sub ( ' \ d+ ' , ' n ' , name , 1 )
tag = ' %s : %s ' % ( name , param . DisplayName )
2022-10-24 05:19:37 -03:00
t + = ' \n \n %s ## %s ' % ( OFFSET , tag )
if os . getenv ( ' DIFF ' ) :
continue
2017-03-02 20:15:02 -04:00
if d . get ( ' User ' , None ) == ' Advanced ' :
t + = ' \n \n *Note: This parameter is for advanced users* '
t + = " \n \n %s " % param . Description
for field in param . __dict__ . keys ( ) :
if field not in [ ' name ' , ' DisplayName ' , ' Description ' , ' User ' ] and field in known_param_fields :
if field == ' Values ' and Emit . prog_values_field . match ( param . __dict__ [ field ] ) :
values = ( param . __dict__ [ field ] ) . split ( ' , ' )
t + = " \n \n |Value|Meaning| "
t + = " \n |:---:|:---:| "
for value in values :
v = value . split ( ' : ' )
2018-03-25 22:35:13 -03:00
if len ( v ) != 2 :
raise ValueError ( " Bad value ( %s ) " % v )
2017-03-02 20:15:02 -04:00
t + = " \n | %s | %s | " % ( v [ 0 ] , v [ 1 ] )
else :
t + = " \n \n - %s : %s " % ( field , param . __dict__ [ field ] )
self . t + = t