Team Kombat / Nokia_5110

Dependents:   NOKIA_KOMBAT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NOKIA_5110.cpp Source File

NOKIA_5110.cpp

00001 // Project: Nokia5110 - Controlling a NK5110 display from an NXP LPC1768
00002 // File: NOKIA_5110.cpp
00003 // Author: Chris Yan
00004 // Created: January, 2012
00005 // Revised: January, 2014
00006 //  Desc: Supporting code for the NokiaLcd class
00007 
00008 #include "NOKIA_5110.h"
00009 #include "mbed.h"
00010 
00011 NokiaLcd::NokiaLcd(LcdPins pinout)
00012 {
00013     // SPI
00014     LcdSpi = new SPI(pinout.mosi, pinout.miso, pinout.sclk);
00015     LcdSpi->format(LCD_SPI_BITS, LCD_SPI_MODE);
00016     LcdSpi->frequency(LCD_FREQ);
00017 
00018     // Control Pins
00019     Pins = new DigitalOut*[3];
00020     Pins[PIN_RST]   = new DigitalOut(pinout.rst);
00021     Pins[PIN_SCE]   = new DigitalOut(pinout.sce);
00022     Pins[PIN_DC]    = new DigitalOut(pinout.dc);
00023 
00024     // Initial Command Instructions, note the initial command mode
00025     FunctionSet.V   = CMD_FS_HORIZONTAL_MODE;
00026     FunctionSet.H   = CMD_FS_EXTENDED_MODE;
00027     FunctionSet.PD  = CMD_FS_ACTIVE_MODE;
00028     FunctionChar    = CreateFunctionChar();
00029 
00030     TempControlChar = CMD_TC_TEMP_2;
00031     DispControlChar = CMD_DC_NORMAL_MODE;
00032     BiasChar        = CMD_BI_MUX_48;
00033     VopChar         = CMD_VOP_7V38;
00034 }
00035 
00036 void NokiaLcd::ShutdownLcd()
00037 {
00038     FunctionSet.PD  = CMD_FS_POWER_DOWN_MODE;
00039 
00040     ClearLcdMem();
00041     SendFunction( CMD_DC_CLEAR_DISPLAY );
00042     SendFunction( CreateFunctionChar() );
00043 }
00044 
00045 void NokiaLcd::ClearLcdMem()
00046 {
00047     for(int tick = 0; tick <= 503; tick++)
00048         LcdSpi->write(0x00);
00049 }
00050 
00051 void NokiaLcd::TestLcd(char test_pattern)
00052 {
00053     for(int tick = 0; tick <= 503; tick++)
00054         LcdSpi->write(test_pattern);         // Command gets sent
00055 }
00056 
00057 void NokiaLcd::InitLcd()
00058 {
00059     ResetLcd();
00060     Pins[PIN_SCE]->write(0);     // Chip Select goes low
00061 
00062     // Redefine the FunctionChar in case it has changed
00063     FunctionSet.V   = CMD_FS_HORIZONTAL_MODE;
00064     FunctionSet.H   = CMD_FS_EXTENDED_MODE;
00065     FunctionSet.PD  = CMD_FS_ACTIVE_MODE;
00066     SendFunction( CreateFunctionChar() );   // Extended CMD set
00067     SendFunction( VopChar );                // | Vop
00068     SendFunction( TempControlChar );        // | Temp
00069     SendFunction( BiasChar );               // | Bias
00070 
00071     FunctionSet.H   = CMD_FS_BASIC_MODE;
00072     SendFunction( CreateFunctionChar() );   // Basic CMD set
00073     SendFunction( DispControlChar );        // | Display Mode
00074 
00075     ClearLcdMem();
00076     Pins[PIN_DC]->write(1);     // Data/CMD goes back to Data mode
00077 }
00078 
00079 void NokiaLcd::ResetLcd()
00080 {
00081     Pins[PIN_RST]->write(0);    // Reset goes low
00082     Pins[PIN_RST]->write(1);    // Reset goes high
00083 }
00084 
00085 char NokiaLcd::CreateFunctionChar()
00086 {
00087     return ( 0x20 | FunctionSet.PD | FunctionSet.V | FunctionSet.H );
00088 }
00089 
00090 void NokiaLcd::SendDrawData(char data)
00091 {
00092     LcdSpi->write(data);         // Command gets sent
00093 }
00094 
00095 void NokiaLcd::DrawChar(char character)
00096 {
00097     for( int i = 0; i < 6; i++)
00098         SendDrawData( FONT_6x6[ ((character - 32)*6) + i] );
00099 }
00100 
00101 void NokiaLcd::DrawString(char* s)
00102 {
00103     char len = strlen(s);
00104     for( int idx = 0; idx < len; idx++ ) {
00105         for( int i = 0; i < 6; i++)
00106             SendDrawData( FONT_6x6[ ((s[idx] - 32)*6) + i] );
00107     }
00108 }
00109 
00110 void NokiaLcd::DrawFrameChar(char character)
00111 {
00112     for( int i = 0; i < 6; i++)
00113         SendDrawData((( FONT_6x6[ ((character - 32)*6) + i]  ) << 1 ) | 0x81);
00114 }
00115 
00116 void NokiaLcd::DrawNegFrameChar(char character)
00117 {
00118     for( int i = 0; i < 6; i++)
00119         SendDrawData(~(( FONT_6x6[ ((character - 32)*6) + i]  ) << 1 ) | 0x81);
00120 }
00121 
00122 char* NokiaLcd::NumToStr(int num)
00123 {
00124     if(num <= 0)
00125         return "0";
00126 
00127     double length = 0;
00128     int tlen = 0;
00129     int temp = 1;
00130     char c;
00131 
00132     // Get number of digits
00133     while( temp <= num ) {
00134         temp *= 10;
00135         length++;
00136     }
00137     tlen = length;
00138     char* numString = new char[tlen+1];
00139 
00140     // Convert each place in number to a stand-alone representative number
00141     temp = 0;
00142     for(int idx = pow(10, length); idx>1; idx = (idx/10)) {
00143         c = (char)( ((num % idx)-(num % (idx/10)))/(idx/10) + 48);
00144         numString[temp] = c;
00145         temp++;
00146     }
00147     numString[temp] = '\0';
00148     return numString;
00149 }
00150 
00151 void NokiaLcd::SetXY(char x, char y)
00152 {
00153     if( (x > 83) || (y > 5) )
00154         return;
00155 
00156     SendFunction( x | 0x80 );
00157     SendFunction( y | 0x40 );
00158 }
00159 
00160 void NokiaLcd::SendFunction(char cmd) //TODO:Detection of what H should be
00161 {
00162     Pins[PIN_DC]->write(0);     // Data/CMD goes low
00163     LcdSpi->write(cmd);         // Command gets sent
00164     Pins[PIN_DC]->write(1);     // Data/CMD goes back to Data mode
00165 }
00166 
00167 
00168 //------------------------------------------------------------------------------
00169 /*
00170     int x: x coordinate of printing [0,83]
00171     int y: y coordinate of printing [0,5] be careful here, you can only select the portion, adjust your byte codes accordingly
00172     char bitmap: idk what * asterisk stands for, but it is the bitmap array you are going to print
00173     int w: with of the image in actual pixel size
00174     int h: height of the image in actual pixels, meanin that starting pixel is 1 e.g. 48 pixels in y column of the lcd 
00175 */
00176 void NokiaLcd::DrawBitmap(int x, int y, char *bitmap, int w, int h)
00177 { 
00178     int h_adjusted; //this is for addressing y between 0 to 5
00179     h_adjusted = h / 8; //this address is in range of 1-6 but in for loop, it is compensated with usage of < instead of <=
00180 
00181     for( int j = 0; j < h_adjusted; j++) {
00182         for( int i = 0 ; i < w ; i++ ) {
00183             SetXY( x + i, y + j);
00184             SendDrawData( bitmap[(j*w) + i]);
00185         }
00186     }
00187 }
00188 //------------------------------------------------------------------------------
00189 
00190 NokiaLcd::~NokiaLcd()
00191 {
00192     ShutdownLcd();
00193 }
00194