Snake game on nokia N5110 LCD

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers N5110.h Source File

N5110.h

Go to the documentation of this file.
00001 /**
00002 @file N5110.h
00003  
00004 @brief Header file containing member functions and variables
00005  
00006 */
00007  
00008 #ifndef N5110_H
00009 #define N5110_H
00010  
00011 // Command Bytes - taken from Chris Yan's library
00012 // More information can be found in the display datasheet
00013 // H = 0 - Basic instructions
00014 #define CMD_DC_CLEAR_DISPLAY   0x08
00015 #define CMD_DC_NORMAL_MODE     0x0C
00016 #define CMD_DC_FILL_DISPLAY    0x09
00017 #define CMD_DC_INVERT_VIDEO    0x0D
00018 #define CMD_FS_HORIZONTAL_MODE 0x00
00019 #define CMD_FS_VERTICAL_MODE   0x02
00020 #define CMD_FS_BASIC_MODE      0x00
00021 #define CMD_FS_EXTENDED_MODE   0x01
00022 #define CMD_FS_ACTIVE_MODE     0x00
00023 #define CMD_FS_POWER_DOWN_MODE 0x04
00024 // H = 1 - Extended instructions
00025 #define CMD_TC_TEMP_0          0x04
00026 #define CMD_TC_TEMP_1          0x05
00027 #define CMD_TC_TEMP_2          0x06
00028 #define CMD_TC_TEMP_3          0x07
00029 #define CMD_BI_MUX_24          0x15
00030 #define CMD_BI_MUX_48          0x13
00031 #define CMD_BI_MUX_100         0x10
00032 #define CMD_VOP_6V06           0xB2
00033 #define CMD_VOP_7V38           0xC8
00034  
00035 #include "mbed.h"
00036  
00037 /** 
00038 @brief Simple library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed.
00039 @brief The display is powered from a GPIO pin meaning it can be controlled via software.  The LED backlight is also software-controllable (via PWM pin).
00040 @brief Can print characters and strings to the display using the included 5x7 font.  
00041 @brief The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read.
00042  
00043 @brief Acknowledgements to Chris Yan's Nokia_5110 Library.
00044  
00045 @brief Revision 1.0
00046  
00047 @author Craig A. Evans
00048 @date   January 2014
00049  *
00050  * Example:
00051  * @code
00052  
00053  #include "mbed.h"
00054  #include "N5110.h"
00055  
00056   //    VCC,SCE,RST,D/C,MOSI,SCLK,LED
00057  N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
00058  
00059  int main() {
00060     
00061    // initialise display 
00062   lcd.init();
00063   // print a string in top-left corner
00064   lcd.printString("Hello, World!",0,0);
00065   // move cursor to 4th row
00066   lcd.setXYAddress(0,3);
00067   // print character
00068   lcd.printChar('X');
00069     
00070   while(1);
00071  }
00072   
00073  * @endcode
00074  */
00075 class N5110
00076 {
00077  
00078 public:
00079     /** Create a N5110 object connected to the specified pins
00080     *
00081     * @param pwr Pin connected to Vcc on the LCD display (pin 1)
00082     * @param sce Pin connected to chip enable (pin 3)
00083     * @param rst Pin connected to reset (pin 4)
00084     * @param dc  Pin connected to data/command select (pin 5)
00085     * @param mosi Pin connected to data input (MOSI) (pin 6)
00086     * @param sclk Pin connected to serial clock (SCLK) (pin 7)
00087     * @param led Pin connected to LED backlight (must be PWM) (pin 8) 
00088     * 
00089     */
00090     N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin);
00091     
00092     /** Initialise display
00093     *
00094     *   Powers up the display and turns on backlight (50% brightness default).
00095     *   Sets the display up in horizontal addressing mode and with normal video mode.
00096     */
00097     void init();
00098     
00099     /** Turn off
00100     *
00101     *   Powers down the display and turns of the backlight.
00102     *   Needs to be reinitialised before being re-used.
00103     */
00104     void turnOff();
00105     
00106     /** Clears
00107     *
00108     *   Clears the screen.
00109     */
00110     void clear();
00111     
00112     /** Turn on normal video mode (default)
00113     *  Black on white
00114     */
00115     void normalMode();
00116     
00117     /** Turn on inverse video mode (default)
00118     *  White on black
00119     */
00120     void inverseMode();
00121     
00122     /** Set Brightness
00123     *
00124     *   Sets brightness of LED backlight.
00125     *   @param brightness - float in range 0.0 to 1.0
00126     */
00127     void setBrightness(float brightness);
00128     
00129     /** Set XY Address
00130     * 
00131     *   Sets the X and Y address of where the next data sent to the displa will be written in RAM.
00132     *   @param  x - the column number (0 to 83) - is automatically incremented after data is written
00133     *   @param  y - the row number (0 to 5) - the diplay is split into 6 banks - each bank can be considered a row
00134     */
00135     void setXYAddress(int x, int y);
00136     
00137     /** Print String
00138     *
00139     *   Prints a string of characters to the display.  
00140     *   @param x - the column number (0 to 83)
00141     *   @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row
00142     */
00143     void printString(const char * str,int x,int y);
00144     
00145     /** Print Character
00146     *
00147     *   Sends a character to the display.  Will be printed at the current address.
00148     *   X address is autoincremented by 1 to leave a pixel between successive characters.
00149     *   @param  c - the character to print. Can print ASCII as so printChar('C').
00150     */
00151     void printChar(char c);
00152     
00153     /** Set a Pixel
00154     *
00155     *   This function sets a pixel in the display. A call to refresh() must be made
00156     *   to update the display to reflect the change in pixels.
00157     *   @param  x - the x co-ordinate of the pixel (0 to 83)
00158     *   @param  y - the y co-ordinate of the pixel (0 to 47) 
00159     */
00160     void setPixel(int x, int y);
00161     
00162     /** Clear a Pixel
00163     *
00164     *   This function clears pixel in the display. A call to refresh() must be made
00165     *   to update the display to reflect the change in pixels.
00166     *   @param  x - the x co-ordinate of the pixel (0 to 83)
00167     *   @param  y - the y co-ordinate of the pixel (0 to 47) 
00168     */
00169     void clearPixel(int x, int y);
00170     
00171     /** Get a Pixel
00172     *
00173     *   This function gets the status of a pixel in the display.
00174     *   @param  x - the x co-ordinate of the pixel (0 to 83)
00175     *   @param  y - the y co-ordinate of the pixel (0 to 47) 
00176     *   @returns 
00177     *       0           - pixel is clear
00178     *       non-zero    - pixel is set
00179     */
00180     unsigned char getPixel(int x, int y);
00181     
00182     /** Refresh display
00183     *
00184     *   This functions refreshes the display to reflect the current data in the buffer.
00185     */    
00186     void refresh();
00187     
00188     /** Randomise buffer
00189     *
00190     *   This function fills the buffer with random data.  Can be used to test the display.  
00191     *   A call to refresh() must be made to update the display to reflect the change in pixels.
00192     *   The seed is not set and so the generated pattern will probably be the same each time.
00193     *   TODO: Randomise the seed - maybe using the noise on the AnalogIn pins.
00194     */
00195     void randomiseBuffer();
00196  
00197 private:
00198     void initSPI();
00199     void turnOn();
00200     void reset();
00201     void clearRAM();
00202     void clearBuffer();
00203     void sendCommand(unsigned char command);
00204     void sendData(unsigned char data);
00205  
00206 public:
00207     unsigned char buffer[84][6];  // screen buffer - the 6 is for the banks - each one is 8 bits;
00208  
00209 private:  // private variables
00210     SPI*    spi;
00211     PwmOut* led;
00212     DigitalOut* pwr;
00213     DigitalOut* sce;
00214     DigitalOut* rst;
00215     DigitalOut* dc;
00216  
00217 };
00218  
00219 const unsigned char font5x7[480] = {
00220     0x00, 0x00, 0x00, 0x00, 0x00,// (space)
00221     0x00, 0x00, 0x5F, 0x00, 0x00,// !
00222     0x00, 0x07, 0x00, 0x07, 0x00,// "
00223     0x14, 0x7F, 0x14, 0x7F, 0x14,// #
00224     0x24, 0x2A, 0x7F, 0x2A, 0x12,// $
00225     0x23, 0x13, 0x08, 0x64, 0x62,// %
00226     0x36, 0x49, 0x55, 0x22, 0x50,// &
00227     0x00, 0x05, 0x03, 0x00, 0x00,// '
00228     0x00, 0x1C, 0x22, 0x41, 0x00,// (
00229     0x00, 0x41, 0x22, 0x1C, 0x00,// )
00230     0x08, 0x2A, 0x1C, 0x2A, 0x08,// *
00231     0x08, 0x08, 0x3E, 0x08, 0x08,// +
00232     0x00, 0x50, 0x30, 0x00, 0x00,// ,
00233     0x08, 0x08, 0x08, 0x08, 0x08,// -
00234     0x00, 0x60, 0x60, 0x00, 0x00,// .
00235     0x20, 0x10, 0x08, 0x04, 0x02,// /
00236     0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
00237     0x00, 0x42, 0x7F, 0x40, 0x00,// 1
00238     0x42, 0x61, 0x51, 0x49, 0x46,// 2
00239     0x21, 0x41, 0x45, 0x4B, 0x31,// 3
00240     0x18, 0x14, 0x12, 0x7F, 0x10,// 4
00241     0x27, 0x45, 0x45, 0x45, 0x39,// 5
00242     0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
00243     0x01, 0x71, 0x09, 0x05, 0x03,// 7
00244     0x36, 0x49, 0x49, 0x49, 0x36,// 8
00245     0x06, 0x49, 0x49, 0x29, 0x1E,// 9
00246     0x00, 0x36, 0x36, 0x00, 0x00,// :
00247     0x00, 0x56, 0x36, 0x00, 0x00,// ;
00248     0x00, 0x08, 0x14, 0x22, 0x41,// <
00249     0x14, 0x14, 0x14, 0x14, 0x14,// =
00250     0x41, 0x22, 0x14, 0x08, 0x00,// >
00251     0x02, 0x01, 0x51, 0x09, 0x06,// ?
00252     0x32, 0x49, 0x79, 0x41, 0x3E,// @
00253     0x7E, 0x11, 0x11, 0x11, 0x7E,// A
00254     0x7F, 0x49, 0x49, 0x49, 0x36,// B
00255     0x3E, 0x41, 0x41, 0x41, 0x22,// C
00256     0x7F, 0x41, 0x41, 0x22, 0x1C,// D
00257     0x7F, 0x49, 0x49, 0x49, 0x41,// E
00258     0x7F, 0x09, 0x09, 0x01, 0x01,// F
00259     0x3E, 0x41, 0x41, 0x51, 0x32,// G
00260     0x7F, 0x08, 0x08, 0x08, 0x7F,// H
00261     0x00, 0x41, 0x7F, 0x41, 0x00,// I
00262     0x20, 0x40, 0x41, 0x3F, 0x01,// J
00263     0x7F, 0x08, 0x14, 0x22, 0x41,// K
00264     0x7F, 0x40, 0x40, 0x40, 0x40,// L
00265     0x7F, 0x02, 0x04, 0x02, 0x7F,// M
00266     0x7F, 0x04, 0x08, 0x10, 0x7F,// N
00267     0x3E, 0x41, 0x41, 0x41, 0x3E,// O
00268     0x7F, 0x09, 0x09, 0x09, 0x06,// P
00269     0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
00270     0x7F, 0x09, 0x19, 0x29, 0x46,// R
00271     0x46, 0x49, 0x49, 0x49, 0x31,// S
00272     0x01, 0x01, 0x7F, 0x01, 0x01,// T
00273     0x3F, 0x40, 0x40, 0x40, 0x3F,// U
00274     0x1F, 0x20, 0x40, 0x20, 0x1F,// V
00275     0x7F, 0x20, 0x18, 0x20, 0x7F,// W
00276     0x63, 0x14, 0x08, 0x14, 0x63,// X
00277     0x03, 0x04, 0x78, 0x04, 0x03,// Y
00278     0x61, 0x51, 0x49, 0x45, 0x43,// Z
00279     0x00, 0x00, 0x7F, 0x41, 0x41,// [
00280     0x02, 0x04, 0x08, 0x10, 0x20,// "\"
00281     0x41, 0x41, 0x7F, 0x00, 0x00,// ]
00282     0x04, 0x02, 0x01, 0x02, 0x04,// ^
00283     0x40, 0x40, 0x40, 0x40, 0x40,// _
00284     0x00, 0x01, 0x02, 0x04, 0x00,// `
00285     0x20, 0x54, 0x54, 0x54, 0x78,// a
00286     0x7F, 0x48, 0x44, 0x44, 0x38,// b
00287     0x38, 0x44, 0x44, 0x44, 0x20,// c
00288     0x38, 0x44, 0x44, 0x48, 0x7F,// d
00289     0x38, 0x54, 0x54, 0x54, 0x18,// e
00290     0x08, 0x7E, 0x09, 0x01, 0x02,// f
00291     0x08, 0x14, 0x54, 0x54, 0x3C,// g
00292     0x7F, 0x08, 0x04, 0x04, 0x78,// h
00293     0x00, 0x44, 0x7D, 0x40, 0x00,// i
00294     0x20, 0x40, 0x44, 0x3D, 0x00,// j
00295     0x00, 0x7F, 0x10, 0x28, 0x44,// k
00296     0x00, 0x41, 0x7F, 0x40, 0x00,// l
00297     0x7C, 0x04, 0x18, 0x04, 0x78,// m
00298     0x7C, 0x08, 0x04, 0x04, 0x78,// n
00299     0x38, 0x44, 0x44, 0x44, 0x38,// o
00300     0x7C, 0x14, 0x14, 0x14, 0x08,// p
00301     0x08, 0x14, 0x14, 0x18, 0x7C,// q
00302     0x7C, 0x08, 0x04, 0x04, 0x08,// r
00303     0x48, 0x54, 0x54, 0x54, 0x20,// s
00304     0x04, 0x3F, 0x44, 0x40, 0x20,// t
00305     0x3C, 0x40, 0x40, 0x20, 0x7C,// u
00306     0x1C, 0x20, 0x40, 0x20, 0x1C,// v
00307     0x3C, 0x40, 0x30, 0x40, 0x3C,// w
00308     0x44, 0x28, 0x10, 0x28, 0x44,// x
00309     0x0C, 0x50, 0x50, 0x50, 0x3C,// y
00310     0x44, 0x64, 0x54, 0x4C, 0x44,// z
00311     0x00, 0x08, 0x36, 0x41, 0x00,// {
00312     0x00, 0x00, 0x7F, 0x00, 0x00,// |
00313     0x00, 0x41, 0x36, 0x08, 0x00,// }
00314     0x08, 0x08, 0x2A, 0x1C, 0x08,// ->
00315     0x08, 0x1C, 0x2A, 0x08, 0x08 // <-
00316 };
00317  
00318 #endif