Skip to content

Commit 302c76d

Browse files
committed
Fixes for usermod autodetection
1 parent fd62520 commit 302c76d

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

tools/cdata.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,40 @@ umCfg.initPage('${displayName}', { saveButton: '#save-btn' });
485485
return outPath;
486486
}
487487

488+
// Check if a USERMOD_ flag is defined in build config files
489+
function isUsermodEnabled(flagName) {
490+
// Check my_config.h - must be an uncommented #define
491+
const myConfigPaths = ['wled00/my_config.h', '../wled00/my_config.h'];
492+
for (const p of myConfigPaths) {
493+
try {
494+
if (fs.existsSync(p)) {
495+
const content = fs.readFileSync(p, 'utf8');
496+
const lines = content.split('\n');
497+
for (const line of lines) {
498+
const trimmed = line.trim();
499+
// Skip commented lines
500+
if (trimmed.startsWith('//') || trimmed.startsWith('/*') || trimmed.startsWith('*')) continue;
501+
if (line.includes(`#define ${flagName}`) || line.includes(`#undef ${flagName}`)) {
502+
return true;
503+
}
504+
}
505+
}
506+
} catch (e) {}
507+
}
508+
// Check platformio.ini for build_flags with -DUSERMOD_<NAME>
509+
try {
510+
if (fs.existsSync('platformio.ini')) {
511+
const content = fs.readFileSync('platformio.ini', 'utf8');
512+
// Look for -D USERMOD_EXAMPLE (with space) in build_flags
513+
const flagRe = new RegExp(`-D\\s*${flagName}\\b`);
514+
if (flagRe.test(content)) {
515+
return true;
516+
}
517+
}
518+
} catch (e) {}
519+
return false;
520+
}
521+
488522
// Generate usermod settings registry header
489523
function generateUsermodSettingsRegistry() {
490524
const pages = [];
@@ -509,13 +543,19 @@ function generateUsermodSettingsRegistry() {
509543
if (configResult.fields.length > 0) break;
510544
}
511545

512-
// If we have config fields but no settings page, generate skeleton
513-
if (configResult.fields.length > 0 && existingFiles.length === 0) {
546+
// Derive USERMOD_ flag name from directory: usermod_v2_foo -> USERMOD_FOO
547+
const flagName = 'USERMOD_' + dir.replace(/^usermod_v2_/i, '').toUpperCase();
548+
const isEnabled = isUsermodEnabled(flagName);
549+
550+
// If we have config fields but no settings page, generate skeleton (only if enabled)
551+
if (isEnabled && configResult.fields.length > 0 && existingFiles.length === 0) {
514552
const baseName = dir.replace(/^usermod_v2_/, '').toLowerCase();
515553
generateSkeletonSettingsPage(usermodDir, baseName, configResult.fields, configResult.defaults);
516554
}
517555

518-
// Process existing settings pages
556+
// Process existing settings pages (only if usermod is enabled in build)
557+
if (!isEnabled) continue;
558+
519559
for (const file of existingFiles) {
520560
const baseName = file.replace(/^settings_/, '').replace(/\.htm$/, '');
521561
const pageName = 'PAGE_settings_' + baseName;

wled00/usermods_list.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@
214214
#include "../usermods/usermod_v2_artnetmap/usermod_v2_artnetmap.h"
215215
#endif
216216

217+
#ifdef USERMOD_EXAMPLE
218+
#include "../usermods/usermod_v2_example/usermod_v2_example.h"
219+
#endif
220+
217221
void registerUsermods()
218222
{
219223
/*
@@ -424,5 +428,8 @@ void registerUsermods()
424428
usermods.add(new AutoPlaylistUsermod(false));
425429
#endif
426430

431+
#ifdef USERMOD_EXAMPLE
432+
usermods.add(new ExampleUsermod(false));
433+
#endif
427434

428435
}

0 commit comments

Comments
 (0)