Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.

Dependents:   LEDFun NetTester

Fork of N3310LCD by Andrew Lindsay

Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.

Revision:
1:51961974fe55
Parent:
0:7efa6655d94b
Child:
3:9808f63fd2fe
--- a/N3310LCD.cpp	Sun Mar 10 18:15:25 2013 +0000
+++ b/N3310LCD.cpp	Sun Mar 10 18:29:09 2013 +0000
@@ -13,7 +13,7 @@
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
-* 
+*
 * N3310LCD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -28,10 +28,10 @@
 
 static unsigned char lcd_buffer[LCDROWMAX][LCDCOLMAX];
 
-N3310LCD::N3310LCD (PinName mosi, PinName miso, PinName sck, 
-                    PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on) : 
-                    lcdPort(mosi, miso, sck),
-                    ceWire(ce), dcWire(dat_cmd), rstWire(lcd_rst), blWire(bl_on)
+N3310LCD::N3310LCD (PinName mosi, PinName miso, PinName sck,
+                    PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on) :
+    lcdPort(mosi, miso, sck),
+    ceWire(ce), dcWire(dat_cmd), rstWire(lcd_rst), blWire(bl_on)
 {
 }
 
@@ -40,29 +40,28 @@
     // use default SPI format
     lcdPort.format(8,0);
     lcdPort.frequency(1000000);
-    
+
     // lcd reset
     wait_ms(1);
     rstWire = 0;
     wait_ms(1);
     rstWire = 1;
-    
-    write(0x21, CMD);    
-    write(0xc8, CMD);    
-    write(0x06, CMD);    
-    write(0x13, CMD);    
-    write(0x20, CMD);    
-    cls();            
+
+    write(0x21, CMD);
+    write(0xc8, CMD);
+    write(0x06, CMD);
+    write(0x13, CMD);
+    write(0x20, CMD);
+    cls();
     write(0x0c, CMD);
 }
 
 void N3310LCD::cls()
 {
-    write(0x0c, CMD);            
-    write(0x80, CMD);            
+    write(0x0c, CMD);
+    write(0x80, CMD);
 
-    for (int i = 0; i < 504; i++)
-    {
+    for (int i = 0; i < 504; i++) {
         write(0, DATA);
     }
 }
@@ -77,14 +76,14 @@
 {
     // bring CS low for write
     ceWire = 0;
-    
+
     if (CMD == req_type)
         dcWire = 0;
     else // DATA
         dcWire = 1;
-        
+
     lcdPort.write(data);
-    
+
     // write finished
     ceWire = 1;
 }
@@ -92,75 +91,71 @@
 void N3310LCD::locate(BYTE xPos, BYTE yPos)
 {
     write(0x40 | yPos, CMD);      // column
-    write(0x80 | xPos, CMD);      // row    
+    write(0x80 | xPos, CMD);      // row
 }
 
 void N3310LCD::drawBitmap(BYTE xPos, BYTE yPos, BYTE* bitmap, BYTE bmpXSize, BYTE bmpYSize)
 {
     BYTE row;
-    
+
     if (0 == bmpYSize % 8)
-        row = bmpYSize/8;  
+        row = bmpYSize/8;
     else
         row = bmpYSize/8 + 1;
-    
-    for (BYTE n = 0; n < row; n++)
-    {
+
+    for (BYTE n = 0; n < row; n++) {
         locate(xPos, yPos);
-        for(BYTE i = 0; i < bmpXSize; i++)
-        {
+        for(BYTE i = 0; i < bmpXSize; i++) {
             write(bitmap[i + (n * bmpXSize)], DATA);
         }
-        yPos++;                       
+        yPos++;
     }
 }
 
 /*
- * Name         : clearBitmap 
+ * Name         : clearBitmap
  * Description  : Clear an area of the screen, usually to blank out a
  *        previously drawn image or part of image.
  * Argument(s)  : x, y - Position on screen, x 0-83, y 1-6
  *                size_x,size_y - Size of the image in pixels,
  *                size_y is multiple of 8
- * Return value : none 
+ * Return value : none
  */
 void N3310LCD::clearBitmap( unsigned char x,unsigned char y,
-                  unsigned char size_x,unsigned char size_y)
-  {
+                            unsigned char size_x,unsigned char size_y)
+{
     unsigned int i,n;
     unsigned char row;
-    
+
     row = (size_y % 8 == 0 ) ? size_y / 8 : size_y / 8 + 1;
 //    if (size_y % 8==0)
-//      row=size_y/8;  
+//      row=size_y/8;
 //    else
 //          row=size_y/8+1;
-    
-    for (n=0;n<row;n++) {
+
+    for (n=0; n<row; n++) {
         locate(x,y);
         for(i=0; i<size_x; i++) {
             write( 0x00, DATA );
         }
-        y++;                       
-    }      
+        y++;
+    }
 }
 
 void N3310LCD::writeString(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode)
 {
     locate(xPos, yPos);
-    
-    while (*string) 
-    {
+
+    while (*string) {
         writeChar(*string++, mode);
     }
 }
-                  
+
 void N3310LCD::writeStringBig(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode)
 {
-    while (*string)
-    {     
+    while (*string) {
         writeCharBig(xPos, yPos, *string , mode);
-        
+
         if('.' == *string++)
             xPos += 5;
         else
@@ -171,12 +166,11 @@
 void N3310LCD::writeChar(BYTE ch, eDisplayMode mode)
 {
     BYTE sendByte;
-    
+
     unsigned char* pFont = (unsigned char*)font6_8;
     ch -= 32;
 
-    for (BYTE line = 0; line < 6; line++)
-    {
+    for (BYTE line = 0; line < 6; line++) {
         sendByte = *(pFont + ch*6 + line);
         write((mode == NORMAL)? sendByte: (sendByte ^ 0xff) , DATA);
     }
@@ -185,9 +179,9 @@
 void N3310LCD::writeCharBig(BYTE xPos, BYTE yPos, BYTE ch, eDisplayMode mode)
 {
     BYTE sendByte;
-   
+
     unsigned char* pFont = (unsigned char *) big_number;
-   
+
     if('.' == ch)
         ch = 10;
     else if ('+' == ch)
@@ -196,13 +190,11 @@
         ch = 12;
     else
         ch = ch & 0x0f;
-    
-    for(BYTE i = 0; i < 3; i++)
-    {    
+
+    for(BYTE i = 0; i < 3; i++) {
         locate(xPos, yPos + i);
- 
-        for(BYTE j = 0; j < 16; j++)
-        {
+
+        for(BYTE j = 0; j < 16; j++) {
             sendByte =  *(pFont + ch*48 + i*16 + j);
             write((mode == NORMAL)? sendByte : (sendByte^0xff), DATA);
         }
@@ -216,10 +208,11 @@
  *                c - colour, either PIXEL_ON, PIXEL_OFF or PIXEL_XOR
  * Return value : none
  */
-void N3310LCD::setPixel( unsigned char x, unsigned char y, unsigned char c ) {
-unsigned char value;
-unsigned char row;
-    
+void N3310LCD::setPixel( unsigned char x, unsigned char y, unsigned char c )
+{
+    unsigned char value;
+    unsigned char row;
+
 //    if( x < 0 || x >= LCDCOLMAX || y < 0 || y >= LCDPIXELROWMAX ) return;
     if( x >= LCDCOLMAX || y >= LCDPIXELROWMAX ) return;
 
@@ -250,7 +243,8 @@
  * Return value : none
  */
 void N3310LCD::drawLine(unsigned char x1, unsigned char y1,
-        unsigned char x2, unsigned char y2, unsigned char c) {
+                        unsigned char x2, unsigned char y2, unsigned char c)
+{
     int dx, dy, stepx, stepy, fraction;
 
     /* Calculate differential form */
@@ -326,7 +320,8 @@
  * Return value : none
  */
 void N3310LCD::drawRectangle(unsigned char x1, unsigned char y1,
-        unsigned char x2, unsigned char y2, unsigned char c){
+                             unsigned char x2, unsigned char y2, unsigned char c)
+{
     drawLine( x1, y1, x2, y1, c );
     drawLine( x1, y1, x1, y2, c );
     drawLine( x1, y2, x2, y2, c );
@@ -344,7 +339,8 @@
  * Return value : none
  */
 void N3310LCD::drawFilledRectangle(unsigned char x1, unsigned char y1,
-        unsigned char x2, unsigned char y2, unsigned char c) {
+                                   unsigned char x2, unsigned char y2, unsigned char c)
+{
     for(int i=y1; i <= y2; i++ ) {
         drawLine( x1, i, x2, i, c );
     }
@@ -353,22 +349,23 @@
 
 /*
  * Name         : drawCircle
- * Description  : Draw a circle using Bresenham's algorithm. 
+ * Description  : Draw a circle using Bresenham's algorithm.
  *        Some small circles will look like squares!!
  * Argument(s)  : xc, yc - Centre of circle
  *        r - Radius
  *        c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR
- * Return value : None 
+ * Return value : None
  */
 void N3310LCD::drawCircle(unsigned char xc, unsigned char yc,
-        unsigned char r, unsigned char c) {
+                          unsigned char r, unsigned char c)
+{
     int x=0;
     int y=r;
     int p=3-(2*r);
 
-        setPixel( (uint8_t)(xc+x),(uint8_t)(yc-y), c);
+    setPixel( (uint8_t)(xc+x),(uint8_t)(yc-y), c);
 
-    for(x=0;x<=y;x++) {
+    for(x=0; x<=y; x++) {
         if (p<0) {
             y=y;
             p=(p+(4*x)+6);