Library for interfacing to Nokia 5110 LCD display with Image Loader (as found on the SparkFun website).

Revision:
43:c2598020fcac
Parent:
42:596c207519de
Child:
44:57f9d32fb521
--- a/N5110.cpp	Tue Mar 21 10:43:19 2017 +0000
+++ b/N5110.cpp	Tue Mar 21 11:46:14 2017 +0000
@@ -370,12 +370,13 @@
                      unsigned int const y1,
                      unsigned int const type)
 {
-    int y_range = y1-y0;  // calc range of y and x
-    int x_range = x1-x0;
-    int start,stop,step;
+    // Note that the ranges can be negative so we have to turn the input values
+    // into signed integers first
+    int const y_range = static_cast<int>(y1) - static_cast<int>(y0);
+    int const x_range = static_cast<int>(x1) - static_cast<int>(x0);
 
     // if dotted line, set step to 2, else step is 1
-    step = (type==2) ? 2:1;
+    unsigned int const step = (type==2) ? 2:1;
 
     // make sure we loop over the largest range to get the most pixels on the display
     // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
@@ -383,13 +384,14 @@
     if ( abs(x_range) > abs(y_range) ) {
 
         // ensure we loop from smallest to largest or else for-loop won't run as expected
-        start = x1>x0 ? x0:x1;
-        stop =  x1>x0 ? x1:x0;
+        unsigned int const start = x_range > 0 ? x0:x1;
+        unsigned int const stop =  x_range > 0 ? x1:x0;
 
         // loop between x pixels
-        for (int x = start; x<= stop ; x+=step) {
+        for (unsigned int x = start; x<= stop ; x+=step) {
             // do linear interpolation
-            int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
+            int const dx = static_cast<int>(x)-static_cast<int>(x0);
+            unsigned int const y = y0 + y_range * dx / x_range;
 
             // If the line type is '0', this will clear the pixel
             // If it is '1' or '2', the pixel will be set
@@ -398,12 +400,13 @@
     } else {
 
         // ensure we loop from smallest to largest or else for-loop won't run as expected
-        start = y1>y0 ? y0:y1;
-        stop =  y1>y0 ? y1:y0;
+        unsigned int const start = y_range > 0 ? y0:y1;
+        unsigned int const stop =  y_range > 0 ? y1:y0;
 
-        for (int y = start; y<= stop ; y+=step) {
+        for (unsigned int y = start; y<= stop ; y+=step) {
             // do linear interpolation
-            int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
+            int const dy = static_cast<int>(y)-static_cast<int>(y0);
+            unsigned int const x = x0 + x_range * dy / y_range;
 
             // If the line type is '0', this will clear the pixel
             // If it is '1' or '2', the pixel will be set
@@ -437,7 +440,7 @@
                        int nrows,
                        int ncols,
                        int *sprite)
-{ 
+{
     for (int i = 0; i < nrows; i++) {
         for (int j = 0 ; j < ncols ; j++) {