Extendes Version of TextLCD which scrolls oversized lines.

Dependents:   RF22_MAX_test_Send

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCDScroll.cpp Source File

TextLCDScroll.cpp

00001 #include <string.h>
00002 #include "TextLCDScroll.h"
00003 #include "mbed.h"
00004 
00005 TextLCDScroll::TextLCDScroll(PinName rs, PinName e, PinName d4, PinName d5,
00006                              PinName d6, PinName d7, TextLCD::LCDType type): TextLCD(rs,e,d4,d5,d6,d7,type)
00007 {
00008 
00009     _direction = new int[rows()];
00010     _actPos = new int[rows()];
00011 
00012     //_mode = leftright;
00013     _mode = left;
00014 
00015     //set Speed and start Ticker
00016     setSpeed(5);
00017     
00018     cls();
00019 
00020     // reduce interrupt level for the ticker timers. so other things (RF22 ) come first
00021     //NVIC_SetPriority(TIMER3_IRQn, 10);
00022 
00023 }
00024 
00025 void TextLCDScroll::cls()
00026 {
00027     for (int i=0; i<rows(); i++) {
00028         _direction[i]=1;
00029         _actPos[i] = 0;
00030         setLine(i,"");
00031         TextLCD::cls();
00032     }
00033 }
00034 
00035 bool TextLCDScroll::setLine( int Line, char  *str)
00036 {
00037     if (Line >= 0 && Line < rows()) {
00038         //stop interrupts
00039         tick.detach();
00040         // free the old memory
00041         //if (line[Line] != NULL) {
00042         //    free(line[Line]);
00043         //}
00044         // malloc new space for string
00045         //line[Line] = (char*)malloc((strlen(str)+1)*sizeof(char));
00046         //Realocate memory
00047         line[Line] = (char*)realloc(line[Line], (strlen(str)+1)*sizeof(char));
00048         //copy the string
00049         strcpy(line[Line], str);
00050         // be sure to refresh the display
00051         TextLCD::cls();
00052         // start at beginning again
00053         _actPos[Line] = 0;
00054         _direction[Line] =1;
00055         startTicker();
00056         return(true);
00057     } else {
00058         return (false);
00059     }
00060 }
00061 
00062 void TextLCDScroll::startTicker(){
00063         if (_mode == leftright)
00064             tick.attach(this,&TextLCDScroll::ScrollRightLeft, 1.0/_speed);
00065         else
00066             tick.attach(this,&TextLCDScroll::ScrollLeft, 1.0/_speed);
00067 }
00068 
00069 bool TextLCDScroll::setSpeed( int speed)
00070 {
00071     if ((speed >= 0.1) && (speed <= 10)) {
00072         tick.detach();
00073         _speed = speed;
00074         startTicker();
00075         return(true);
00076     } else {
00077         return(false);
00078     }
00079 }
00080 
00081 bool TextLCDScroll::setScrollMode( ScrollModes mode)
00082 {
00083     _mode = mode;
00084     return(true);
00085 }
00086 
00087 void TextLCDScroll::ScrollRightLeft()
00088 {
00089     int i, j;
00090     //all rows
00091     for (i=0; i<rows(); i++) {
00092         // all columns
00093         for (j=0; j<columns(); j++) {
00094 
00095             locate(j,i);
00096             // is string shorter than width of display
00097             if (strlen(line[i]) <= columns()) {
00098                 if (j < strlen(line[i])) {
00099                     putc(line[i][j]);
00100                 } else {
00101                     putc(' ');
00102                 }
00103             } else { // sting is longer -> scroll
00104                 if ((_actPos[i]+j < columns()) || (_actPos[i]+j >= columns()+strlen(line[i]))) {
00105                     putc(' ');
00106                 } else {
00107                     putc(line[i][_actPos[i]-columns()+j]);
00108                 }
00109             }
00110         }
00111         // shift start-position of string
00112         // left = ++
00113         if (_direction[i] == 1) {
00114             if (_actPos[i] < (strlen(line[i])+(columns()))) {
00115                 _actPos[i]++;
00116             } else {
00117                 _direction[i] = 0; // reverse direction
00118                 _actPos[i]--;
00119             }
00120         } else { //right = --
00121             if (_actPos[i] > 1 ) {
00122                 _actPos[i]--;
00123             } else {
00124                 _direction[i] = 1; // reverse direction
00125                 _actPos[i]++;
00126             }
00127         }
00128 
00129     }
00130 
00131 }
00132 
00133 void TextLCDScroll::ScrollLeft()
00134 {
00135     int i, j;
00136     //all rows
00137     for (i=0; i<rows(); i++) {
00138         // all columns
00139         for (j=0; j<columns(); j++) {
00140 
00141             locate(j,i);
00142             // is string shorter than width of display
00143             if (strlen(line[i]) <= columns()) {
00144                 if (j < strlen(line[i])) {
00145                     putc(line[i][j]);
00146                 } else {
00147                     putc(' ');
00148                 }
00149             } else { // sting is longer -> scroll
00150                 if ((_actPos[i]+j < columns()) || (_actPos[i]+j >= columns()+strlen(line[i]))) {
00151                     putc(' ');
00152                 } else {
00153                     putc(line[i][_actPos[i]-columns()+j]);
00154                 }
00155             }
00156         }
00157         // shift start-position of string
00158         if (_actPos[i] < (strlen(line[i])+(columns()))) {
00159             _actPos[i]++;
00160         } else {
00161             _actPos[i]=0;
00162         }
00163     }
00164 
00165 }