Shifting One Array Into/Onto Another Fixed Size Array From Left Or Right

207 views Asked by At

I've been thinking about this for a while and keep falling back on nested For Loops and a bunch of If/Thens...

I am trying to create a text scrolling/slide-in effect for a LCD Character Display on a single line. I would like to be able to set the input start point and end point into the array. For example, I have a base byte array size of 16 and want to start to shift an array onto/into it.

Output would be something like this with each line being an iteration of the array which is sent to the display:


_______________ <-start with blank array[16]
_____________H_ <-start to shift in at specified start position e.g. [14]
____________He_
___________Hel_
__________Hell_
_________Hello_
________Hello__
_______Hello___
______Hello____
_____Hello_____ <-end shifting as specified position e.g. [5]

Conversely, I would like to be able to shift this out like so:
_____Hello_____ <-Begining Array <-This needs to be created
____Hello______
___Hello_______
__Hello________
_Hello_________
_ello__________
_llo___________
_lo____________
_o_____________
_______________

Is there an efficient/native way to do this? I'm using NetMF so there are some limitations to the framework.

footnote: I suppose this could be done by directly manipulating the string to be displayed then I convert it to a byte array to send to the display but I think this might be slower.

1

There are 1 answers

3
Fabjan On BEST ANSWER
  class Program2
    {
        static void AnimateString(int leftEdge, int RightEdge, int length, char fillWith, string text, int delay_ms)
        {
            int x = RightEdge;
            string line = "";
            int count = 0;
            string leftBorder = "";            

        while (true)
        {
            Console.CursorLeft = 0;
            Console.Write(new String(fillWith, length));
            Console.CursorLeft = 0;

            if (x < leftEdge) ++count;                

            leftBorder = new String(fillWith, x - 1 > leftEdge ? x - 1 : leftEdge);                

            line = leftBorder + text.Substring(
                    x > leftEdge - 1? 0 : count, 
                    x > leftEdge - 1 ? (x + text.Length > RightEdge ? RightEdge - x : text.Length) : text.Length - count);
            Console.Write(line);
            Thread.Sleep(delay_ms);
            --x;
            if (count >= text.Length) { x = RightEdge; count = 0; line = ""; }
        }       
    }

    static void Main()
    {
        string blank = new String('-', 32);
        string text = "Hello world!";
        AnimateString(4, 20, 24, '-', "Hello world", 100);

        Console.ReadKey();
    }
}

Check this line of code:

line = leftBorder + text.Substring(
        x > leftEdge - 1? 0 : count, 
        x > leftEdge - 1 ? (x + text.Length > RightEdge ? RightEdge - x : text.Length) : text.Length - count);

We already created the 'left part' of our string - line of text starts with 0 and ends either with position of x or left border (in case when x - leftEdge is less than zero. Now it's time to calculate: 1) what index in text ('hello world') should we take for SubString method and 2) how many characters we'd like to extract.

1) depends on : has our x position reached the left border ? no) we start from index 0
yes) we start from count value. Count - is our field that we use to calculate the left shift inside our text string

2) this depends on position of x against right border. If our x position + text string.length are not exceeding the borders we'd take that string. In other case we should count how many characters are within the limits and extract them. Also if x - leftEdge < 0 and we started from index of count - we'd like to subtract count value from string length so we get only the remaining part of a string and not exceeding it.

enter image description here