Saad Tayyab / N5110

Files at this revision

API Documentation at this revision

Comitter:
eencae
Date:
Wed Apr 22 12:41:45 2015 +0000
Parent:
17:780a542d5f8b
Child:
19:ba8addc061ea
Commit message:
printString() and printChar() updated so that strings/chars are cut-off after the 83rd pixel and fit on the display. Previously this would cause the buffer array to go out of bounds and cause a crash.

Changed in this revision

N5110.cpp Show annotated file Show diff for this revision Revisions of this file
N5110.h Show annotated file Show diff for this revision Revisions of this file
--- a/N5110.cpp	Tue Mar 17 12:56:03 2015 +0000
+++ b/N5110.cpp	Wed Apr 22 12:41:45 2015 +0000
@@ -220,33 +220,46 @@
 // function to print 5x7 font
 void N5110::printChar(char c,int x,int y)
 {
-    for (int i = 0; i < 5 ; i++ ) {
-        buffer[x+i][y] = font5x7[(c - 32)*5 + i];
-        // array is offset by 32 relative to ASCII, each character is 5 pixels wide
+    if (y>=0 && y<6) {  // check if printing in range of y banks
+
+        for (int i = 0; i < 5 ; i++ ) {
+            int pixel_x = x+i;
+            if (pixel_x > 83)  // ensure pixel isn't outside the buffer size (0 - 83)
+                break;
+            buffer[pixel_x][y] = font5x7[(c - 32)*5 + i];
+            // array is offset by 32 relative to ASCII, each character is 5 pixels wide
+        }
+
+        refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
     }
-
-    refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
 }
 
 // function to print string at specified position
 void N5110::printString(const char * str,int x,int y)
 {
-    int n = 0 ; // counter for number of characters in string
-    // loop through string and print character
-    while(*str) {
+    if (y>=0 && y<6) {  // check if printing in range of y banks
+
+        int n = 0 ; // counter for number of characters in string
+        // loop through string and print character
+        while(*str) {
 
-        // writes the character bitmap data to the buffer, so that
-        // text and pixels can be displayed at the same time
-        for (int i = 0; i < 5 ; i++ ) {
-            buffer[x+i+n*6][y] = font5x7[(*str - 32)*5 + i];
+            // writes the character bitmap data to the buffer, so that
+            // text and pixels can be displayed at the same time
+            for (int i = 0; i < 5 ; i++ ) {
+                int pixel_x = x+i+n*6;
+                if (pixel_x > 83) // ensure pixel isn't outside the buffer size (0 - 83)
+                    break;
+                buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i];
+            }
+
+            str++;  // go to next character in string
+
+            n++;    // increment index
+
         }
 
-        str++;  // go to next character in string
-        n++;    // increment index
+        refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
     }
-
-    refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
-
 }
 
 // function to clear the screen
--- a/N5110.h	Tue Mar 17 12:56:03 2015 +0000
+++ b/N5110.h	Wed Apr 22 12:41:45 2015 +0000
@@ -210,7 +210,7 @@
 
     /** Print String
     *
-    *   Prints a string of characters to the display.
+    *   Prints a string of characters to the display. String is cut-off after the 83rd pixel.
     *   @param x - the column number (0 to 83)
     *   @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row
     */
@@ -218,7 +218,7 @@
 
     /** Print Character
     *
-    *   Sends a character to the display.  Printed at the specified location
+    *   Sends a character to the display.  Printed at the specified location. Character is cut-off after the 83rd pixel.
     *   @param  c - the character to print. Can print ASCII as so printChar('C').
     *   @param x - the column number (0 to 83)
     *   @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row