new fluorescent algorithm

This commit is contained in:
interfisch 2019-01-26 23:51:30 +01:00
parent c9973d111c
commit 40d1f31a17
2 changed files with 147 additions and 139 deletions

View File

@ -3,6 +3,8 @@
//curl -X PUT http://homie.config/config -d @config.json --header "Content-Type: application/json" //curl -X PUT http://homie.config/config -d @config.json --header "Content-Type: application/json"
// homie/ledstoffroehre/lamp/fluorescent0/set
#define PIN_LAMP0_EDGE D5 #define PIN_LAMP0_EDGE D5
#define PIN_LAMP0 D6 #define PIN_LAMP0 D6
#define PIN_LAMP1_EDGE D7 #define PIN_LAMP1_EDGE D7
@ -33,13 +35,29 @@ int setbrightness0 = 0;
int setbrightness1 = 0; int setbrightness1 = 0;
bool fluorescent0Active=false; bool fluorescent0Active=false;
bool fluorescent1Active=false; bool fluorescent1Active=false;
int fluorescent0Age=10; int fluorescent0Quality=50; // 0 to 100
int fluorescent1Age=10; int fluorescent1Quality=50;
#define FLUORESCENTAGEMAX 20 #define FLUORESCENQUALITYMAX 100
#define FLUORESCENTTEMPMAX 1000 #define FLUORESCENTTEMPMAX 1000
int fluorescent0Temp=0; int fluorescent0Temp=0;
int fluorescent1Temp=0; int fluorescent1Temp=0;
#define GLOWBRIGHTNESS 5 #define GLOWBRIGHTNESS 10
int flashprobability0=300; //the higher the lesser
int flashprobabilitymin0=100; //the higher the lesser (at peak)
int tempincreasemax0=10; //the higher the faster lightup
int flashprobability1=300; //the higher the lesser
int flashprobabilitymin1=100; //the higher the lesser (at peak)
int tempincreasemax1=10; //the higher the faster lightup
#define FLASHPROBABILITY_MIN 100
#define FLASHPROBABILITY_MAX 600
#define FLASHPROBABILITYMIN_MIN 50
#define FLASHPROBABILITYMIN_MAX 100 //should not be more than FLASHPROBABILITY_MIN
#define TEMPINCREASEMAX_MIN 5
#define TEMPINCREASEMAX_MAX FLUORESCENTTEMPMAX/10
bool lastSensorValue = false; bool lastSensorValue = false;
@ -59,14 +77,8 @@ bool fluorescentHandler(const HomieRange& range, const String& value) {
fluorescent1Temp=0; fluorescent1Temp=0;
if (setbrightness0 == 0 || setbrightness1 == 0) { // turned off if (setbrightness0 == 0 || setbrightness1 == 0) { // turned off
fluorescent0Active = false; //set effect off resetLamp0();
fluorescent1Active = false; //set effect off resetLamp1();
fluorescent0LastActivated = millis();
fluorescent1LastActivated = millis();
lamp0e=0;
lamp0=0;
lamp1e=0;
lamp1=0;
} else { //turned on } else { //turned on
//Initialization //Initialization
fluorescent0Active = true; //start effect fluorescent0Active = true; //start effect
@ -82,10 +94,7 @@ bool fluorescent0Handler(const HomieRange& range, const String& value) {
fluorescent0Temp=0; fluorescent0Temp=0;
if (setbrightness0 == 0) { // turned off if (setbrightness0 == 0) { // turned off
fluorescent0Active = false; //set effect off resetLamp0();
fluorescent0LastActivated = millis();
lamp0e=0;
lamp0 = 0;
} else { //turned on } else { //turned on
//Initialization //Initialization
fluorescent0Active = true; //start effect fluorescent0Active = true; //start effect
@ -100,10 +109,7 @@ bool fluorescent1Handler(const HomieRange& range, const String& value) {
fluorescent1Temp=0; fluorescent1Temp=0;
if (setbrightness1 == 0) { // turned off if (setbrightness1 == 0) { // turned off
fluorescent1Active = false; //set effect off resetLamp1();
fluorescent1LastActivated = millis();
lamp1e=0;
lamp1 = 0;
} else { //turned on } else { //turned on
//Initialization //Initialization
fluorescent1Active = true; //start effect fluorescent1Active = true; //start effect
@ -112,20 +118,42 @@ bool fluorescent1Handler(const HomieRange& range, const String& value) {
return true; return true;
} }
void resetLamp0(){
fluorescent0Active = false; //set effect off
fluorescent0LastActivated = millis();
lamp0e=0;
lamp0=0;
float speedpercent=fluorescent0Quality*1.0/FLUORESCENQUALITYMAX;
flashprobability0=random(FLASHPROBABILITY_MIN,FLASHPROBABILITY_MAX);
flashprobabilitymin0=random(FLASHPROBABILITYMIN_MIN,FLASHPROBABILITYMIN_MAX);
tempincreasemax0=TEMPINCREASEMAX_MIN+speedpercent*(TEMPINCREASEMAX_MAX-TEMPINCREASEMAX_MIN);//random(TEMPINCREASEMAX_MIN,TEMPINCREASEMAX_MAX);
}
void resetLamp1(){
fluorescent1Active = false; //set effect off
fluorescent1LastActivated = millis();
lamp1e=0;
lamp1=0;
float speedpercent=fluorescent1Quality*1.0/FLUORESCENQUALITYMAX;
flashprobability1=random(FLASHPROBABILITY_MIN,FLASHPROBABILITY_MAX);
flashprobabilitymin1=random(FLASHPROBABILITYMIN_MIN,FLASHPROBABILITYMIN_MAX);
tempincreasemax0=TEMPINCREASEMAX_MIN+speedpercent*(TEMPINCREASEMAX_MAX-TEMPINCREASEMAX_MIN);
}
bool fluorescentAgeHandler(const HomieRange& range, const String& value) { bool fluorescentAgeHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "fluorescentAge " << ": " << value << endl; Homie.getLogger() << "fluorescentAge " << ": " << value << endl;
fluorescent0Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); fluorescent0Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
fluorescent1Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); fluorescent1Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
return true; return true;
} }
bool fluorescent0AgeHandler(const HomieRange& range, const String& value) { bool fluorescent0QualityHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "fluorescent0Age " << ": " << value << endl; Homie.getLogger() << "fluorescent0Quality " << ": " << value << endl;
fluorescent0Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); fluorescent0Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
return true; return true;
} }
bool fluorescent1AgeHandler(const HomieRange& range, const String& value) { bool fluorescent1QualityHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "fluorescent1Age " << ": " << value << endl; Homie.getLogger() << "fluorescent1Quality " << ": " << value << endl;
fluorescent1Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); fluorescent1Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
return true; return true;
} }
@ -175,7 +203,7 @@ void output() {
void loopHandler() void loopHandler()
{ {
//mosquitto_pub -h raum.ctdo.de -t "homie/esp-deckenlicht/strip/fluorescent/set" -m "255" //mosquitto_pub -h raum.ctdo.de -t "homie/ledstoffroehre/lamp/fluorescent0/set" -m "255"
if (millis() > fluorescentLastUpdated + FLUORESCENTUPDATEINTERVAL) { //Update values if (millis() > fluorescentLastUpdated + FLUORESCENTUPDATEINTERVAL) { //Update values
@ -184,27 +212,20 @@ void loopHandler()
if (fluorescent0Active) { if (fluorescent0Active) {
//long _time = millis() - fluorescent0LastActivated; //time since activated //long _time = millis() - fluorescent0LastActivated; //time since activated
int fluorescentTempIncreaseMax=61-(fluorescent0Age*3); fluorescent0Temp+=random(1,tempincreasemax0);
fluorescent0Temp +=1+ random(0,fluorescentTempIncreaseMax *fluorescent0Temp/FLUORESCENTTEMPMAX); if (random(0,flashprobability0-constrain(fluorescent0Temp*flashprobability0/FLUORESCENTTEMPMAX,0,flashprobability0-flashprobabilitymin0))==0)
{
if (random(0,80 )==0){ //ignite lamp0=setbrightness0*random(30,100)/10;
lamp0=setbrightness0*random(50,100)/100; lamp0e=lamp0; //flash everything
}
lamp0-=40;
if (random(0,100)>95){
lamp0e+=constrain(random(0,fluorescent0Temp*10/FLUORESCENTTEMPMAX) , 0, 10); //start glowing slowly
} }
if (fluorescent0Temp>200){ //warm enough to glow if (lamp0e>GLOWBRIGHTNESS)
if (lamp0<20){ //if under glow brightness {
lamp0+=5; //start glowing lamp0e-=constrain(lamp0e-GLOWBRIGHTNESS,0,20); //make sides darker until glowbrightness
}else if (lamp0>50){ //too bright for glow
lamp0-=random(0,30); //reduce intensity (flashing effect)
}
}else{ //not warm enough to glow
if (lamp0>0){ //too bright for glow
lamp0-=random(20,50); //reduce intensity (flashing effect)
}
}
if(lamp0>GLOWBRIGHTNESS){
lamp0e=lamp0;
} }
if (fluorescent0Temp>=FLUORESCENTTEMPMAX){ if (fluorescent0Temp>=FLUORESCENTTEMPMAX){
@ -220,31 +241,23 @@ void loopHandler()
} }
if (fluorescent1Active) { if (fluorescent1Active) {
//long _time = millis() - fluorescent0LastActivated; //time since activated //long _time = millis() - fluorescent0LastActivated; //time since activated
int fluorescentTempIncreaseMax=61-(fluorescent1Age*3); fluorescent1Temp+=random(1,tempincreasemax1);
fluorescent1Temp +=1+ random(0,fluorescentTempIncreaseMax *fluorescent1Temp/FLUORESCENTTEMPMAX); //+= 1+ random(0,20* fluorescent0Temp/FLUORESCENTTEMPMAX); if (random(0,flashprobability1-constrain(fluorescent1Temp*flashprobability1/FLUORESCENTTEMPMAX,0,flashprobability1-flashprobabilitymin1))==0)
{
if (random(0,80 )==0){ //ignite lamp1=setbrightness1*random(30,100)/10;
lamp1=setbrightness1*random(50,100)/100; lamp1e=lamp1; //flash everything
}
lamp1-=40;
if (random(0,100)>95){
lamp1e+=constrain(random(0,fluorescent1Temp*10/FLUORESCENTTEMPMAX) , 0, 10); //start glowing slowly
} }
if (fluorescent1Temp>200){ //warm enough to glow if (lamp1e>GLOWBRIGHTNESS)
if (lamp1<20){ //if under glow brightness {
lamp1+=5; //start glowing lamp1e-=constrain(lamp1e-GLOWBRIGHTNESS,0,20); //make sides darker until glowbrightness
}else if (lamp1>50){ //too bright for glow
lamp1-=random(0,30); //reduce intensity (flashing effect)
}
}else{ //not warm enough to glow
if (lamp1>0){ //too bright for glow
lamp1-=random(20,50); //reduce intensity (flashing effect)
}
}
if(lamp1>GLOWBRIGHTNESS){
lamp1e=lamp1;
} }
if (fluorescent1Temp>=FLUORESCENTTEMPMAX){ if (fluorescent1Temp>=FLUORESCENTTEMPMAX){
@ -253,12 +266,12 @@ void loopHandler()
lamp1e=setbrightness1; lamp1e=setbrightness1;
} }
lamp1=constrain(lamp1, 0,255); lamp1=constrain(lamp1, 0,255);
lamp1e=constrain(lamp1e, 0,255); lamp1e=constrain(lamp1e, 0,255);
output(); output();
} }
} }
@ -295,9 +308,9 @@ void setup() {
lightNode.advertise("fluorescent").settable(fluorescentHandler); lightNode.advertise("fluorescent").settable(fluorescentHandler);
lightNode.advertise("fluorescentage").settable(fluorescentAgeHandler); lightNode.advertise("fluorescentage").settable(fluorescentAgeHandler);
lightNode.advertise("fluorescent0").settable(fluorescent0Handler); lightNode.advertise("fluorescent0").settable(fluorescent0Handler);
lightNode.advertise("fluorescent0age").settable(fluorescent0AgeHandler); lightNode.advertise("fluorescent0Quality").settable(fluorescent0QualityHandler);
lightNode.advertise("fluorescent1").settable(fluorescent1Handler); lightNode.advertise("fluorescent1").settable(fluorescent1Handler);
lightNode.advertise("fluorescent1age").settable(fluorescent1AgeHandler); lightNode.advertise("fluorescent1Quality").settable(fluorescent1QualityHandler);
sensorNode.advertise("motion"); sensorNode.advertise("motion");

View File

@ -17,15 +17,48 @@ clock = pygame.time.Clock()
import time import time
FLUORESCENTTEMPMAX=400 lamp0e=0
lamp0=0
fluorescentCurrentBrightness=0 lamp1e=0
fluorescentTemp=0 lamp1=0
fluorescentActive=True
fluorescentSet=255 setbrightness0 = 0
#setbrightness1 = 0;
fluorescent0Active=False
#fluorescent1Active=False;
fluorescent0Age=10
#fluorescent1Age=10;
FLUORESCENTAGEMAX= 20
FLUORESCENTTEMPMAX= 1000
fluorescent0Temp=0
#fluorescent1Temp=0;
GLOWBRIGHTNESS= 50
flashprobability0=300 #the higher the lesser
flashprobabilitymin0=100 #the higher the lesser (at peak)
tempincreasemax0=10 #the higher the faster lightup
FLASHPROBABILITY_MIN=200
FLASHPROBABILITY_MAX=600
FLASHPROBABILITYMIN_MIN=100
FLASHPROBABILITYMIN_MAX=150
TEMPINCREASEMAX_MIN=5
TEMPINCREASEMAX_MAX=15
flashprobability0=random.randint(FLASHPROBABILITY_MIN,FLASHPROBABILITY_MAX)
flashprobabilitymin0=random.randint(FLASHPROBABILITYMIN_MIN,FLASHPROBABILITYMIN_MAX)
tempincreasemax0=random.randint(TEMPINCREASEMAX_MIN,TEMPINCREASEMAX_MAX)
fluorescent0Active=True #start
setbrightness0=255
def constrain(val, min_val, max_val):
return min(max_val, max(min_val, val))
# Loop as long as done == False # Loop as long as done == False
while not done: while not done:
@ -37,71 +70,33 @@ while not done:
if (fluorescent0Active):
''' fluorescent0Temp+=random.randint(1,tempincreasemax0)
if (fluorescentActive): if (random.randint(0,flashprobability0-constrain(fluorescent0Temp*flashprobability0/FLUORESCENTTEMPMAX,0,flashprobability0-flashprobabilitymin0))==0):
fluorescentTemp+=random.randint(0,3 +1) lamp0=setbrightness0*random.randint(30,100)/10
lamp0e=lamp0 #flash everything
if (random.randint(0, 256+1) < fluorescentTemp*1.0/FLUORESCENTTEMPMAX*256): lamp0-=20
if (random.randint(0, 40+1) ==0): #ignite lamp0e+=constrain(random.randint(-10,fluorescent0Temp*10/FLUORESCENTTEMPMAX) , 0, 10) #start glowing slowly
fluorescentCurrentBrightness=fluorescentSet*random.randint(50,100)/100 if (lamp0e>GLOWBRIGHTNESS):
lamp0e-=constrain(lamp0e-GLOWBRIGHTNESS,0,20) #make sides darker until glowbrightness
if (random.randint(0, 256+1) > fluorescentTemp*1.0/FLUORESCENTTEMPMAX*256):
if (fluorescentCurrentBrightness<5):
if (random.randint(0,50 +1)==0): #ignite
fluorescentCurrentBrightness=fluorescentSet*random.randint(50,100)/100
if (fluorescentCurrentBrightness>20):
fluorescentCurrentBrightness-=30
if (fluorescentTemp>=FLUORESCENTTEMPMAX):
fluorescentActive=False
fluorescentCurrentBrightness=fluorescentSet
if (fluorescentCurrentBrightness>255): if fluorescent0Temp>=FLUORESCENTTEMPMAX:
fluorescentCurrentBrightness=255 fluorescent0Active=False
if (fluorescentCurrentBrightness<0): fluorescent0Temp=0
fluorescentCurrentBrightness=0 lamp0=setbrightness0
lamp0e=setbrightness0
print("finished")
COLOR = (fluorescentCurrentBrightness,fluorescentCurrentBrightness,fluorescentCurrentBrightness) lamp0=constrain(int(lamp0),0,255)
else: lamp0e=constrain(int(lamp0e),0,255)
COLOR = (fluorescentSet,fluorescentSet,fluorescentSet) clamp0 = (lamp0,lamp0,lamp0)
''' clamp0e = (lamp0e,lamp0e,lamp0e)
if (fluorescentActive): screen.fill(clamp0) #center
fluorescentTemp+=1+ random.randint(0,20* fluorescentTemp/FLUORESCENTTEMPMAX) pygame.draw.rect(screen,clamp0e,(0,0,20,size[1])) #left edge
#fluorescentTemp+=3 pygame.draw.rect(screen,clamp0e,(size[0]-20,0,20,size[1]))#right edge
if (random.randint(0,80 +1)==0): #ignite
fluorescentCurrentBrightness=fluorescentSet*random.randint(50,100)/100
if (fluorescentTemp>200): #warm enough to glow
if (fluorescentCurrentBrightness<20): #if under glow brightness
fluorescentCurrentBrightness+=5 #start glowing
elif (fluorescentCurrentBrightness>50): #too bright for glow
fluorescentCurrentBrightness-=random.randint(0,30) #reduce intencity (flashing effect)
else: #not warm enough to glow
if (fluorescentCurrentBrightness>0): #too bright for glow
fluorescentCurrentBrightness-=random.randint(20,50) #reduce intencity (flashing effect)
if (fluorescentTemp>=FLUORESCENTTEMPMAX):
fluorescentActive=False
fluorescentCurrentBrightness=fluorescentSet
print("Finished")
if (fluorescentCurrentBrightness>255):
fluorescentCurrentBrightness=255
if (fluorescentCurrentBrightness<0):
fluorescentCurrentBrightness=0
COLOR = (fluorescentCurrentBrightness,fluorescentCurrentBrightness,fluorescentCurrentBrightness)
else:
COLOR = (fluorescentSet,fluorescentSet,fluorescentSet)
screen.fill(COLOR)
pygame.display.flip() pygame.display.flip()