From 6b6a9b3ce15fa6a3264127dad0f5d7e1427091d0 Mon Sep 17 00:00:00 2001 From: Luca Paolini Date: Thu, 18 Feb 2021 21:09:51 +0100 Subject: [PATCH] Bugfixes. --- src/JoinedStrip.cpp | 233 ++++++++++++++++++++++++-------------------- src/JoinedStrip.h | 6 ++ 2 files changed, 134 insertions(+), 105 deletions(-) diff --git a/src/JoinedStrip.cpp b/src/JoinedStrip.cpp index 59e96f8..b929525 100644 --- a/src/JoinedStrip.cpp +++ b/src/JoinedStrip.cpp @@ -7,7 +7,7 @@ JoinedStrip::JoinedStrip(Strip *strip1, Strip *strip2, uint16_t gap) { this->gap = gap; buffer = new CRGB[gap]; bufferSet = new CRGBSet(buffer, gap); - gapStrip = new PhysicalStrip(bufferSet); + gapStrip = new StatefulStrip(bufferSet); } Strip *JoinedStrip::buffered() { @@ -15,7 +15,7 @@ Strip *JoinedStrip::buffered() { } uint16_t JoinedStrip::size() { - return strip1->size() + gap + strip2->size(); + return strip1->size() + gapStrip->size() + strip2->size(); } uint16_t JoinedStrip::first() { @@ -54,18 +54,34 @@ uint16_t JoinedStrip::fromNormalizedPosition(float normalizedPosition, uint16_t return int(normalizedPosition * (last() - excludeCount)); } -bool JoinedStrip::isInRange(int16_t index) { - return index >= 0 && index < size(); +bool JoinedStrip::isInStrip1(int16_t index) { + return strip1->isInRange(toStrip1(index)); } -uint16_t JoinedStrip::limitToRange(int16_t index) { - return max(min(index, last()), 0); +bool JoinedStrip::isInGap(int16_t index) { + return gapStrip->isInRange(toGap(index)); +} + +bool JoinedStrip::isInStrip2(int16_t index) { + return strip2->isInRange(toStrip2(index)); +} + +int16_t JoinedStrip::toStrip1(int16_t index) { + return index; +} + +int16_t JoinedStrip::toGap(int16_t index) { + return index - strip1->size(); +} + +int16_t JoinedStrip::toStrip2(int16_t index) { + return index - strip1->size() - gapStrip->size(); } void JoinedStrip::off() { strip1->off(); - strip2->off(); gapStrip->off(); + strip2->off(); } void JoinedStrip::rainbow(uint8_t initialHue) { @@ -82,27 +98,27 @@ void JoinedStrip::rainbow(uint8_t initialHue, uint16_t indexFrom, uint16_t index } void JoinedStrip::rainbow(uint8_t initialHue, uint8_t deltaHue, uint16_t indexFrom, uint16_t indexTo) { - if (strip1->isInRange(indexFrom)) { - if (strip1->isInRange(indexTo)) { + if (isInStrip1(indexFrom)) { + if (isInStrip1(indexTo)) { // starts and ends in strip 1 - strip1->rainbow(initialHue, deltaHue, indexFrom, indexTo); - } else if (strip2->isInRange(indexTo - strip1->size() - gap)) { - // starts in strip 1 and ends in strip 2 - strip1->rainbow(initialHue, deltaHue, indexFrom, strip1->last()); - gapStrip->rainbow(initialHue + deltaHue * (strip1->size() - indexFrom), deltaHue); - strip2->rainbow(initialHue + deltaHue * (strip1->size() + gap - indexFrom), deltaHue, strip2->first(), indexTo - strip1->size()); - } else { + strip1->rainbow(initialHue, deltaHue, toStrip1(indexFrom), toStrip1(indexTo)); + } else if (isInGap(indexTo)) { // starts in strip 1 and ends in gap - strip1->rainbow(initialHue, deltaHue, indexFrom, strip1->last()); + strip1->rainbow(initialHue, deltaHue, toStrip1(indexFrom), strip1->last()); + gapStrip->rainbow(initialHue + deltaHue * (strip1->size() - indexFrom), deltaHue, gapStrip->first(), toGap(indexTo)); + } else if (isInStrip2(indexTo)) { + // starts in strip 1 and ends in strip 2 + strip1->rainbow(initialHue, deltaHue, toStrip1(indexFrom), strip1->last()); gapStrip->rainbow(initialHue + deltaHue * (strip1->size() - indexFrom), deltaHue); + strip2->rainbow(initialHue + deltaHue * (strip1->size() + gap - indexFrom), deltaHue, strip2->first(), toStrip2(indexTo)); } - } else if (strip2->isInRange(indexFrom - strip1->size() - gap)) { - // starts and ends in strip 2 - strip2->rainbow(initialHue, deltaHue, indexFrom - strip1->size() - gap, indexTo - strip1->size() - gap); - } else { + } else if (isInGap(indexFrom)) { // starts in gap and ends in strip 2 - gapStrip->rainbow(initialHue, deltaHue, indexFrom - strip1->size(), gap - 1); - strip2->rainbow(initialHue + deltaHue * (strip1->size() + gap - indexFrom), deltaHue, 0, indexTo - strip1->size() - gap); + gapStrip->rainbow(initialHue, deltaHue, toGap(indexFrom), gapStrip->last()); + strip2->rainbow(initialHue + deltaHue * (strip1->size() + gap - indexFrom), deltaHue, strip2->first(), toStrip2(indexTo)); + } else if (isInStrip2(indexFrom)) { + // starts and ends in strip 2 + strip2->rainbow(initialHue, deltaHue, toStrip2(indexFrom), toStrip2(indexTo)); } } @@ -113,27 +129,27 @@ void JoinedStrip::fade(uint8_t amount) { } void JoinedStrip::fade(uint8_t amount, uint16_t indexFrom, uint16_t indexTo) { - if (strip1->isInRange(indexFrom)) { - if (strip1->isInRange(indexTo)) { + if (isInStrip1(indexFrom)) { + if (isInStrip1(indexTo)) { // starts and ends in strip 1 - strip1->fade(amount, indexFrom, indexTo); - } else if (strip2->isInRange(indexTo - strip1->size() - gap)) { + strip1->fade(amount, toStrip1(indexFrom), toStrip1(indexTo)); + } else if (isInGap(indexTo)) { + // starts in strip 1 and ends in gap + strip1->fade(amount, toStrip1(indexFrom), strip1->last()); + gapStrip->fade(amount, gapStrip->first(), toGap(indexTo)); + } else if (isInStrip2(indexTo)) { // starts in strip 1 and ends in strip 2 - strip1->fade(amount, indexFrom, strip1->last()); + strip1->fade(amount, toStrip1(indexFrom), strip1->last()); gapStrip->fade(amount); - strip2->fade(amount, strip2->first(), indexTo - strip1->size()); - } else { - // starts in strip 1 and ends in gap - strip1->fade(amount, indexFrom, strip1->last()); - gapStrip->fade(amount, 0, indexTo - strip1->size()); + strip2->fade(amount, strip2->first(), toStrip2(indexTo)); } - } else if (strip2->isInRange(indexFrom - strip1->size() - gap)) { - // starts and ends in strip 2 - strip2->fade(amount, indexFrom - strip1->size() - gap, indexTo - strip1->size() - gap); - } else { + } else if (isInGap(indexFrom)) { // starts in gap and ends in strip 2 - gapStrip->fade(amount, indexFrom - strip1->size(), gap - 1); - strip2->fade(amount, 0, indexTo - strip1->size() - gap); + gapStrip->fade(amount, toGap(indexFrom), gapStrip->last()); + strip2->fade(amount, strip2->first(), toStrip2(indexTo)); + } else if (isInStrip2(indexFrom)) { + // starts and ends in strip 2 + strip2->fade(amount, toStrip2(indexFrom), toStrip2(indexTo)); } } @@ -144,28 +160,27 @@ void JoinedStrip::blur(uint8_t amount) { } void JoinedStrip::blur(uint8_t amount, uint16_t indexFrom, uint16_t indexTo) { - if (strip1->isInRange(indexFrom)) { - if (strip1->isInRange(indexTo)) { + if (isInStrip1(indexFrom)) { + if (isInStrip1(indexTo)) { // starts and ends in strip 1 - strip1->blur(amount, indexFrom, indexTo); - } else if (strip2->isInRange(indexTo - strip1->size() - gap)) { + strip1->blur(amount, toStrip1(indexFrom), toStrip1(indexTo)); + } else if (isInGap(indexTo)) { + // starts in strip 1 and ends in gap + strip1->blur(amount, toStrip1(indexFrom), strip1->last()); + gapStrip->blur(amount, gapStrip->first(), toGap(indexTo)); + } else if (isInStrip2(indexTo)) { // starts in strip 1 and ends in strip 2 - strip1->blur(amount, indexFrom, strip1->last()); - // bufferSet->blur1d(amount); + strip1->blur(amount, toStrip1(indexFrom), strip1->last()); gapStrip->blur(amount); - strip2->blur(amount, strip2->first(), indexTo - strip1->size()); - } else { - // starts in strip 1 and ends in gap - strip1->blur(amount, indexFrom, strip1->last()); - gapStrip->blur(amount, 0, indexTo - strip1->size()); + strip2->blur(amount, strip2->first(), toStrip2(indexTo)); } - } else if (strip2->isInRange(indexFrom - strip1->size() - gap)) { - // starts and ends in strip 2 - strip2->blur(amount, indexFrom - strip1->size() - gap, indexTo - strip1->size() - gap); - } else { + } else if (isInGap(indexFrom)) { // starts in gap and ends in strip 2 - gapStrip->blur(amount, indexFrom - strip1->size(), gap - 1); - strip2->blur(amount, 0, indexTo - strip1->size() - gap); + gapStrip->blur(amount, toGap(indexFrom), gapStrip->last()); + strip2->blur(amount, strip2->first(), toStrip2(indexTo)); + } else if (isInStrip2(indexFrom)) { + // starts and ends in strip 2 + strip2->blur(amount, toStrip2(indexFrom), toStrip2(indexTo)); } } @@ -174,24 +189,29 @@ CRGB JoinedStrip::shiftUp(CRGB in) { } CRGB JoinedStrip::shiftUp(uint16_t indexFrom, uint16_t indexTo, CRGB in) { - if (strip1->isInRange(indexFrom)) { - if (strip1->isInRange(indexTo)) { + if (isInStrip1(indexFrom)) { + if (isInStrip1(indexTo)) { // starts and ends in strip 1 - return strip1->shiftUp(indexFrom, indexTo, in); - } else if (strip2->isInRange(indexTo - strip1->size() - gap)) { - // starts in strip 1 and ends in strip 2 - return strip2->shiftUp(strip2->first(), indexTo - strip1->size(), gapStrip->shiftUp(strip1->shiftUp(indexFrom, strip1->last(), in))); - } else { + return strip1->shiftUp(toStrip1(indexFrom), toStrip1(indexTo), in); + } else if (isInGap(indexTo)) { // starts in strip 1 and ends in gap - return gapStrip->shiftUp(0, indexTo - strip1->size(), strip1->shiftUp(indexFrom, strip1->last(), in)); + CRGB fromStrip1 = strip1->shiftUp(toStrip1(indexFrom), strip1->last(), in); + return gapStrip->shiftUp(gapStrip->first(), toGap(indexTo), fromStrip1); + } else if (isInStrip2(indexTo)) { + // starts in strip 1 and ends in strip 2 + CRGB fromStrip1 = strip1->shiftUp(toStrip1(indexFrom), strip1->last(), in); + CRGB fromGap = gapStrip->shiftUp(fromStrip1); + return strip2->shiftUp(strip2->first(), toStrip2(indexTo), fromGap); } - } else if (strip2->isInRange(indexFrom - strip1->size() - gap)) { - // starts and ends in strip 2 - return strip2->shiftUp(indexFrom - strip1->size() - gap, indexTo - strip1->size() - gap, in); - } else { + } else if (isInGap(indexFrom)) { // starts in gap and ends in strip 2 - return strip2->shiftUp(0, indexTo - strip1->size() - gap, gapStrip->shiftUp(indexFrom - strip1->size(), gap - 1, in)); + CRGB fromGap = gapStrip->shiftUp(toGap(indexFrom), gapStrip->last(), in); + return strip2->shiftUp(strip2->first(), toStrip2(indexTo), fromGap); + } else if (isInStrip2(indexFrom)) { + // starts and ends in strip 2 + return strip2->shiftUp(toStrip2(indexFrom), toStrip2(indexTo), in); } + return CRGB::Black; } CRGB JoinedStrip::shiftDown(CRGB in) { @@ -199,24 +219,29 @@ CRGB JoinedStrip::shiftDown(CRGB in) { } CRGB JoinedStrip::shiftDown(uint16_t indexFrom, uint16_t indexTo, CRGB in) { - if (strip1->isInRange(indexFrom)) { - if (strip1->isInRange(indexTo)) { + if (isInStrip1(indexFrom)) { + if (isInStrip1(indexTo)) { // starts and ends in strip 1 - return strip1->shiftDown(indexFrom, indexTo, in); - } else if (strip2->isInRange(indexTo - strip1->size() - gap)) { - // starts in strip 1 and ends in strip 2 - return strip1->shiftDown(indexFrom, strip1->last(), gapStrip->shiftDown(strip2->shiftDown(strip2->first(), indexTo - strip1->size(), in))); - } else { + return strip1->shiftDown(toStrip1(indexFrom), toStrip1(indexTo), in); + } else if (isInGap(indexTo)) { // starts in strip 1 and ends in gap - return strip1->shiftDown(indexFrom, strip1->last(), gapStrip->shiftDown(0, indexTo - strip1->size(), in)); + CRGB fromGap = gapStrip->shiftDown(gapStrip->first(), toGap(indexTo), in); + return strip1->shiftDown(toStrip1(indexFrom), strip1->last(), fromGap); + } else if (isInStrip2(indexTo)) { + // starts in strip 1 and ends in strip 2 + CRGB fromStrip2 = strip2->shiftDown(strip2->first(), toStrip2(indexTo), in); + CRGB fromGap = gapStrip->shiftDown(fromStrip2); + return strip1->shiftDown(toStrip1(indexFrom), strip1->last(), fromGap); } - } else if (strip2->isInRange(indexFrom - strip1->size() - gap)) { - // starts and ends in strip 2 - return strip2->shiftDown(indexFrom - strip1->size() - gap, indexTo - strip1->size() - gap, in); - } else { + } else if (isInGap(indexFrom)) { // starts in gap and ends in strip 2 - return gapStrip->shiftDown(indexFrom - strip1->size(), gap - 1, strip2->shiftDown(0, indexTo - strip1->size() - gap, in)); + CRGB fromStrip2 = strip2->shiftDown(strip2->first(), toStrip2(indexTo), in); + return gapStrip->shiftDown(toGap(indexFrom), gapStrip->last(), fromStrip2); + } else if (isInStrip2(indexFrom)) { + // starts and ends in strip 2 + return strip2->shiftDown(toStrip2(indexFrom), toStrip2(indexTo), in); } + return CRGB::Black; } void JoinedStrip::paint(CRGB color, bool add) { @@ -226,40 +251,38 @@ void JoinedStrip::paint(CRGB color, bool add) { } bool JoinedStrip::paint(int16_t index, CRGB color, bool add) { - if (strip1->isInRange(index)) { - return strip1->paint(index, color, add); - } else if (strip2->isInRange(index - strip1->size() - gap)) { - return strip2->paint(index - strip1->size() - gap, color, add); - } else { - return gapStrip->paint(index - strip1->size(), color, add); - // return false; + if (isInStrip1(index)) { + return strip1->paint(toStrip1(index), color, add); + } else if (isInGap(index)) { + return gapStrip->paint(toGap(index), color, add); + } else if (isInStrip2(index)) { + return strip2->paint(toStrip2(index), color, add); } + return false; } bool JoinedStrip::paint(int16_t indexFrom, int16_t indexTo, CRGB color, bool add) { - if (strip1->isInRange(indexFrom)) { - if (strip1->isInRange(indexTo)) { + if (isInStrip1(indexFrom)) { + if (isInStrip1(indexTo)) { // starts and ends in strip 1 - return strip1->paint(indexFrom, indexTo, color, add); - } else if (strip2->isInRange(indexTo - strip1->size() - gap)) { + return strip1->paint(toStrip1(indexFrom), toStrip1(indexTo), color, add); + } else if (isInStrip2(indexTo)) { // starts in strip 1 and ends in strip 2 - bool s1 = strip1->paint(indexFrom, strip1->last(), color, add); + bool s1 = strip1->paint(toStrip1(indexFrom), strip1->last(), color, add); gapStrip->paint(color, add); - bool s2 = strip2->paint(strip2->first(), indexTo - strip1->size(), color, add); + bool s2 = strip2->paint(strip2->first(), toStrip2(indexTo), color, add); return s1 || s2; - } else { - // starts in strip 1 and ends in gap - gapStrip->paint(0, indexTo - strip1->size(), color, add); - return strip1->paint(indexFrom, strip1->last(), color, add); } - } else if (strip2->isInRange(indexFrom - strip1->size() - gap)) { - // starts and ends in strip 2 - return strip2->paint(indexFrom - strip1->size() - gap, indexTo - strip1->size() - gap, color, add); - } else { + } else if (isInGap(indexFrom)) { // starts in gap and ends in strip 2 - gapStrip->paint(indexFrom - strip1->size(), gap - 1, color, add); - return strip2->paint(0, indexTo - strip1->size() - gap, color, add); + bool sg = gapStrip->paint(toGap(indexFrom), gapStrip->last(), color, add); + bool s2 = strip2->paint(strip2->first(), toStrip2(indexTo), color, add); + return sg || s2; + } else if (isInStrip2(indexFrom)) { + // starts and ends in strip 2 + return strip2->paint(toStrip2(indexFrom), toStrip2(indexTo), color, add); } + return false; } bool JoinedStrip::paintNormalized(float position, CRGB color, bool add) { diff --git a/src/JoinedStrip.h b/src/JoinedStrip.h index bd02a8b..1b68386 100644 --- a/src/JoinedStrip.h +++ b/src/JoinedStrip.h @@ -12,6 +12,12 @@ class JoinedStrip : public Strip { CRGB *buffer; CRGBSet *bufferSet; StatefulStrip *gapStrip; + bool isInStrip1(int16_t index); + bool isInGap(int16_t index); + bool isInStrip2(int16_t index); + int16_t toStrip1(int16_t index); + int16_t toGap(int16_t index); + int16_t toStrip2(int16_t index); public: JoinedStrip(Strip *strip, Strip *strip2, uint16_t gap = 0);