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 12 19:40:29 2013 +0000
Parent:
2:66723c542cef
Child:
4:92a07dbc9222
Commit message:
TextLCDScroll changed to c-strings and removed printf from ISR

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
--- a/TextLCDScroll.cpp	Tue Apr 24 06:18:21 2012 +0000
+++ b/TextLCDScroll.cpp	Tue Mar 12 19:40:29 2013 +0000
@@ -1,41 +1,48 @@
+#include <string.h>
 #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) {
+                             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);
+    cls();
 
     // reduce interrupt level for the ticker timers. so other things (RF22 ) come first
-    NVIC_SetPriority(TIMER3_IRQn, 10);
+    //NVIC_SetPriority(TIMER3_IRQn, 10);
 
 }
 
+void TextLCDScroll::cls()
+{
+    for (int i=0; i<rows(); i++) {
+        _direction[i]=1;
+        _actPos[i] = 0;
+        setLine(i,"");
+        TextLCD::cls();
+    }
+}
 
-bool TextLCDScroll::setLine( int Line, string str) {
+bool TextLCDScroll::setLine( int Line, char  *str)
+{
     if (Line >= 0 && Line < rows()) {
-        if ((str.length() > columns()) && (_mode == left))
-            _stringArray[Line] = _spaces + str + _spaces;
-        else
-            _stringArray[Line] = str;
-
+        // free the old memory
+        if (line[Line] != NULL) {
+            free(line[Line]);
+        }
+        // malloc new space for string
+        line[Line] = (char*)malloc((strlen(str)+1)*sizeof(char));
+        //copy the string
+        strcpy(line[Line], str);
         // be sure to refresh the display
-        cls();
+        TextLCD::cls();
         // start at beginning again
         _actPos[Line] = 0;
         _direction[Line] =1;
@@ -45,7 +52,8 @@
     }
 }
 
-bool TextLCDScroll::setSpeed( int speed) {
+bool TextLCDScroll::setSpeed( int speed)
+{
     if ((speed >= 0.1) && (speed <= 10)) {
         tick.detach();
         if (_mode == leftright)
@@ -58,52 +66,88 @@
     }
 }
 
-bool TextLCDScroll::setScrollMode( ScrollModes mode) {
+bool TextLCDScroll::setScrollMode( ScrollModes mode)
+{
     _mode = mode;
     return(true);
 }
 
-void TextLCDScroll::ScrollRightLeft() {
-    int i;
+void TextLCDScroll::ScrollRightLeft()
+{
+    int i, j;
+    //all rows
     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;
+        // all columns
+        for (j=0; j<columns(); j++) {
+
+            locate(j,i);
+            // is string shorter than width of display
+            if (strlen(line[i]) < columns()) {
+                if (j < strlen(line[i])) {
+                    putc(line[i][j]);
                 } else {
-                    _direction[i] = -1;
+                    putc(' ');
                 }
-            } else {
-                if (_actPos[i] > 0) {
-                    _actPos[i] -= 1;
+            } else { // sting is longer -> scroll
+                if ((_actPos[i]+j < columns()) || (_actPos[i]+j >= columns()+strlen(line[i]))) {
+                    putc(' ');
                 } else {
-                    _direction[i]=1;
+                    putc(line[i][_actPos[i]-columns()+j]);
                 }
             }
-        } else {
-            printf(_stringArray[i].c_str());
         }
+        // shift start-position of string
+        // left = ++
+        if (_direction[i] == 1) {
+            if (_actPos[i] < (strlen(line[i])+(columns()))) {
+                _actPos[i]++;
+            } else {
+                _direction[i] = 0; // reverse direction
+                _actPos[i]--;
+            }
+        } else { //right = --
+            if (_actPos[i] > 1 ) {
+                _actPos[i]--;
+            } else {
+                _direction[i] = 1; // reverse direction
+                _actPos[i]++;
+            }
+        }
+
     }
+
 }
 
-void TextLCDScroll::ScrollLeft() {
-    int i;
+void TextLCDScroll::ScrollLeft()
+{
+    int i, j;
+    //all rows
     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;
+        // all columns
+        for (j=0; j<columns(); j++) {
+
+            locate(j,i);
+            // is string shorter than width of display
+            if (strlen(line[i]) < columns()) {
+                if (j < strlen(line[i])) {
+                    putc(line[i][j]);
                 } else {
-                    _actPos[i]=0;
+                    putc(' ');
+                }
+            } else { // sting is longer -> scroll
+                if ((_actPos[i]+j < columns()) || (_actPos[i]+j >= columns()+strlen(line[i]))) {
+                    putc(' ');
+                } else {
+                    putc(line[i][_actPos[i]-columns()+j]);
                 }
             }
+        }
+        // shift start-position of string
+        if (_actPos[i] < (strlen(line[i])+(columns()))) {
+            _actPos[i]++;
         } else {
-            printf(_stringArray[i].c_str());
+            _actPos[i]=0;
         }
     }
+
 }
-
--- a/TextLCDScroll.h	Tue Apr 24 06:18:21 2012 +0000
+++ b/TextLCDScroll.h	Tue Mar 12 19:40:29 2013 +0000
@@ -25,8 +25,6 @@
 
 #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
   *
@@ -68,9 +66,14 @@
 
     /** Display a string str on line Line. Replaces old text on this Line.
       * @param  Line    Line, where text should be displayed. first Line=0
-      * @param  str     the string to display
+      * @param  *str    the string to display (c-string)
       */
-    bool setLine( int Line, string str);
+    bool setLine( int Line, char *str);
+    
+    /* Clear the display
+    *
+    */
+    void cls();
 
     /** Set the speed, how fast to scroll the text in Characters per Second
      * @param  speed Speed for scrolling in Characters per Second. Range 0.1 ... 10
@@ -89,13 +92,13 @@
     void ScrollLeft();
 
     Ticker tick;
-    string* _stringArray;
-    string _spaces;
     ScrollModes _mode;
 
     // these are changed in interrupt-routine!
     volatile int* _direction;
     volatile int* _actPos;
+    
+    char* line[99];
 
 };