Conversions for the LCD display in FRDM-KL46Z

Dependents:   eem202a_display eem202a_resolutedreamer_1

convert.cpp

Committer:
Waldek
Date:
2014-04-19
Revision:
1:cf83568dc17a
Parent:
0:ca69bce3284f
Child:
2:9467805cb02b

File content as of revision 1:cf83568dc17a:

#include "convert.h"

Convert::Convert()
{
    this->prepare();
   
    return;
}

void Convert::prepare(void)
{
    this->blink(-1); 
    this->clear();
    this->DP1(0);
    this->DP2(0);
    this->DP3(0);
    this->Colon(0);
    this->Home();
    
    return;
}

bool Convert::display_digits(unsigned int number, bool negate)
{
    register unsigned short int c1 = ((unsigned int) number / 1000);
    register unsigned short int c2 = ((unsigned int) number / 100) - (c1 * 10);
    register unsigned short int c3 = ((unsigned int) number / 10) - ((c1 *100) + (c2 *10));
    register unsigned short int c4 = number - ((c1 *1000) + (c2 *100) + (c3 *10));
    
    this->blink(-1); 
    this->Home();
    if (negate)
        this->putc('-');
    else
        this->putc('0'+c1);
    this->putc('0'+c2);
    this->putc('0'+c3);
    this->putc('0'+c4);
    
    return false;
}

bool Convert::display(unsigned int number)
{
    if (number > 9999)
    {
        this->Home();
        this->blink(2); 
        this->putc(' ');
        this->putc('O');
        this->putc('F');
        this->putc('L');
        return true;
    }
    else
    {
        this->display_digits(number); 
    }
    return false;
}

bool Convert::display(int number)
{
    bool negate = (number < 0);
    if (negate)
    {
        if (number < -999)
        {
            this->Home();
            this->blink(3); 
            this->putc('-');
            this->putc('O');
            this->putc('F');
            this->putc('L');
            return true;
        }
        else
        {
            return this->display_digits ((unsigned int) -number, negate);
        }
    }
    else
    {
        return this->display ((unsigned int) number);
    }
}

bool Convert::display(double number)
{
// >=10000   OVL
// >=1000   1234
    if (number >= 1000.)
    {
        this->DP1(false);
        this->DP2(false);
        this->DP3(false);
        return this->display((unsigned int) (number));
    }
// >=100    123.4
    else if (number >= 100.)
    {
        this->DP1(false);
        this->DP2(false);
        this->DP3(true);
        return this->display((unsigned int) (number*10.));
    }
// >=10     12.34
    else if (number >= 10.)
    {
        this->DP1(false);
        this->DP2(true);
        this->DP3(false);
        return this->display((unsigned int) (number*100.));
    }
// >=0      1.234
    else if (number >= 0.)
    {
        this->DP1(true);
        this->DP2(false);
        this->DP3(false);
        return this->display((int) (number*1000.));
    }
// <0
// <=-1     -1.23
// <=-10    -12.3
    else if (number >= -10.)
    {
        this->DP1(false);
        this->DP2(true);
        this->DP3(false);
        return this->display((int) (number*100.));
    }
// <=-100   -123
    else if (number >= -100.)
    {
        this->DP1(false);
        this->DP2(false);
        this->DP3(true);
        return this->display((int) (number*10.));
    }
    else
    {
        this->DP1(false);
        this->DP2(false);
        this->DP3(false);
        return this->display((int) (number*1.));
    };
// <=-1000  -OVL
       
    return true;
}

bool Convert::display(float number)
{
    return display((double) number);
}

bool Convert::display(char text[4])
{
    this->DP1(false);
    this->DP2(false);
    this->DP3(false);
    this->putc(text[0]);
    this->putc(text[1]);
    this->putc(text[2]);
    this->putc(text[3]);
    return false;
}