Skip to content

Commit 232d7c4

Browse files
committed
PWM_fan: Use function API to access temperature
Instead of dealing with runtime lookup, use an external function to look up the temperature value.
1 parent d1d9dec commit 232d7c4

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

usermods/PWM_fan/PWM_fan.cpp

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#include "wled.h"
22

3-
#if defined(USERMOD_DALLASTEMPERATURE)
4-
#include "UsermodTemperature.h"
5-
#elif defined(USERMOD_SHT)
6-
#include "ShtUsermod.h"
7-
#else
8-
#error The "PWM fan" usermod requires "Dallas Temeprature" or "SHT" usermod to function properly.
9-
#endif
10-
3+
// Function reference to read temperature; some other usermod must provide this
4+
namespace Temperature {
5+
float getTemperatureC();
6+
}
117

128

139
// PWM & tacho code curtesy of @KlausMu
@@ -44,12 +40,6 @@ class PWMFanUsermod : public Usermod {
4440
#endif
4541
bool lockFan = false;
4642

47-
#ifdef USERMOD_DALLASTEMPERATURE
48-
UsermodTemperature* tempUM;
49-
#elif defined(USERMOD_SHT)
50-
ShtUsermod* tempUM;
51-
#endif
52-
5343
// configurable parameters
5444
int8_t tachoPin = TACHO_PIN;
5545
int8_t pwmPin = PWM_PIN;
@@ -157,16 +147,8 @@ class PWMFanUsermod : public Usermod {
157147
#endif
158148
}
159149

160-
float getActualTemperature(void) {
161-
#if defined(USERMOD_DALLASTEMPERATURE) || defined(USERMOD_SHT)
162-
if (tempUM != nullptr)
163-
return tempUM->getTemperatureC();
164-
#endif
165-
return -127.0f;
166-
}
167-
168150
void setFanPWMbasedOnTemperature(void) {
169-
float temp = getActualTemperature();
151+
float temp = Temperature::getTemperatureC();
170152
// dividing minPercent and maxPercent into equal pwmvalue sizes
171153
int pwmStepSize = ((maxPWMValuePct - minPWMValuePct) * _pwmMaxValue) / (_pwmMaxStepCount*100);
172154
int pwmStep = calculatePwmStep(temp - targetTemperature);
@@ -193,12 +175,6 @@ class PWMFanUsermod : public Usermod {
193175
// gets called once at boot. Do all initialization that doesn't depend on
194176
// network here
195177
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
202178
initTacho();
203179
initPWMfan();
204180
updateFanSpeed((minPWMValuePct * 255) / 100); // inital fan speed

usermods/Temperature/Temperature.cpp

Lines changed: 7 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";
@@ -369,15 +367,19 @@ const char UsermodTemperature::_temperature[] PROGMEM = "temperature";
369367
const char UsermodTemperature::_Temperature[] PROGMEM = "/temperature";
370368
const char UsermodTemperature::_data_fx[] PROGMEM = "Temperature@Min,Max;;!;01;pal=54,sx=255,ix=0";
371369

370+
static UsermodTemperature temperature;
371+
REGISTER_USERMOD(temperature);
372+
373+
// Effect function for temperature
372374
static uint16_t mode_temperature() {
373375
float low = roundf(mapf((float)SEGMENT.speed, 0.f, 255.f, -150.f, 150.f)); // default: 15°C, range: -15°C to 15°C
374376
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)
377+
float temp = constrain(temperature.getTemperatureC()*10.f, low, high); // get a little better resolution (*10)
376378
unsigned i = map(roundf(temp), (unsigned)low, (unsigned)high, 0, 248);
377379
SEGMENT.fill(SEGMENT.color_from_palette(i, false, false, 255));
378380
return FRAMETIME;
379381
}
380382

381383

382-
static UsermodTemperature temperature;
383-
REGISTER_USERMOD(temperature);
384+
// External API
385+
float Temperature::getTemperatureC() { return temperature.getTemperatureC(); }

usermods/Temperature/UsermodTemperature.h

Lines changed: 6 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,8 @@ class UsermodTemperature : public Usermod {
108105
void appendConfigData() override;
109106
};
110107

108+
109+
// External API
110+
namespace Temperature {
111+
float getTemperatureC();
112+
}

usermods/sht/ShtUsermod.h

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

7070
uint16_t getId() { return USERMOD_ID_SHT; }
7171
};
72+
73+
// External API
74+
namespace Temperature {
75+
float getTemperatureC();
76+
}

usermods/sht/sht.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,9 @@ const char* ShtUsermod::getUnitString() {
413413

414414
static ShtUsermod sht;
415415
REGISTER_USERMOD(sht);
416+
417+
418+
/**
419+
* External API
420+
*/
421+
float Temperature::getTemperatureC() { return sht.getTemperatureC(); }

0 commit comments

Comments
 (0)