Skip to content

Commit

Permalink
FX: Usermod Temperature effect
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Sep 15, 2024
1 parent 0806c7f commit 65a8dbf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
23 changes: 22 additions & 1 deletion usermods/Temperature/usermod_temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#define USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL 60000
#endif

static uint16_t mode_temperature();

class UsermodTemperature : public Usermod {

private:
Expand Down Expand Up @@ -60,6 +62,7 @@ class UsermodTemperature : public Usermod {
static const char _sensor[];
static const char _temperature[];
static const char _Temperature[];
static const char _data_fx[];

//Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013
float readDallas();
Expand All @@ -70,8 +73,13 @@ class UsermodTemperature : public Usermod {
void publishHomeAssistantAutodiscovery();
#endif

static UsermodTemperature* _instance; // to overcome nonstatic getTemperatureC() method and avoid usermods.lookup(USERMOD_ID_TEMPERATURE);

public:

UsermodTemperature() { _instance = this; }
static UsermodTemperature *getInstance() { return UsermodTemperature::_instance; }

/*
* API calls te enable data exchange between WLED modules
*/
Expand Down Expand Up @@ -234,6 +242,7 @@ void UsermodTemperature::setup() {
}
temperaturePin = -1; // allocation failed
}
if (sensorFound && !initDone) strip.addEffect(255, &mode_temperature, _data_fx);
}
lastMeasurement = millis() - readingInterval + 10000;
initDone = true;
Expand Down Expand Up @@ -440,6 +449,8 @@ const char *UsermodTemperature::getTemperatureUnit() {
return degC ? "°C" : "°F";
}

UsermodTemperature* UsermodTemperature::_instance = nullptr;

// strings to reduce flash memory usage (used more than twice)
const char UsermodTemperature::_name[] PROGMEM = "Temperature";
const char UsermodTemperature::_enabled[] PROGMEM = "enabled";
Expand All @@ -449,4 +460,14 @@ const char UsermodTemperature::_parasitePin[] PROGMEM = "parasite-pwr-pin";
const char UsermodTemperature::_domoticzIDX[] PROGMEM = "domoticz-idx";
const char UsermodTemperature::_sensor[] PROGMEM = "sensor";
const char UsermodTemperature::_temperature[] PROGMEM = "temperature";
const char UsermodTemperature::_Temperature[] PROGMEM = "/temperature";
const char UsermodTemperature::_Temperature[] PROGMEM = "/temperature";
const char UsermodTemperature::_data_fx[] PROGMEM = "Temperature@Min,Max;;!;01;pal=54";

static uint16_t mode_temperature() {
float low = mapf((float)SEGMENT.speed, 0.f, 255.f, -150.f, 149.9f); // default: 0°C, range: -15°C to 15°C
float high = mapf((float)SEGMENT.intensity, 0.f, 255.f, 150.f, 450.f); // default: 30°C, range 15°C to 45°C
float temp = constrain(UsermodTemperature::getInstance()->getTemperatureC()*10.f, low, high); // get a little better resolution
unsigned i = map(temp, (unsigned)low, (unsigned)high, 0, 255);
SEGMENT.fill(SEGMENT.color_from_palette(i, false, false, 255));
return FRAMETIME;
}
6 changes: 0 additions & 6 deletions usermods/audioreactive/audio_reactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ static bool useBandPassFilter = false; // if true, enables a
////////////////////

// some prototypes, to ensure consistent interfaces
static float mapf(float x, float in_min, float in_max, float out_min, float out_max); // map function for float
static float fftAddAvg(int from, int to); // average of several FFT result bins
void FFTcode(void * parameter); // audio processing task: read samples, run FFT, fill GEQ channels from FFT results
static void runMicFilter(uint16_t numSamples, float *sampleBuffer); // pre-filtering of raw samples (band-pass)
Expand Down Expand Up @@ -211,11 +210,6 @@ static ArduinoFFT<float> FFT = ArduinoFFT<float>( vReal, vImag, samplesFFT, SAMP

// Helper functions

// float version of map()
static float mapf(float x, float in_min, float in_max, float out_min, float out_max){
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// compute average of several FFT result bins
static float fftAddAvg(int from, int to) {
float result = 0.0f;
Expand Down
5 changes: 0 additions & 5 deletions wled00/FX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6483,11 +6483,6 @@ static const char _data_FX_MODE_2DWAVERLY[] PROGMEM = "Waverly@Amplification,Sen

#endif // WLED_DISABLE_2D

// float version of map()
static float mapf(float x, float in_min, float in_max, float out_min, float out_max){
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// Gravity struct requited for GRAV* effects
typedef struct Gravity {
int topLED;
Expand Down
1 change: 1 addition & 0 deletions wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ uint16_t crc16(const unsigned char* data_p, size_t length);
um_data_t* simulateSound(uint8_t simulationId);
void enumerateLedmaps();
uint8_t get_random_wheel_index(uint8_t pos);
float mapf(float x, float in_min, float in_max, float out_min, float out_max);

// RAII guard class for the JSON Buffer lock
// Modeled after std::lock_guard
Expand Down
5 changes: 5 additions & 0 deletions wled00/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,8 @@ uint8_t get_random_wheel_index(uint8_t pos) {
}
return r;
}

// float version of map()
float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

0 comments on commit 65a8dbf

Please sign in to comment.