Skip to content

Commit

Permalink
minor refactoring and docs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabiano Riccardi committed Oct 26, 2019
1 parent 85000e3 commit f0adef9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/dimmable_light_linearized.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ class DimmableLightLinearized{
-0.12471*bri
+8.3201
#endif

tempBrightness *= 1000;
Serial.println(String("in:") + bri + " out:" + tempBrightness);

thyristor.setDelay(tempBrightness);
};
Expand Down
31 changes: 14 additions & 17 deletions src/thyristor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,31 @@ static const uint16_t gateTurnOffTime = 300;

static_assert( endMargin - gateTurnOffTime > mergePeriod, "endMargin must be greater than (gateTurnOffTime + mergePeriod)");

#ifdef PREDEFINED_PULSE_LENGTH
// Length of pulse of sync gate. this parameter is not applied if thyristor is fully on or off
static uint8_t pulseWidth = 15;
#endif


struct PinDelay{
uint8_t pin;
uint16_t delay;
};

/**
* Temporary struct to provide the interrupt a memory concurrent-safe
* NOTE: this structure is manipulated by interrupt routine
*/
static struct PinDelay pinDelay[Thyristor::N];

/**
* Number of thyristors already managed in the current semi-wave
* Number of thyristors already managed in the current semi-period
*/
static uint8_t thyristorManaged = 0;

/**
* Number of thyristors to be turned off by the end of the period
* Number of thyristors FULLY on. The remaining ones must be turned
* to be turned off by turn_off_gates_int at the end of the semi-period.
*/
static uint8_t alwaysOnCounter = 0;

Expand All @@ -122,9 +130,7 @@ void IRAM_ATTR activateThyristors(){
#else
void activateThyristors(){
#endif
// Alternative way to manage the pin, it should become low after the triac started
//delayMicroseconds(10);
//digitalWrite(AC_LOADS[phase],LOW);

const uint8_t firstToBeUpdated=thyristorManaged;

for(; (thyristorManaged<Thyristor::nThyristors-1 && pinDelay[thyristorManaged+1].delay-pinDelay[firstToBeUpdated].delay<mergePeriod) && (pinDelay[thyristorManaged].delay<semiPeriodLength-endMargin); thyristorManaged++){
Expand All @@ -139,7 +145,6 @@ void activateThyristors(){
thyristorManaged++;
}

uint8_t pulseWidth = 15;
#ifdef PREDEFINED_PULSE_LENGTH
delayMicroseconds(pulseWidth);

Expand Down Expand Up @@ -178,6 +183,7 @@ void activateThyristors(){
// Given actual HAL, AVR counter automatically stops on interrupt
#endif
#else
// If there are not more thyristors to serve, set timer to turn off gates' signal
uint16_t delay = semiPeriodLength-gateTurnOffTime-pinDelay[firstToBeUpdated].delay;
#if defined(ESP8266)
timer1_attachInterrupt(turn_off_gates_int);
Expand Down Expand Up @@ -405,7 +411,6 @@ void Thyristor::setDelay(uint16_t newDelay){
thyristors[target]=this;
this->posIntoArray=target;
}

}else if(newDelay<delay){
if(verbosity>2) Serial.println("\traising the light..");
bool done=false;
Expand Down Expand Up @@ -456,8 +461,6 @@ void Thyristor::setDelay(uint16_t newDelay){
Serial.println(thyristors[i]->delay);
}
}

//Serial.println(String("Brightness (in ms to wait): ") + delay);
}

/**
Expand Down Expand Up @@ -492,23 +495,17 @@ Thyristor::Thyristor(int pin)
}

newDelayValues=true;

// NO because this struct is updated by the routine!
// pinDelay[posIntoArray].pin;
// pinDelay[posIntoArray].delay=semiPeriodLength;

updatingStruct=false;
}else{
// return error or exception
// TODO return error or exception
}
}

Thyristor::~Thyristor(){
// Recompact the array
updatingStruct=true;
nThyristors--;
//remove the light from the static pinDelay array
Serial.println("I should implement the array shrinking");
// TODO remove light from the static pinDelay array, and shrink the array
updatingStruct=false;
}

Expand Down
7 changes: 5 additions & 2 deletions src/thyristor.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class Thyristor{
private:
/**
* Check if all thyristors are on or off. The result of this comparison
* is stored in allOff and allOn variables. This methos must be called
* every time a thyristor's delay is updated.
* is stored in allOff, allOn and allMixedOnOff variables. This methods
* must be called every time a thyristor's delay is updated.
*
* @param[in] newDelay the new delay just set
* @return true if interrupt for zero cross detection should be enabled,
Expand Down Expand Up @@ -152,6 +152,9 @@ class Thyristor{
*/
static bool allMixedOnOff;

/**
* Pin receiving the external Zero Cross signal.
*/
static uint8_t syncPin;

/**
Expand Down

0 comments on commit f0adef9

Please sign in to comment.