Skip to content

Commit d6be26c

Browse files
committed
PWM_fan: Use direct source UM reference
Identify and select the source usermod at build time; no need for runtime lookup.
1 parent d1d9dec commit d6be26c

File tree

5 files changed

+13
-27
lines changed

5 files changed

+13
-27
lines changed

usermods/PWM_fan/PWM_fan.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ class PWMFanUsermod : public Usermod {
4444
#endif
4545
bool lockFan = false;
4646

47-
#ifdef USERMOD_DALLASTEMPERATURE
48-
UsermodTemperature* tempUM;
49-
#elif defined(USERMOD_SHT)
50-
ShtUsermod* tempUM;
51-
#endif
52-
5347
// configurable parameters
5448
int8_t tachoPin = TACHO_PIN;
5549
int8_t pwmPin = PWM_PIN;
@@ -158,11 +152,11 @@ class PWMFanUsermod : public Usermod {
158152
}
159153

160154
float getActualTemperature(void) {
161-
#if defined(USERMOD_DALLASTEMPERATURE) || defined(USERMOD_SHT)
162-
if (tempUM != nullptr)
163-
return tempUM->getTemperatureC();
155+
#if defined(USERMOD_DALLASTEMPERATURE)
156+
return temperature_mod.getTemperatureC();
157+
#elif defined(USERMOD_SHT)
158+
return sht_mod.getTemperatureC();
164159
#endif
165-
return -127.0f;
166160
}
167161

168162
void setFanPWMbasedOnTemperature(void) {
@@ -193,12 +187,6 @@ class PWMFanUsermod : public Usermod {
193187
// gets called once at boot. Do all initialization that doesn't depend on
194188
// network here
195189
void setup() override {
196-
#ifdef USERMOD_DALLASTEMPERATURE
197-
// This Usermod requires Temperature usermod
198-
tempUM = (UsermodTemperature*) UsermodManager::lookup(USERMOD_ID_TEMPERATURE);
199-
#elif defined(USERMOD_SHT)
200-
tempUM = (ShtUsermod*) UsermodManager::lookup(USERMOD_ID_SHT);
201-
#endif
202190
initTacho();
203191
initPWMfan();
204192
updateFanSpeed((minPWMValuePct * 255) / 100); // inital fan speed

usermods/Temperature/Temperature.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,6 @@ const char *UsermodTemperature::getTemperatureUnit() {
354354
return degC ? "°C" : "°F";
355355
}
356356

357-
UsermodTemperature* UsermodTemperature::_instance = nullptr;
358-
359357
// strings to reduce flash memory usage (used more than twice)
360358
const char UsermodTemperature::_name[] PROGMEM = "Temperature";
361359
const char UsermodTemperature::_enabled[] PROGMEM = "enabled";
@@ -372,12 +370,12 @@ const char UsermodTemperature::_data_fx[] PROGMEM = "Temperature@Min,Max;;!
372370
static uint16_t mode_temperature() {
373371
float low = roundf(mapf((float)SEGMENT.speed, 0.f, 255.f, -150.f, 150.f)); // default: 15°C, range: -15°C to 15°C
374372
float high = roundf(mapf((float)SEGMENT.intensity, 0.f, 255.f, 300.f, 600.f)); // default: 30°C, range 30°C to 60°C
375-
float temp = constrain(UsermodTemperature::getInstance()->getTemperatureC()*10.f, low, high); // get a little better resolution (*10)
373+
float temp = constrain(temperature_mod.getTemperatureC()*10.f, low, high); // get a little better resolution (*10)
376374
unsigned i = map(roundf(temp), (unsigned)low, (unsigned)high, 0, 248);
377375
SEGMENT.fill(SEGMENT.color_from_palette(i, false, false, 255));
378376
return FRAMETIME;
379377
}
380378

381379

382-
static UsermodTemperature temperature;
383-
REGISTER_USERMOD(temperature);
380+
UsermodTemperature temperature_mod;
381+
REGISTER_USERMOD(temperature_mod);

usermods/Temperature/UsermodTemperature.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,9 @@ class UsermodTemperature : public Usermod {
7272
void publishHomeAssistantAutodiscovery();
7373
#endif
7474

75-
static UsermodTemperature* _instance; // to overcome nonstatic getTemperatureC() method and avoid UsermodManager::lookup(USERMOD_ID_TEMPERATURE);
76-
7775
public:
7876

79-
UsermodTemperature() { _instance = this; }
80-
static UsermodTemperature *getInstance() { return UsermodTemperature::_instance; }
77+
UsermodTemperature() { }
8178

8279
/*
8380
* API calls te enable data exchange between WLED modules
@@ -108,3 +105,4 @@ class UsermodTemperature : public Usermod {
108105
void appendConfigData() override;
109106
};
110107

108+
extern UsermodTemperature temperature_mod;

usermods/sht/ShtUsermod.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ class ShtUsermod : public Usermod
6969

7070
uint16_t getId() { return USERMOD_ID_SHT; }
7171
};
72+
73+
extern ShtUsermod sht_mod;

usermods/sht/sht.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,5 +411,5 @@ const char* ShtUsermod::getUnitString() {
411411
return unitOfTemp ? "°F" : "°C";
412412
}
413413

414-
static ShtUsermod sht;
415-
REGISTER_USERMOD(sht);
414+
ShtUsermod sht_mod;
415+
REGISTER_USERMOD(sht_mod);

0 commit comments

Comments
 (0)