PaulStoffregen / Tlc5940

16 channel PWM LED driver based on the Texas Instruments TLC5940 chip.
https://github.com/PaulStoffregen/Tlc5940
81 stars 40 forks source link

Allow NUM_TLCS to be specified with compile flags #5

Closed aphelps closed 3 years ago

aphelps commented 8 years ago

This change allows the number of TLCs that are controlled to be specified by compiler flag, so it can be set at compile time when using platformio and similar build systems.

oujesky commented 3 years ago

For anybody running into this for platformio, until this is merged, it can be worked around using custom build script:

  1. Add extra_scripts = post:build/patch.py to platformio.ini file
  2. Create build/patch.py file with the following contents:
from os.path import join, isfile

# actual number of TLC5940 chips to patch into the tlc_config.h file
NUM_TLCS = 2

Import("projenv")

tlc_lib = next(filter(lambda x: x.name == "Tlc5940", projenv.GetLibBuilders()));
tlc_config_file = join(tlc_lib.src_dir, "tlc_config.h")

assert isfile(tlc_config_file)

with open(tlc_config_file, "r") as file:
    tlc_config = file.read()

if tlc_config.find("#define NUM_TLCS    1") != -1:
    print("Patching tlc_config.h")

    tlc_config = tlc_config.replace("#define NUM_TLCS    1", "#define NUM_TLCS    " + NUM_TLCS)

    with open(tlc_config_file, "w") as file:
        file.write(tlc_config)

This script will be executed by platformio when the code is being built and if the tlc_config.h is not yet patched, then it will update the number of chips to the NUM_TLCS constant.