Karl Zweimüller / TextLCDScroll

Dependents:   RF22_MAX_test_Send

TextLCDScroll.cpp

Committer:
charly
Date:
2012-04-24
Revision:
2:66723c542cef
Parent:
0:0ae9963c4e06
Child:
3:1d7a7a249647

File content as of revision 2:66723c542cef:

#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);

    // reduce interrupt level for the ticker timers. so other things (RF22 ) come first
    NVIC_SetPriority(TIMER3_IRQn, 10);

}


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());
        }
    }
}