Extendes Version of TextLCD which scrolls oversized lines.

Dependents:   RF22_MAX_test_Send

Extended version of TextLCD, which can scoll oversized lines. Uses a ticker!

Files at this revision

API Documentation at this revision

Comitter:
charly
Date:
Tue Mar 27 20:13:41 2012 +0000
Child:
1:d7cc0e2a55ba
Commit message:
Initial version of TextLCDScroll

Changed in this revision

TextLCDScroll.cpp Show annotated file Show diff for this revision Revisions of this file
TextLCDScroll.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCDScroll.cpp	Tue Mar 27 20:13:41 2012 +0000
@@ -0,0 +1,106 @@
+#include "TextLCDScroll.h"
+#include "mbed.h"
+
+#include <string>
+using namespace std;
+
+    TextLCDScroll::TextLCDScroll(PinName rs, PinName e, PinName d4, PinName d5,
+                  PinName d6, PinName d7, TextLCD::LCDType type): TextLCD(rs,e,d4,d5,d6,d7,type) {
+
+        _direction = new int[rows()];
+        _actPos = new int[rows()];
+        _stringArray = new string[rows()];
+        for (int i=0; i<rows(); i++) {
+            _direction[i]=1;
+            _actPos[i] = 0;
+            _stringArray[i] = "";
+        }
+        _spaces = string(columns(),' ');
+        //_mode = leftright;
+        _mode = left;
+
+        setSpeed(5);
+
+    }
+
+
+    bool TextLCDScroll::setLine( int Line, string str) {
+        if (Line >= 0 && Line < rows()) {
+            if ((str.length() > columns()) && (_mode == left))
+                _stringArray[Line] = _spaces + str + _spaces;
+            else
+                _stringArray[Line] = str;
+
+            // be sure to refresh the display
+            cls();
+            // start at beginning again
+            _actPos[Line] = 0;
+            _direction[Line] =1;
+            return(true);
+        } else {
+            return (false);
+        }
+    }
+
+    bool TextLCDScroll::setSpeed( int speed) {
+        if ((speed >= 0.1) && (speed <= 10)) {
+            tick.detach();
+            if (_mode == leftright)
+                tick.attach(this,&TextLCDScroll::ScrollRightLeft, 1.0/speed);
+            else
+                tick.attach(this,&TextLCDScroll::ScrollLeft, 1.0/speed);
+            return(true);
+        } else {
+            return(false);
+        }
+    }
+
+    bool TextLCDScroll::setScrollMode( ScrollModes mode) {
+        _mode = mode;
+        return(true);
+    }
+
+    void TextLCDScroll::ScrollRightLeft() {
+        int i;
+        for (i=0; i<rows(); i++) {
+            locate(0,i);
+            if (_stringArray[i].length() > columns()) {
+                printf(_stringArray[i].substr(_actPos[i],columns()).c_str());
+                if (_direction[i] == 1) {
+                    if (_stringArray[i].length() > _actPos[i]+columns()) {
+                        _actPos[i] += 1;
+                    } else {
+                        _direction[i] = -1;
+                    }
+                } else {
+                    if (_actPos[i] > 0) {
+                        _actPos[i] -= 1;
+                    } else {
+                        _direction[i]=1;
+                    }
+                }
+            } else {
+                printf(_stringArray[i].c_str());
+            }
+        }
+    }
+
+    void TextLCDScroll::ScrollLeft() {
+        int i;
+        for (i=0; i<rows(); i++) {
+            locate(0,i);
+            if (_stringArray[i].length() > columns()) {
+                printf(_stringArray[i].substr(_actPos[i],columns()).c_str());
+                if (_direction[i] == 1) {
+                    if (_stringArray[i].length() > _actPos[i]+columns()) {
+                        _actPos[i] += 1;
+                    } else {
+                        _actPos[i]=0;
+                    }
+                }
+            } else {
+                printf(_stringArray[i].c_str());
+            }
+        }
+    }
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCDScroll.h	Tue Mar 27 20:13:41 2012 +0000
@@ -0,0 +1,99 @@
+/* mbed Library for scrolling text in Text-LCDs based on the Class TextLCD
+ * Copyright by Karl Zweimueller
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+*/
+
+#ifndef TEXTLCDSCROLL_H
+#define TEXTLCDSCROLL_H
+
+#include "TextLCD.h"
+
+#include <string>
+using namespace std;
+
+/** TextLCDScroll class which handles horizontal scrolling text if it doesn't fit in one line, based on class TextLCD
+  *
+  * Example:
+  * @code
+  * #include "TextLCDScroll.h"
+  * #include "mbed.h"
+  *
+  * TextLCDScroll lcd(p30, p29, p28, p27, p26, p25, TextLCD::LCD16x2); // rs, e, d0-d3
+  *
+  * int main() {
+  *
+  *     lcd.cls();
+  *     lcd.setLine(0,"TextLCDScroll!");
+  *     lcd.setLine(1,"This line can be rather long. The quick brown fox jumps over the lazy dog.");
+  * }
+  * @endcode
+*/
+class TextLCDScroll: public TextLCD {
+public:
+
+    /** How should we scroll horizontal in one line
+      */
+    enum ScrollModes {
+        leftright,    /** Scroll left ande the right */
+        left          /** Scroll only left - default */
+    };
+
+    /** Create a TextLCDScroll Object with pins and Type from TextLCD
+      *
+      * @param rs    Instruction/data control line
+      * @param e     Enable line (clock)
+      * @param d4-d7 Data lines for using as a 4-bit interface
+      * @param type  Sets the panel size/addressing mode (default = LCD16x2)
+      */
+    TextLCDScroll(PinName rs, PinName e, PinName d4, PinName d5,
+                  PinName d6, PinName d7, TextLCD::LCDType type);
+
+
+    /** Display a string str on line Line. Replaces old text on Line.
+      * @param  Line    Line, where text should be displayed. first Line=0
+      * @param  str     the string to display
+      */
+    bool setLine( int Line, string str);
+
+    /** Set the speed, how fast to scroll the text in Characters per Seconds
+     * @param  speed Speed for scrolling in Characters per Second. Range 0.1 ... 10
+     */
+    bool setSpeed( int speed);
+
+    /** Set the Scroll-Mode. Deault "left"
+      * @param  mode    a valid mode. See ScrollModes
+      */
+    bool setScrollMode( ScrollModes mode);
+
+private:
+
+    void ScrollRightLeft();
+
+    void ScrollLeft();
+
+    Ticker tick;
+    int* _direction;
+    int* _actPos;
+    string* _stringArray;
+    string _spaces;
+    ScrollModes _mode;
+};
+
+#endif
\ No newline at end of file