quickPatterns icon indicating copy to clipboard operation
quickPatterns copied to clipboard

Reversing the direction for comet doesn't work (includes working code)

Open spikerXneh opened this issue 1 year ago • 0 comments

I noticed that reversing the direction for comet doesn't work. There is no logic to handle the -1. I updated my local version with a working version of it but unsure if this library is still being maintained but i've added my updated logic here in case it might help someone else.

class qpComet : public qpPattern {

    private:
        byte length;
        bool bounce;
        int pos = 0;
        int dir = 1;

        bool _inBounds(int pos) { return ((pos >= 0) && (pos < _numLeds)); }

    public:

        qpComet(byte length, bool bounce = false, int dir = 1) : length(length), bounce(bounce), dir(dir) { if (dir == -1){ pos = _numLeds;}}

        void draw() {
            
            if(_inBounds(this->pos))
                _targetLeds[this->pos] = _getColor();

            if(_inBounds(this->pos - (this->length*this->dir)))
                _targetLeds[(this->pos - (this->length*this->dir))] = CRGB::Black;
            
            if((this->pos >= (_numLeds + this->length)) || (this->pos <= (0 - this->length))) {
                if(this->bounce)
                    this->dir *= -1;
                else
					if(dir == -1){
						this->pos = _numLeds;
					}else{
						this->pos = 0;    
					}
                    
                _countCycle();
            }

            this->pos += this->dir;            
      }

};

spikerXneh avatar Jan 15 '25 14:01 spikerXneh