Files
espurna/code/scripts/espurna_utils/libalgobsec.py
Rui Marinho 6266930a4d sns: Add BME680 (#2295)
Add support for BME680 using libalgobsec proprietary algorithms for precise Indoor Air Quality (IAQ) measurement. Unlike traditional CO2 sensors - and good ones are expensive - it measures nearly all VOCs compounds in the air (plus other gases) and compensates those measurements with its built-in temperature and humidity sensors to determine indoor air quality.

Co-authored-by: Max Prokhorov <prokhorov.max@outlook.com>
2020-08-02 09:12:38 +03:00

70 lines
2.1 KiB
Python

import os
import sys
def libalgobsec_inject_patcher(env):
libalgobsec_builder = next(
(
builder
for builder in env.GetLibBuilders()
if builder.name == "BSEC Software Library"
),
None,
)
if libalgobsec_builder is None:
return
def process_archive(target, source, env):
import subprocess
# Allows `import espurna_utils` for external scripts, where we might not be within scons runtime
from SCons.Script import Delete, Mkdir
tmpdir = env.get(
"ESPURNA_LIBALGOBSEC_PATCHER_TMPDIR",
os.path.join(str(target[0].get_dir()), "_tmpdir"),
)
env.Execute(Mkdir(tmpdir))
# XXX: $AR does not support output argument for the extraction
# always switch into tmpdir when running commands
def run(cmd):
sys.stdout.write(" ".join(cmd))
sys.stdout.write("\n")
subprocess.check_call(cmd, cwd=tmpdir)
run([env.subst("$AR"), "x", source[0].abspath])
names = []
for infilename in os.listdir(tmpdir):
newname = infilename
if not infilename.endswith(".c.o"):
newname = infilename.replace(".o", ".c.o")
os.rename(os.path.join(tmpdir, infilename), os.path.join(tmpdir, newname))
names.append(newname)
pack_cmd = [env.subst("$AR"), "cr", target[0].abspath]
pack_cmd.extend(names)
run(pack_cmd)
env.Execute(Delete(tmpdir))
# Instead of replacing the file in-place, link with the patched version
libalgobsec_dir = os.path.join(libalgobsec_builder.src_dir, "esp8266")
target = env.File(
"libalgobsec.a", directory=env.subst("$BUILD_DIR/libalgobsec_patched")
)
source = env.File("libalgobsec.a", directory=libalgobsec_dir)
command = env.Command(target, source, process_archive)
patcher = env.Alias("patch-libalgobsec", command)
env.Append(LIBPATH=[target.get_dir()])
env.Append(LIBS=["algobsec"])
env.Depends("$BUILD_DIR/${PROGNAME}.elf", patcher)