Conversions for the LCD display in FRDM-KL46Z

Dependents:   eem202a_display eem202a_resolutedreamer_1

Revision:
0:ca69bce3284f
Child:
1:cf83568dc17a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/convert.cpp	Sat Apr 19 08:20:12 2014 +0000
@@ -0,0 +1,156 @@
+#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);
+        this->display((unsigned int) (number));
+    }
+// >=100    123.4
+    else if (number >= 100.)
+    {
+        this->DP1(false);
+        this->DP2(false);
+        this->DP3(true);
+        this->display((unsigned int) (number*10.));
+    }
+// >=10     12.34
+    else if (number >= 10.)
+    {
+        this->DP1(false);
+        this->DP2(true);
+        this->DP3(false);
+        this->display((unsigned int) (number*100.));
+    }
+// >=0      1.234
+    else if (number >= 0.)
+    {
+        this->DP1(true);
+        this->DP2(false);
+        this->DP3(false);
+        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);
+        this->display((int) (number*100.));
+    }
+// <=-100   -123
+    else if (number >= -100.)
+    {
+        this->DP1(false);
+        this->DP2(false);
+        this->DP3(true);
+        this->display((int) (number*10.));
+    }
+    else
+    {
+        this->DP1(false);
+        this->DP2(false);
+        this->DP3(false);
+        this->display((int) (number*1.));
+    };
+// <=-1000  -OVL
+       
+    return true;
+}
+
+bool convert::display(float number)
+{
+    return display((double) number);
+}