Karl Zweimüller / TextLCDScroll

Dependents:   RF22_MAX_test_Send

Revision:
3:1d7a7a249647
Parent:
2:66723c542cef
Child:
4:92a07dbc9222
--- 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;
         }
     }
+
 }
-