Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create ability to check scrolling layer's position and possibly be able to pause/resume it #182

Open
prabbit237 opened this issue Feb 11, 2024 · 2 comments

Comments

@prabbit237
Copy link

prabbit237 commented Feb 11, 2024

Exposing the text width and scroll position of a scrolling layer would be nice to have.

template<typename RGB, unsigned int optionFlags>
unsigned int SMLayerScrolling<RGB, optionFlags>::getTextWidth() {
    return textWidth;
}

template<typename RGB, unsigned int optionFlags>
int SMLayerScrolling<RGB, optionFlags>::getPosition() {
    return scrollPosition;
}

Use-case:

...
indexed.fillScreen(BLACK);
indexed.swapBuffers(true);
scroll.start("12:04pm,1);

// of course this requires that the width of the string being scrolled is less than the matrix size
while (scroll.getPosition()<MatrixWidth-scroll.getTextWidth()/2)
   {}
int pos=scroll.getPosition(); 
scroll.stop();
indexed.drawString(pos,10,WHITE,"12:04pm");
indexed.swapBuffers(true);
delay(1000);
// yes, a delay of exactly one second would cause clock drift. It'd need something like 
// xTaskDelayUntill() or elapsedMillis() to keep it timed correctly.
indexed.fillScreen(BLACK);
indexed.swapBuffers(true);
scroll.setStartOffsetFromLeft(pos);
scroll.start("12:04pm,1);
//repeat the above with "12:05pm", etc

That would make the time scroll out to centered on the screen and then "freeze." A minute later, the time starts scrolling again to the right and disappears.

Adding scroll.pause() and scroll.resume() functions as well would make the above sample even easier as there'd be no mucking about with the indexed layer and saving the position, etc. It would be just:

scroll.start("12:04pm,1);
while (scroll.getPosition()<)MatrixWidth-scroll.getTextWidth())/2)
   {}
scroll.pause();       // "12:04pm" has found its Chi and is now centered and stilled in the cosmos
delay(1000);
scroll.resume();     // "12:04pm" continues its journey into oblivion to be lost forever
while (scroll.getStatus())
   {}
//repeat above with "12:05pm", etc.

Other use-cases would include a sign that scrolls across "Now serving #12. Now serving #12" and as each "#12" reaches the middle, and indexed later above it prints "#12" in a different color, pauses, clears it and then resumes the scroll. Thus making it more noticeable via the blink (written as I look at my own ticket with # 4633668346 on it, sigh and try to remember if I still have that pillow in the car since I'm going to be here for a while.)

@prabbit237 prabbit237 changed the title create ability to check scrollinglayer's position. create ability to check scrolling layer's position and possibly be able to pause/resume it Feb 11, 2024
@prabbit237
Copy link
Author

Actually, a "pause/resume" is already implemented (even if it wasn't intended to be such.) A quick test showed that the following code will pause the scrolling for 10 seconds simply by changing the speed to 0.

  ...
  scrollingLayer1.setSpeed(15);
  scrollingLayer1.start("SmartMatrix & FastLED", 2);
  while (scrollingLayer1.getStatus())
  {
    delay(10000);
    scrollingLayer1.setSpeed(0);
    delay(10000);
    scrollingLayer1.setSpeed(15);
  }

"Don't call it a bug and fix it. Leave it as-is and call it a feature!"
Me: Hey, support, my computer keeps getting a BSOD! What gives? Fix this issue!!!11!!!!!1!!!!!!
MS Support: It's not a bug. It's a great new feature in Windows that encourages you to take a break from work every hour or so.

@prabbit237
Copy link
Author

This was the code I came up with for the time display on my project where I'm using the two suggested new functions of getPosition() and getTextWidth().

void xTaskDisplayTime(void *pvParameters)
{
    // The task is created before matrix.begin to allow for any allocation of stack space, etc.
    // So the pertinent section of code in setup(void) is:
    //
    //  xTaskCreate(
    //      xTaskDisplayTime,
    //      "DisplayTime",
    //      10000,
    //      NULL,
    //      1,
    //      NULL);

    // matrix.addLayer(&backgroundLayer);
    // matrix.addLayer(&timeScrollLayer);

    // matrix.begin();
    // matrixIsStarted = true;

    // Then this while() simply holds it here till matrix.begin() has run so that it doesn't try to use the matric too early.
    while (!matrixIsStarted)
        vTaskDelay(1);

    byte lastSecond = 60;
    timeScrollLayer.setMode(wrapForward);
    timeScrollLayer.setOffsetFromTop(3);
    timeScrollLayer.setSpeed(20);
    timeScrollLayer.setFont(font8x13);
    String timeNow;

    for (;;)
    {
        if (lastSecond!=tz.second())
            {
                lastSecond = tz.second();
                // Let's change the colors as the seconds pass.
                timeScrollLayer.setColor({0xff - (lastSecond * 4), 0xff, 0x0F + (lastSecond * 4)});
                if (!tz.second())
                {
                    // Make sure any previous time has scrolled back off first.
                    // This also means at boot, it'll not show the time for 
                    // up to 60 seconds :(
                    timeScrollLayer.setSpeed(20);
                    while (timeScrollLayer.getStatus())
                        vTaskDelay(25);

                    //now let's scroll in the new time
                    timeNow = tz.dateTime("g:ia");
                    timeScrollLayer.start((char *)&timeNow[0], 1);
                    //self-centering no matter what font is used
                    while (timeScrollLayer.getPosition()>(kMatrixWidth - timeScrollLayer.getTextWidth())/2)
                        //need to keep this delay short to avoid overshooting the target
                        vTaskDelay(10);
                    timeScrollLayer.setSpeed(0);
                }
            }
        // Wait for the next cycle.
        vTaskDelay(50);
        // Perform action here. xWasDelayed value can be used to determine
        // whether a deadline was missed if the code here took too long.
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant