iNavFlight / inav

INAV: Navigation-enabled flight control software
https://inavflight.github.io
GNU General Public License v3.0
3.18k stars 1.48k forks source link

Potentially unused #define in targets #3423

Closed flounderscore closed 5 years ago

flounderscore commented 6 years ago

I wrote a little python3 script (to be run out of the src/main/targets directory) that counts how many times each #define in the target.h is used overall, my thought being that #defines only used in one target might be mistakes, e.g. caused by copy&paste of betaflight files. I used it to find #3404.

Unfortunately, I know too little about the low-level magic of iNav to determine which define is made by mistake.

The output of the script (below) is a little lengthy, but I'll include a few lines:

BARO_EOC_PIN    1   PORT103R
USE_SDCARD_SPI3 1   YUPIF4
USE_QUAD_MIXER_ONLY 1   CRAZEPONYMINI
MPU6000_EXTI_PIN    1   REVO
SPEKTRUM_BIND   1   FF_FORTINIF4
INVERTER_PIN_UART   1   PIXRACER
MPU9250_I2C_BUS 1   SPRACINGF3MINI
PORT103R_SPI_CS_PIN 1   PORT103R
USE_RX_INAV 1   STM32F3DISCOVERY
CMS 1   FF_FORTINIF4
MAG_IST8310_ALIGN   1   FISHDRONEF4
LED1_A  1   ALIENFLIGHTF3
MPU6500_I2C_BUS 1   SPRACINGF3MINI
ICM20608_EXTI_PIN   1   PIXRACER
...
import os
import operator
import glob

if __name__ == '__main__':
    files = glob.glob('*/target.h')
    defines = {}
    for file in files:
        target = os.path.dirname(file)
        with open(file, 'r') as handle:
            for line in handle:
                line = line.strip()
                if line.startswith('#define'):
                    try:
                        line = line.replace('\t', ' ')
                        define = line.split(' ')[1].strip()
                        if define not in defines:
                            defines[define] = []
                        defines[define].append(target)
                    except IndexError:
                        pass
    counts = {k: len(v) for k, v in defines.items()}

    for define, count in sorted(counts.items(), key=operator.itemgetter(1)):
        print("{}\t{}\t{}".format(define, count, ', '.join(sorted(defines[define]))))
digitalentity commented 6 years ago

Good stuff. Maybe we can add this script to src/utils folder and create a make target to call it easily